KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_base_set.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#define BOOST_TEST_NO_MAIN
20#include <boost/test/unit_test.hpp>
21#include "base_set.h"
22
23BOOST_AUTO_TEST_SUITE( BaseSetTests )
24
25BOOST_AUTO_TEST_CASE( ConstructionAndSize )
26{
27 BASE_SET bs( 10 );
28 BOOST_CHECK_EQUAL( bs.size(), 10 );
29 BOOST_CHECK_EQUAL( bs.count(), 0 );
30
31 bs.resize( 20 );
32 BOOST_CHECK_EQUAL( bs.size(), 20 );
33 BOOST_CHECK_EQUAL( bs.count(), 0 );
34}
35
36BOOST_AUTO_TEST_CASE( BitSettingAndResetting )
37{
38 BASE_SET bs( 10 );
39 bs.set( 2 );
40 BOOST_CHECK( bs.test( 2 ) );
41 BOOST_CHECK_EQUAL( bs.count(), 1 );
42
43 bs.reset( 2 );
44 BOOST_CHECK( !bs.test( 2 ) );
45 BOOST_CHECK_EQUAL( bs.count(), 0 );
46}
47
48BOOST_AUTO_TEST_CASE( SetOutOfRange )
49{
50 BASE_SET bs( 10 );
51 BOOST_CHECK_EQUAL( bs.size(), 10 );
52 BOOST_CHECK_EQUAL( bs.count(), 0 );
53
54 bs.set( 10 );
55 BOOST_CHECK_EQUAL( bs.size(), 11 );
56 BOOST_CHECK_EQUAL( bs.count(), 1 );
57
58 bs.reset( 10 );
59 BOOST_CHECK_EQUAL( bs.size(), 11 );
60 BOOST_CHECK_EQUAL( bs.count(), 0 );
61
62 bs.reset( 20 );
63 BOOST_CHECK_EQUAL( bs.size(), 21 );
64 BOOST_CHECK_EQUAL( bs.count(), 0 );
65}
66
67BOOST_AUTO_TEST_CASE( IteratingSetBits )
68{
69 BASE_SET bs( 10 );
70 bs.set( 2 );
71 bs.set( 4 );
72
73 auto it = bs.set_bits_begin();
74 BOOST_CHECK_EQUAL( *it, 2 );
75 ++it;
76 BOOST_CHECK_EQUAL( *it, 4 );
77 ++it;
78 BOOST_CHECK( it == bs.set_bits_end() );
79
80 // Custom reverse iterator test
81 std::vector<size_t> reverse_set_bits;
82 for( auto rit = bs.set_bits_rbegin(); rit != bs.set_bits_rend(); ++rit )
83 {
84 reverse_set_bits.push_back( *rit );
85 }
86
87 BOOST_CHECK_EQUAL( reverse_set_bits.size(), 2 );
88 BOOST_CHECK_EQUAL( reverse_set_bits[0], 4 );
89 BOOST_CHECK_EQUAL( reverse_set_bits[1], 2 );
90}
91
92// Test equality operator
93BOOST_AUTO_TEST_CASE( BASE_SETEqualityOperator )
94{
95 BASE_SET set1( 10 );
96 BASE_SET set2( 10 );
97 BASE_SET set3( 15 );
98
99 set1.set( 2 );
100 set1.set( 4 );
101 set2.set( 2 );
102 set2.set( 4 );
103 set3.set( 2 );
104
105 BOOST_CHECK( set1 == set2 );
106 BOOST_CHECK( !( set1 == set3 ) );
107 BOOST_CHECK( !( set2 == set3 ) );
108}
109
110// Test less-than operator
111BOOST_AUTO_TEST_CASE(BASE_SETComparisonOperator)
112{
113 BASE_SET set1( 10 );
114 BASE_SET set2( 10 );
115 BASE_SET set3( 15 );
116
117 set1.set( 2 );
118 set1.set( 5 );
119 set2.set( 2 );
120 set3.set( 2 );
121
122 BOOST_CHECK( set3 < set1 ); // Although set3 is larger, set1 has a 1 at position 5, so set3 is less
123 BOOST_CHECK( set2 < set3 ); // set2 and set3 both have the same values set but set3 has more positions available
124 BOOST_CHECK( !( set1 < set3 ) );
125 BOOST_CHECK( !( set1 < set2 ) ); // Although sizes are equal, elements in set2 are subsets of set1
126}
127
128// Test compare function
129BOOST_AUTO_TEST_CASE(BASE_SETCompareFunction)
130{
131 BASE_SET set1( 10 );
132 BASE_SET set2( 10 );
133 BASE_SET set3( 10 );
134 BASE_SET set4( 15 );
135
136 set1.set( 2 );
137 set1.set( 4 );
138
139 set2.set( 2 );
140 set2.set( 4 );
141
142 set3.set( 2 );
143
144 set4.set( 2 );
145
146 BOOST_CHECK_EQUAL( set1.compare( set2 ), 0 ); // set1 and set2 are equal
147 BOOST_CHECK_EQUAL( set1.compare( set3 ), 1 ); // set1 is greater than set3
148 BOOST_CHECK_EQUAL( set3.compare( set1 ), -1 ); // set3 is less than set1
149 BOOST_CHECK_EQUAL( set3.compare( set4 ), -1 ); // set3 is less than set4
150 BOOST_CHECK_EQUAL( set4.compare( set3 ), 1 ); // set4 is greater than set3
151}
152
153
154// Test boolean operator&=
155BOOST_AUTO_TEST_CASE(BASE_SETAndAssignment)
156{
157 BASE_SET bs1( 10 );
158 BASE_SET bs2( 10 );
159 bs1.set( 1 );
160 bs1.set( 3 );
161 bs2.set( 2 );
162 bs2.set( 3 );
163
164 bs1 &= bs2;
165 BOOST_CHECK_EQUAL( bs1.test( 1 ), false );
166 BOOST_CHECK_EQUAL( bs1.test( 2 ), false );
167 BOOST_CHECK_EQUAL( bs1.test( 3 ), true );
168}
169
170// Test boolean operator|=
171BOOST_AUTO_TEST_CASE(BASE_SETOrAssignment)
172{
173 BASE_SET bs1( 10 );
174 BASE_SET bs2( 10 );
175 bs1.set( 1 );
176 bs2.set( 2 );
177 bs2.set( 3 );
178
179 bs1 |= bs2;
180 BOOST_CHECK_EQUAL( bs1.test( 1 ), true );
181 BOOST_CHECK_EQUAL( bs1.test( 2 ), true );
182 BOOST_CHECK_EQUAL( bs1.test( 3 ), true );
183}
184
185// Test boolean operator^=
186BOOST_AUTO_TEST_CASE(BASE_SETXorAssignment)
187{
188 BASE_SET bs1( 10 );
189 BASE_SET bs2( 10 );
190 bs1.set( 1 );
191 bs1.set( 3 );
192 bs2.set( 2 );
193 bs2.set( 3 );
194
195 bs1 ^= bs2;
196 BOOST_CHECK_EQUAL( bs1.test( 1 ), true );
197 BOOST_CHECK_EQUAL( bs1.test( 2 ), true );
198 BOOST_CHECK_EQUAL( bs1.test( 3 ), false );
199}
200
201// Test boolean operator~
202BOOST_AUTO_TEST_CASE(BASE_SETNotOperator)
203{
204 BASE_SET bs1( 4 );
205 bs1.set( 1 );
206 bs1.set( 3 );
207 BASE_SET bs2 = ~bs1;
208
209 BOOST_CHECK_EQUAL( bs2.test( 0 ), true );
210 BOOST_CHECK_EQUAL( bs2.test( 1 ), false );
211 BOOST_CHECK_EQUAL( bs2.test( 2 ), true );
212 BOOST_CHECK_EQUAL( bs2.test( 3 ), false );
213}
214
215// Test non-member operator&
216BOOST_AUTO_TEST_CASE(BASE_SETAndOperator)
217{
218 BASE_SET bs1( 10 );
219 BASE_SET bs2( 10 );
220 bs1.set( 1 );
221 bs1.set( 3 );
222 bs2.set( 2 );
223 bs2.set( 3 );
224
225 BASE_SET result = bs1 & bs2;
226 BOOST_CHECK_EQUAL( result.test( 1 ), false );
227 BOOST_CHECK_EQUAL( result.test( 2 ), false );
228 BOOST_CHECK_EQUAL( result.test( 3 ), true );
229}
230
231// Test non-member operator|
232BOOST_AUTO_TEST_CASE(BASE_SETOrOperator)
233{
234 BASE_SET bs1( 10 );
235 BASE_SET bs2( 10 );
236 bs1.set( 1 );
237 bs2.set( 2 );
238 bs2.set( 3 );
239
240 BASE_SET result = bs1 | bs2;
241 BOOST_CHECK_EQUAL( result.test( 1 ), true );
242 BOOST_CHECK_EQUAL( result.test( 2 ), true );
243 BOOST_CHECK_EQUAL( result.test( 3 ), true );
244}
245
246// Test non-member operator^
247BOOST_AUTO_TEST_CASE(BASE_SETXorOperator)
248{
249 BASE_SET bs1( 10 );
250 BASE_SET bs2( 10 );
251 bs1.set( 1 );
252 bs1.set( 3 );
253 bs2.set( 2 );
254 bs2.set( 3 );
255
256 BASE_SET result = bs1 ^ bs2;
257 BOOST_CHECK_EQUAL( result.test( 1 ), true );
258 BOOST_CHECK_EQUAL( result.test( 2 ), true );
259 BOOST_CHECK_EQUAL( result.test( 3 ), false );
260}
261
262// Test std::hash specialization
263BOOST_AUTO_TEST_CASE( BASE_SETHash )
264{
265 BASE_SET bs1( 10 );
266 bs1.set( 1 );
267 bs1.set( 3 );
268
269 std::hash<BASE_SET> hashFn;
270 size_t hash = hashFn( bs1 );
271
272 BASE_SET bs2( 10 );
273 bs2.set( 1 );
274 bs2.set( 3 );
275
276 BOOST_CHECK_EQUAL( hash, hashFn( bs2 ) );
277
278 bs2.set( 2 );
279 BOOST_CHECK_NE( hash, hashFn( bs2 ) );
280}
281
set_bits_reverse_iterator set_bits_rbegin() const
Definition: base_set.h:308
set_bits_iterator set_bits_end() const
Definition: base_set.h:306
BASE_SET & reset(size_t pos)
Definition: base_set.h:142
set_bits_reverse_iterator set_bits_rend() const
Definition: base_set.h:312
int compare(const BASE_SET &other) const
Definition: base_set.h:206
BASE_SET & set(size_t pos)
Definition: base_set.h:115
set_bits_iterator set_bits_begin() const
Definition: base_set.h:305
BOOST_AUTO_TEST_CASE(ConstructionAndSize)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()