10 #include "../stdafx.h"
12 #include "../3rdparty/catch2/catch.hpp"
14 #include "../core/math_func.hpp"
16 TEST_CASE(
"DivideApproxTest - Negative")
23 TEST_CASE(
"DivideApproxTest, Divide")
31 TEST_CASE(
"IntSqrtTest - Zero")
36 TEST_CASE(
"IntSqrtTest - FindSqRt")
41 CHECK(1696 ==
IntSqrt(2876278));
47 CHECK(0 == ClampTo<uint8_t>(std::numeric_limits<int64_t>::lowest()));
48 CHECK(0 == ClampTo<uint8_t>(-1));
49 CHECK(0 == ClampTo<uint8_t>(0));
50 CHECK(1 == ClampTo<uint8_t>(1));
52 CHECK(255 == ClampTo<uint8_t>(std::numeric_limits<uint64_t>::max()));
53 CHECK(255 == ClampTo<uint8_t>(256));
54 CHECK(255 == ClampTo<uint8_t>(255));
55 CHECK(254 == ClampTo<uint8_t>(254));
57 CHECK(-128 == ClampTo<int8_t>(std::numeric_limits<int64_t>::lowest()));
58 CHECK(-128 == ClampTo<int8_t>(-129));
59 CHECK(-128 == ClampTo<int8_t>(-128));
60 CHECK(-127 == ClampTo<int8_t>(-127));
62 CHECK(127 == ClampTo<int8_t>(std::numeric_limits<uint64_t>::max()));
63 CHECK(127 == ClampTo<int8_t>(128));
64 CHECK(127 == ClampTo<int8_t>(127));
65 CHECK(126 == ClampTo<int8_t>(126));
67 CHECK(126 == ClampTo<int64_t>(
static_cast<uint8_t
>(126)));
68 CHECK(126 == ClampTo<uint64_t>(
static_cast<int8_t
>(126)));
69 CHECK(0 == ClampTo<uint64_t>(
static_cast<int8_t
>(-126)));
70 CHECK(0 == ClampTo<uint8_t>(
static_cast<int8_t
>(-126)));
75 CHECK(std::numeric_limits<uint64_t>::lowest() == ClampTo<uint64_t>(std::numeric_limits<uint64_t>::lowest()));
76 CHECK(std::numeric_limits<uint64_t>::max() == ClampTo<uint64_t>(std::numeric_limits<uint64_t>::max()));
79 CHECK(0 == ClampTo<uint64_t>(std::numeric_limits<int64_t>::lowest()));
80 CHECK(0 == ClampTo<uint64_t>(int64_t(-1)));
82 CHECK(1 == ClampTo<uint64_t>(int64_t(1)));
83 CHECK(
static_cast<uint64_t
>(std::numeric_limits<int64_t>::max()) == ClampTo<uint64_t>(std::numeric_limits<int64_t>::max()));
86 CHECK(std::numeric_limits<int64_t>::max() == ClampTo<int64_t>(std::numeric_limits<uint64_t>::max()));
90 TEST_CASE(
"SoftClamp")
94 int million = 1000 * 1000;
95 CHECK(1250 * million ==
SoftClamp(0, 1500 * million, 1000 * million));
96 CHECK(0 ==
SoftClamp(0, 1500 * million, -1500 * million));
int DivideApprox(int a, int b)
Deterministic approximate division.
uint32_t IntSqrt(uint32_t num)
Compute the integer square root.
constexpr T SoftClamp(const T a, const T min, const T max)
Clamp a value between an interval.