types.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include <catch.hpp>
  2. #include <luwra.hpp>
  3. #include <cstring>
  4. #include <string>
  5. #include <utility>
  6. #include <type_traits>
  7. #include <vector>
  8. #include <list>
  9. #include <initializer_list>
  10. // Numbers are royally fucked. They might or might not be stored in a floating-point number, which
  11. // makes testing for integer limits pointless.
  12. //
  13. // template <typename I>
  14. // struct NumericTest {
  15. // static
  16. // void test(lua_State* state) {
  17. // const I max_value = std::numeric_limits<I>::max();
  18. // const I min_value = std::numeric_limits<I>::lowest();
  19. // const I avg_value = (max_value + min_value) / 2;
  20. // // Largest value
  21. // CHECK((luwra::push(state, max_value) == 1));
  22. // CHECK((luwra::read<I>(state, -1) == max_value));
  23. // // Lowest value
  24. // CHECK((luwra::push(state, min_value) == 1));
  25. // CHECK((luwra::read<I>(state, -1) == min_value));
  26. // // Average value
  27. // CHECK((luwra::push(state, avg_value) == 1));
  28. // CHECK((luwra::read<I>(state, -1) == avg_value));
  29. // }
  30. // };
  31. // TEST_CASE("NumberLimits") {
  32. // luwra::StateWrapper state;
  33. // // Integer-based types
  34. // NumericTest<signed char>::test(state);
  35. // NumericTest<unsigned char>::test(state);
  36. // NumericTest<signed short>::test(state);
  37. // NumericTest<unsigned short>::test(state);
  38. // NumericTest<signed int>::test(state);
  39. // NumericTest<unsigned int>::test(state);
  40. // NumericTest<signed long int>::test(state);
  41. // NumericTest<unsigned long int>::test(state);
  42. // NumericTest<signed long long int>::test(state);
  43. // NumericTest<unsigned long long int>::test(state);
  44. // // Number-based types
  45. // NumericTest<float>::test(state);
  46. // NumericTest<double>::test(state);
  47. // NumericTest<long double>::test(state);
  48. // }
  49. TEST_CASE("Numbers") {
  50. luwra::StateWrapper state;
  51. REQUIRE(luwra::push(state, 1337) == 1);
  52. REQUIRE(luwra::push(state, 13.37) == 1);
  53. REQUIRE(luwra::read<int>(state, -2) == 1337);
  54. REQUIRE(luwra::read<float>(state, -1) == 13.37f);
  55. }
  56. TEST_CASE("Strings") {
  57. luwra::StateWrapper state;
  58. const char* test_cstr = "Luwra Test String";
  59. std::string test_str(test_cstr);
  60. // Safety first
  61. REQUIRE(test_str == test_cstr);
  62. // Push both strings
  63. REQUIRE(luwra::push(state, test_cstr) == 1);
  64. REQUIRE(luwra::push(state, test_str) == 1);
  65. // They must be equal to Lua
  66. REQUIRE(luwra::equal(state, -1, -2));
  67. // Extraction as C string must not change the string's value
  68. const char* l_cstr1 = luwra::read<const char*>(state, -1);
  69. const char* l_cstr2 = luwra::read<const char*>(state, -2);
  70. REQUIRE(std::strcmp(test_cstr, l_cstr1) == 0);
  71. REQUIRE(std::strcmp(test_cstr, l_cstr2) == 0);
  72. REQUIRE(std::strcmp(test_str.c_str(), l_cstr1) == 0);
  73. REQUIRE(std::strcmp(test_str.c_str(), l_cstr2) == 0);
  74. REQUIRE(std::strcmp(l_cstr1, l_cstr2) == 0);
  75. // Extraction as C++ string must not change the string's value
  76. std::string l_str1 = luwra::read<std::string>(state, -1);
  77. std::string l_str2 = luwra::read<std::string>(state, -2);
  78. REQUIRE(l_str1 == test_cstr);
  79. REQUIRE(l_str2 == test_cstr);
  80. REQUIRE(test_str == l_str1);
  81. REQUIRE(test_str == l_str2);
  82. REQUIRE(l_str1 == l_str2);
  83. }
  84. TEST_CASE("Tuples") {
  85. luwra::StateWrapper state;
  86. int a = 13;
  87. std::string b("Hello");
  88. float c = 0.37;
  89. // Push normal tuple
  90. auto tuple = std::make_tuple(a, b, c);
  91. REQUIRE(luwra::push(state, tuple) == 3);
  92. REQUIRE(luwra::read<int>(state, -3) == a);
  93. REQUIRE(luwra::read<std::string>(state, -2) == b);
  94. REQUIRE(luwra::read<float>(state, -1) == c);
  95. // Push nested tuple
  96. auto tuple_nested = std::make_tuple(a, b, c, tuple);
  97. REQUIRE(luwra::push(state, tuple_nested) == 6);
  98. REQUIRE(luwra::read<int>(state, -6) == a);
  99. REQUIRE(luwra::read<std::string>(state, -5) == b);
  100. REQUIRE(luwra::read<float>(state, -4) == c);
  101. REQUIRE(luwra::read<int>(state, -3) == a);
  102. REQUIRE(luwra::read<std::string>(state, -2) == b);
  103. REQUIRE(luwra::read<float>(state, -1) == c);
  104. }
  105. TEST_CASE("Boolean") {
  106. luwra::StateWrapper state;
  107. REQUIRE(luwra::push(state, true) == 1);
  108. REQUIRE(luwra::read<bool>(state, -1) == true);
  109. REQUIRE(luwra::push(state, false) == 1);
  110. REQUIRE(luwra::read<bool>(state, -1) == false);
  111. }
  112. TEST_CASE("Pushable") {
  113. luwra::StateWrapper state;
  114. luwra::Pushable pushable(1337);
  115. REQUIRE(luwra::push(state, pushable) == 1);
  116. REQUIRE(luwra::read<int>(state, -1) == 1337);
  117. }
  118. TEST_CASE("Value<vector>") {
  119. luwra::StateWrapper state;
  120. state.loadStandardLibrary();
  121. std::vector<int> v {1, 2, 3, 4, 5};
  122. REQUIRE(luwra::push(state, v) == 1);
  123. state["v"] = v;
  124. REQUIRE(state.runString(
  125. "x = 0\n"
  126. "for i, j in ipairs(v) do x = x + i + j end\n"
  127. "return x"
  128. ) == LUA_OK);
  129. REQUIRE(luwra::read<int>(state, -1) == 30);
  130. }
  131. TEST_CASE("Value<list>") {
  132. luwra::StateWrapper state;
  133. state.loadStandardLibrary();
  134. std::list<int> v {1, 2, 3, 4, 5};
  135. REQUIRE(luwra::push(state, v) == 1);
  136. state["v"] = v;
  137. REQUIRE(state.runString(
  138. "x = 0\n"
  139. "for i, j in ipairs(v) do x = x + i + j end\n"
  140. "return x"
  141. ) == LUA_OK);
  142. REQUIRE(luwra::read<int>(state, -1) == 30);
  143. }
  144. TEST_CASE("Value<map>") {
  145. luwra::StateWrapper state;
  146. std::map<luwra::Pushable, luwra::Pushable> v {
  147. {"hello", 13},
  148. {37, "world"}
  149. };
  150. REQUIRE(luwra::push(state, v) == 1);
  151. state["v"] = v;
  152. REQUIRE(state.runString("return v.hello, v[37]") == LUA_OK);
  153. REQUIRE(luwra::read<int>(state, -2) == 13);
  154. REQUIRE(luwra::read<std::string>(state, -1) == "world");
  155. }