From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org> To: Igor Munkin <imun@tarantool.org>, Maxim Kokryashkin <m.kokryashkin@tarantool.org>, Sergey Bronnikov <sergeyb@tarantool.org> Cc: tarantool-patches@dev.tarantool.org Subject: [Tarantool-patches] [PATCH v3 luajit 3/6] test: introduce utils.h helper for C tests Date: Mon, 5 Jun 2023 13:41:14 +0300 [thread overview] Message-ID: <56adbfb4fe47496e63d888ce3543ac0cce65427e.1685613304.git.skaplun@tarantool.org> (raw) In-Reply-To: <cover.1685613304.git.skaplun@tarantool.org> This header contains generic init and close functions for tests and helpers for loading auxiliary Lua script with functions to run inside a C test. See more information in the <README.md> file. It will be properly used in the next commit. Part of tarantool/tarantool#7900 --- test/tarantool-c-tests/CMakeLists.txt | 3 ++ test/tarantool-c-tests/README.md | 36 +++++++++++++ test/tarantool-c-tests/utils.h | 75 +++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 test/tarantool-c-tests/README.md create mode 100644 test/tarantool-c-tests/utils.h diff --git a/test/tarantool-c-tests/CMakeLists.txt b/test/tarantool-c-tests/CMakeLists.txt index da128457..17255345 100644 --- a/test/tarantool-c-tests/CMakeLists.txt +++ b/test/tarantool-c-tests/CMakeLists.txt @@ -26,6 +26,9 @@ set_target_properties(libtest PROPERTIES # lj_arch.h in compiled test are consistent with the LuaJIT library # to link. AppendFlags(TESTS_C_FLAGS ${TARGET_C_FLAGS}) +# Use directory for absolute path to the Lua script helper. So, +# test binary can be run from any directory. +AppendFlags(TESTS_C_FLAGS "-D__LJ_TEST_DIR__='\"${CMAKE_CURRENT_SOURCE_DIR}\"'") set(CTEST_SRC_SUFFIX ".test.c") file(GLOB tests "${CMAKE_CURRENT_SOURCE_DIR}/*${CTEST_SRC_SUFFIX}") diff --git a/test/tarantool-c-tests/README.md b/test/tarantool-c-tests/README.md new file mode 100644 index 00000000..102ec151 --- /dev/null +++ b/test/tarantool-c-tests/README.md @@ -0,0 +1,36 @@ +This directory contains C tests for Tarantool's fork of LuaJIT. + +They should test C API, some unit functionality, etc.. + +The group of unit tests is declared like the following: +```c +void *t_state = NULL; +const struct test_unit tgroup[] = { + test_unit_new(test_base), + test_unit_new(test_simple), +}; +return test_run_group(tgroup, t_state); +``` + +Sometimes we need to test C API for modules, that show some Lua metrics (like +`luaM_metrics` or sysprof). In these cases, the required Lua script should be +named like the following: `<ctestname-script.lua>` and contains a table with a +bunch of Lua functions named the same as the unit C test, that uses this +function. + +```lua +local M = {} +M.base = function() + -- ... +end + +M.test_simple = function() + -- ... +end + +return M +``` + +The script is loaded via `utils_load_aux_script(L, script_name)`. It loads the file +and place the table with functions at the top of Lua stack. Each function is get +from the table via `utils_get_aux_lfunc(L)` helper in the corresponding test. diff --git a/test/tarantool-c-tests/utils.h b/test/tarantool-c-tests/utils.h new file mode 100644 index 00000000..714aa61a --- /dev/null +++ b/test/tarantool-c-tests/utils.h @@ -0,0 +1,75 @@ +#ifndef TARANTOOL_LUAJIT_TEST_UTILS_H +#define TARANTOOL_LUAJIT_TEST_UTILS_H + +#include <limits.h> +#include <string.h> + +#include "lauxlib.h" +#include "lua.h" +#include "luajit.h" +#include "lualib.h" + +#include "test.h" + +#define UTILS_UNUSED __attribute__((unused)) + +/* Generic init for our tests. */ +static lua_State *utils_lua_init(void) +{ + lua_State *L = luaL_newstate(); + if (!L) + bail_out("Can't init Lua state"); + /* Stop collector during library initialization. */ + lua_gc(L, LUA_GCSTOP, 0); + luaL_openlibs(L); + lua_gc(L, LUA_GCRESTART, -1); + return L; +} + +/* Generic close for our tests. */ +static void utils_lua_close(lua_State *L) +{ + lua_close(L); +} + +/* + * Load the pair to the test file <filename-script.lua>. + * Each file should return the table with functions (named the + * same as the corresponding unit tests) to call in unit tests. + * Script file name is given as the second argument. + * Push the table with those functions on the Lua stack. + */ +UTILS_UNUSED static void utils_load_aux_script(lua_State *L, const char *file) +{ + /* + * Format script name. + * `__LJ_TEST_DIR__` is set via CMake. + */ + char script[PATH_MAX] = __LJ_TEST_DIR__; + char *script_name = script + sizeof(__LJ_TEST_DIR__) - 1; + /* Replace '\0' with '/'. */ + *script_name++ = '/'; + /* Append script filename. */ + strcpy(script_name, file); + + if (luaL_dofile(L, script) != LUA_OK) { + test_comment("Can't load %s: '%s'", script, + lua_tostring(L, -1)); + bail_out("Can't load auxiliary script"); + } + + if (!lua_istable(L, -1)) + bail_out("Returned value from script is not a table"); +} + +/* + * Accept a table on top of the Lua stack which containing the + * function named as the unit test we currently executing. + */ +#define utils_get_aux_lfunc(L) do { \ + lua_getfield((L), -1, __func__); \ + if (!lua_isfunction((L), -1)) \ + bail_out("Can't get auxiliary test function"); \ +} while (0) + +#endif /* TARANTOOL_LUAJIT_TEST_UTILS_H */ -- 2.34.1
next prev parent reply other threads:[~2023-06-05 10:46 UTC|newest] Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-06-05 10:41 [Tarantool-patches] [PATCH v3 luajit 0/6] Reworking " Sergey Kaplun via Tarantool-patches 2023-06-05 10:41 ` [Tarantool-patches] [PATCH v3 luajit 1/6] test: fix setting of {DY}LD_LIBRARY_PATH variables Sergey Kaplun via Tarantool-patches 2023-06-05 14:21 ` Sergey Bronnikov via Tarantool-patches 2023-06-05 10:41 ` [Tarantool-patches] [PATCH v3 luajit 2/6] test: introduce module for C tests Sergey Kaplun via Tarantool-patches 2023-06-05 15:08 ` Sergey Bronnikov via Tarantool-patches 2023-06-07 15:51 ` Sergey Bronnikov via Tarantool-patches 2023-06-05 10:41 ` Sergey Kaplun via Tarantool-patches [this message] 2023-06-05 15:11 ` [Tarantool-patches] [PATCH v3 luajit 3/6] test: introduce utils.h helper " Sergey Bronnikov via Tarantool-patches 2023-06-05 10:41 ` [Tarantool-patches] [PATCH v3 luajit 4/6] test: rewrite misclib-getmetrics-capi test in C Sergey Kaplun via Tarantool-patches 2023-06-07 16:25 ` Sergey Bronnikov via Tarantool-patches 2023-06-05 10:41 ` [Tarantool-patches] [PATCH v3 luajit 5/6] test: rewrite misclib-sysprof-capi " Sergey Kaplun via Tarantool-patches 2023-06-07 16:18 ` Sergey Bronnikov via Tarantool-patches 2023-06-05 10:41 ` [Tarantool-patches] [PATCH v3 luajit 6/6] test: rewrite lj-49-bad-lightuserdata " Sergey Kaplun via Tarantool-patches 2023-06-07 16:14 ` Sergey Bronnikov via Tarantool-patches
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=56adbfb4fe47496e63d888ce3543ac0cce65427e.1685613304.git.skaplun@tarantool.org \ --to=tarantool-patches@dev.tarantool.org \ --cc=imun@tarantool.org \ --cc=m.kokryashkin@tarantool.org \ --cc=sergeyb@tarantool.org \ --cc=skaplun@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v3 luajit 3/6] test: introduce utils.h helper for C tests' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox