#include "hs_tests.h" static int testfunc(lua_State *L) { return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * tests for hs_create_table * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ TEST(table_correct_index) { int top_old = lua_gettop(L); int index = hs_create_table(L); mu_assert("the stack is unchanged!", lua_gettop(L) != top_old); mu_assert("returned incorrect index!", index == lua_gettop(L)); return 0; } #define load_key(index, value, pushf) do { \ pushf(L, value); lua_gettable(L, index); \ mu_assert("value at key '" #value "' is nil!", \ !lua_isnil(L, -1)); \ } while(0) #define loadkey_bool(index, value) \ load_key(index, value, lua_pushboolean) #define loadkey_int(index, value) \ load_key(index, value, lua_pushinteger) #define loadkey_num(index, value) \ load_key(index, value, lua_pushnumber) #define loadkey_tbl(index, value) do { \ lua_pushvalue(L, value); lua_gettable(L, index); \ mu_assert("value at table (index '" #value "') is nil!", \ !lua_isnil(L, -1)); \ } while(0) #define loadkey_func(index, value) do { \ lua_pushvalue(L, value); lua_gettable(L, index); \ mu_assert("value at function (index '" #value "') is nil!", \ !lua_isnil(L, -1)); \ } while(0) #define loadkey_cfunc(index, value) \ load_key(index, value, lua_pushcfunction) #define loadkey_user(index, value) do { \ lua_pushvalue(L, value); lua_gettable(L, index); \ mu_assert("value at userdata (index '" #value "') is nil!", \ !lua_isnil(L, -1)); \ } while(0) #define loadkey_light(index, value) \ load_key(index, value, lua_pushlightuserdata) #define check_value(is_type, typestring, type, conversion, comparison) \ do { \ mu_assert("value is not of type " typestring, is_type(L, -1)); \ type value = conversion(L, -1); lua_pop(L, 1); \ mu_assert("test " #comparison " failed!", (comparison)); \ lua_pop(L, 1); /* remove key */ \ } while(0) #define checkval_bool(expected) \ check_value(lua_isboolean, "boolean", bool, lua_toboolean, value==expected) #define checkval_int(expected) \ check_value(lua_isnumber, "integer", lua_Integer, lua_tointeger, value==expected) #define checkval_num(expected) \ check_value(lua_isnumber, "number", lua_Number, lua_tonumber, value==expected) #define checkval_str(expected) \ check_value(lua_isstring, "string", const char *, lua_tostring, strcmp(value, expected)) #define checkval_tbl(expected) \ check_value(lua_istable, "table", void *, lua_topointer, value==lua_topointer(L, expected) #define checkval_func(expected) \ check_value(lua_isfunction, "function", void *, lua_topointer, value==lua_topointer(L, expected)) #define checkval_cfunc(expected) \ check_value(lua_iscfunction, "C function", lua_CFunction, lua_tocfunction, value==expected) #define checkval_user(expected) \ check_value(lua_isuserdata, "userdata", void *, lua_topointer, value==lua_topointer(L, expected)) #define checkval_light(expected) \ check_value(lua_islightuserdata, "light userdata", void *, lua_tolightuserdata, value==expected) // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ TEST(create_table_bool_bool) { int index = hs_create_table (L, hs_bool_bool(true, true), hs_bool_bool(false, true)); loadkey_bool(index, true); checkval_bool(true); loadkey_bool(index, false); checkval_bool(true); return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * test suite * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ void hs_create_table_tests() { printf("running hs_create_table() tests...\n"); mu_run_test("return correct stack index", table_correct_index); mu_run_test("create table with boolean keys and boolean values", create_table_bool_bool); }