KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_grid_helper.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 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#define BOOST_TEST_NO_MAIN
19#include <boost/test/unit_test.hpp>
20
21#include <tool/grid_helper.h>
22
24{
25 helper.clearAnchors();
26}
27
28BOOST_AUTO_TEST_SUITE( GridHelperTest )
29
30BOOST_AUTO_TEST_CASE( DefaultConstructor )
31{
32 GRID_HELPER helper;
33
34 // Test default state
35 BOOST_CHECK( helper.GetSnap() );
36 BOOST_CHECK( helper.GetUseGrid() );
37
38 // Test that manual setters work
39 helper.SetGridSize( VECTOR2D( 100, 100 ) );
40 helper.SetOrigin( VECTOR2I( 50, 50 ) );
41 helper.SetGridSnapping( true );
42
43 VECTOR2I grid = helper.GetGrid();
44 BOOST_CHECK_EQUAL( grid.x, 100 );
45 BOOST_CHECK_EQUAL( grid.y, 100 );
46
47 VECTOR2I origin = helper.GetOrigin();
48 BOOST_CHECK_EQUAL( origin.x, 50 );
49 BOOST_CHECK_EQUAL( origin.y, 50 );
50}
51
53{
54 GRID_HELPER helper;
55 helper.SetGridSize( VECTOR2D( 100, 100 ) );
56 helper.SetOrigin( VECTOR2I( 0, 0 ) );
57 helper.SetGridSnapping( true );
58
59 // Test basic alignment - should round to nearest grid point
60 VECTOR2I aligned = helper.Align( VECTOR2I( 149, 251 ) );
61 BOOST_CHECK_EQUAL( aligned.x, 100 );
62 BOOST_CHECK_EQUAL( aligned.y, 300 );
63
64 // Test exact grid points
65 aligned = helper.Align( VECTOR2I( 200, 300 ) );
66 BOOST_CHECK_EQUAL( aligned.x, 200 );
67 BOOST_CHECK_EQUAL( aligned.y, 300 );
68
69 // Test negative coordinates
70 aligned = helper.Align( VECTOR2I( -149, -251 ) );
71 BOOST_CHECK_EQUAL( aligned.x, -100 );
72 BOOST_CHECK_EQUAL( aligned.y, -300 );
73}
74
75BOOST_AUTO_TEST_CASE( AlignGridWithCustomGrid )
76{
77 GRID_HELPER helper;
78 helper.SetGridSize( VECTOR2D( 50, 50 ) );
79 helper.SetOrigin( VECTOR2I( 0, 0 ) );
80 helper.SetGridSnapping( true );
81
82 VECTOR2I aligned = helper.AlignGrid( VECTOR2I( 26, 74 ) );
83 BOOST_CHECK_EQUAL( aligned.x, 50 );
84 BOOST_CHECK_EQUAL( aligned.y, 50 );
85
86 // Test AlignGrid with specific grid parameter
87 aligned = helper.AlignGrid( VECTOR2I( 26, 74 ), VECTOR2D( 25, 25 ) );
88 BOOST_CHECK_EQUAL( aligned.x, 25 );
89 BOOST_CHECK_EQUAL( aligned.y, 75 );
90}
91
92BOOST_AUTO_TEST_CASE( AlignWithOriginOffset )
93{
94 GRID_HELPER helper;
95 helper.SetGridSize( VECTOR2D( 100, 100 ) );
96 helper.SetOrigin( VECTOR2I( 25, 25 ) );
97 helper.SetGridSnapping( true );
98
99 // When grid has an origin offset, alignment should still work from (0,0) reference
100 // AlignGrid doesn't use origin, just pure grid alignment
101 VECTOR2I aligned = helper.AlignGrid( VECTOR2I( 149, 251 ) );
102 BOOST_CHECK_EQUAL( aligned.x, 100 );
103 BOOST_CHECK_EQUAL( aligned.y, 300 );
104}
105
106BOOST_AUTO_TEST_CASE( AlignWithAuxiliaryAxes )
107{
108 GRID_HELPER helper;
109 helper.SetGridSize( VECTOR2D( 100, 100 ) );
110 helper.SetOrigin( VECTOR2I( 0, 0 ) );
111 helper.SetGridSnapping( true );
112
113 // Set auxiliary axis at (75, 75)
114 helper.SetAuxAxes( true, VECTOR2I( 75, 75 ) );
115
116 // Point closer to aux axis than grid should snap to aux axis
117 VECTOR2I aligned = helper.Align( VECTOR2I( 80, 80 ) );
118 BOOST_CHECK_EQUAL( aligned.x, 75 ); // Closer to aux axis X
119 BOOST_CHECK_EQUAL( aligned.y, 75 ); // Closer to aux axis Y
120
121 // Point closer to grid than aux axis should snap to grid
122 aligned = helper.Align( VECTOR2I( 95, 95 ) );
123 BOOST_CHECK_EQUAL( aligned.x, 100 ); // Closer to grid
124 BOOST_CHECK_EQUAL( aligned.y, 100 ); // Closer to grid
125
126 // Disable aux axes
127 helper.SetAuxAxes( false );
128 aligned = helper.Align( VECTOR2I( 80, 80 ) );
129 BOOST_CHECK_EQUAL( aligned.x, 100 ); // Should snap to grid only
130 BOOST_CHECK_EQUAL( aligned.y, 100 );
131}
132
133BOOST_AUTO_TEST_CASE( GridSnappingDisabled )
134{
135 GRID_HELPER helper;
136 helper.SetGridSize( VECTOR2D( 100, 100 ) );
137 helper.SetOrigin( VECTOR2I( 0, 0 ) );
138 helper.SetGridSnapping( false ); // Disable grid snapping
139
140 // When grid snapping is disabled, Align should return original point
141 VECTOR2I original( 149, 251 );
142 VECTOR2I aligned = helper.Align( original );
143 BOOST_CHECK_EQUAL( aligned.x, original.x );
144 BOOST_CHECK_EQUAL( aligned.y, original.y );
145
146 // AlignGrid should still work regardless of grid snapping setting
147 aligned = helper.AlignGrid( original );
148 BOOST_CHECK_EQUAL( aligned.x, 100 );
149 BOOST_CHECK_EQUAL( aligned.y, 300 );
150}
151
152BOOST_AUTO_TEST_CASE( UseGridDisabled )
153{
154 GRID_HELPER helper;
155 helper.SetGridSize( VECTOR2D( 100, 100 ) );
156 helper.SetOrigin( VECTOR2I( 0, 0 ) );
157 helper.SetGridSnapping( true );
158 helper.SetUseGrid( false ); // Disable grid usage
159
160 // When grid usage is disabled, Align should return original point
161 VECTOR2I original( 149, 251 );
162 VECTOR2I aligned = helper.Align( original );
163 BOOST_CHECK_EQUAL( aligned.x, original.x );
164 BOOST_CHECK_EQUAL( aligned.y, original.y );
165}
166
167BOOST_AUTO_TEST_CASE( AsymmetricGrid )
168{
169 GRID_HELPER helper;
170 helper.SetGridSize( VECTOR2D( 25, 75 ) ); // Different X and Y grid sizes
171 helper.SetOrigin( VECTOR2I( 0, 0 ) );
172 helper.SetGridSnapping( true );
173
174 VECTOR2I aligned = helper.Align( VECTOR2I( 30, 100 ) );
175 BOOST_CHECK_EQUAL( aligned.x, 25 ); // Nearest 25-unit boundary
176 BOOST_CHECK_EQUAL( aligned.y, 75 ); // Nearest 75-unit boundary
177
178 aligned = helper.Align( VECTOR2I( 40, 120 ) );
179 BOOST_CHECK_EQUAL( aligned.x, 50 ); // Next 25-unit boundary
180 BOOST_CHECK_EQUAL( aligned.y, 150 ); // Next 75-unit boundary
181}
182
184{
185 GRID_HELPER helper;
186
187 // Test snap flag getters/setters
188 BOOST_CHECK( helper.GetSnap() ); // Default should be true
189
190 helper.SetSnap( false );
191 BOOST_CHECK( !helper.GetSnap() );
192
193 helper.SetSnap( true );
194 BOOST_CHECK( helper.GetSnap() );
195
196 // Test grid usage flag
197 BOOST_CHECK( helper.GetUseGrid() ); // Default should be true
198
199 helper.SetUseGrid( false );
200 BOOST_CHECK( !helper.GetUseGrid() );
201
202 helper.SetUseGrid( true );
203 BOOST_CHECK( helper.GetUseGrid() );
204}
205
206BOOST_AUTO_TEST_CASE( MaskOperations )
207{
208 GRID_HELPER helper;
209
210 // Test mask operations
214
215 // These don't have getters, so we can't verify the mask state directly
216 // but we can verify the methods don't crash
217}
218
220{
221 GRID_HELPER helper;
222
223 // Test skip point operations
224 helper.SetSkipPoint( VECTOR2I( 100, 100 ) );
225 helper.ClearSkipPoint();
226
227 // These methods should not crash
228}
229
230BOOST_AUTO_TEST_CASE( GridTypeAlignment )
231{
232 GRID_HELPER helper;
233 helper.SetGridSize( VECTOR2D( 100, 100 ) );
234 helper.SetOrigin( VECTOR2I( 0, 0 ) );
235 helper.SetGridSnapping( true );
236
237 // Test alignment with specific grid type
238 VECTOR2I aligned = helper.Align( VECTOR2I( 149, 251 ), GRID_CURRENT );
239 BOOST_CHECK_EQUAL( aligned.x, 100 );
240 BOOST_CHECK_EQUAL( aligned.y, 300 );
241
242 aligned = helper.AlignGrid( VECTOR2I( 149, 251 ), GRID_CURRENT );
243 BOOST_CHECK_EQUAL( aligned.x, 100 );
244 BOOST_CHECK_EQUAL( aligned.y, 300 );
245}
246
248{
249 GRID_HELPER helper;
250 helper.SetGridSize( VECTOR2D( 1, 1 ) ); // Very small grid
251 helper.SetOrigin( VECTOR2I( 0, 0 ) );
252 helper.SetGridSnapping( true );
253
254 // Test with very small grid
255 VECTOR2I aligned = helper.Align( VECTOR2I( 5, 5 ) );
256 BOOST_CHECK_EQUAL( aligned.x, 5 );
257 BOOST_CHECK_EQUAL( aligned.y, 5 );
258
259 // Test with zero point
260 aligned = helper.Align( VECTOR2I( 0, 0 ) );
261 BOOST_CHECK_EQUAL( aligned.x, 0 );
262 BOOST_CHECK_EQUAL( aligned.y, 0 );
263
264 // Test with large grid
265 helper.SetGridSize( VECTOR2D( 10000, 10000 ) );
266 aligned = helper.Align( VECTOR2I( 3000, 7000 ) );
267 BOOST_CHECK_EQUAL( aligned.x, 0 ); // Closer to 0 than 10000
268 BOOST_CHECK_EQUAL( aligned.y, 10000 ); // Closer to 10000 than 0
269}
270
272{
273 GRID_HELPER helper;
274
275 // Test GetGridSize with different grid types
276 helper.SetGridSize( VECTOR2D( 50, 75 ) );
277
278 VECTOR2D gridSize = helper.GetGridSize( GRID_CURRENT );
279 BOOST_CHECK_EQUAL( gridSize.x, 50 );
280 BOOST_CHECK_EQUAL( gridSize.y, 75 );
281
282 // Other grid types should return the same in the base implementation
283 gridSize = helper.GetGridSize( GRID_CONNECTABLE );
284 BOOST_CHECK_EQUAL( gridSize.x, 50 );
285 BOOST_CHECK_EQUAL( gridSize.y, 75 );
286}
287
289{
290 GRID_HELPER helper;
291 helper.SetVisibleGridSize( VECTOR2D( 25, 35 ) );
292
293 VECTOR2D visibleGrid = helper.GetVisibleGrid();
294 BOOST_CHECK_EQUAL( visibleGrid.x, 25 );
295 BOOST_CHECK_EQUAL( visibleGrid.y, 35 );
296}
297
298BOOST_AUTO_TEST_CASE( SnapPointManagement )
299{
300 GRID_HELPER helper;
301
302 // Initially should have no snapped point
303 auto snappedPoint = helper.GetSnappedPoint();
304 BOOST_CHECK( !snappedPoint.has_value() );
305
306 // After clearing anchors, still no snapped point
307 TEST_CLEAR_ANCHORS( helper );
308 snappedPoint = helper.GetSnappedPoint();
309 BOOST_CHECK( !snappedPoint.has_value() );
310}
311
bool GetSnap() const
Definition: grid_helper.h:119
void SetSnap(bool aSnap)
Definition: grid_helper.h:118
void SetSkipPoint(const VECTOR2I &aPoint)
Definition: grid_helper.h:105
bool GetUseGrid() const
Definition: grid_helper.h:122
void SetOrigin(const VECTOR2I &aOrigin)
Definition: grid_helper.h:69
void SetVisibleGridSize(const VECTOR2D &aGrid)
Definition: grid_helper.h:68
void ClearSkipPoint()
Clear the skip point by setting it to an unreachable position, thereby preventing matching.
Definition: grid_helper.h:113
void SetGridSnapping(bool aEnable)
Definition: grid_helper.h:70
void SetUseGrid(bool aSnapToGrid)
Definition: grid_helper.h:121
std::optional< VECTOR2I > GetSnappedPoint() const
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0))
VECTOR2D GetVisibleGrid() const
virtual VECTOR2D GetGridSize(GRID_HELPER_GRIDS aGrid) const
Return the size of the specified grid.
VECTOR2I GetGrid() const
VECTOR2I GetOrigin() const
void SetGridSize(const VECTOR2D &aGrid)
Definition: grid_helper.h:67
void clearAnchors()
Definition: grid_helper.h:198
void SetMaskFlag(int aFlag)
Definition: grid_helper.h:127
void SetMask(int aMask)
Definition: grid_helper.h:126
virtual VECTOR2I Align(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition: grid_helper.h:74
void ClearMaskFlag(int aFlag)
Definition: grid_helper.h:128
virtual VECTOR2I AlignGrid(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition: grid_helper.h:79
@ GRID_CURRENT
Definition: grid_helper.h:45
@ GRID_CONNECTABLE
Definition: grid_helper.h:47
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK_EQUAL(ret, c.m_exp_result)
void TEST_CLEAR_ANCHORS(GRID_HELPER &helper)
BOOST_AUTO_TEST_CASE(DefaultConstructor)
BOOST_AUTO_TEST_SUITE_END()
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695
VECTOR2< double > VECTOR2D
Definition: vector2d.h:694