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 6A0794A153B; Mon, 5 Jun 2023 18:11:47 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 6A0794A153B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1685977907; bh=18EJAkPOtDc/Fl2wls+z00rngCsXbm1At6KfLDTx4IE=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=hPQdCS7Cv/NLZEWz7OdDmEauFk2ncnyXlVj14C6B0s/cmX1ylnLEKXiuTY187Wlg3 Mp+eblL9bf2c+W72sObHebPE7Ax2lxv3Tj6TkkUCMS8zCwGPZOET0JBGpnaVTTdkaZ k+wilnoFe9+fAJxPRtxOkPOstfv1ls6iaQQ08Jt4= Received: from smtp44.i.mail.ru (smtp44.i.mail.ru [95.163.41.82]) (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 1C74E454541 for ; Mon, 5 Jun 2023 18:11:46 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 1C74E454541 Received: by smtp44.i.mail.ru with esmtpa (envelope-from ) id 1q6Bs1-00125a-8a; Mon, 05 Jun 2023 18:11:45 +0300 Message-ID: <2e88c4d8-3c61-d8cc-171f-45e5043960d1@tarantool.org> Date: Mon, 5 Jun 2023 18:11:44 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Content-Language: en-US To: Sergey Kaplun , Igor Munkin , Maxim Kokryashkin Cc: tarantool-patches@dev.tarantool.org References: <56adbfb4fe47496e63d888ce3543ac0cce65427e.1685613304.git.skaplun@tarantool.org> In-Reply-To: <56adbfb4fe47496e63d888ce3543ac0cce65427e.1685613304.git.skaplun@tarantool.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD93D74B10BAB639DE3F7F3D18868BBEBB7BD235496955735B300894C459B0CD1B9FDF48B3AEF586A35607DABE57FBE820A80E5F50E1CABBD74AEBB3C0239580454 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7DB84ED444C624799EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006371C57DA68CF6C21388638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8882BE8C9EF1A7B8C22449E487FB1544F117882F44604297287769387670735204B6963042765DA4BBDFBBEFFF4125B51D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE7B96B19DC4093321FCB1554B277F7060D8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE3706E30CA5231861903F1AB874ED89028C4224003CC836476E2F48590F00D11D6E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F7900637BC468E7E89D8C5D6EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: 0D63561A33F958A571AFDD42CFC8BEACE07E98811D5E5D9130C4A78C5D876EF2F87CCE6106E1FC07E67D4AC08A07B9B01F9513A7CA91E555CB5012B2E24CD356 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF7064DF432A1C236F9F4AD5AA36D9A297B0A9EF087257EB8FE853DBBE62488A2C192AF36DABE85845588A46A5EBCE8E379CDC3583B2E70272E63C6C81C07FFDF7A74DFFEFA5DC0E7F02C26D483E81D6BE0DBAE6F56676BC7117BB6831D7356A2DEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojxchphH+YLHwBzs5wPj7vuA== X-Mailru-Sender: 11C2EC085EDE56FAC07928AF2646A7698883E2A0BB861A4E41264BD4961E3F287345D355D45E21A6EBA65886582A37BD66FEC6BF5C9C28D98A98C1125256619760D574B6FC815AB872D6B4FCE48DF648AE208404248635DF X-Mras: Ok Subject: Re: [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 Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Sergey, thanks for patches and fixes. LGTM with a couple of minor comments below On 6/5/23 13:41, Sergey Kaplun wrote: > 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. */ Message would be more helpful with explanation *why* GC should be stopped. > + 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 . Nit: replace filename-script with file (same as in argument name, this allows easily match them on reading > + * 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 */