KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_layer_range.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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
21#define BOOST_TEST_NO_MAIN
22#include <boost/test/unit_test.hpp>
23#include <layer_range.h>
24#include <vector>
25
26BOOST_AUTO_TEST_SUITE(LayerRangeTests)
27
28BOOST_AUTO_TEST_CASE( ForwardIterationTwoLayers )
29{
30 LAYER_RANGE range( F_Cu, B_Cu, 2 );
31 std::vector<PCB_LAYER_ID> expected = { F_Cu, B_Cu };
32 std::vector<PCB_LAYER_ID> result;
33
34 for( auto layer : range )
35 {
36 result.push_back( layer );
37 }
38
39 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(), expected.begin(), expected.end() );
40}
41
42BOOST_AUTO_TEST_CASE( ForwardIterationFourLayers )
43{
44 LAYER_RANGE range( F_Cu, B_Cu, 4 );
45 std::vector<PCB_LAYER_ID> expected = { F_Cu, In1_Cu, In2_Cu, B_Cu };
46 std::vector<PCB_LAYER_ID> result;
47
48 for( auto layer : range )
49 {
50 result.push_back( layer );
51 }
52
53 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(), expected.begin(), expected.end() );
54}
55
56BOOST_AUTO_TEST_CASE( ReverseIterationFourLayers )
57{
58 LAYER_RANGE range( B_Cu, F_Cu, 4 );
59 std::vector<PCB_LAYER_ID> expected = { B_Cu, In2_Cu, In1_Cu, F_Cu };
60 std::vector<PCB_LAYER_ID> result;
61
62 for( auto layer : range )
63 {
64 result.push_back( layer );
65 }
66
67 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(), expected.begin(), expected.end() );
68}
69
70BOOST_AUTO_TEST_CASE( PartialRangeForward )
71{
72 LAYER_RANGE range( In1_Cu, B_Cu, 6 );
73 std::vector<PCB_LAYER_ID> expected = { In1_Cu, In2_Cu, In3_Cu, In4_Cu, B_Cu };
74 std::vector<PCB_LAYER_ID> result;
75
76 for( auto layer : range )
77 {
78 result.push_back( layer );
79 }
80
81 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(), expected.begin(), expected.end() );
82}
83
84BOOST_AUTO_TEST_CASE( PartialRangeReverse )
85{
86 LAYER_RANGE range( In3_Cu, F_Cu, 6 );
87 std::vector<PCB_LAYER_ID> expected = { In3_Cu, In2_Cu, In1_Cu, F_Cu };
88 std::vector<PCB_LAYER_ID> result;
89
90 for( auto layer : range )
91 {
92 result.push_back( layer );
93 }
94
95 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(), expected.begin(), expected.end() );
96}
97
98BOOST_AUTO_TEST_CASE( FromBackToInner )
99{
100 // Backdrill-from-bottom walks physically upward from B_Cu through the inner stack.
101 // Even though B_Cu's numeric id (2) is less than any inner layer's, the iterator must
102 // reverse so that the stack is traversed in physical order.
103 LAYER_RANGE range( B_Cu, In2_Cu, 4 );
104 std::vector<PCB_LAYER_ID> expected = { B_Cu, In2_Cu };
105 std::vector<PCB_LAYER_ID> result;
106
107 for( auto layer : range )
108 {
109 result.push_back( layer );
110 }
111
112 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(), expected.begin(), expected.end() );
113}
114
115BOOST_AUTO_TEST_CASE( FromBackToInnerSixLayer )
116{
117 LAYER_RANGE range( B_Cu, In2_Cu, 6 );
118 std::vector<PCB_LAYER_ID> expected = { B_Cu, In4_Cu, In3_Cu, In2_Cu };
119 std::vector<PCB_LAYER_ID> result;
120
121 for( auto layer : range )
122 {
123 result.push_back( layer );
124 }
125
126 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(), expected.begin(), expected.end() );
127}
128
129BOOST_AUTO_TEST_CASE( SizeMatchesIteration )
130{
131 // size() must match how many layers the iterator yields, even when B_Cu is an endpoint.
132 struct Case
133 {
134 PCB_LAYER_ID start;
135 PCB_LAYER_ID stop;
136 int layer_count;
137 size_t expected;
138 };
139
140 const Case cases[] = {
141 { F_Cu, B_Cu, 2, 2 },
142 { F_Cu, B_Cu, 4, 4 },
143 { F_Cu, In1_Cu, 4, 2 },
144 { In1_Cu, B_Cu, 6, 5 },
145 { In3_Cu, F_Cu, 6, 4 },
146 { B_Cu, In2_Cu, 4, 2 },
147 { B_Cu, In2_Cu, 6, 4 },
148 };
149
150 for( const Case& c : cases )
151 {
152 LAYER_RANGE range( c.start, c.stop, c.layer_count );
153 size_t counted = 0;
154
155 for( auto layer : range )
156 {
157 (void) layer;
158 ++counted;
159 }
160
161 BOOST_CHECK_EQUAL( range.size(), c.expected );
162 BOOST_CHECK_EQUAL( counted, c.expected );
163 }
164}
165
166BOOST_AUTO_TEST_CASE( InvalidLayerThrowsException )
167{
168 BOOST_CHECK_THROW( LAYER_RANGE( F_Mask, B_Cu, 4 ), std::invalid_argument );
169 BOOST_CHECK_THROW( LAYER_RANGE( F_Cu, B_Mask, 4 ), std::invalid_argument );
170}
171
172BOOST_AUTO_TEST_CASE( SingleLayerRange )
173{
174 LAYER_RANGE range( In2_Cu, In2_Cu, 6 );
175 std::vector<PCB_LAYER_ID> expected = { In2_Cu };
176 std::vector<PCB_LAYER_ID> result;
177
178 for( auto layer : range )
179 {
180 result.push_back( layer );
181 }
182
183 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(), expected.begin(), expected.end() );
184}
185
186BOOST_AUTO_TEST_CASE( ZeroLayerCountClampedToTwo )
187{
188 LAYER_RANGE range( F_Cu, B_Cu, 0 );
189 std::vector<PCB_LAYER_ID> expected = { F_Cu, B_Cu };
190 std::vector<PCB_LAYER_ID> result;
191
192 for( auto layer : range )
193 {
194 result.push_back( layer );
195 }
196
197 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(), expected.begin(), expected.end() );
198}
199
200BOOST_AUTO_TEST_CASE( OneLayerCountClampedToTwo )
201{
202 LAYER_RANGE range( F_Cu, B_Cu, 1 );
203 std::vector<PCB_LAYER_ID> expected = { F_Cu, B_Cu };
204 std::vector<PCB_LAYER_ID> result;
205
206 for( auto layer : range )
207 {
208 result.push_back( layer );
209 }
210
211 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(), expected.begin(), expected.end() );
212}
213
214BOOST_AUTO_TEST_CASE( NegativeLayerCountClampedToTwo )
215{
216 LAYER_RANGE range( F_Cu, B_Cu, -5 );
217 std::vector<PCB_LAYER_ID> expected = { F_Cu, B_Cu };
218 std::vector<PCB_LAYER_ID> result;
219
220 for( auto layer : range )
221 {
222 result.push_back( layer );
223 }
224
225 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(), expected.begin(), expected.end() );
226}
227
228BOOST_AUTO_TEST_CASE( MaxLayerCount )
229{
231 std::vector<PCB_LAYER_ID> expected = { F_Cu };
232
233
234 for( int i = In1_Cu; i < 2 * PCB_LAYER_ID_COUNT; i += 2 )
235 {
236 expected.push_back( static_cast<PCB_LAYER_ID>( i ) );
237 }
238 expected.push_back( B_Cu );
239
240 std::vector<PCB_LAYER_ID> result;
241 for( auto layer : range )
242 {
243 result.push_back( layer );
244 }
245
246 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(), expected.begin(), expected.end() );
247}
248
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ B_Mask
Definition layer_ids.h:94
@ B_Cu
Definition layer_ids.h:61
@ F_Mask
Definition layer_ids.h:93
@ In2_Cu
Definition layer_ids.h:63
@ In4_Cu
Definition layer_ids.h:65
@ In1_Cu
Definition layer_ids.h:62
@ PCB_LAYER_ID_COUNT
Definition layer_ids.h:167
@ In3_Cu
Definition layer_ids.h:64
@ F_Cu
Definition layer_ids.h:60
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(ForwardIterationTwoLayers)
VECTOR3I expected(15, 30, 45)
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")