OpenTTD Source: src/tests/math_func.cpp Source File

OpenTTD Source 14.0-RC3

math_func.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #include "../stdafx.h"
11
12 #include "../3rdparty/catch2/catch.hpp"
13
14 #include "../core/math_func.hpp"
15
16 TEST_CASE("DivideApproxTest - Negative")
17 {
18 CHECK(-2 == DivideApprox(-5, 2));
19 CHECK(2 == DivideApprox(-5, -2));
20 CHECK(-1 == DivideApprox(-66, 80));
21 }
22
23 TEST_CASE("DivideApproxTest, Divide")
24 {
25 CHECK(2 == DivideApprox(5, 2));
26 CHECK(3 == DivideApprox(80, 30));
27 CHECK(3 == DivideApprox(8, 3));
28 CHECK(0 == DivideApprox(3, 8));
29 }
30
31 TEST_CASE("IntSqrtTest - Zero")
32 {
33 CHECK(0 == IntSqrt(0));
34 }
35
36 TEST_CASE("IntSqrtTest - FindSqRt")
37 {
38 CHECK(5 == IntSqrt(25));
39 CHECK(10 == IntSqrt(100));
40 CHECK(9 == IntSqrt(88));
41 CHECK(1696 == IntSqrt(2876278));
42 }
43
44
45 TEST_CASE("ClampTo")
46 {
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));
51
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));
56
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));
61
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));
66
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)));
71
72 /* The realm around 64 bits types is tricky as there is not one type/method that works for all. */
73
74 /* lowest/max uint64_t does not get clamped when clamping to uint64_t. */
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()));
77
78 /* negative int64_t get clamped to 0. */
79 CHECK(0 == ClampTo<uint64_t>(std::numeric_limits<int64_t>::lowest()));
80 CHECK(0 == ClampTo<uint64_t>(int64_t(-1)));
81 /* positive int64_t remain the same. */
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()));
84
85 /* max uint64_t gets clamped to max int64_t. */
86 CHECK(std::numeric_limits<int64_t>::max() == ClampTo<int64_t>(std::numeric_limits<uint64_t>::max()));
87 }
88
89
90 TEST_CASE("SoftClamp")
91 {
92 /* Special behaviour of soft clamp returning the average of min/max when min is higher than max. */
93 CHECK(1250 == SoftClamp(0, 1500, 1000));
94 int million = 1000 * 1000;
95 CHECK(1250 * million == SoftClamp(0, 1500 * million, 1000 * million));
96 CHECK(0 == SoftClamp(0, 1500 * million, -1500 * million));
97 }
IntSqrt
uint32_t IntSqrt(uint32_t num)
Compute the integer square root.
Definition: math_func.cpp:42
SoftClamp
constexpr T SoftClamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:102
DivideApprox
int DivideApprox(int a, int b)
Deterministic approximate division.
Definition: math_func.cpp:22