diff --git a/tempest/Math.hpp b/tempest/Math.hpp new file mode 100644 index 0000000..846b49f --- /dev/null +++ b/tempest/Math.hpp @@ -0,0 +1,6 @@ +#ifndef TEMPEST_MATH_HPP +#define TEMPEST_MATH_HPP + +#include "tempest/math/CMath.hpp" + +#endif diff --git a/tempest/math/CMath.hpp b/tempest/math/CMath.hpp new file mode 100644 index 0000000..8fc85c4 --- /dev/null +++ b/tempest/math/CMath.hpp @@ -0,0 +1,30 @@ +#ifndef TEMPEST_MATH_C_MATH_HPP +#define TEMPEST_MATH_C_MATH_HPP + +#include +#include +#include + +class CMath { + public: + // Static variables + static constexpr float PI = 3.1415927f; + static constexpr float TWO_PI = 6.2831855f; + static constexpr float OO_TWO_PI = 1.0f / TWO_PI; + + // Static functions + static uint32_t fuint(float n) { + return static_cast(n); + } + + static uint32_t fuint_n(float n) { + return static_cast(n + 0.5f); + } + + static float sqrt(float x) { + STORM_ASSERT(x >= 0.0f); + return ::sqrt(x); + } +}; + +#endif diff --git a/test/Math.cpp b/test/Math.cpp new file mode 100644 index 0000000..d45b807 --- /dev/null +++ b/test/Math.cpp @@ -0,0 +1,58 @@ +#include "tempest/Math.hpp" +#include "test/Test.hpp" + +TEST_CASE("CMath::fuint", "[math]") { + SECTION("converts 1.0f to 1u") { + auto result = CMath::fuint(1.0f); + REQUIRE(result == 1u); + } + + SECTION("converts 1.5f to 1u") { + auto result = CMath::fuint(1.5f); + REQUIRE(result == 1u); + } + + SECTION("converts 1.999f to 1u") { + auto result = CMath::fuint(1.999f); + REQUIRE(result == 1u); + } + + SECTION("converts 0.0f to 0u") { + auto result = CMath::fuint(0.0f); + REQUIRE(result == 0u); + } +} + +TEST_CASE("CMath::fuint_n", "[math]") { + SECTION("converts 1.0f to 1u") { + auto result = CMath::fuint_n(1.0f); + REQUIRE(result == 1u); + } + + SECTION("converts 1.5f to 2u") { + auto result = CMath::fuint_n(1.5f); + REQUIRE(result == 2u); + } + + SECTION("converts 1.999f to 2u") { + auto result = CMath::fuint_n(1.999f); + REQUIRE(result == 2u); + } + + SECTION("converts 0.0f to 0u") { + auto result = CMath::fuint_n(0.0f); + REQUIRE(result == 0u); + } +} + +TEST_CASE("CMath::sqrt", "[math]") { + SECTION("returns the square root of 4.0f") { + auto result = CMath::sqrt(4.0f); + REQUIRE(result == 2.0f); + } + + SECTION("returns the square root of 0.0f") { + auto result = CMath::sqrt(0.0f); + REQUIRE(result == 0.0f); + } +}