KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_distribute.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
20
#include <boost/test/unit_test.hpp>
21
22
#include <
geometry/distribute.h
>
23
24
#include "
geom_test_utils.h
"
25
26
struct
DistributeFixture
27
{
28
};
29
33
BOOST_FIXTURE_TEST_SUITE( Distribute,
DistributeFixture
)
34
35
struct
DISTRIBUTE_GAPS_TEST_CASE
36
{
37
std::vector<std::pair<int, int>>
m_extents
;
38
std::vector<int>
m_expectedDeltas
;
39
};
40
41
static
void
DoDistributeGapsTestChecks
(
const
DISTRIBUTE_GAPS_TEST_CASE
& aTestCase )
42
{
43
// Actally do the chamfer
44
const
std::vector<int> deltas =
GetDeltasForDistributeByGaps
( aTestCase.
m_extents
);
45
46
BOOST_REQUIRE_EQUAL( deltas.size(), aTestCase.
m_expectedDeltas
.size() );
47
48
// First and last items should not be moved
49
BOOST_CHECK_EQUAL
( deltas.front(), 0 );
50
BOOST_CHECK_EQUAL
( deltas.back(), 0 );
51
52
for
(
size_t
i = 0; i < deltas.size(); ++i )
53
{
54
BOOST_CHECK_EQUAL
( deltas[i], aTestCase.
m_expectedDeltas
[i] );
55
}
56
}
57
58
BOOST_AUTO_TEST_CASE
( DistributeGapsNoChangeNeeded )
59
{
60
const
DISTRIBUTE_GAPS_TEST_CASE
testcase{
61
{
62
// Already evenly spaced (100 gaps)
63
{ 0, 100 },
64
{ 200, 300 },
65
{ 400, 500 },
66
},
67
{ 0, 0, 0 },
68
};
69
70
DoDistributeGapsTestChecks
( testcase );
71
}
72
73
BOOST_AUTO_TEST_CASE
( DistributeGapsSimpleShiftNeeded )
74
{
75
const
DISTRIBUTE_GAPS_TEST_CASE
testcase{
76
{
77
// Need to move item 1 51 to the right
78
{ 0, 100 },
79
{ 149, 249 },
80
{ 400, 500 },
81
},
82
{ 0, 51, 0 },
83
};
84
85
DoDistributeGapsTestChecks
( testcase );
86
}
87
88
BOOST_AUTO_TEST_CASE
( DistributeGapsRounding )
89
{
90
const
DISTRIBUTE_GAPS_TEST_CASE
testcase{
91
{
92
// Have to fit 3 gaps into total sum of gaps of 100
93
// so 33.333333 per gap
94
// (note one rounds up, the other down)
95
{ -100, 0 },
96
{ 0, 100 },
// Move this to 33 .. 133
97
{ 0, 100 },
// Move this to 167 .. 267
98
{ 300, 400 },
99
},
100
{ 0, 33, 167, 0 },
101
};
102
103
DoDistributeGapsTestChecks
( testcase );
104
}
105
106
107
struct
DISTRIBUTE_POINTS_TEST_CASE
108
{
109
std::vector<int>
m_points
;
110
std::vector<int>
m_expectedDeltas
;
111
};
112
113
static
void
DoDistributePointsTestChecks
(
const
DISTRIBUTE_POINTS_TEST_CASE
& aTestCase )
114
{
115
// Actally do the chamfer
116
const
std::vector<int> deltas =
GetDeltasForDistributeByPoints
( aTestCase.
m_points
);
117
118
BOOST_REQUIRE_EQUAL( deltas.size(), aTestCase.
m_expectedDeltas
.size() );
119
120
// First and last items should not be moved
121
BOOST_CHECK_EQUAL
( deltas.front(), 0 );
122
BOOST_CHECK_EQUAL
( deltas.back(), 0 );
123
124
for
(
size_t
i = 0; i < deltas.size(); ++i )
125
{
126
BOOST_CHECK_EQUAL
( deltas[i], aTestCase.
m_expectedDeltas
[i] );
127
}
128
}
129
130
BOOST_AUTO_TEST_CASE
( DistributePointsNoChangeNeeded )
131
{
132
const
DISTRIBUTE_POINTS_TEST_CASE
testcase{
133
// Already evenly spaced (100 gaps)
134
{ 0, 100, 200, 300, 400 },
135
{ 0, 0, 0, 0, 0 },
136
};
137
138
DoDistributePointsTestChecks
( testcase );
139
}
140
141
BOOST_AUTO_TEST_CASE
( DistributePointsSimpleShiftNeeded )
142
{
143
const
DISTRIBUTE_POINTS_TEST_CASE
testcase{
144
// Need to move item 1 51 to the right
145
{ 0, 49, 200 },
146
{ 0, 51, 0 },
147
};
148
149
DoDistributePointsTestChecks
( testcase );
150
}
151
152
BOOST_AUTO_TEST_CASE
( DistributePointsRounding )
153
{
154
const
DISTRIBUTE_POINTS_TEST_CASE
testcase{
155
// Have to fit 3 gaps into total sum of gaps of 100
156
// so 33.333333 per gap
157
// (note one rounds up, the other down)
158
{ 0, 0, 0, 100 },
159
{ 0, 33, 67, 0 },
160
};
161
162
DoDistributePointsTestChecks
( testcase );
163
}
164
165
BOOST_AUTO_TEST_SUITE_END
()
distribute.h
GetDeltasForDistributeByGaps
std::vector< int > GetDeltasForDistributeByGaps(const std::vector< std::pair< int, int > > &aItemExtents)
Given a list of 'n' item spans (e.g.
Definition
distribute.cpp:25
GetDeltasForDistributeByPoints
std::vector< int > GetDeltasForDistributeByPoints(const std::vector< int > &aItemPositions)
Definition
distribute.cpp:67
geom_test_utils.h
DISTRIBUTE_GAPS_TEST_CASE
Declares the FilletFixture struct as the boost test fixture.
Definition
test_distribute.cpp:36
DISTRIBUTE_GAPS_TEST_CASE::m_extents
std::vector< std::pair< int, int > > m_extents
Definition
test_distribute.cpp:37
DISTRIBUTE_GAPS_TEST_CASE::m_expectedDeltas
std::vector< int > m_expectedDeltas
Definition
test_distribute.cpp:38
DISTRIBUTE_POINTS_TEST_CASE
Definition
test_distribute.cpp:108
DISTRIBUTE_POINTS_TEST_CASE::m_points
std::vector< int > m_points
Definition
test_distribute.cpp:109
DISTRIBUTE_POINTS_TEST_CASE::m_expectedDeltas
std::vector< int > m_expectedDeltas
Definition
test_distribute.cpp:110
DistributeFixture
Definition
test_distribute.cpp:27
DoDistributeGapsTestChecks
static void DoDistributeGapsTestChecks(const DISTRIBUTE_GAPS_TEST_CASE &aTestCase)
Definition
test_distribute.cpp:41
DoDistributePointsTestChecks
static void DoDistributePointsTestChecks(const DISTRIBUTE_POINTS_TEST_CASE &aTestCase)
Definition
test_distribute.cpp:113
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(DistributeGapsNoChangeNeeded)
Definition
test_distribute.cpp:58
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL
BOOST_CHECK_EQUAL(result, "25.4")
src
qa
tests
libs
kimath
geometry
test_distribute.cpp
Generated on Fri Jun 26 2026 00:05:44 for KiCad PCB EDA Suite by
1.13.2