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 <inttypes.h>
26
27// Code under test
28#include <math/util.h>
29
31{
32 int64_t m_numerator;
33 int64_t m_value;
35 int64_t m_result;
36};
37
41BOOST_AUTO_TEST_SUITE( MathUtil )
42
43BOOST_AUTO_TEST_CASE( test_rescale_int64 )
44{
45 // Order: numerator, value, denominator, result.
46 const std::vector<TEST_RESCALE_I64_CASE> rescale_i64_cases = {
47 { 10LL, 10LL, 1LL, 100LL },
48 { 10LL, 10LL, -1LL, -100LL },
49 { 10LL, -10LL, 1LL, -100LL },
50 { 10LL, -10LL, -1LL, 100LL },
51
52 { 1LL, 9LL, 1LL, 9LL },
53 { 1LL, 9LL, -1LL, -9LL },
54 { 1LL, -9LL, 1LL, -9LL },
55 { 1LL, -9LL, -1LL, 9LL },
56
57 { 10LL, 10LL, 2LL, 50LL },
58 { 10LL, 10LL, -2LL, -50LL },
59 { 10LL, -10LL, 2LL, -50LL },
60 { 10LL, -10LL, -2LL, 50LL },
61
62 { 1LL, 9LL, 2LL, 5LL },
63 { 1LL, 9LL, -2LL, -5LL },
64 { 1LL, -9LL, 2LL, -5LL },
65 { 1LL, -9LL, -2LL, 5LL },
66
67 { 1LL, 17LL, 4LL, 4LL },
68 { 1LL, 17LL, -4LL, -4LL },
69 { 1LL, -17LL, 4LL, -4LL },
70 { 1LL, -17LL, -4LL, 4LL },
71
72 { 1LL, 19LL, 4LL, 5LL },
73 { 1LL, 19LL, -4LL, -5LL },
74 { 1LL, -19LL, 4LL, -5LL },
75 { 1LL, -19LL, -4LL, 5LL },
76
77 { 1LL, 0LL, 4LL, 0LL },
78 { 1LL, 0LL, -4LL, 0LL },
79 { -1LL, 0LL, 4LL, 0LL },
80 { -1LL, 0LL, -4LL, 0LL },
81
82 // sqrt(2^63) = 3037000499.98..
83 { 3037000499LL, 3037000499LL, 1LL, 9223372030926249001LL },
84 { 3037000499LL, 3037000499LL, -1LL, -9223372030926249001LL },
85 { 3037000499LL, -3037000499LL, 1LL, -9223372030926249001LL },
86 { 3037000499LL, -3037000499LL, -1LL, 9223372030926249001LL },
87
88 // sqrt(2^63 * 10) = 9603838834.99..
89 { 9603838834LL, 9603838834LL, 10LL, 9223372034944647956LL },
90 { 9603838834LL, 9603838834LL, -10LL, -9223372034944647956LL },
91 { 9603838834LL, -9603838834LL, 10LL, -9223372034944647956LL },
92 { 9603838834LL, -9603838834LL, -10LL, 9223372034944647956LL },
93
94 // INT64_MAX = 9223372036854775807
95 { INT64_MAX, 10LL, 10LL, INT64_MAX },
96 { INT64_MAX, 10LL, -10LL, -INT64_MAX },
97 { INT64_MAX, -10LL, 10LL, -INT64_MAX },
98 { INT64_MAX, -10LL, -10LL, INT64_MAX },
99
100 { INT64_MAX, 10LL, INT64_MAX, 10LL },
101 { INT64_MAX, 10LL, -INT64_MAX, -10LL },
102 { INT64_MAX, -10LL, INT64_MAX, -10LL },
103 { INT64_MAX, -10LL, -INT64_MAX, 10LL },
104
105 { INT64_MAX, INT64_MAX, INT64_MAX, INT64_MAX },
106 { INT64_MAX, INT64_MAX, -INT64_MAX, -INT64_MAX },
107 { INT64_MAX, -INT64_MAX, INT64_MAX, -INT64_MAX },
108 { INT64_MAX, -INT64_MAX, -INT64_MAX, INT64_MAX },
109 };
110
111 for( const TEST_RESCALE_I64_CASE& entry : rescale_i64_cases )
112 {
113 int64_t calculated = rescale( entry.m_numerator, entry.m_value, entry.m_denominator );
114 wxString msg;
115
116 msg << "rescale<int64_t>( " << entry.m_numerator << ", " << entry.m_value << ", "
117 << entry.m_denominator << " ) failed. ";
118 msg << "\nExpected: " << entry.m_result;
119 msg << "\nGot: " << calculated;
120
121 BOOST_CHECK_MESSAGE( calculated == entry.m_result, msg );
122 }
123}
124
Test suite for KiCad math code.
Definition test_util.cpp:31
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition util.h:135