stack.cpp 876 B

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