#include #include #include #include #include #include // Numbers are royally fucked. They might or might not be stored in a floating-point number, which // makes testing for integer limits pointless. // // template // struct NumericTest { // static // void test(lua_State* state) { // const I max_value = std::numeric_limits::max(); // const I min_value = std::numeric_limits::lowest(); // const I avg_value = (max_value + min_value) / 2; // // Largest value // CHECK((luwra::push(state, max_value) == 1)); // CHECK((luwra::read(state, -1) == max_value)); // // Lowest value // CHECK((luwra::push(state, min_value) == 1)); // CHECK((luwra::read(state, -1) == min_value)); // // Average value // CHECK((luwra::push(state, avg_value) == 1)); // CHECK((luwra::read(state, -1) == avg_value)); // } // }; // TEST_CASE("NumberLimits") { // luwra::StateWrapper state; // // Integer-based types // NumericTest::test(state); // NumericTest::test(state); // NumericTest::test(state); // NumericTest::test(state); // NumericTest::test(state); // NumericTest::test(state); // NumericTest::test(state); // NumericTest::test(state); // NumericTest::test(state); // NumericTest::test(state); // // Number-based types // NumericTest::test(state); // NumericTest::test(state); // NumericTest::test(state); // } TEST_CASE("Numbers") { luwra::StateWrapper state; REQUIRE(luwra::push(state, 1337) == 1); REQUIRE(luwra::push(state, 13.37) == 1); REQUIRE(luwra::read(state, -2) == 1337); REQUIRE(luwra::read(state, -1) == 13.37f); } TEST_CASE("Strings") { luwra::StateWrapper state; const char* test_cstr = "Luwra Test String"; std::string test_str(test_cstr); // Safety first REQUIRE(test_str == test_cstr); // Push both strings REQUIRE(luwra::push(state, test_cstr) == 1); REQUIRE(luwra::push(state, test_str) == 1); // They must be equal to Lua REQUIRE(luwra::equal(state, -1, -2)); // Extraction as C string must not change the string's value const char* l_cstr1 = luwra::read(state, -1); const char* l_cstr2 = luwra::read(state, -2); REQUIRE(std::strcmp(test_cstr, l_cstr1) == 0); REQUIRE(std::strcmp(test_cstr, l_cstr2) == 0); REQUIRE(std::strcmp(test_str.c_str(), l_cstr1) == 0); REQUIRE(std::strcmp(test_str.c_str(), l_cstr2) == 0); REQUIRE(std::strcmp(l_cstr1, l_cstr2) == 0); // Extraction as C++ string must not change the string's value std::string l_str1 = luwra::read(state, -1); std::string l_str2 = luwra::read(state, -2); REQUIRE(l_str1 == test_cstr); REQUIRE(l_str2 == test_cstr); REQUIRE(test_str == l_str1); REQUIRE(test_str == l_str2); REQUIRE(l_str1 == l_str2); } TEST_CASE("Tuples") { luwra::StateWrapper state; int a = 13; std::string b("Hello"); float c = 0.37; // Push normal tuple auto tuple = std::make_tuple(a, b, c); REQUIRE(luwra::push(state, tuple) == 3); REQUIRE(luwra::read(state, -3) == a); REQUIRE(luwra::read(state, -2) == b); REQUIRE(luwra::read(state, -1) == c); // Push nested tuple auto tuple_nested = std::make_tuple(a, b, c, tuple); REQUIRE(luwra::push(state, tuple_nested) == 6); REQUIRE(luwra::read(state, -6) == a); REQUIRE(luwra::read(state, -5) == b); REQUIRE(luwra::read(state, -4) == c); REQUIRE(luwra::read(state, -3) == a); REQUIRE(luwra::read(state, -2) == b); REQUIRE(luwra::read(state, -1) == c); } TEST_CASE("Boolean") { luwra::StateWrapper state; REQUIRE(luwra::push(state, true) == 1); REQUIRE(luwra::read(state, -1) == true); REQUIRE(luwra::push(state, false) == 1); REQUIRE(luwra::read(state, -1) == false); } TEST_CASE("Pushable") { luwra::StateWrapper state; luwra::Pushable pushable(1337); REQUIRE(luwra::push(state, pushable) == 1); REQUIRE(luwra::read(state, -1) == 1337); }