1 #define SOL_CHECK_ARGUMENTS
2 
3 #include <catch.hpp>
4 #include <sol.hpp>
5 
6 TEST_CASE("threading/coroutines", "ensure calling a coroutine works") {
7 	const auto& script = R"(counter = 20
8 
9 function loop()
10     while counter ~= 30
11     do
12         coroutine.yield(counter);
13         counter = counter + 1;
14     end
15     return counter
16 end
17 )";
18 
19 	sol::state lua;
20 	lua.open_libraries(sol::lib::base, sol::lib::coroutine);
21 	lua.script(script);
22 	sol::coroutine cr = lua["loop"];
23 
24 	int counter;
25 	for (counter = 20; counter < 31 && cr; ++counter) {
26 		int value = cr();
27 		if (counter != value) {
28 			throw std::logic_error("fuck");
29 		}
30 	}
31 	counter -= 1;
32 	REQUIRE(counter == 30);
33 }
34 
35 TEST_CASE("threading/new-thread-coroutines", "ensure calling a coroutine works when the work is put on a different thread") {
36 	const auto& script = R"(counter = 20
37 
38 function loop()
39     while counter ~= 30
40     do
41         coroutine.yield(counter);
42         counter = counter + 1;
43     end
44     return counter
45 end
46 )";
47 
48 	sol::state lua;
49 	lua.open_libraries(sol::lib::base, sol::lib::coroutine);
50 	lua.script(script);
51 	sol::thread runner = sol::thread::create(lua.lua_state());
52 	sol::state_view runnerstate = runner.state();
53 	sol::coroutine cr = runnerstate["loop"];
54 
55 	int counter;
56 	for (counter = 20; counter < 31 && cr; ++counter) {
57 		int value = cr();
58 		if (counter != value) {
59 			throw std::logic_error("fuck");
60 		}
61 	}
62 	counter -= 1;
63 	REQUIRE(counter == 30);
64 }