stack.cpp 896 B

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