KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_vector2.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) 2020 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
30// Code under test
31#include <math/vector2d.h>
32
36BOOST_AUTO_TEST_SUITE( VECTOR2TESTS )
37
39{
40 constexpr VECTOR2I vi_1_2{ 1, 2 };
41 // Something to test at runtime
42 BOOST_TEST( vi_1_2 == VECTOR2I( 1, 2 ) );
43 static_assert( vi_1_2.x == 1 );
44 static_assert( vi_1_2.y == 2 );
45
46 constexpr VECTOR2I vi_3_5 = vi_1_2 + VECTOR2I( 3, 4 ) - VECTOR2I( 1, 1 );
47
48 static_assert( vi_3_5 == VECTOR2I( 3, 5 ) );
49
50 static_assert( vi_3_5.SquaredEuclideanNorm() == 9 + 25 );
51 static_assert( vi_3_5 > vi_1_2 );
52 static_assert( vi_1_2 < vi_3_5 );
53 static_assert( vi_1_2 != vi_3_5 );
54 static_assert( vi_1_2 <= vi_1_2 );
55 static_assert( vi_1_2 >= vi_1_2 );
56
57 static_assert( vi_1_2.SquaredDistance( vi_3_5 ) == 4 + 9 );
58
59 static_assert( LexicographicalCompare( vi_1_2, vi_3_5 ) < 0 );
60 // Was the copy avoided?
61 static_assert( &LexicographicalMin( vi_1_2, vi_3_5 ) == &vi_1_2 );
62 static_assert( &LexicographicalMin( vi_1_2, vi_3_5 ) == &vi_1_2 );
63
64 constexpr VECTOR2D vd_1_2{ 1.0, 2.0 };
65 constexpr VECTOR2D vd_3_4{ 3.0, 4.0 };
66
67 static_assert( vd_1_2 == VECTOR2D( 1.0, 2.0 ) );
68 static_assert( vd_1_2 != vd_3_4 );
69 static_assert( vd_1_2 < vd_3_4 );
70 static_assert( vd_1_2 <= vd_3_4 );
71 static_assert( vd_3_4 > vd_1_2 );
72
73 // After C++23
74 //static_assert( equals( vd_1_2, vd_3_4, 2.0 ) );
75}
76
77BOOST_AUTO_TEST_CASE( test_cross_product, *boost::unit_test::tolerance( 0.000001 ) )
78{
79 VECTOR2I v1(0, 1);
80 VECTOR2I v2(1, 0);
81
82 BOOST_TEST( v2.Cross( v1 ) == 1 );
83}
84
85BOOST_AUTO_TEST_CASE( test_dot_product, *boost::unit_test::tolerance( 0.000001 ) )
86{
87 VECTOR2I v1( 0, 1 );
88 VECTOR2I v2( 1, 0 );
89
90 BOOST_TEST( v2.Dot( v1 ) == 0 );
91}
92
93BOOST_AUTO_TEST_CASE( test_resize, *boost::unit_test::tolerance( 0.000001 ) )
94{
95 // just some arbitrary vectors
96 VECTOR2I v1( 4, 3 );
97 VECTOR2I v2( 5, -1 );
98 VECTOR2I v3( -2, 1 );
99 VECTOR2I v4( 1, 1 );
100 VECTOR2I v5( -70, -70 );
101
102 BOOST_TEST( v1.Resize( 8 ) == VECTOR2I( 6, 5 ) );
103 BOOST_TEST( v2.Resize( 10 ) == VECTOR2I( 10, -2 ) );
104 BOOST_TEST( v3.Resize( 4 ) == VECTOR2I( -4, 2 ) );
105 BOOST_TEST( v4.Resize( 1 ) == VECTOR2I( 1, 1 ) );
106 BOOST_TEST( v5.Resize( 100 ) == VECTOR2I( -71, -71 ) );
107}
108
109BOOST_AUTO_TEST_CASE( test_casting )
110{
111 VECTOR2I vint( 4, 3 );
112 VECTOR2D vdouble( 4.0, 3.0 );
113 VECTOR2L vlong( 4, 3 );
114 VECTOR2<float> vfloat( 4.0f, 3.0f );
115 VECTOR2<unsigned> vunsigned( 4, 3 );
116
117 BOOST_CHECK_EQUAL( vint, VECTOR2I( vdouble ) );
118 BOOST_CHECK_EQUAL( vint, VECTOR2I( vlong ) );
119 BOOST_CHECK_EQUAL( vint, VECTOR2I( vfloat ) );
120 BOOST_CHECK_EQUAL( vint, VECTOR2I( vunsigned ) );
121
122 BOOST_CHECK_EQUAL( vdouble, VECTOR2D( vint ) );
123 BOOST_CHECK_EQUAL( vdouble, VECTOR2D( vlong ) );
124 BOOST_CHECK_EQUAL( vdouble, VECTOR2D( vfloat ) );
125 BOOST_CHECK_EQUAL( vdouble, VECTOR2D( vunsigned ) );
126
127 BOOST_CHECK_EQUAL( vlong, VECTOR2L( vint ) );
128 BOOST_CHECK_EQUAL( vlong, VECTOR2L( vdouble ) );
129 BOOST_CHECK_EQUAL( vlong, VECTOR2L( vfloat ) );
130 BOOST_CHECK_EQUAL( vlong, VECTOR2L( vunsigned ) );
131
132 BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( vint ) );
133 BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( vdouble ) );
134 BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( vlong ) );
135 BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( vunsigned ) );
136
137 BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( vint ) );
138 BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( vdouble ) );
139 BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( vlong ) );
140 BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( vfloat ) );
141
142 // Check that negative values are handled correctly
143 vint = vint - 1;
144 vdouble = vdouble - 1;
145 vlong = vlong - 1;
146 vfloat = vfloat - 1;
147 vunsigned = vunsigned - 1;
148
149 BOOST_CHECK_EQUAL( vint, VECTOR2I( 3, 2 ) );
150 BOOST_CHECK_EQUAL( vdouble, VECTOR2D( 3.0, 2.0 ) );
151 BOOST_CHECK_EQUAL( vlong, VECTOR2L( 3, 2 ) );
152 BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( 3.0f, 2.0f ) );
153 BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( 3, 2 ) );
154
155 // Check that subtracting unsigned values works correctly
156 vint = vint - (unsigned)1;
157 vdouble = vdouble - (unsigned)1;
158 vlong = vlong - (unsigned)1;
159 vfloat = vfloat - (unsigned)1;
160 vunsigned = vunsigned - (unsigned)1;
161
162 BOOST_CHECK_EQUAL( vint, VECTOR2I( 2, 1 ) );
163 BOOST_CHECK_EQUAL( vdouble, VECTOR2D( 2.0, 1.0 ) );
164 BOOST_CHECK_EQUAL( vlong, VECTOR2L( 2, 1 ) );
165 BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( 2.0f, 1.0f ) );
166 BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( 2, 1 ) );
167
168 vint = vint - 5.0;
169 vdouble = vdouble - 5.0;
170 vlong = vlong - 5.0;
171 vfloat = vfloat - 5.0;
172 vunsigned = vunsigned - 5.0;
173
174 BOOST_CHECK_EQUAL( vint, VECTOR2I( -3, -4 ) );
175 BOOST_CHECK_EQUAL( vdouble, VECTOR2D( -3.0, -4.0 ) );
176 BOOST_CHECK_EQUAL( vlong, VECTOR2L( -3, -4 ) );
177 BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( -3.0f, -4.0f ) );
178 BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( 4294967293, 4294967292 ) ); // roll over unsigned when explicitly subtracting.
179
180 // Check that negative initial values are handled correctly
181 vint = VECTOR2I( -4, -3 );
182 vdouble = VECTOR2D( -4.0, -3.0 );
183 vlong = VECTOR2L( -4, -3 );
184 vfloat = VECTOR2<float>( -4.0f, -3.0f );
185 vunsigned = VECTOR2<unsigned>( -4, -3 );
186
187 vint = vint - 1;
188 vdouble = vdouble - 1;
189 vlong = vlong - 1;
190 vfloat = vfloat - 1;
191 vunsigned = vunsigned - 1;
192
193 BOOST_CHECK_EQUAL( vint, VECTOR2I( -5, -4 ) );
194 BOOST_CHECK_EQUAL( vdouble, VECTOR2D( -5.0, -4.0 ) );
195 BOOST_CHECK_EQUAL( vlong, VECTOR2L( -5, -4 ) );
196 BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( -5.0f, -4.0f ) );
197 BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( 4294967291, 4294967292 ) );
198
199 vint = vint - 1u;
200 vdouble = vdouble - 1u;
201 vlong = vlong - 1u;
202 vfloat = vfloat - 1u;
203
204 BOOST_CHECK_EQUAL( vint, VECTOR2I( -6, -5 ) );
205 BOOST_CHECK_EQUAL( vdouble, VECTOR2D( -6.0, -5.0 ) );
206 BOOST_CHECK_EQUAL( vlong, VECTOR2L( -6, -5 ) );
207 BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( -6.0f, -5.0f ) );
208
209 auto add = vint + vdouble;
210 BOOST_CHECK_EQUAL( add, VECTOR2D( -12.0, -10.0 ) );
211
212 auto sub = vint - 2 * vlong;
213 BOOST_CHECK_EQUAL( sub.x, 6 );
214 BOOST_CHECK_EQUAL( sub.y, 5 );
215
216 vunsigned = VECTOR2<unsigned>( std::numeric_limits<unsigned>::max(), std::numeric_limits<unsigned>::max() );
217 vint = VECTOR2I( vunsigned );
218 BOOST_CHECK_EQUAL( vint.x, std::numeric_limits<int>::max() );
219
220 vunsigned += 1;
221 BOOST_CHECK_EQUAL( vunsigned.x, 0 );
222}
223
constexpr extended_type Cross(const VECTOR2< T > &aVector) const
Compute cross product of self with aVector.
Definition: vector2d.h:542
constexpr extended_type SquaredEuclideanNorm() const
Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
Definition: vector2d.h:307
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:550
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:385
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
VECTOR3I v1(5, 5, 5)
BOOST_AUTO_TEST_CASE(Constexpr)
Test suite for KiCad math code.
VECTOR2I v2(1, 0)
VECTOR2I v4(1, 1)
VECTOR2I v5(-70, -70)
BOOST_TEST(v2.Cross(v1)==1)
VECTOR2I v3(-2, 1)
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690
constexpr const VECTOR2< T > & LexicographicalMin(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:628
VECTOR2< int64_t > VECTOR2L
Definition: vector2d.h:692
constexpr int LexicographicalCompare(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:640