KiCad PCB EDA Suite
Loading...
Searching...
No Matches
common/test_lset.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 <lset.h>
24#include <lseq.h>
25
26// Macros for easier test specification
27#define PCB_LAYER_COUNT PCB_LAYER_ID_COUNT
28
29BOOST_AUTO_TEST_SUITE(LSETTests)
30
31// Initialize an empty LSET
32BOOST_AUTO_TEST_CASE(LSETConstructorEmpty)
33{
34 LSET set;
35 BOOST_CHECK_EQUAL(set.count(), 0);
36}
37
38// Initialize LSET from another BASE_SET
39BOOST_AUTO_TEST_CASE(LSETConstructorFromBaseSet)
40{
42 base.set(F_Cu);
43 base.set(In1_Cu);
44
45 LSET set(base);
46 BOOST_CHECK_EQUAL(set.count(), 2);
47 BOOST_CHECK(set.test(F_Cu));
48 BOOST_CHECK(set.test(In1_Cu));
49}
50
51// Initialize LSET from a specific PCB_LAYER_ID
52BOOST_AUTO_TEST_CASE(LSETConstructorFromLayer)
53{
54 LSET set( { F_Cu } );
55 BOOST_CHECK_EQUAL(set.count(), 1);
56 BOOST_CHECK(set.test(F_Cu));
57}
58
59// Initialize LSET from an initializer list
60BOOST_AUTO_TEST_CASE(LSETConstructorFromList)
61{
62 LSET set({F_Cu, In1_Cu, In2_Cu});
63 BOOST_CHECK_EQUAL(set.count(), 3);
64 BOOST_CHECK(set.test(F_Cu));
65 BOOST_CHECK(set.test(In1_Cu));
66 BOOST_CHECK(set.test(In2_Cu));
67}
68
69// Initialize LSET from LSEQ
70BOOST_AUTO_TEST_CASE(LSETConstructorFromSequence)
71{
72 LSEQ seq = {F_Cu, In1_Cu, In2_Cu};
73 LSET set(seq);
74 BOOST_CHECK_EQUAL(set.count(), 3);
75 BOOST_CHECK(set.test(F_Cu));
76 BOOST_CHECK(set.test(In1_Cu));
77 BOOST_CHECK(set.test(In2_Cu));
78}
79
80// Test Containment Check
82{
83 LSET set({F_Cu, In1_Cu, In2_Cu});
84 BOOST_CHECK(set.Contains(F_Cu));
85 BOOST_CHECK(set.Contains(In1_Cu));
86 BOOST_CHECK(!set.Contains(In30_Cu));
87}
88
89BOOST_AUTO_TEST_CASE( LSETContainsAllWithDifferentSizes )
90{
91 BASE_SET base( 64 );
92 base.set( F_Cu );
93
94 LSET shortSet( base );
95 LSET fullSet( { F_Cu } );
96
97 BOOST_CHECK( shortSet.ContainsAll( fullSet ) );
98 BOOST_CHECK( fullSet.ContainsAll( shortSet ) );
99
100 fullSet.set( User_14 );
101
102 BOOST_CHECK( !shortSet.ContainsAll( fullSet ) );
103 BOOST_CHECK( fullSet.ContainsAll( shortSet ) );
104}
105
106// Test Sequence Generation
107BOOST_AUTO_TEST_CASE(LSETSequenceGeneration)
108{
109 LSET set({F_Cu, In1_Cu, In2_Cu});
110
111 LSEQ sequence = set.Seq();
112 BOOST_CHECK_EQUAL(sequence.size(), 3);
113 BOOST_CHECK_EQUAL(sequence[0], F_Cu);
114 BOOST_CHECK_EQUAL(sequence[1], In1_Cu);
115 BOOST_CHECK_EQUAL(sequence[2], In2_Cu);
116}
117
118// Test Hex and Binary Formatting
119BOOST_AUTO_TEST_CASE(LSETFormatting)
120{
121 LSET set({F_Cu, In1_Cu, In2_Cu});
122
123 std::string hexString = set.FmtHex();
124 std::string expectedHexString = "00000000_00000000_00000000_00000051"; // depends on bit ordering
125
126 BOOST_CHECK_EQUAL(hexString, expectedHexString);
127
128 std::string binString = set.FmtBin();
129 std::string expectedBinString = "0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|"
130 "0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0101_0001"; // depends on bit ordering
131
132 BOOST_CHECK_EQUAL(binString, expectedBinString);
133}
134// Test ExtractLayer and Flip
135BOOST_AUTO_TEST_CASE(LSETManipulations)
136{
137 LSET set({F_Cu, In1_Cu, In2_Cu});
138
139 // Test ExtractLayer: should extract the layer set or undefined if more than one
140 PCB_LAYER_ID extractedLayer = set.ExtractLayer();
141 BOOST_CHECK_EQUAL(extractedLayer, UNDEFINED_LAYER);
142
143 // Test Flip: should swap front and back layers
144 set.FlipStandardLayers( 4 );
145 BOOST_CHECK(set.Contains(B_Cu));
146 BOOST_CHECK(set.Contains(In1_Cu)); // Internal layers remain unchanged
147
148 // Test setting a single layer
149 set = {F_Cu};
150 extractedLayer = set.ExtractLayer();
151 BOOST_CHECK_EQUAL(extractedLayer, F_Cu);
152 set.FlipStandardLayers();
153 extractedLayer = set.ExtractLayer();
154 BOOST_CHECK_EQUAL(extractedLayer, B_Cu);
155}
156
157// Test Static Mask Methods
158BOOST_AUTO_TEST_CASE(LSETStaticMasks)
159{
160 LSET internalCuMask = LSET::InternalCuMask();
161 BOOST_CHECK(internalCuMask.Contains(PCB_LAYER_ID::In1_Cu));
162 BOOST_CHECK(internalCuMask.Contains(PCB_LAYER_ID::In30_Cu));
163 BOOST_CHECK(!internalCuMask.Contains(PCB_LAYER_ID::F_Cu));
164 BOOST_CHECK(!internalCuMask.Contains(PCB_LAYER_ID::B_Cu));
165}
166
167// Auxiliary function to compare LSEQ objects
168bool compareLSEQ( const LSEQ& seq1, const LSEQ& seq2 )
169{
170 if( seq1.size() != seq2.size() )
171 return false;
172 for( size_t i = 0; i < seq1.size(); ++i )
173 {
174 if( seq1[i] != seq2[i] )
175 return false;
176 }
177 return true;
178}
179
180
181// Test LSET::Seq base case
182BOOST_AUTO_TEST_CASE( LSETSeqBaseCase )
183{
184 LSET lset( { F_Cu, B_Cu, In1_Cu, In2_Cu } );
186
187 LSEQ result = lset.Seq();
188 BOOST_CHECK( compareLSEQ( result, expected ) );
189}
190
191// Test LSET::Seq empty case
192BOOST_AUTO_TEST_CASE( LSETSeqEmptyCase )
193{
194 LSET lset;
195 LSEQ expected = {};
196
197 LSEQ result = lset.Seq();
198 BOOST_CHECK( compareLSEQ( result, expected ) );
199}
200
201// Test LSET::SeqStackupTop2Bottom base case
202BOOST_AUTO_TEST_CASE( LSETSeqStackupTop2BottomBaseCase )
203{
207
208 LSEQ result = lset.SeqStackupTop2Bottom( UNDEFINED_LAYER );
209 BOOST_CHECK( compareLSEQ( result, expected ) );
210}
211
212// Test LSET::SeqStackupTop2Bottom prioritizing selected layer
213BOOST_AUTO_TEST_CASE( LSETSeqStackupTop2BottomWithSelection )
214{
217
218 LSEQ result = lset.SeqStackupTop2Bottom( F_SilkS );
219 BOOST_CHECK( compareLSEQ( result, expected ) );
220}
221
222// Test LSET::SeqStackupForPlotting base case
223BOOST_AUTO_TEST_CASE( LSETSeqStackupForPlottingBaseCase )
224{
227
228 LSEQ result = lset.SeqStackupForPlotting();
229 BOOST_CHECK( compareLSEQ( result, expected ) );
230}
231
232
BASE_SET & set(size_t pos)
Definition base_set.h:126
std::string FmtHex() const
Return a hex string showing contents of this set.
Definition base_set.h:312
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition lseq.h:47
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
bool ContainsAll(const LSET &aLayers) const
See if this layer set contains all layers in another set.
Definition lset.h:85
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition lset.cpp:309
static const LSET & InternalCuMask()
Return a complete set of internal copper layers which is all Cu layers except F_Cu and B_Cu.
Definition lset.cpp:573
bool Contains(PCB_LAYER_ID aLayer) const
See if the layer set contains a PCB layer.
Definition lset.h:63
BOOST_AUTO_TEST_CASE(LSETConstructorEmpty)
bool compareLSEQ(const LSEQ &seq1, const LSEQ &seq2)
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ In30_Cu
Definition layer_ids.h:91
@ Edge_Cuts
Definition layer_ids.h:108
@ Dwgs_User
Definition layer_ids.h:103
@ B_Cu
Definition layer_ids.h:61
@ User_14
Definition layer_ids.h:133
@ In2_Cu
Definition layer_ids.h:63
@ Margin
Definition layer_ids.h:109
@ F_SilkS
Definition layer_ids.h:96
@ UNDEFINED_LAYER
Definition layer_ids.h:57
@ In1_Cu
Definition layer_ids.h:62
@ B_SilkS
Definition layer_ids.h:97
@ PCB_LAYER_ID_COUNT
Definition layer_ids.h:167
@ F_Cu
Definition layer_ids.h:60
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
VECTOR3I expected(15, 30, 45)
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")