From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 0D3D94A0A1A; Mon, 5 Jun 2023 13:46:59 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 0D3D94A0A1A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1685962019; bh=UMcm8oh/kKa9jWV025y27sT0lp33f1evT5lu6rk4ulo=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=b/HoYGUbnma1N4LGdG/PSJyqU+fk/Ni+PlfB0ZJqlxRC/e6va11f0d7iXlBKPEX75 /MhFql5RMKvzU4nclTYi6i2mkZ1l9VDTSaCGVlSvpFQvIL6O7/dSuFYKBdZCm5kD+a K33GU8zdJY6T5ljkfQ2iQ8539UOIZffUHJ15yzwM= Received: from smtp61.i.mail.ru (smtp61.i.mail.ru [95.163.41.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id AD69C4A0A1C for ; Mon, 5 Jun 2023 13:45:30 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org AD69C4A0A1C Received: by smtp61.i.mail.ru with esmtpa (envelope-from ) id 1q67iL-00BFih-MZ; Mon, 05 Jun 2023 13:45:30 +0300 To: Igor Munkin , Maxim Kokryashkin , Sergey Bronnikov Date: Mon, 5 Jun 2023 13:41:14 +0300 Message-Id: <56adbfb4fe47496e63d888ce3543ac0cce65427e.1685613304.git.skaplun@tarantool.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD988F25420CCA9469A00ED12DDEDD8E56A13598C451414178F182A05F538085040253EBDA43B456B2643048A41779B93E516D4AC37F42AE04D16F3C5429E1E170A X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE78E8764B5BC580342EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006374E296C64F8F911C48638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8330051599142393B69FBD466D17C7774117882F44604297287769387670735204B6963042765DA4B618001F51B5FD3F9D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE7532CA1512B819811DBC1C451FC279AAD8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE3643FE6A0CAC512C7AD7EC71F1DB88427C4224003CC836476E2F48590F00D11D6E2021AF6380DFAD1A18204E546F3947CA9FF340AA05FB58C2E808ACE2090B5E1725E5C173C3A84C3C5EA940A35A165FF2DBA43225CD8A89F83C798A30B85E16B156CCFE7AF13BCA4B5C8C57E37DE458BEDA766A37F9254B7 X-C1DE0DAB: 0D63561A33F958A5AE19961BCF2D33F8290AA7B82D7AB2597599E52AC8BB2FDAF87CCE6106E1FC07E67D4AC08A07B9B0735DFC8FA7AC12079C5DF10A05D560A950611B66E3DA6D700B0A020F03D25A0997E3FB2386030E77 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF2E8329AE64B4E32DB29A3735E8D0D4B510F708A9CEE2DCC069B6938A7C610797A7F6D8DEE3A50DD4588A46A5EBCE8E370860E072BF337835CB856401294BEFF4461A413F07889F2102C26D483E81D6BE5EF9655DD6DEA7D65774BB76CC95456EEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojbL9S8ysBdXhLPelb+eUiUKrTSONFaHg7 X-Mailru-Sender: 11C2EC085EDE56FAC07928AF2646A769568B96912E2DB96443048A41779B93E56ECF975C7BB2667FDEDBA653FF35249392D99EB8CC7091A70E183A470755BFD208F19895AA18418972D6B4FCE48DF648AE208404248635DF X-Mras: Ok Subject: [Tarantool-patches] [PATCH v3 luajit 3/6] test: introduce utils.h helper for C tests X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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 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: `` 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 +#include + +#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 . + * 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