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

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11
12#include "../3rdparty/catch2/catch.hpp"
13
14#include "../core/math_func.hpp"
15
16#include "../safeguards.h"
17
18TEST_CASE("DivideApproxTest - Negative")
19{
20 CHECK(-2 == DivideApprox(-5, 2));
21 CHECK(2 == DivideApprox(-5, -2));
22 CHECK(-1 == DivideApprox(-66, 80));
23}
24
25TEST_CASE("DivideApproxTest, Divide")
26{
27 CHECK(2 == DivideApprox(5, 2));
28 CHECK(3 == DivideApprox(80, 30));
29 CHECK(3 == DivideApprox(8, 3));
30 CHECK(0 == DivideApprox(3, 8));
31}
32
33TEST_CASE("IntSqrtTest - Zero")
34{
35 CHECK(0 == IntSqrt(0));
36}
37
38TEST_CASE("IntSqrtTest - FindSqRt")
39{
40 CHECK(5 == IntSqrt(25));
41 CHECK(10 == IntSqrt(100));
42 CHECK(9 == IntSqrt(88));
43 CHECK(1696 == IntSqrt(2876278));
44}
45
46
47TEST_CASE("ClampTo")
48{
49 CHECK(0 == ClampTo<uint8_t>(std::numeric_limits<int64_t>::lowest()));
50 CHECK(0 == ClampTo<uint8_t>(-1));
51 CHECK(0 == ClampTo<uint8_t>(0));
52 CHECK(1 == ClampTo<uint8_t>(1));
53
54 CHECK(255 == ClampTo<uint8_t>(std::numeric_limits<uint64_t>::max()));
55 CHECK(255 == ClampTo<uint8_t>(256));
56 CHECK(255 == ClampTo<uint8_t>(255));
57 CHECK(254 == ClampTo<uint8_t>(254));
58
59 CHECK(-128 == ClampTo<int8_t>(std::numeric_limits<int64_t>::lowest()));
60 CHECK(-128 == ClampTo<int8_t>(-129));
61 CHECK(-128 == ClampTo<int8_t>(-128));
62 CHECK(-127 == ClampTo<int8_t>(-127));
63
64 CHECK(127 == ClampTo<int8_t>(std::numeric_limits<uint64_t>::max()));
65 CHECK(127 == ClampTo<int8_t>(128));
66 CHECK(127 == ClampTo<int8_t>(127));
67 CHECK(126 == ClampTo<int8_t>(126));
68
69 CHECK(126 == ClampTo<int64_t>(static_cast<uint8_t>(126)));
70 CHECK(126 == ClampTo<uint64_t>(static_cast<int8_t>(126)));
71 CHECK(0 == ClampTo<uint64_t>(static_cast<int8_t>(-126)));
72 CHECK(0 == ClampTo<uint8_t>(static_cast<int8_t>(-126)));
73
74 /* The realm around 64 bits types is tricky as there is not one type/method that works for all. */
75
76 /* lowest/max uint64_t does not get clamped when clamping to uint64_t. */
77 CHECK(std::numeric_limits<uint64_t>::lowest() == ClampTo<uint64_t>(std::numeric_limits<uint64_t>::lowest()));
78 CHECK(std::numeric_limits<uint64_t>::max() == ClampTo<uint64_t>(std::numeric_limits<uint64_t>::max()));
79
80 /* negative int64_t get clamped to 0. */
81 CHECK(0 == ClampTo<uint64_t>(std::numeric_limits<int64_t>::lowest()));
82 CHECK(0 == ClampTo<uint64_t>(int64_t(-1)));
83 /* positive int64_t remain the same. */
84 CHECK(1 == ClampTo<uint64_t>(int64_t(1)));
85 CHECK(static_cast<uint64_t>(std::numeric_limits<int64_t>::max()) == ClampTo<uint64_t>(std::numeric_limits<int64_t>::max()));
86
87 /* max uint64_t gets clamped to max int64_t. */
88 CHECK(std::numeric_limits<int64_t>::max() == ClampTo<int64_t>(std::numeric_limits<uint64_t>::max()));
89}
90
91
92TEST_CASE("SoftClamp")
93{
94 /* Special behaviour of soft clamp returning the average of min/max when min is higher than max. */
95 CHECK(1250 == SoftClamp(0, 1500, 1000));
96 int million = 1000 * 1000;
97 CHECK(1250 * million == SoftClamp(0, 1500 * million, 1000 * million));
98 CHECK(0 == SoftClamp(0, 1500 * million, -1500 * million));
99}
DivideApprox
int DivideApprox(int a, int b)
Deterministic approximate division.
Definition math_func.cpp:22
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