Add: Overlay cargo icon in vehicle/depot list when holding shift+ctrl. (#12938)
[openttd-github.git] / src / tests / math_func.cpp
blob1f53d005b74cedbc0f1d81b0fe0463b54206a4e5
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 */
8 /** @file math_func.cpp Test functionality from core/math_func. */
10 #include "../stdafx.h"
12 #include "../3rdparty/catch2/catch.hpp"
14 #include "../core/math_func.hpp"
16 TEST_CASE("DivideApproxTest - Negative")
18 CHECK(-2 == DivideApprox(-5, 2));
19 CHECK(2 == DivideApprox(-5, -2));
20 CHECK(-1 == DivideApprox(-66, 80));
23 TEST_CASE("DivideApproxTest, Divide")
25 CHECK(2 == DivideApprox(5, 2));
26 CHECK(3 == DivideApprox(80, 30));
27 CHECK(3 == DivideApprox(8, 3));
28 CHECK(0 == DivideApprox(3, 8));
31 TEST_CASE("IntSqrtTest - Zero")
33 CHECK(0 == IntSqrt(0));
36 TEST_CASE("IntSqrtTest - FindSqRt")
38 CHECK(5 == IntSqrt(25));
39 CHECK(10 == IntSqrt(100));
40 CHECK(9 == IntSqrt(88));
41 CHECK(1696 == IntSqrt(2876278));
45 TEST_CASE("ClampTo")
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)));
72 /* The realm around 64 bits types is tricky as there is not one type/method that works for all. */
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()));
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()));
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()));
90 TEST_CASE("SoftClamp")
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));