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{
41 BASE_SET base;
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));
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));
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_00000007"; // 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|0000_0111"; // depends on bit ordering
113
114 BOOST_CHECK_EQUAL(binString, expectedBinString);
115}
116
117// Test ExtractLayer and Flip
118BOOST_AUTO_TEST_CASE(LSETManipulations)
119{
120 LSET set({F_Cu, In1_Cu, In2_Cu});
121
122 // Test ExtractLayer: should extract the layer set or undefined if more than one
123 PCB_LAYER_ID extractedLayer = set.ExtractLayer();
124 BOOST_CHECK_EQUAL(extractedLayer, UNDEFINED_LAYER);
125
126 // Test Flip: should swap front and back layers
127 set.Flip( 4 );
128 BOOST_CHECK(set.Contains(B_Cu));
129 BOOST_CHECK(set.Contains(In1_Cu)); // Internal layers remain unchanged
130
131 // Test setting a single layer
132 set = {F_Cu};
133 extractedLayer = set.ExtractLayer();
134 BOOST_CHECK_EQUAL(extractedLayer, F_Cu);
135 set.Flip();
136 extractedLayer = set.ExtractLayer();
137 BOOST_CHECK_EQUAL(extractedLayer, B_Cu);
138}
139
140// Test Static Mask Methods
141BOOST_AUTO_TEST_CASE(LSETStaticMasks)
142{
143 LSET internalCuMask = LSET::InternalCuMask();
146 BOOST_CHECK(!internalCuMask.Contains(PCB_LAYER_ID::F_Cu));
147 BOOST_CHECK(!internalCuMask.Contains(PCB_LAYER_ID::B_Cu));
148}
149
150BOOST_AUTO_TEST_SUITE_END()
BASE_SET & set(size_t pos=std::numeric_limits< size_t >::max(), bool value=true)
Definition: base_set.h:61
bool test(size_t pos) const
Definition: base_set.h:47
size_t count() const
Definition: base_set.h:106
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:35
bool Contains(PCB_LAYER_ID aLayer)
See if the layer set contains a PCB layer.
Definition: lset.h:79
PCB_LAYER_ID ExtractLayer() const
Find the first set PCB_LAYER_ID.
Definition: lset.cpp:686
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:721
std::string FmtHex() const
Return a hex string showing contents of this LSEQ.
Definition: lset.cpp:302
BOOST_AUTO_TEST_CASE(LSETConstructorEmpty)
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ In30_Cu
Definition: layer_ids.h:94
@ B_Cu
Definition: layer_ids.h:95
@ In2_Cu
Definition: layer_ids.h:66
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
@ In1_Cu
Definition: layer_ids.h:65
@ F_Cu
Definition: layer_ids.h:64
BOOST_CHECK(box.ClosestPointTo(VECTOR2D(0, 0))==VECTOR2D(1, 2))
Test suite for KiCad math code.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)