types.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <catch.hpp>
  2. #include <luwra.hpp>
  3. #include <cstring>
  4. #include <string>
  5. #include <utility>
  6. #include <type_traits>
  7. #if false
  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. TEST_CASE("NumberLimits") {
  27. luwra::StateWrapper state;
  28. // Integer-based types
  29. NumericTest<signed char>::test(state);
  30. NumericTest<unsigned char>::test(state);
  31. NumericTest<signed short>::test(state);
  32. NumericTest<unsigned short>::test(state);
  33. NumericTest<signed int>::test(state);
  34. NumericTest<unsigned int>::test(state);
  35. NumericTest<signed long int>::test(state);
  36. NumericTest<unsigned long int>::test(state);
  37. NumericTest<signed long long int>::test(state);
  38. NumericTest<unsigned long long int>::test(state);
  39. // Number-based types
  40. NumericTest<float>::test(state);
  41. NumericTest<double>::test(state);
  42. NumericTest<long double>::test(state);
  43. }
  44. #endif /* false */
  45. TEST_CASE("Numbers") {
  46. luwra::StateWrapper state;
  47. REQUIRE(luwra::push(state, 1337) == 1);
  48. REQUIRE(luwra::push(state, 13.37) == 1);
  49. REQUIRE(luwra::read<int>(state, -2) == 1337);
  50. REQUIRE(luwra::read<float>(state, -1) == 13.37f);
  51. }
  52. TEST_CASE("Strings") {
  53. luwra::StateWrapper state;
  54. const char* test_cstr = "Luwra Test String";
  55. std::string test_str(test_cstr);
  56. // Safety first
  57. REQUIRE(test_str == test_cstr);
  58. // Push both strings
  59. REQUIRE(luwra::push(state, test_cstr) == 1);
  60. REQUIRE(luwra::push(state, test_str) == 1);
  61. // They must be equal to Lua
  62. REQUIRE(luwra::equal(state, -1, -2));
  63. // Extraction as C string must not change the string's value
  64. const char* l_cstr1 = luwra::read<const char*>(state, -1);
  65. const char* l_cstr2 = luwra::read<const char*>(state, -2);
  66. REQUIRE(std::strcmp(test_cstr, l_cstr1) == 0);
  67. REQUIRE(std::strcmp(test_cstr, l_cstr2) == 0);
  68. REQUIRE(std::strcmp(test_str.c_str(), l_cstr1) == 0);
  69. REQUIRE(std::strcmp(test_str.c_str(), l_cstr2) == 0);
  70. REQUIRE(std::strcmp(l_cstr1, l_cstr2) == 0);
  71. // Extraction as C++ string must not change the string's value
  72. std::string l_str1 = luwra::read<std::string>(state, -1);
  73. std::string l_str2 = luwra::read<std::string>(state, -2);
  74. REQUIRE(l_str1 == test_cstr);
  75. REQUIRE(l_str2 == test_cstr);
  76. REQUIRE(test_str == l_str1);
  77. REQUIRE(test_str == l_str2);
  78. REQUIRE(l_str1 == l_str2);
  79. }
  80. TEST_CASE("Tuples") {
  81. luwra::StateWrapper state;
  82. int a = 13;
  83. std::string b("Hello");
  84. float c = 0.37;
  85. // Push normal tuple
  86. auto tuple = std::make_tuple(a, b, c);
  87. REQUIRE(luwra::push(state, tuple) == 3);
  88. // Push nested tuple
  89. auto tuple_nested = std::make_tuple(a, b, c, tuple);
  90. REQUIRE(luwra::push(state, tuple_nested) == 6);
  91. }
  92. TEST_CASE("Boolean") {
  93. luwra::StateWrapper state;
  94. bool value = true;
  95. REQUIRE(luwra::push(state, value) == 1);
  96. REQUIRE(luwra::read<bool>(state, -1) == value);
  97. }
  98. TEST_CASE("Pushable") {
  99. luwra::StateWrapper state;
  100. luwra::Pushable pushable(1337);
  101. REQUIRE(luwra::push(state, pushable) == 1);
  102. REQUIRE(luwra::read<int>(state, -1) == 1337);
  103. }