From 377472a6dd2c9bdff4d5667f5789c49aee2abb4c Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Fri, 7 Oct 2016 14:43:09 +0200 Subject: Added sol2 header only library as future replacement for luabridge (nw) --- 3rdparty/sol2/test_coroutines.cpp | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 3rdparty/sol2/test_coroutines.cpp (limited to '3rdparty/sol2/test_coroutines.cpp') diff --git a/3rdparty/sol2/test_coroutines.cpp b/3rdparty/sol2/test_coroutines.cpp new file mode 100644 index 00000000000..1581dc3d5a8 --- /dev/null +++ b/3rdparty/sol2/test_coroutines.cpp @@ -0,0 +1,64 @@ +#define SOL_CHECK_ARGUMENTS + +#include +#include + +TEST_CASE("threading/coroutines", "ensure calling a coroutine works") { + const auto& script = R"(counter = 20 + +function loop() + while counter ~= 30 + do + coroutine.yield(counter); + counter = counter + 1; + end + return counter +end +)"; + + sol::state lua; + lua.open_libraries(sol::lib::base, sol::lib::coroutine); + lua.script(script); + sol::coroutine cr = lua["loop"]; + + int counter; + for (counter = 20; counter < 31 && cr; ++counter) { + int value = cr(); + if (counter != value) { + throw std::logic_error("fuck"); + } + } + counter -= 1; + REQUIRE(counter == 30); +} + +TEST_CASE("threading/new-thread-coroutines", "ensure calling a coroutine works when the work is put on a different thread") { + const auto& script = R"(counter = 20 + +function loop() + while counter ~= 30 + do + coroutine.yield(counter); + counter = counter + 1; + end + return counter +end +)"; + + sol::state lua; + lua.open_libraries(sol::lib::base, sol::lib::coroutine); + lua.script(script); + sol::thread runner = sol::thread::create(lua.lua_state()); + sol::state_view runnerstate = runner.state(); + sol::coroutine cr = runnerstate["loop"]; + + int counter; + for (counter = 20; counter < 31 && cr; ++counter) { + int value = cr(); + if (counter != value) { + throw std::logic_error("fuck"); + } + } + counter -= 1; + REQUIRE(counter == 30); +} \ No newline at end of file -- cgit v1.2.3-70-g09d2