| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #include <catch.hpp>
- #include <luwra.hpp>
- #include <cstring>
- #include <string>
- #include <utility>
- #include <type_traits>
- #include <vector>
- #include <list>
- #include <initializer_list>
- // Numbers are royally fucked. They might or might not be stored in a floating-point number, which
- // makes testing for integer limits pointless.
- //
- // template <typename I>
- // struct NumericTest {
- // static
- // void test(lua_State* state) {
- // const I max_value = std::numeric_limits<I>::max();
- // const I min_value = std::numeric_limits<I>::lowest();
- // const I avg_value = (max_value + min_value) / 2;
- // // Largest value
- // CHECK((luwra::push(state, max_value) == 1));
- // CHECK((luwra::read<I>(state, -1) == max_value));
- // // Lowest value
- // CHECK((luwra::push(state, min_value) == 1));
- // CHECK((luwra::read<I>(state, -1) == min_value));
- // // Average value
- // CHECK((luwra::push(state, avg_value) == 1));
- // CHECK((luwra::read<I>(state, -1) == avg_value));
- // }
- // };
- // TEST_CASE("NumberLimits") {
- // luwra::StateWrapper state;
- // // Integer-based types
- // NumericTest<signed char>::test(state);
- // NumericTest<unsigned char>::test(state);
- // NumericTest<signed short>::test(state);
- // NumericTest<unsigned short>::test(state);
- // NumericTest<signed int>::test(state);
- // NumericTest<unsigned int>::test(state);
- // NumericTest<signed long int>::test(state);
- // NumericTest<unsigned long int>::test(state);
- // NumericTest<signed long long int>::test(state);
- // NumericTest<unsigned long long int>::test(state);
- // // Number-based types
- // NumericTest<float>::test(state);
- // NumericTest<double>::test(state);
- // NumericTest<long double>::test(state);
- // }
- TEST_CASE("Numbers") {
- luwra::StateWrapper state;
- REQUIRE(luwra::push(state, 1337) == 1);
- REQUIRE(luwra::push(state, 13.37) == 1);
- REQUIRE(luwra::read<int>(state, -2) == 1337);
- REQUIRE(luwra::read<float>(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<const char*>(state, -1);
- const char* l_cstr2 = luwra::read<const char*>(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<std::string>(state, -1);
- std::string l_str2 = luwra::read<std::string>(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<int>(state, -3) == a);
- REQUIRE(luwra::read<std::string>(state, -2) == b);
- REQUIRE(luwra::read<float>(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<int>(state, -6) == a);
- REQUIRE(luwra::read<std::string>(state, -5) == b);
- REQUIRE(luwra::read<float>(state, -4) == c);
- REQUIRE(luwra::read<int>(state, -3) == a);
- REQUIRE(luwra::read<std::string>(state, -2) == b);
- REQUIRE(luwra::read<float>(state, -1) == c);
- }
- TEST_CASE("Boolean") {
- luwra::StateWrapper state;
- REQUIRE(luwra::push(state, true) == 1);
- REQUIRE(luwra::read<bool>(state, -1) == true);
- REQUIRE(luwra::push(state, false) == 1);
- REQUIRE(luwra::read<bool>(state, -1) == false);
- }
- TEST_CASE("Pushable") {
- luwra::StateWrapper state;
- luwra::Pushable pushable(1337);
- REQUIRE(luwra::push(state, pushable) == 1);
- REQUIRE(luwra::read<int>(state, -1) == 1337);
- }
- TEST_CASE("Value<vector>") {
- luwra::StateWrapper state;
- state.loadStandardLibrary();
- std::vector<int> v {1, 2, 3, 4, 5};
- REQUIRE(luwra::push(state, v) == 1);
- state["v"] = v;
- REQUIRE(state.runString(
- "x = 0\n"
- "for i, j in ipairs(v) do x = x + i + j end\n"
- "return x"
- ) == LUA_OK);
- REQUIRE(luwra::read<int>(state, -1) == 30);
- }
- TEST_CASE("Value<list>") {
- luwra::StateWrapper state;
- state.loadStandardLibrary();
- std::list<int> v {1, 2, 3, 4, 5};
- REQUIRE(luwra::push(state, v) == 1);
- state["v"] = v;
- REQUIRE(state.runString(
- "x = 0\n"
- "for i, j in ipairs(v) do x = x + i + j end\n"
- "return x"
- ) == LUA_OK);
- REQUIRE(luwra::read<int>(state, -1) == 30);
- }
- TEST_CASE("Value<map>") {
- luwra::StateWrapper state;
- std::map<luwra::Pushable, luwra::Pushable> v {
- {"hello", 13},
- {37, "world"}
- };
- REQUIRE(luwra::push(state, v) == 1);
- state["v"] = v;
- REQUIRE(state.runString("return v.hello, v[37]") == LUA_OK);
- REQUIRE(luwra::read<int>(state, -2) == 13);
- REQUIRE(luwra::read<std::string>(state, -1) == "world");
- }
|