types.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <catch.hpp>
  2. #include <luwra.hpp>
  3. #include <cstring>
  4. #include <string>
  5. #include <utility>
  6. #include <type_traits>
  7. #if LUA_VERSION_NUM >= 503
  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("NumberLimits") {
  38. luwra::StateWrapper state;
  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. }
  55. #endif /* LUA_VERSION_NUM >= 503 */
  56. TEST_CASE("Numbers") {
  57. luwra::StateWrapper state;
  58. REQUIRE(luwra::push(state, 1337) == 1);
  59. REQUIRE(luwra::push(state, 13.37) == 1);
  60. REQUIRE(luwra::read<int>(state, -2) == 1337);
  61. REQUIRE(luwra::read<float>(state, -1) == 13.37f);
  62. }
  63. TEST_CASE("Strings") {
  64. luwra::StateWrapper state;
  65. const char* test_cstr = "Luwra Test String";
  66. std::string test_str(test_cstr);
  67. // Safety first
  68. REQUIRE(test_str == test_cstr);
  69. // Push both strings
  70. REQUIRE(luwra::push(state, test_cstr) == 1);
  71. REQUIRE(luwra::push(state, test_str) == 1);
  72. // They must be equal to Lua
  73. REQUIRE(luwra::equal(state, -1, -2));
  74. // Extraction as C string must not change the string's value
  75. const char* l_cstr1 = luwra::read<const char*>(state, -1);
  76. const char* l_cstr2 = luwra::read<const char*>(state, -2);
  77. REQUIRE(std::strcmp(test_cstr, l_cstr1) == 0);
  78. REQUIRE(std::strcmp(test_cstr, l_cstr2) == 0);
  79. REQUIRE(std::strcmp(test_str.c_str(), l_cstr1) == 0);
  80. REQUIRE(std::strcmp(test_str.c_str(), l_cstr2) == 0);
  81. REQUIRE(std::strcmp(l_cstr1, l_cstr2) == 0);
  82. // Extraction as C++ string must not change the string's value
  83. std::string l_str1 = luwra::read<std::string>(state, -1);
  84. std::string l_str2 = luwra::read<std::string>(state, -2);
  85. REQUIRE(l_str1 == test_cstr);
  86. REQUIRE(l_str2 == test_cstr);
  87. REQUIRE(test_str == l_str1);
  88. REQUIRE(test_str == l_str2);
  89. REQUIRE(l_str1 == l_str2);
  90. }
  91. TEST_CASE("Tuples") {
  92. luwra::StateWrapper state;
  93. int a = 13;
  94. std::string b("Hello");
  95. float c = 0.37;
  96. // Push normal tuple
  97. auto tuple = std::make_tuple(a, b, c);
  98. REQUIRE(luwra::push(state, tuple) == 3);
  99. // Push nested tuple
  100. auto tuple_nested = std::make_tuple(a, b, c, tuple);
  101. REQUIRE(luwra::push(state, tuple_nested) == 6);
  102. }
  103. TEST_CASE("Boolean") {
  104. luwra::StateWrapper state;
  105. bool value = true;
  106. REQUIRE(luwra::push(state, value) == 1);
  107. REQUIRE(luwra::read<bool>(state, -1) == value);
  108. }