|
| 1 | + |
| 2 | +#include <catch2/catch_test_macros.hpp> |
| 3 | +#include <catch2/catch_approx.hpp> |
| 4 | + |
| 5 | +#include "threepp/math/MathUtils.hpp" |
| 6 | + |
| 7 | +#include <regex> |
| 8 | + |
| 9 | +using namespace threepp; |
| 10 | + |
| 11 | +TEST_CASE("genereteUUID") { |
| 12 | + |
| 13 | + const auto uuid = math::generateUUID(); |
| 14 | + const std::regex uuidRegex("[A-Z0-9]{8}-[A-Z0-9]{4}-4[A-Z0-9]{3}-[A-Z0-9]{4}-[A-Z0-9]{12}", std::regex_constants::icase); |
| 15 | + |
| 16 | + REQUIRE(std::regex_match(uuid, uuidRegex)); |
| 17 | +} |
| 18 | + |
| 19 | +TEST_CASE("euclideanModulo") { |
| 20 | + CHECK(std::isnan(math::euclideanModulo(6, 0))); |
| 21 | + CHECK(math::euclideanModulo(6, 1) == Catch::Approx(0.)); |
| 22 | + CHECK(math::euclideanModulo(6, 2) == Catch::Approx(0.)); |
| 23 | + CHECK(math::euclideanModulo(6, 5) == Catch::Approx(1.)); |
| 24 | + CHECK(math::euclideanModulo(6, 6) == Catch::Approx(0.)); |
| 25 | + CHECK(math::euclideanModulo(6, 7) == Catch::Approx(6.)); |
| 26 | +} |
| 27 | + |
| 28 | +TEST_CASE("mapLinear") { |
| 29 | + // Value within range |
| 30 | + CHECK(math::mapLinear(0.5, 0.0, 1.0, 0.0, 10.0) == Catch::Approx(5.0)); |
| 31 | + |
| 32 | + // Value equal to lower boundary |
| 33 | + CHECK(math::mapLinear(0.0, 0.0, 1.0, 0.0, 10.0) == Catch::Approx(0.0)); |
| 34 | + |
| 35 | + // Value equal to upper boundary |
| 36 | + CHECK(math::mapLinear(1.0, 0.0, 1.0, 0.0, 10.0) == Catch::Approx(10.0)); |
| 37 | +} |
| 38 | + |
| 39 | +TEST_CASE("inverseLerp") { |
| 40 | + |
| 41 | + // 50% Percentage |
| 42 | + CHECK(math::inverseLerp(1.0, 2.0, 1.5) == Catch::Approx(0.5)); |
| 43 | + // 100% Percentage |
| 44 | + CHECK(math::inverseLerp(1.0, 2.0, 2.0) == Catch::Approx(1.)); |
| 45 | + // 0% Percentage |
| 46 | + CHECK(math::inverseLerp(1.0, 2.0, 1.0) == Catch::Approx(0.)); |
| 47 | + // 0% Percentage, no NaN |
| 48 | + CHECK(math::inverseLerp(1.0, 1.0, 1.0) == Catch::Approx(0.)); |
| 49 | +} |
| 50 | + |
| 51 | +TEST_CASE("lerp") { |
| 52 | + // Value equal to lower boundary |
| 53 | + CHECK(math::lerp(1.0, 2.0, 0.0) == Catch::Approx(1.0)); |
| 54 | + |
| 55 | + // Value equal to upper boundary |
| 56 | + CHECK(math::lerp(1.0, 2.0, 1.0) == Catch::Approx(2.0)); |
| 57 | + |
| 58 | + // Value within range |
| 59 | + CHECK(math::lerp(1.0, 2.0, 0.4) == Catch::Approx(1.4)); |
| 60 | +} |
0 commit comments