/***************************************************************************************** * * * OpenSpace * * * * Copyright (c) 2014-2020 * * * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * * software and associated documentation files (the "Software"), to deal in the Software * * without restriction, including without limitation the rights to use, copy, modify, * * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * * permit persons to whom the Software is furnished to do so, subject to the following * * conditions: * * * * The above copyright notice and this permission notice shall be included in all copies * * or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ #include "catch2/catch.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { constexpr const int NumberFuzzTests = 10000; } // namespace TEST_CASE("LuaConversion: LuaExecution", "[luaconversion]") { lua_State* state = luaL_newstate(); luaL_openlibs(state); const int status = luaL_loadstring(state, ""); REQUIRE(status == LUA_OK); lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion", "[luaconversion]", bool, char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, float, double, long double, glm::vec2, glm::vec3, glm::vec4, glm::dvec2, glm::dvec3, glm::dvec4, glm::ivec2, glm::ivec3, glm::ivec4, glm::uvec2, glm::uvec3, glm::uvec4, glm::mat2x2, glm::mat2x3, glm::mat2x4, glm::mat3x2, glm::mat3x3, glm::mat3x4, glm::mat4x2, glm::mat4x3, glm::mat4x4, glm::dmat2x2, glm::dmat2x3, glm::dmat2x4, glm::dmat3x2, glm::dmat3x3, glm::dmat3x4, glm::dmat4x2, glm::dmat4x3, glm::dmat4x4) { using T = TestType; lua_State* state = luaL_newstate(); luaL_openlibs(state); const T val(1); using namespace openspace::properties; const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion Fuzz dis( std::numeric_limits::lowest(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion Fuzz >short", "[luaconversion]", short, unsigned short, int, unsigned int) { using T = TestType; using namespace openspace::properties; lua_State* state = luaL_newstate(); std::mt19937 gen(1337); std::uniform_int_distribution dis( std::numeric_limits::lowest(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion Fuzz Limited Signed", "[luaconversion]", long, long long) { using T = TestType; lua_State* state = luaL_newstate(); using namespace openspace::properties; std::mt19937 gen(1337); std::uniform_int_distribution<> dis( // We need to limit the range of values as Lua uses 'doubles' to store, and some // values will not be representable std::numeric_limits::lowest(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion Fuzz Limited Unsigned", "[luaconversion]", unsigned long, unsigned long long) { using T = TestType; lua_State* state = luaL_newstate(); using namespace openspace::properties; std::mt19937 gen(1337); std::uniform_int_distribution dis( // We need to limit the range of values as Lua uses 'doubles' to store, and some // values will not be representable std::numeric_limits::lowest(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion Float Fuzz", "[luaconversion]", float, double, long double) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_real_distribution dis( 0.f, std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Vec2 Float Fuzz", "[luaconversion]", glm::vec2, glm::dvec2) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_real_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Vec2 Fuzz", "[luaconversion]", glm::ivec2, glm::uvec2) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_int_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Vec3 Float Fuzz", "[luaconversion]", glm::vec3, glm::dvec3) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_real_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Vec3 Fuzz", "[luaconversion]", glm::ivec3, glm::uvec3) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_int_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Vec4 Float Fuzz", "[luaconversion]", glm::vec4, glm::dvec4) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_real_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen), dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Vec4 Fuzz", "[luaconversion]", glm::ivec4, glm::uvec4) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_int_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen), dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Mat2x2 Fuzz", "[luaconversion]", glm::mat2x2, glm::dmat2x2) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_real_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen), dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Mat2x3 Fuzz", "[luaconversion]", glm::mat2x3, glm::dmat2x3) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_real_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Mat2x4 Fuzz", "[luaconversion]", glm::mat2x4, glm::dmat2x4) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_real_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Mat3x2 Fuzz", "[luaconversion]", glm::mat3x2, glm::dmat3x2) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_real_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Mat3x3 Fuzz", "[luaconversion]", glm::mat3x3, glm::dmat3x3) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_real_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Mat3x4 Fuzz", "[luaconversion]", glm::mat3x4, glm::dmat3x4) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_real_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Mat4x2 Fuzz", "[luaconversion]", glm::mat4x2, glm::dmat4x2) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_real_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Mat4x3 Fuzz", "[luaconversion]", glm::mat4x3, glm::dmat4x3) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_real_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEMPLATE_TEST_CASE("LuaConversion: Mat4x4 Fuzz", "[luaconversion]", glm::mat4x4, glm::dmat4x4) { lua_State* state = luaL_newstate(); using namespace openspace::properties; using T = TestType; std::mt19937 gen(1337); std::uniform_real_distribution dis( std::numeric_limits::min(), std::numeric_limits::max() ); for (int i = 0; i < NumberFuzzTests; ++i) { const T val = T(dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen), dis(gen)); const bool success = PropertyDelegate>::template toLuaValue( state, val ); REQUIRE(success); bool success2; const T value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == val); lua_pop(state, 1); } lua_close(state); } TEST_CASE("LuaConversion: String", "[luaconversion]") { lua_State* state = luaL_newstate(); using namespace openspace::properties; bool success = PropertyDelegate>::template toLuaValue( state, "value"); REQUIRE(success); std::string value; bool success2; value = PropertyDelegate>::template fromLuaValue( state, success2 ); REQUIRE(success2); REQUIRE(value == "value"); lua_close(state); }