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 (C) 2022 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
29#include <inttypes.h>
30
31// Code under test
32#include <math/util.h>
33
35{
36 int64_t m_numerator;
37 int64_t m_value;
39 int64_t m_result;
40};
41
45BOOST_AUTO_TEST_SUITE( MathUtil )
46
47BOOST_AUTO_TEST_CASE( test_rescale_int64 )
48{
49 // Order: numerator, value, denominator, result.
50 const std::vector<TEST_RESCALE_I64_CASE> rescale_i64_cases = {
51 { 10LL, 10LL, 1LL, 100LL },
52 { 10LL, 10LL, -1LL, -100LL },
53 { 10LL, -10LL, 1LL, -100LL },
54 { 10LL, -10LL, -1LL, 100LL },
55
56 { 1LL, 9LL, 1LL, 9LL },
57 { 1LL, 9LL, -1LL, -9LL },
58 { 1LL, -9LL, 1LL, -9LL },
59 { 1LL, -9LL, -1LL, 9LL },
60
61 { 10LL, 10LL, 2LL, 50LL },
62 { 10LL, 10LL, -2LL, -50LL },
63 { 10LL, -10LL, 2LL, -50LL },
64 { 10LL, -10LL, -2LL, 50LL },
65
66 { 1LL, 9LL, 2LL, 5LL },
67 { 1LL, 9LL, -2LL, -5LL },
68 { 1LL, -9LL, 2LL, -5LL },
69 { 1LL, -9LL, -2LL, 5LL },
70
71 { 1LL, 17LL, 4LL, 4LL },
72 { 1LL, 17LL, -4LL, -4LL },
73 { 1LL, -17LL, 4LL, -4LL },
74 { 1LL, -17LL, -4LL, 4LL },
75
76 { 1LL, 19LL, 4LL, 5LL },
77 { 1LL, 19LL, -4LL, -5LL },
78 { 1LL, -19LL, 4LL, -5LL },
79 { 1LL, -19LL, -4LL, 5LL },
80
81 { 1LL, 0LL, 4LL, 0LL },
82 { 1LL, 0LL, -4LL, 0LL },
83 { -1LL, 0LL, 4LL, 0LL },
84 { -1LL, 0LL, -4LL, 0LL },
85
86 // sqrt(2^63) = 3037000499.98..
87 { 3037000499LL, 3037000499LL, 1LL, 9223372030926249001LL },
88 { 3037000499LL, 3037000499LL, -1LL, -9223372030926249001LL },
89 { 3037000499LL, -3037000499LL, 1LL, -9223372030926249001LL },
90 { 3037000499LL, -3037000499LL, -1LL, 9223372030926249001LL },
91
92 // sqrt(2^63 * 10) = 9603838834.99..
93 { 9603838834LL, 9603838834LL, 10LL, 9223372034944647956LL },
94 { 9603838834LL, 9603838834LL, -10LL, -9223372034944647956LL },
95 { 9603838834LL, -9603838834LL, 10LL, -9223372034944647956LL },
96 { 9603838834LL, -9603838834LL, -10LL, 9223372034944647956LL },
97
98 // INT64_MAX = 9223372036854775807
99 { INT64_MAX, 10LL, 10LL, INT64_MAX },
100 { INT64_MAX, 10LL, -10LL, -INT64_MAX },
101 { INT64_MAX, -10LL, 10LL, -INT64_MAX },
102 { INT64_MAX, -10LL, -10LL, INT64_MAX },
103
104 { INT64_MAX, 10LL, INT64_MAX, 10LL },
105 { INT64_MAX, 10LL, -INT64_MAX, -10LL },
106 { INT64_MAX, -10LL, INT64_MAX, -10LL },
107 { INT64_MAX, -10LL, -INT64_MAX, 10LL },
108
109 { INT64_MAX, INT64_MAX, INT64_MAX, INT64_MAX },
110 { INT64_MAX, INT64_MAX, -INT64_MAX, -INT64_MAX },
111 { INT64_MAX, -INT64_MAX, INT64_MAX, -INT64_MAX },
112 { INT64_MAX, -INT64_MAX, -INT64_MAX, INT64_MAX },
113 };
114
115 for( const TEST_RESCALE_I64_CASE& entry : rescale_i64_cases )
116 {
117 int64_t calculated = rescale( entry.m_numerator, entry.m_value, entry.m_denominator );
118 wxString msg;
119
120 msg << "rescale<int64_t>( " << entry.m_numerator << ", " << entry.m_value << ", "
121 << entry.m_denominator << " ) failed. ";
122 msg << "\nExpected: " << entry.m_result;
123 msg << "\nGot: " << calculated;
124
125 BOOST_CHECK_MESSAGE( calculated == entry.m_result, msg );
126 }
127}
128
Test suite for KiCad math code.
Definition: test_util.cpp:35
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(test_rescale_int64)
Declare the test suite.
Definition: test_util.cpp:47
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:153