types.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "catch.hpp"
  2. #include <lua.hpp>
  3. #include <luwra.hpp>
  4. #include <iostream>
  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. REQUIRE(luwra::Value<I>::push(state, max_value) == 1);
  16. REQUIRE(luwra::Value<I>::read(state, -1) == max_value);
  17. lua_pop(state, 1);
  18. // Lowest value
  19. REQUIRE(luwra::Value<I>::push(state, min_value) == 1);
  20. REQUIRE(luwra::Value<I>::read(state, -1) == min_value);
  21. lua_pop(state, 1);
  22. // Average value
  23. REQUIRE(luwra::Value<I>::push(state, avg_value) == 1);
  24. REQUIRE(luwra::Value<I>::read(state, -1) == avg_value);
  25. lua_pop(state, 1);
  26. }
  27. };
  28. struct TautologyTest {
  29. static
  30. void test(lua_State*) {}
  31. };
  32. template <typename B, typename I>
  33. using SelectNumericTest =
  34. typename std::conditional<
  35. luwra::internal::NumericContainedValueBase<I, B>::qualifies,
  36. NumericTest<I>,
  37. TautologyTest
  38. >::type;
  39. TEST_CASE("Test Value specialization for numeric C types", "types_numeric") {
  40. lua_State* state = luaL_newstate();
  41. // Integer-based types
  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. }