aboutsummaryrefslogtreecommitdiff
path: root/unit_tests
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-12-20 15:58:02 +0100
committerBenoit Germain <benoit.germain@ubisoft.com>2024-12-20 15:58:02 +0100
commit7f5fb073782555839c11e914092d7b7ae98b62ab (patch)
tree06dd6e818efe778193b2083a0ff92cfc4aefe2b5 /unit_tests
parentfe8d396980f18bf09ee2717817cd795130cf64d1 (diff)
downloadlanes-7f5fb073782555839c11e914092d7b7ae98b62ab.tar.gz
lanes-7f5fb073782555839c11e914092d7b7ae98b62ab.tar.bz2
lanes-7f5fb073782555839c11e914092d7b7ae98b62ab.zip
Reorganized tests for the Test Explorer
Diffstat (limited to 'unit_tests')
-rw-r--r--unit_tests/deep_tests.cpp2
-rw-r--r--unit_tests/embedded_tests.cpp2
-rw-r--r--unit_tests/init_and_shutdown.cpp40
-rw-r--r--unit_tests/lane_tests.cpp60
-rw-r--r--unit_tests/legacy_tests.cpp47
-rw-r--r--unit_tests/linda_tests.cpp22
-rw-r--r--unit_tests/shared.cpp24
-rw-r--r--unit_tests/shared.h11
8 files changed, 155 insertions, 53 deletions
diff --git a/unit_tests/deep_tests.cpp b/unit_tests/deep_tests.cpp
index 60dadb7..2c9ccdf 100644
--- a/unit_tests/deep_tests.cpp
+++ b/unit_tests/deep_tests.cpp
@@ -8,7 +8,7 @@
8// ################################################################################################# 8// #################################################################################################
9// ################################################################################################# 9// #################################################################################################
10 10
11TEST_CASE("deep_test") 11TEST_CASE("misc.deep_test")
12{ 12{
13 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; 13 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } };
14 S.requireSuccess( 14 S.requireSuccess(
diff --git a/unit_tests/embedded_tests.cpp b/unit_tests/embedded_tests.cpp
index a6952b2..bf2dd6e 100644
--- a/unit_tests/embedded_tests.cpp
+++ b/unit_tests/embedded_tests.cpp
@@ -23,7 +23,7 @@ namespace
23 23
24// ################################################################################################# 24// #################################################################################################
25 25
26TEST_CASE("embedding") 26TEST_CASE("lanes.embedding")
27{ 27{
28 LuaState S{ LuaState::WithBaseLibs{ false }, LuaState::WithFixture{ false } }; 28 LuaState S{ LuaState::WithBaseLibs{ false }, LuaState::WithFixture{ false } };
29 29
diff --git a/unit_tests/init_and_shutdown.cpp b/unit_tests/init_and_shutdown.cpp
index 83c0350..78c472f 100644
--- a/unit_tests/init_and_shutdown.cpp
+++ b/unit_tests/init_and_shutdown.cpp
@@ -3,7 +3,7 @@
3 3
4// ################################################################################################# 4// #################################################################################################
5 5
6TEST_CASE("require 'lanes'") 6TEST_CASE("lanes.require 'lanes'")
7{ 7{
8 LuaState L{ LuaState::WithBaseLibs{ false }, LuaState::WithFixture{ false } }; 8 LuaState L{ LuaState::WithBaseLibs{ false }, LuaState::WithFixture{ false } };
9 9
@@ -668,7 +668,7 @@ TEST_CASE("lanes.finally")
668 { 668 {
669 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; 669 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } };
670 // we need Lanes to be up. Since we run several 'scripts', we store it as a global 670 // we need Lanes to be up. Since we run several 'scripts', we store it as a global
671 S.requireSuccess("lanes = require 'lanes'"); 671 S.requireSuccess("lanes = require 'lanes'.configure()");
672 // we can set a function 672 // we can set a function
673 S.requireSuccess("lanes.finally(function() end)"); 673 S.requireSuccess("lanes.finally(function() end)");
674 // we can clear it 674 // we can clear it
@@ -689,7 +689,7 @@ TEST_CASE("lanes.finally")
689 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; 689 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } };
690 690
691 // we need Lanes to be up. Since we run several 'scripts', we store it as a global 691 // we need Lanes to be up. Since we run several 'scripts', we store it as a global
692 S.requireSuccess("lanes = require 'lanes'"); 692 S.requireSuccess("lanes = require 'lanes'.configure()");
693 // works because we have package.preload.fixture = luaopen_fixture 693 // works because we have package.preload.fixture = luaopen_fixture
694 S.requireSuccess("fixture = require 'fixture'"); 694 S.requireSuccess("fixture = require 'fixture'");
695 // set our detectable finalizer 695 // set our detectable finalizer
@@ -699,6 +699,38 @@ TEST_CASE("lanes.finally")
699 // the finalizer should be called 699 // the finalizer should be called
700 REQUIRE(S.finalizerWasCalled); 700 REQUIRE(S.finalizerWasCalled);
701 } 701 }
702
703 // ---------------------------------------------------------------------------------------------
704
705 SECTION("shutdown with an uncooperative lane")
706 {
707 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } };
708 S.requireSuccess("lanes = require 'lanes'.configure()");
709
710 // prepare a callback for lanes.finally()
711 static bool _wasCalled{};
712 static bool _allLanesTerminated{};
713 auto _finallyCB{ +[](lua_State* const L_) { _wasCalled = true; _allLanesTerminated = lua_toboolean(L_, 1); return 0; } };
714 lua_pushcfunction(S, _finallyCB);
715 lua_setglobal(S, "finallyCB");
716 // start a lane that lasts a long time
717 std::string_view const _script{
718 " lanes.finally(finallyCB)"
719 " g = lanes.gen('*',"
720 " {name = 'auto'},"
721 " function()"
722 " for i = 1,1e37 do end" // no cooperative cancellation checks here!
723 " end)"
724 " g()"
725 };
726 S.requireSuccess(_script);
727 // close the state before the lane ends.
728 // since we don't wait at all, it is possible that the OS thread for the lane hasn't even started at that point
729 S.close();
730 // the finally handler should have been called, and told all lanes are stopped
731 REQUIRE(_wasCalled);
732 REQUIRE(_allLanesTerminated);
733 }
702} 734}
703 735
704// ################################################################################################# 736// #################################################################################################
@@ -719,7 +751,7 @@ namespace
719 751
720// ################################################################################################# 752// #################################################################################################
721 753
722TEST_CASE("on_state_create setting") 754TEST_CASE("lanes.on_state_create setting")
723{ 755{
724 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; 756 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } };
725 757
diff --git a/unit_tests/lane_tests.cpp b/unit_tests/lane_tests.cpp
index 7f5f112..498c96e 100644
--- a/unit_tests/lane_tests.cpp
+++ b/unit_tests/lane_tests.cpp
@@ -89,35 +89,6 @@ TEST_CASE("lanes.gen")
89 89
90 // --------------------------------------------------------------------------------------------- 90 // ---------------------------------------------------------------------------------------------
91 91
92 SECTION("lanes.finally") // TODO: move this section somewhere else, in a dedicated finally TEST_CASE
93 {
94 // prepare a callback for lanes.finally()
95 static bool _wasCalled{};
96 static bool _allLanesTerminated{};
97 auto _finallyCB{ +[](lua_State* const L_) { _wasCalled = true; _allLanesTerminated = lua_toboolean(L_, 1); return 0; } };
98 lua_pushcfunction(S, _finallyCB);
99 lua_setglobal(S, "finallyCB");
100 // start a lane that lasts a long time
101 std::string_view const _script{
102 " lanes.finally(finallyCB)"
103 " g = lanes.gen('*',"
104 " {name = 'auto'},"
105 " function()"
106 " for i = 1,1e37 do end" // no cooperative cancellation checks here!
107 " end)"
108 " g()"
109 };
110 S.requireSuccess(_script);
111 // close the state before the lane ends.
112 // since we don't wait at all, it is possible that the OS thread for the lane hasn't even started at that point
113 S.close();
114 // the finally handler should have been called, and told all lanes are stopped
115 REQUIRE(_wasCalled);
116 REQUIRE(_allLanesTerminated);
117 }
118
119 // ---------------------------------------------------------------------------------------------
120
121 SECTION("default thread name is '<unnamed>'") 92 SECTION("default thread name is '<unnamed>'")
122 { 93 {
123 std::string_view const _script{ 94 std::string_view const _script{
@@ -172,7 +143,7 @@ TEST_CASE("lanes.gen")
172// ################################################################################################# 143// #################################################################################################
173// ################################################################################################# 144// #################################################################################################
174 145
175TEST_CASE("lane:cancel") 146TEST_CASE("lane.cancel")
176{ 147{
177 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; 148 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } };
178 149
@@ -252,7 +223,33 @@ TEST_CASE("lane:cancel")
252// ################################################################################################# 223// #################################################################################################
253// ################################################################################################# 224// #################################################################################################
254 225
255TEST_CASE("lane scripted tests") 226// unfortunately, VS Test adapter does not list individual sections,
227// so let's create a separate test case for each file with an ugly macro...
228
229#define MAKE_TEST_CASE(DIR, FILE, CONDITION)\
230TEST_CASE("scripted tests." #DIR "." #FILE) \
231{ \
232 FileRunner _runner(R"(.\lanes\unit_tests\scripts)"); \
233 _runner.performTest(FileRunnerParam{ #DIR "/" #FILE, TestType::CONDITION }); \
234}
235
236#if LUAJIT_FLAVOR() == 0
237MAKE_TEST_CASE(lane, cooperative_shutdown, AssertNoLuaError)
238#endif // LUAJIT_FLAVOR
239MAKE_TEST_CASE(lane, uncooperative_shutdown, AssertThrows)
240MAKE_TEST_CASE(lane, tasking_basic, AssertNoLuaError)
241MAKE_TEST_CASE(lane, tasking_cancelling, AssertNoLuaError)
242MAKE_TEST_CASE(lane, tasking_comms_criss_cross, AssertNoLuaError)
243MAKE_TEST_CASE(lane, tasking_communications, AssertNoLuaError)
244MAKE_TEST_CASE(lane, tasking_error, AssertNoLuaError)
245MAKE_TEST_CASE(lane, tasking_join_test, AssertNoLuaError)
246MAKE_TEST_CASE(lane, tasking_send_receive_code, AssertNoLuaError)
247MAKE_TEST_CASE(lane, stdlib_naming, AssertNoLuaError)
248MAKE_TEST_CASE(coro, basics, AssertNoLuaError)
249MAKE_TEST_CASE(coro, error_handling, AssertNoLuaError)
250
251/*
252TEST_CASE("lanes.scripted tests")
256{ 253{
257 auto const& _testParam = GENERATE( 254 auto const& _testParam = GENERATE(
258 FileRunnerParam{ PUC_LUA_ONLY("lane/cooperative_shutdown"), TestType::AssertNoLuaError }, // 0 255 FileRunnerParam{ PUC_LUA_ONLY("lane/cooperative_shutdown"), TestType::AssertNoLuaError }, // 0
@@ -272,3 +269,4 @@ TEST_CASE("lane scripted tests")
272 FileRunner _runner(R"(.\lanes\unit_tests\scripts)"); 269 FileRunner _runner(R"(.\lanes\unit_tests\scripts)");
273 _runner.performTest(_testParam); 270 _runner.performTest(_testParam);
274} 271}
272*/
diff --git a/unit_tests/legacy_tests.cpp b/unit_tests/legacy_tests.cpp
index cb91c93..509bb22 100644
--- a/unit_tests/legacy_tests.cpp
+++ b/unit_tests/legacy_tests.cpp
@@ -7,7 +7,51 @@
7// ################################################################################################# 7// #################################################################################################
8// ################################################################################################# 8// #################################################################################################
9 9
10TEST_CASE("legacy scripted tests") 10// unfortunately, VS Test adapter does not list individual sections,
11// so let's create a separate test case for each file with an ugly macro...
12
13#define MAKE_TEST_CASE(FILE) \
14TEST_CASE("scripted tests.legacy." #FILE) \
15{ \
16 FileRunner _runner(R"(.\lanes\tests\)"); \
17 _runner.performTest(FileRunnerParam{ #FILE, TestType::AssertNoLuaError }); \
18}
19
20MAKE_TEST_CASE(appendud)
21MAKE_TEST_CASE(atexit)
22MAKE_TEST_CASE(atomic)
23MAKE_TEST_CASE(basic)
24MAKE_TEST_CASE(cancel)
25MAKE_TEST_CASE(cyclic)
26MAKE_TEST_CASE(deadlock)
27MAKE_TEST_CASE(errhangtest)
28MAKE_TEST_CASE(error)
29MAKE_TEST_CASE(fibonacci)
30MAKE_TEST_CASE(fifo)
31MAKE_TEST_CASE(finalizer)
32MAKE_TEST_CASE(func_is_string)
33MAKE_TEST_CASE(irayo_closure)
34MAKE_TEST_CASE(irayo_recursive)
35MAKE_TEST_CASE(keeper)
36//MAKE_TEST_CASE(linda_perf)
37#if LUA_VERSION_NUM == 504
38MAKE_TEST_CASE(manual_register)
39#endif // LUA_VERSION_NUM
40MAKE_TEST_CASE(nameof)
41MAKE_TEST_CASE(objects)
42MAKE_TEST_CASE(package)
43MAKE_TEST_CASE(pingpong)
44MAKE_TEST_CASE(recursive)
45MAKE_TEST_CASE(require)
46MAKE_TEST_CASE(rupval)
47MAKE_TEST_CASE(timer)
48#if LUA_VERSION_NUM == 504
49MAKE_TEST_CASE(tobeclosed)
50#endif // LUA_VERSION_NUM
51MAKE_TEST_CASE(track_lanes)
52
53/*
54TEST_CASE("lanes.legacy scripted tests")
11{ 55{
12 auto const& _testParam = GENERATE( 56 auto const& _testParam = GENERATE(
13 FileRunnerParam{ "appendud", TestType::AssertNoLuaError } // 0 57 FileRunnerParam{ "appendud", TestType::AssertNoLuaError } // 0
@@ -43,5 +87,6 @@ TEST_CASE("legacy scripted tests")
43 FileRunner _runner(R"(.\lanes\tests\)"); 87 FileRunner _runner(R"(.\lanes\tests\)");
44 _runner.performTest(_testParam); 88 _runner.performTest(_testParam);
45} 89}
90*/
46 91
47#endif // RUN_LEGACY_TESTS \ No newline at end of file 92#endif // RUN_LEGACY_TESTS \ No newline at end of file
diff --git a/unit_tests/linda_tests.cpp b/unit_tests/linda_tests.cpp
index 29eb28c..e956999 100644
--- a/unit_tests/linda_tests.cpp
+++ b/unit_tests/linda_tests.cpp
@@ -3,7 +3,7 @@
3 3
4// ################################################################################################# 4// #################################################################################################
5 5
6TEST_CASE("single Keeper Lindas") 6TEST_CASE("linda.single Keeper")
7{ 7{
8 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; 8 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } };
9 S.requireSuccess("lanes = require 'lanes'"); 9 S.requireSuccess("lanes = require 'lanes'");
@@ -306,7 +306,7 @@ TEST_CASE("single Keeper Lindas")
306// ################################################################################################# 306// #################################################################################################
307// ################################################################################################# 307// #################################################################################################
308 308
309TEST_CASE("multi Keeper Lindas") 309TEST_CASE("linda.multi Keeper")
310{ 310{
311 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; 311 LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } };
312 312
@@ -323,7 +323,22 @@ TEST_CASE("multi Keeper Lindas")
323// ################################################################################################# 323// #################################################################################################
324// ################################################################################################# 324// #################################################################################################
325 325
326TEST_CASE("linda scripted tests") 326// unfortunately, VS Test adapter does not list individual sections,
327// so let's create a separate test case for each file with an ugly macro...
328
329#define MAKE_TEST_CASE(DIR, FILE) \
330TEST_CASE("scripted tests." #DIR "." #FILE) \
331{ \
332 FileRunner _runner(R"(.\lanes\unit_tests\scripts)"); \
333 _runner.performTest(FileRunnerParam{ #DIR "/" #FILE, TestType::AssertNoLuaError }); \
334}
335
336MAKE_TEST_CASE(linda, send_receive)
337MAKE_TEST_CASE(linda, send_registered_userdata)
338MAKE_TEST_CASE(linda, multiple_keepers)
339
340/*
341TEST_CASE("linda.scripted tests")
327{ 342{
328 auto const& _testParam = GENERATE( 343 auto const& _testParam = GENERATE(
329 FileRunnerParam{ "linda/send_receive", TestType::AssertNoLuaError }, 344 FileRunnerParam{ "linda/send_receive", TestType::AssertNoLuaError },
@@ -334,3 +349,4 @@ TEST_CASE("linda scripted tests")
334 FileRunner _runner(R"(.\lanes\unit_tests\scripts)"); 349 FileRunner _runner(R"(.\lanes\unit_tests\scripts)");
335 _runner.performTest(_testParam); 350 _runner.performTest(_testParam);
336} 351}
352*/ \ No newline at end of file
diff --git a/unit_tests/shared.cpp b/unit_tests/shared.cpp
index 0825227..d2b4ef7 100644
--- a/unit_tests/shared.cpp
+++ b/unit_tests/shared.cpp
@@ -98,7 +98,7 @@ namespace
98// ################################################################################################# 98// #################################################################################################
99// ################################################################################################# 99// #################################################################################################
100 100
101TEST_CASE("stack checker") 101TEST_CASE("lanes.stack checker")
102{ 102{
103 LuaState _L{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; 103 LuaState _L{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } };
104 StackChecker::CallsCassert = false; 104 StackChecker::CallsCassert = false;
@@ -292,10 +292,10 @@ LuaError LuaState::loadFile(std::filesystem::path const& root_, std::string_view
292void LuaState::requireFailure(std::string_view const& script_) 292void LuaState::requireFailure(std::string_view const& script_)
293{ 293{
294 auto const _result{ doString(script_) }; 294 auto const _result{ doString(script_) };
295 REQUIRE(_result != LuaError::OK);
296 if (_result == LuaError::OK) { 295 if (_result == LuaError::OK) {
297 INFO(luaG_tostring(L, kIdxTop)); 296 WARN(luaG_tostring(L, kIdxTop));
298 } 297 }
298 REQUIRE(_result != LuaError::OK);
299 lua_settop(L, 0); 299 lua_settop(L, 0);
300} 300}
301 301
@@ -304,10 +304,10 @@ void LuaState::requireFailure(std::string_view const& script_)
304void LuaState::requireNotReturnedString(std::string_view const& script_, std::string_view const& unexpected_) 304void LuaState::requireNotReturnedString(std::string_view const& script_, std::string_view const& unexpected_)
305{ 305{
306 auto const _result{ doStringAndRet(script_) }; 306 auto const _result{ doStringAndRet(script_) };
307 REQUIRE(_result != unexpected_);
308 if (_result == unexpected_) { 307 if (_result == unexpected_) {
309 INFO(_result); 308 WARN(_result);
310 } 309 }
310 REQUIRE(_result != unexpected_);
311 lua_settop(L, 0); 311 lua_settop(L, 0);
312} 312}
313 313
@@ -316,10 +316,10 @@ void LuaState::requireNotReturnedString(std::string_view const& script_, std::st
316void LuaState::requireReturnedString(std::string_view const& script_, std::string_view const& expected_) 316void LuaState::requireReturnedString(std::string_view const& script_, std::string_view const& expected_)
317{ 317{
318 auto const _result{ doStringAndRet(script_) }; 318 auto const _result{ doStringAndRet(script_) };
319 REQUIRE(_result == expected_);
320 if (_result != expected_) { 319 if (_result != expected_) {
321 INFO(_result); 320 WARN(_result);
322 } 321 }
322 REQUIRE(_result == expected_);
323 lua_settop(L, 0); 323 lua_settop(L, 0);
324} 324}
325 325
@@ -328,10 +328,10 @@ void LuaState::requireReturnedString(std::string_view const& script_, std::strin
328void LuaState::requireSuccess(std::string_view const& script_) 328void LuaState::requireSuccess(std::string_view const& script_)
329{ 329{
330 auto const _result{ doString(script_) }; 330 auto const _result{ doString(script_) };
331 REQUIRE(_result == LuaError::OK);
332 if (_result != LuaError::OK) { 331 if (_result != LuaError::OK) {
333 INFO(luaG_tostring(L, kIdxTop)); 332 WARN(luaG_tostring(L, kIdxTop));
334 } 333 }
334 REQUIRE(_result == LuaError::OK);
335 lua_settop(L, 0); 335 lua_settop(L, 0);
336} 336}
337 337
@@ -340,10 +340,10 @@ void LuaState::requireSuccess(std::string_view const& script_)
340void LuaState::requireSuccess(std::filesystem::path const& root_, std::string_view const& path_) 340void LuaState::requireSuccess(std::filesystem::path const& root_, std::string_view const& path_)
341{ 341{
342 auto const _result{ doFile(root_, path_) }; 342 auto const _result{ doFile(root_, path_) };
343 REQUIRE(_result == LuaError::OK);
344 if (_result != LuaError::OK) { 343 if (_result != LuaError::OK) {
345 INFO(luaG_tostring(L, kIdxTop)); 344 WARN(luaG_tostring(L, kIdxTop));
346 } 345 }
346 REQUIRE(_result == LuaError::OK);
347 lua_settop(L, 0); 347 lua_settop(L, 0);
348} 348}
349 349
@@ -360,7 +360,7 @@ LuaError LuaState::runChunk() const
360// ################################################################################################# 360// #################################################################################################
361// ################################################################################################# 361// #################################################################################################
362 362
363TEST_CASE("LuaState::doString") 363TEST_CASE("LuaState.doString")
364{ 364{
365 LuaState _L{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; 365 LuaState _L{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } };
366 // if the script fails to load, we should find the error message at the top of the stack 366 // if the script fails to load, we should find the error message at the top of the stack
diff --git a/unit_tests/shared.h b/unit_tests/shared.h
index 2a32269..2e319e4 100644
--- a/unit_tests/shared.h
+++ b/unit_tests/shared.h
@@ -67,6 +67,17 @@ struct FileRunnerParam
67 TestType test; 67 TestType test;
68}; 68};
69 69
70// Define a specialization for FileRunnerParam in Catch::Detail::stringify
71namespace Catch {
72 namespace Detail {
73 template <>
74 inline std::string stringify(FileRunnerParam const& param_)
75 {
76 return std::string{ param_.script };
77 }
78 } // namespace Detail
79} // namespace Catch
80
70class FileRunner : private LuaState 81class FileRunner : private LuaState
71{ 82{
72 private: 83 private: