stack.cpp 875 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "catch.hpp"
  2. #include <lua.hpp>
  3. #include <luwra.hpp>
  4. int test_function_1(int a, int b) {
  5. return a - b;
  6. }
  7. int test_function_2(int a, int b, int c) {
  8. return a + b * c;
  9. }
  10. void test_function_3(int) {
  11. }
  12. TEST_CASE("stack_interaction") {
  13. lua_State* state = luaL_newstate();
  14. luwra::push(state, 1);
  15. luwra::push(state, 2);
  16. luwra::push(state, 4);
  17. // Redundant function
  18. luwra::apply(state, test_function_3);
  19. // Absolute index
  20. REQUIRE(luwra::apply(state, test_function_1) == -1);
  21. REQUIRE(luwra::apply(state, 1, test_function_1) == -1);
  22. REQUIRE(luwra::apply(state, test_function_2) == 9);
  23. REQUIRE(luwra::apply(state, 1, test_function_2) == 9);
  24. // Relative index
  25. REQUIRE(luwra::apply(state, -2, test_function_1) == -2);
  26. REQUIRE(luwra::apply(state, -3, test_function_1) == -1);
  27. REQUIRE(luwra::apply(state, -3, test_function_2) == 9);
  28. lua_close(state);
  29. }