types.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <catch.hpp>
  2. #include <luwra.hpp>
  3. #include <cstring>
  4. #include <string>
  5. #include <utility>
  6. #include <type_traits>
  7. // Numbers are royally fucked. They might or might not be stored in a floating-point number, which
  8. // makes testing for integer limits pointless.
  9. //
  10. // template <typename I>
  11. // struct NumericTest {
  12. // static
  13. // void test(lua_State* state) {
  14. // const I max_value = std::numeric_limits<I>::max();
  15. // const I min_value = std::numeric_limits<I>::lowest();
  16. // const I avg_value = (max_value + min_value) / 2;
  17. // // Largest value
  18. // CHECK((luwra::push(state, max_value) == 1));
  19. // CHECK((luwra::read<I>(state, -1) == max_value));
  20. // // Lowest value
  21. // CHECK((luwra::push(state, min_value) == 1));
  22. // CHECK((luwra::read<I>(state, -1) == min_value));
  23. // // Average value
  24. // CHECK((luwra::push(state, avg_value) == 1));
  25. // CHECK((luwra::read<I>(state, -1) == avg_value));
  26. // }
  27. // };
  28. // TEST_CASE("NumberLimits") {
  29. // luwra::StateWrapper state;
  30. // // Integer-based types
  31. // NumericTest<signed char>::test(state);
  32. // NumericTest<unsigned char>::test(state);
  33. // NumericTest<signed short>::test(state);
  34. // NumericTest<unsigned short>::test(state);
  35. // NumericTest<signed int>::test(state);
  36. // NumericTest<unsigned int>::test(state);
  37. // NumericTest<signed long int>::test(state);
  38. // NumericTest<unsigned long int>::test(state);
  39. // NumericTest<signed long long int>::test(state);
  40. // NumericTest<unsigned long long int>::test(state);
  41. // // Number-based types
  42. // NumericTest<float>::test(state);
  43. // NumericTest<double>::test(state);
  44. // NumericTest<long double>::test(state);
  45. // }
  46. TEST_CASE("Numbers") {
  47. luwra::StateWrapper state;
  48. REQUIRE(luwra::push(state, 1337) == 1);
  49. REQUIRE(luwra::push(state, 13.37) == 1);
  50. REQUIRE(luwra::read<int>(state, -2) == 1337);
  51. REQUIRE(luwra::read<float>(state, -1) == 13.37f);
  52. }
  53. TEST_CASE("Strings") {
  54. luwra::StateWrapper state;
  55. const char* test_cstr = "Luwra Test String";
  56. std::string test_str(test_cstr);
  57. // Safety first
  58. REQUIRE(test_str == test_cstr);
  59. // Push both strings
  60. REQUIRE(luwra::push(state, test_cstr) == 1);
  61. REQUIRE(luwra::push(state, test_str) == 1);
  62. // They must be equal to Lua
  63. REQUIRE(luwra::equal(state, -1, -2));
  64. // Extraction as C string must not change the string's value
  65. const char* l_cstr1 = luwra::read<const char*>(state, -1);
  66. const char* l_cstr2 = luwra::read<const char*>(state, -2);
  67. REQUIRE(std::strcmp(test_cstr, l_cstr1) == 0);
  68. REQUIRE(std::strcmp(test_cstr, l_cstr2) == 0);
  69. REQUIRE(std::strcmp(test_str.c_str(), l_cstr1) == 0);
  70. REQUIRE(std::strcmp(test_str.c_str(), l_cstr2) == 0);
  71. REQUIRE(std::strcmp(l_cstr1, l_cstr2) == 0);
  72. // Extraction as C++ string must not change the string's value
  73. std::string l_str1 = luwra::read<std::string>(state, -1);
  74. std::string l_str2 = luwra::read<std::string>(state, -2);
  75. REQUIRE(l_str1 == test_cstr);
  76. REQUIRE(l_str2 == test_cstr);
  77. REQUIRE(test_str == l_str1);
  78. REQUIRE(test_str == l_str2);
  79. REQUIRE(l_str1 == l_str2);
  80. }
  81. TEST_CASE("Tuples") {
  82. luwra::StateWrapper state;
  83. int a = 13;
  84. std::string b("Hello");
  85. float c = 0.37;
  86. // Push normal tuple
  87. auto tuple = std::make_tuple(a, b, c);
  88. REQUIRE(luwra::push(state, tuple) == 3);
  89. REQUIRE(luwra::read<int>(state, -3) == a);
  90. REQUIRE(luwra::read<std::string>(state, -2) == b);
  91. REQUIRE(luwra::read<float>(state, -1) == c);
  92. // Push nested tuple
  93. auto tuple_nested = std::make_tuple(a, b, c, tuple);
  94. REQUIRE(luwra::push(state, tuple_nested) == 6);
  95. REQUIRE(luwra::read<int>(state, -6) == a);
  96. REQUIRE(luwra::read<std::string>(state, -5) == b);
  97. REQUIRE(luwra::read<float>(state, -4) == c);
  98. REQUIRE(luwra::read<int>(state, -3) == a);
  99. REQUIRE(luwra::read<std::string>(state, -2) == b);
  100. REQUIRE(luwra::read<float>(state, -1) == c);
  101. }
  102. TEST_CASE("Boolean") {
  103. luwra::StateWrapper state;
  104. REQUIRE(luwra::push(state, true) == 1);
  105. REQUIRE(luwra::read<bool>(state, -1) == true);
  106. REQUIRE(luwra::push(state, false) == 1);
  107. REQUIRE(luwra::read<bool>(state, -1) == false);
  108. }
  109. TEST_CASE("Pushable") {
  110. luwra::StateWrapper state;
  111. luwra::Pushable pushable(1337);
  112. REQUIRE(luwra::push(state, pushable) == 1);
  113. REQUIRE(luwra::read<int>(state, -1) == 1337);
  114. }