types.cpp 3.6 KB

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