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 (C) 2024 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 along
17 * with this program. If not, see <http://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
89// Test Sequence Generation
90BOOST_AUTO_TEST_CASE(LSETSequenceGeneration)
91{
92 LSET set({F_Cu, In1_Cu, In2_Cu});
93
94 LSEQ sequence = set.Seq();
95 BOOST_CHECK_EQUAL(sequence.size(), 3);
96 BOOST_CHECK_EQUAL(sequence[0], F_Cu);
97 BOOST_CHECK_EQUAL(sequence[1], In1_Cu);
98 BOOST_CHECK_EQUAL(sequence[2], In2_Cu);
99}
100
101// Test Hex and Binary Formatting
102BOOST_AUTO_TEST_CASE(LSETFormatting)
103{
104 LSET set({F_Cu, In1_Cu, In2_Cu});
105
106 std::string hexString = set.FmtHex();
107 std::string expectedHexString = "00000000_00000051"; // depends on bit ordering
108
109 BOOST_CHECK_EQUAL(hexString, expectedHexString);
110
111 std::string binString = set.FmtBin();
112 std::string expectedBinString = "0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0101_0001"; // depends on bit ordering
113}
114// Test ExtractLayer and Flip
115BOOST_AUTO_TEST_CASE(LSETManipulations)
116{
117 LSET set({F_Cu, In1_Cu, In2_Cu});
118
119 // Test ExtractLayer: should extract the layer set or undefined if more than one
120 PCB_LAYER_ID extractedLayer = set.ExtractLayer();
121 BOOST_CHECK_EQUAL(extractedLayer, UNDEFINED_LAYER);
122
123 // Test Flip: should swap front and back layers
124 set.Flip( 4 );
125 BOOST_CHECK(set.Contains(B_Cu));
126 BOOST_CHECK(set.Contains(In1_Cu)); // Internal layers remain unchanged
127
128 // Test setting a single layer
129 set = {F_Cu};
130 extractedLayer = set.ExtractLayer();
131 BOOST_CHECK_EQUAL(extractedLayer, F_Cu);
132 set.Flip();
133 extractedLayer = set.ExtractLayer();
134 BOOST_CHECK_EQUAL(extractedLayer, B_Cu);
135}
136
137// Test Static Mask Methods
138BOOST_AUTO_TEST_CASE(LSETStaticMasks)
139{
140 LSET internalCuMask = LSET::InternalCuMask();
141 BOOST_CHECK(internalCuMask.Contains(PCB_LAYER_ID::In1_Cu));
142 BOOST_CHECK(internalCuMask.Contains(PCB_LAYER_ID::In30_Cu));
143 BOOST_CHECK(!internalCuMask.Contains(PCB_LAYER_ID::F_Cu));
144 BOOST_CHECK(!internalCuMask.Contains(PCB_LAYER_ID::B_Cu));
145}
146
147// Auxiliary function to compare LSEQ objects
148bool compareLSEQ( const LSEQ& seq1, const LSEQ& seq2 )
149{
150 if( seq1.size() != seq2.size() )
151 return false;
152 for( size_t i = 0; i < seq1.size(); ++i )
153 {
154 if( seq1[i] != seq2[i] )
155 return false;
156 }
157 return true;
158}
159
160
161// Test LSET::Seq base case
162BOOST_AUTO_TEST_CASE( LSETSeqBaseCase )
163{
164 LSET lset( { F_Cu, B_Cu, In1_Cu, In2_Cu } );
166
167 LSEQ result = lset.Seq();
168 BOOST_CHECK( compareLSEQ( result, expected ) );
169}
170
171// Test LSET::Seq empty case
172BOOST_AUTO_TEST_CASE( LSETSeqEmptyCase )
173{
174 LSET lset;
175 LSEQ expected = {};
176
177 LSEQ result = lset.Seq();
178 BOOST_CHECK( compareLSEQ( result, expected ) );
179}
180
181// Test LSET::SeqStackupTop2Bottom base case
182BOOST_AUTO_TEST_CASE( LSETSeqStackupTop2BottomBaseCase )
183{
187
188 LSEQ result = lset.SeqStackupTop2Bottom( UNDEFINED_LAYER );
189 BOOST_CHECK( compareLSEQ( result, expected ) );
190}
191
192// Test LSET::SeqStackupTop2Bottom prioritizing selected layer
193BOOST_AUTO_TEST_CASE( LSETSeqStackupTop2BottomWithSelection )
194{
197
198 LSEQ result = lset.SeqStackupTop2Bottom( F_SilkS );
199 BOOST_CHECK( compareLSEQ( result, expected ) );
200}
201
202// Test LSET::SeqStackupForPlotting base case
203BOOST_AUTO_TEST_CASE( LSETSeqStackupForPlottingBaseCase )
204{
207
208 LSEQ result = lset.SeqStackupForPlotting();
209 BOOST_CHECK( compareLSEQ( result, expected ) );
210}
211
212
BASE_SET & set(size_t pos)
Definition: base_set.h:115
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:36
PCB_LAYER_ID ExtractLayer() const
Find the first set PCB_LAYER_ID.
Definition: lset.cpp:640
static LSET InternalCuMask()
Return a complete set of internal copper layers which is all Cu layers except F_Cu and B_Cu.
Definition: lset.cpp:675
std::string FmtHex() const
Return a hex string showing contents of this LSEQ.
Definition: lset.cpp:324
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:420
bool Contains(PCB_LAYER_ID aLayer) const
See if the layer set contains a PCB layer.
Definition: lset.h:62
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:60
@ In30_Cu
Definition: layer_ids.h:95
@ Edge_Cuts
Definition: layer_ids.h:112
@ Dwgs_User
Definition: layer_ids.h:107
@ B_Cu
Definition: layer_ids.h:65
@ In2_Cu
Definition: layer_ids.h:67
@ Margin
Definition: layer_ids.h:113
@ F_SilkS
Definition: layer_ids.h:100
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
@ In1_Cu
Definition: layer_ids.h:66
@ B_SilkS
Definition: layer_ids.h:101
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:135
@ F_Cu
Definition: layer_ids.h:64
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
VECTOR3I expected(15, 30, 45)