types.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "catch.hpp"
  2. #include <lua.hpp>
  3. #include <luwra.hpp>
  4. #include <cstring>
  5. #include <string>
  6. #include <utility>
  7. #include <type_traits>
  8. template <typename I>
  9. struct NumericTest {
  10. static
  11. void test(lua_State* state) {
  12. const I max_value = std::numeric_limits<I>::max();
  13. const I min_value = std::numeric_limits<I>::lowest();
  14. const I avg_value = (max_value + min_value) / 2;
  15. // Largest value
  16. CHECK(luwra::push(state, max_value) == 1);
  17. CHECK(luwra::read<I>(state, -1) == max_value);
  18. // Lowest value
  19. CHECK(luwra::push(state, min_value) == 1);
  20. CHECK(luwra::read<I>(state, -1) == min_value);
  21. // Average value
  22. CHECK(luwra::push(state, avg_value) == 1);
  23. CHECK(luwra::read<I>(state, -1) == avg_value);
  24. }
  25. };
  26. struct TautologyTest {
  27. static
  28. void test(lua_State*) {}
  29. };
  30. template <typename B, typename I>
  31. using SelectNumericTest =
  32. typename std::conditional<
  33. luwra::internal::NumericContainedValueBase<I, B>::qualifies,
  34. NumericTest<I>,
  35. TautologyTest
  36. >::type;
  37. TEST_CASE("types_numeric") {
  38. lua_State* state = luaL_newstate();
  39. // Integer-based types
  40. SelectNumericTest<lua_Integer, signed short>::test(state);
  41. SelectNumericTest<lua_Integer, unsigned short>::test(state);
  42. SelectNumericTest<lua_Integer, signed int>::test(state);
  43. SelectNumericTest<lua_Integer, unsigned int>::test(state);
  44. SelectNumericTest<lua_Integer, signed long int>::test(state);
  45. SelectNumericTest<lua_Integer, unsigned long int>::test(state);
  46. SelectNumericTest<lua_Integer, signed long long int>::test(state);
  47. SelectNumericTest<lua_Integer, unsigned long long int>::test(state);
  48. // Number-based types
  49. SelectNumericTest<lua_Number, float>::test(state);
  50. SelectNumericTest<lua_Number, double>::test(state);
  51. SelectNumericTest<lua_Number, long double>::test(state);
  52. lua_close(state);
  53. }
  54. TEST_CASE("types_string") {
  55. lua_State* state = luaL_newstate();
  56. const char* test_cstr = "Luwra Test String";
  57. std::string test_str(test_cstr);
  58. // Safety first
  59. REQUIRE(test_str == test_cstr);
  60. // Push both strings
  61. REQUIRE(luwra::push(state, test_cstr) == 1);
  62. REQUIRE(luwra::push(state, test_str) == 1);
  63. // They must be equal to Lua
  64. REQUIRE(luwra::equal(state, -1, -2));
  65. // Extraction as C string must not change the string's value
  66. const char* l_cstr1 = luwra::read<const char*>(state, -1);
  67. const char* l_cstr2 = luwra::read<const char*>(state, -2);
  68. REQUIRE(std::strcmp(test_cstr, l_cstr1) == 0);
  69. REQUIRE(std::strcmp(test_cstr, l_cstr2) == 0);
  70. REQUIRE(std::strcmp(test_str.c_str(), l_cstr1) == 0);
  71. REQUIRE(std::strcmp(test_str.c_str(), l_cstr2) == 0);
  72. REQUIRE(std::strcmp(l_cstr1, l_cstr2) == 0);
  73. // Extraction as C++ string must not change the string's value
  74. std::string l_str1 = luwra::read<std::string>(state, -1);
  75. std::string l_str2 = luwra::read<std::string>(state, -2);
  76. REQUIRE(l_str1 == test_cstr);
  77. REQUIRE(l_str2 == test_cstr);
  78. REQUIRE(test_str == l_str1);
  79. REQUIRE(test_str == l_str2);
  80. REQUIRE(l_str1 == l_str2);
  81. lua_close(state);
  82. }
  83. TEST_CASE("types_tuple") {
  84. lua_State* state = luaL_newstate();
  85. int a = 13;
  86. std::string b("Hello");
  87. float c = 0.37;
  88. // Push normal tuple
  89. auto tuple = std::make_tuple(a, b, c);
  90. REQUIRE(luwra::push(state, tuple) == 3);
  91. // Push nested tuple
  92. auto tuple_nested = std::make_tuple(a, b, c, tuple);
  93. REQUIRE(luwra::push(state, tuple_nested) == 6);
  94. lua_close(state);
  95. }
  96. TEST_CASE("types_bool") {
  97. lua_State* state = luaL_newstate();
  98. bool value = true;
  99. REQUIRE(luwra::push(state, value) == 1);
  100. REQUIRE(luwra::read<bool>(state, -1) == value);
  101. lua_close(state);
  102. }