types.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 char>::test(state);
  41. SelectNumericTest<lua_Integer, unsigned char>::test(state);
  42. SelectNumericTest<lua_Integer, signed short>::test(state);
  43. SelectNumericTest<lua_Integer, unsigned short>::test(state);
  44. SelectNumericTest<lua_Integer, signed int>::test(state);
  45. SelectNumericTest<lua_Integer, unsigned int>::test(state);
  46. SelectNumericTest<lua_Integer, signed long int>::test(state);
  47. SelectNumericTest<lua_Integer, unsigned long int>::test(state);
  48. SelectNumericTest<lua_Integer, signed long long int>::test(state);
  49. SelectNumericTest<lua_Integer, unsigned long long int>::test(state);
  50. // Number-based types
  51. SelectNumericTest<lua_Number, float>::test(state);
  52. SelectNumericTest<lua_Number, double>::test(state);
  53. SelectNumericTest<lua_Number, long double>::test(state);
  54. lua_close(state);
  55. }
  56. TEST_CASE("types_string") {
  57. lua_State* state = luaL_newstate();
  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. lua_close(state);
  84. }
  85. TEST_CASE("types_tuple") {
  86. lua_State* state = luaL_newstate();
  87. int a = 13;
  88. std::string b("Hello");
  89. float c = 0.37;
  90. // Push normal tuple
  91. auto tuple = std::make_tuple(a, b, c);
  92. REQUIRE(luwra::push(state, tuple) == 3);
  93. // Push nested tuple
  94. auto tuple_nested = std::make_tuple(a, b, c, tuple);
  95. REQUIRE(luwra::push(state, tuple_nested) == 6);
  96. lua_close(state);
  97. }
  98. TEST_CASE("types_bool") {
  99. lua_State* state = luaL_newstate();
  100. bool value = true;
  101. REQUIRE(luwra::push(state, value) == 1);
  102. REQUIRE(luwra::read<bool>(state, -1) == value);
  103. lua_close(state);
  104. }