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 ), VECTOR2D( 0, 0 ) );
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 work from the new reference point
100 VECTOR2I aligned = helper.AlignGrid( VECTOR2I( 149, 251 ) );
101 BOOST_CHECK_EQUAL( aligned.x, 125 );
102 BOOST_CHECK_EQUAL( aligned.y, 225 );
103}
104
105BOOST_AUTO_TEST_CASE( AlignWithAuxiliaryAxes )
106{
107 GRID_HELPER helper;
108 helper.SetGridSize( VECTOR2D( 100, 100 ) );
109 helper.SetOrigin( VECTOR2I( 0, 0 ) );
110 helper.SetGridSnapping( true );
111
112 // Set auxiliary axis at (75, 75)
113 helper.SetAuxAxes( true, VECTOR2I( 75, 75 ) );
114
115 // Point closer to aux axis than grid should snap to aux axis
116 VECTOR2I aligned = helper.Align( VECTOR2I( 80, 80 ) );
117 BOOST_CHECK_EQUAL( aligned.x, 75 ); // Closer to aux axis X
118 BOOST_CHECK_EQUAL( aligned.y, 75 ); // Closer to aux axis Y
119
120 // Point closer to grid than aux axis should snap to grid
121 aligned = helper.Align( VECTOR2I( 95, 95 ) );
122 BOOST_CHECK_EQUAL( aligned.x, 100 ); // Closer to grid
123 BOOST_CHECK_EQUAL( aligned.y, 100 ); // Closer to grid
124
125 // Disable aux axes
126 helper.SetAuxAxes( false );
127 aligned = helper.Align( VECTOR2I( 80, 80 ) );
128 BOOST_CHECK_EQUAL( aligned.x, 100 ); // Should snap to grid only
129 BOOST_CHECK_EQUAL( aligned.y, 100 );
130}
131
132BOOST_AUTO_TEST_CASE( GridSnappingDisabled )
133{
134 GRID_HELPER helper;
135 helper.SetGridSize( VECTOR2D( 100, 100 ) );
136 helper.SetOrigin( VECTOR2I( 0, 0 ) );
137 helper.SetGridSnapping( false ); // Disable grid snapping
138
139 // When grid snapping is disabled, Align should return original point
140 VECTOR2I original( 149, 251 );
141 VECTOR2I aligned = helper.Align( original );
142 BOOST_CHECK_EQUAL( aligned.x, original.x );
143 BOOST_CHECK_EQUAL( aligned.y, original.y );
144
145 // AlignGrid should still work regardless of grid snapping setting
146 aligned = helper.AlignGrid( original );
147 BOOST_CHECK_EQUAL( aligned.x, 100 );
148 BOOST_CHECK_EQUAL( aligned.y, 300 );
149}
150
151BOOST_AUTO_TEST_CASE( UseGridDisabled )
152{
153 GRID_HELPER helper;
154 helper.SetGridSize( VECTOR2D( 100, 100 ) );
155 helper.SetOrigin( VECTOR2I( 0, 0 ) );
156 helper.SetGridSnapping( true );
157 helper.SetUseGrid( false ); // Disable grid usage
158
159 // When grid usage is disabled, Align should return original point
160 VECTOR2I original( 149, 251 );
161 VECTOR2I aligned = helper.Align( original );
162 BOOST_CHECK_EQUAL( aligned.x, original.x );
163 BOOST_CHECK_EQUAL( aligned.y, original.y );
164}
165
166BOOST_AUTO_TEST_CASE( AsymmetricGrid )
167{
168 GRID_HELPER helper;
169 helper.SetGridSize( VECTOR2D( 25, 75 ) ); // Different X and Y grid sizes
170 helper.SetOrigin( VECTOR2I( 0, 0 ) );
171 helper.SetGridSnapping( true );
172
173 VECTOR2I aligned = helper.Align( VECTOR2I( 30, 100 ) );
174 BOOST_CHECK_EQUAL( aligned.x, 25 ); // Nearest 25-unit boundary
175 BOOST_CHECK_EQUAL( aligned.y, 75 ); // Nearest 75-unit boundary
176
177 aligned = helper.Align( VECTOR2I( 40, 120 ) );
178 BOOST_CHECK_EQUAL( aligned.x, 50 ); // Next 25-unit boundary
179 BOOST_CHECK_EQUAL( aligned.y, 150 ); // Next 75-unit boundary
180}
181
183{
184 GRID_HELPER helper;
185
186 // Test snap flag getters/setters
187 BOOST_CHECK( helper.GetSnap() ); // Default should be true
188
189 helper.SetSnap( false );
190 BOOST_CHECK( !helper.GetSnap() );
191
192 helper.SetSnap( true );
193 BOOST_CHECK( helper.GetSnap() );
194
195 // Test grid usage flag
196 BOOST_CHECK( helper.GetUseGrid() ); // Default should be true
197
198 helper.SetUseGrid( false );
199 BOOST_CHECK( !helper.GetUseGrid() );
200
201 helper.SetUseGrid( true );
202 BOOST_CHECK( helper.GetUseGrid() );
203}
204
205BOOST_AUTO_TEST_CASE( MaskOperations )
206{
207 GRID_HELPER helper;
208
209 // Test mask operations
213
214 // These don't have getters, so we can't verify the mask state directly
215 // but we can verify the methods don't crash
216}
217
219{
220 GRID_HELPER helper;
221
222 // Test skip point operations
223 helper.SetSkipPoint( VECTOR2I( 100, 100 ) );
224 helper.ClearSkipPoint();
225
226 // These methods should not crash
227}
228
229BOOST_AUTO_TEST_CASE( GridTypeAlignment )
230{
231 GRID_HELPER helper;
232 helper.SetGridSize( VECTOR2D( 100, 100 ) );
233 helper.SetOrigin( VECTOR2I( 0, 0 ) );
234 helper.SetGridSnapping( true );
235
236 // Test alignment with specific grid type
237 VECTOR2I aligned = helper.Align( VECTOR2I( 149, 251 ), GRID_CURRENT );
238 BOOST_CHECK_EQUAL( aligned.x, 100 );
239 BOOST_CHECK_EQUAL( aligned.y, 300 );
240
241 aligned = helper.AlignGrid( VECTOR2I( 149, 251 ), GRID_CURRENT );
242 BOOST_CHECK_EQUAL( aligned.x, 100 );
243 BOOST_CHECK_EQUAL( aligned.y, 300 );
244}
245
247{
248 GRID_HELPER helper;
249 helper.SetGridSize( VECTOR2D( 1, 1 ) ); // Very small grid
250 helper.SetOrigin( VECTOR2I( 0, 0 ) );
251 helper.SetGridSnapping( true );
252
253 // Test with very small grid
254 VECTOR2I aligned = helper.Align( VECTOR2I( 5, 5 ) );
255 BOOST_CHECK_EQUAL( aligned.x, 5 );
256 BOOST_CHECK_EQUAL( aligned.y, 5 );
257
258 // Test with zero point
259 aligned = helper.Align( VECTOR2I( 0, 0 ) );
260 BOOST_CHECK_EQUAL( aligned.x, 0 );
261 BOOST_CHECK_EQUAL( aligned.y, 0 );
262
263 // Test with large grid
264 helper.SetGridSize( VECTOR2D( 10000, 10000 ) );
265 aligned = helper.Align( VECTOR2I( 3000, 7000 ) );
266 BOOST_CHECK_EQUAL( aligned.x, 0 ); // Closer to 0 than 10000
267 BOOST_CHECK_EQUAL( aligned.y, 10000 ); // Closer to 10000 than 0
268}
269
271{
272 GRID_HELPER helper;
273
274 // Test GetGridSize with different grid types
275 helper.SetGridSize( VECTOR2D( 50, 75 ) );
276
277 VECTOR2D gridSize = helper.GetGridSize( GRID_CURRENT );
278 BOOST_CHECK_EQUAL( gridSize.x, 50 );
279 BOOST_CHECK_EQUAL( gridSize.y, 75 );
280
281 // Other grid types should return the same in the base implementation
282 gridSize = helper.GetGridSize( GRID_CONNECTABLE );
283 BOOST_CHECK_EQUAL( gridSize.x, 50 );
284 BOOST_CHECK_EQUAL( gridSize.y, 75 );
285}
286
288{
289 GRID_HELPER helper;
290 helper.SetVisibleGridSize( VECTOR2D( 25, 35 ) );
291
292 VECTOR2D visibleGrid = helper.GetVisibleGrid();
293 BOOST_CHECK_EQUAL( visibleGrid.x, 25 );
294 BOOST_CHECK_EQUAL( visibleGrid.y, 35 );
295}
296
297BOOST_AUTO_TEST_CASE( SnapPointManagement )
298{
299 GRID_HELPER helper;
300
301 // Initially should have no snapped point
302 auto snappedPoint = helper.GetSnappedPoint();
303 BOOST_CHECK( !snappedPoint.has_value() );
304
305 // After clearing anchors, still no snapped point
306 TEST_CLEAR_ANCHORS( helper );
307 snappedPoint = helper.GetSnappedPoint();
308 BOOST_CHECK( !snappedPoint.has_value() );
309}
310
bool GetSnap() const
void SetSnap(bool aSnap)
void SetSkipPoint(const VECTOR2I &aPoint)
bool GetUseGrid() const
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.
void SetGridSnapping(bool aEnable)
Definition grid_helper.h:70
void SetUseGrid(bool aSnapToGrid)
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()
void SetMaskFlag(int aFlag)
void SetMask(int aMask)
virtual VECTOR2I Align(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition grid_helper.h:74
void ClearMaskFlag(int aFlag)
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_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
void TEST_CLEAR_ANCHORS(GRID_HELPER &helper)
BOOST_AUTO_TEST_CASE(DefaultConstructor)
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694