KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_util.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
23
25#include <climits>
26#include <inttypes.h>
27#include <limits>
28
29// Code under test
30#include <math/util.h>
31
33{
34 int64_t m_numerator;
35 int64_t m_value;
37 int64_t m_result;
38};
39
43BOOST_AUTO_TEST_SUITE( MathUtil )
44
45BOOST_AUTO_TEST_CASE( test_rescale_int64 )
46{
47 // Order: numerator, value, denominator, result.
48 const std::vector<TEST_RESCALE_I64_CASE> rescale_i64_cases = {
49 { 10LL, 10LL, 1LL, 100LL },
50 { 10LL, 10LL, -1LL, -100LL },
51 { 10LL, -10LL, 1LL, -100LL },
52 { 10LL, -10LL, -1LL, 100LL },
53
54 { 1LL, 9LL, 1LL, 9LL },
55 { 1LL, 9LL, -1LL, -9LL },
56 { 1LL, -9LL, 1LL, -9LL },
57 { 1LL, -9LL, -1LL, 9LL },
58
59 { 10LL, 10LL, 2LL, 50LL },
60 { 10LL, 10LL, -2LL, -50LL },
61 { 10LL, -10LL, 2LL, -50LL },
62 { 10LL, -10LL, -2LL, 50LL },
63
64 { 1LL, 9LL, 2LL, 5LL },
65 { 1LL, 9LL, -2LL, -5LL },
66 { 1LL, -9LL, 2LL, -5LL },
67 { 1LL, -9LL, -2LL, 5LL },
68
69 { 1LL, 17LL, 4LL, 4LL },
70 { 1LL, 17LL, -4LL, -4LL },
71 { 1LL, -17LL, 4LL, -4LL },
72 { 1LL, -17LL, -4LL, 4LL },
73
74 { 1LL, 19LL, 4LL, 5LL },
75 { 1LL, 19LL, -4LL, -5LL },
76 { 1LL, -19LL, 4LL, -5LL },
77 { 1LL, -19LL, -4LL, 5LL },
78
79 { 1LL, 0LL, 4LL, 0LL },
80 { 1LL, 0LL, -4LL, 0LL },
81 { -1LL, 0LL, 4LL, 0LL },
82 { -1LL, 0LL, -4LL, 0LL },
83
84 // sqrt(2^63) = 3037000499.98..
85 { 3037000499LL, 3037000499LL, 1LL, 9223372030926249001LL },
86 { 3037000499LL, 3037000499LL, -1LL, -9223372030926249001LL },
87 { 3037000499LL, -3037000499LL, 1LL, -9223372030926249001LL },
88 { 3037000499LL, -3037000499LL, -1LL, 9223372030926249001LL },
89
90 // sqrt(2^63 * 10) = 9603838834.99..
91 { 9603838834LL, 9603838834LL, 10LL, 9223372034944647956LL },
92 { 9603838834LL, 9603838834LL, -10LL, -9223372034944647956LL },
93 { 9603838834LL, -9603838834LL, 10LL, -9223372034944647956LL },
94 { 9603838834LL, -9603838834LL, -10LL, 9223372034944647956LL },
95
96 // INT64_MAX = 9223372036854775807
97 { INT64_MAX, 10LL, 10LL, INT64_MAX },
98 { INT64_MAX, 10LL, -10LL, -INT64_MAX },
99 { INT64_MAX, -10LL, 10LL, -INT64_MAX },
100 { INT64_MAX, -10LL, -10LL, INT64_MAX },
101
102 { INT64_MAX, 10LL, INT64_MAX, 10LL },
103 { INT64_MAX, 10LL, -INT64_MAX, -10LL },
104 { INT64_MAX, -10LL, INT64_MAX, -10LL },
105 { INT64_MAX, -10LL, -INT64_MAX, 10LL },
106
107 { INT64_MAX, INT64_MAX, INT64_MAX, INT64_MAX },
108 { INT64_MAX, INT64_MAX, -INT64_MAX, -INT64_MAX },
109 { INT64_MAX, -INT64_MAX, INT64_MAX, -INT64_MAX },
110 { INT64_MAX, -INT64_MAX, -INT64_MAX, INT64_MAX },
111 };
112
113 for( const TEST_RESCALE_I64_CASE& entry : rescale_i64_cases )
114 {
115 int64_t calculated = rescale( entry.m_numerator, entry.m_value, entry.m_denominator );
116 wxString msg;
117
118 msg << "rescale<int64_t>( " << entry.m_numerator << ", " << entry.m_value << ", "
119 << entry.m_denominator << " ) failed. ";
120 msg << "\nExpected: " << entry.m_result;
121 msg << "\nGot: " << calculated;
122
123 BOOST_CHECK_MESSAGE( calculated == entry.m_result, msg );
124 }
125}
126
127
128BOOST_AUTO_TEST_CASE( KiROUNDSaturates )
129{
130 const double inf = std::numeric_limits<double>::infinity();
131 const double nan = std::numeric_limits<double>::quiet_NaN();
132
133 BOOST_CHECK_EQUAL( KiROUND( 1e30, true ), INT_MAX );
134 BOOST_CHECK_EQUAL( KiROUND( -1e30, true ), INT_MIN );
135 BOOST_CHECK_EQUAL( KiROUND( inf, true ), INT_MAX );
136 BOOST_CHECK_EQUAL( KiROUND( -inf, true ), INT_MIN );
137 BOOST_CHECK_EQUAL( KiROUND( nan, true ), 0 );
138 BOOST_CHECK_EQUAL( KiROUND( 2147483647.4, true ), INT_MAX );
139 BOOST_CHECK_EQUAL( KiROUND( 2147483647.5, true ), INT_MAX );
140 BOOST_CHECK_EQUAL( KiROUND( -2147483648.5, true ), INT_MIN );
141
142 BOOST_CHECK_EQUAL( ( KiROUND<double, int64_t>( 1e19, true ) ), INT64_MAX );
143 BOOST_CHECK_EQUAL( ( KiROUND<double, int64_t>( 0x1p63, true ) ), INT64_MAX );
144
145 BOOST_CHECK_EQUAL( KiROUND( 1e30f, true ), INT_MAX );
146 BOOST_CHECK_EQUAL( KiROUND( std::numeric_limits<float>::quiet_NaN(), true ), 0 );
147
148 BOOST_CHECK_EQUAL( KiROUND( LLONG_MAX, true ), INT_MAX );
149 BOOST_CHECK_EQUAL( ( KiROUND<int64_t, int64_t>( ( int64_t( 1 ) << 53 ) + 1, true ) ), ( int64_t( 1 ) << 53 ) + 1 );
150}
151
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:982
Test suite for KiCad math code.
Definition test_util.cpp:33
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")
BOOST_AUTO_TEST_CASE(test_rescale_int64)
Declare the test suite.
Definition test_util.cpp:45
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition util.h:160