KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_kiid.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 along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21#include <kiid.h>
22#include <wx/string.h>
23#include <set>
24
26
27
29{
31
32 KIID a0;
33 KIID b0;
34 KIID c0;
35 KIID d0;
36
37 // Make sure the hashes are unique
38 std::set<size_t> hashes;
39 hashes.insert( a0.Hash() );
40 hashes.insert( b0.Hash() );
41 hashes.insert( c0.Hash() );
42 hashes.insert( d0.Hash() );
43 BOOST_CHECK_EQUAL( hashes.size(), 4 );
44
46
47 KIID a;
48 BOOST_CHECK_EQUAL( a.Hash(), a0.Hash() );
49
50 KIID b;
51 BOOST_CHECK_EQUAL( b.Hash(), b0.Hash() );
52
53 KIID c;
54 BOOST_CHECK_EQUAL( c.Hash(), c0.Hash() );
55
56 KIID d;
57 BOOST_CHECK_EQUAL( d.Hash(), d0.Hash() );
58}
59
60
61BOOST_AUTO_TEST_CASE( KiidPathTest )
62{
63 KIID a, b, c, d;
64
65 KIID_PATH path1;
66 KIID_PATH path2;
67
68 path1.push_back( a );
69 path1.push_back( b );
70 path1.push_back( c );
71 path1.push_back( d );
72
73 path2.push_back( b );
74 path2.push_back( c );
75 path2.push_back( d );
76
77 BOOST_CHECK( path1.EndsWith( path2 ) == true );
78 BOOST_CHECK( path2.EndsWith( path1 ) == false );
79}
80
81
82BOOST_AUTO_TEST_CASE( LegacyTimestamp )
83{
84 timestamp_t ts_a = 0xAABBCCDD;
85 timestamp_t ts_b = 0x00000012;
86
87 wxString str_a( wxS( "AABBCCDD" ) );
88 wxString str_b( wxS( "00000012" ) );
89
90 KIID a( ts_a );
91 KIID b( ts_b );
92
93 BOOST_CHECK( a.AsLegacyTimestamp() == ts_a );
94 BOOST_CHECK( a.AsLegacyTimestampString() == str_a );
95
96 BOOST_CHECK( b.AsLegacyTimestamp() == ts_b );
97 BOOST_CHECK( b.AsLegacyTimestampString() == str_b );
98
99 BOOST_CHECK( KIID( str_a ).AsLegacyTimestamp() == ts_a );
100 BOOST_CHECK( KIID( str_b ).AsLegacyTimestamp() == ts_b );
101
102 BOOST_CHECK( KIID( str_a ).AsLegacyTimestampString() == str_a );
103 BOOST_CHECK( KIID( str_b ).AsLegacyTimestampString() == str_b );
104}
105
106
107BOOST_AUTO_TEST_CASE( CombineDeterministic )
108{
109 KIID a, b, c;
110
111 // Combining the same two KIIDs should always produce the same result
112 KIID combined1 = KIID::Combine( a, b );
113 KIID combined2 = KIID::Combine( a, b );
114 BOOST_CHECK_EQUAL( combined1.AsString(), combined2.AsString() );
115
116 // Combined result should be different from inputs
117 BOOST_CHECK( combined1 != a );
118 BOOST_CHECK( combined1 != b );
119
120 // Different inputs should produce different outputs
121 KIID combined3 = KIID::Combine( a, c );
122 BOOST_CHECK( combined1 != combined3 );
123
124 // Order matters for XOR with different values
125 KIID combined4 = KIID::Combine( b, a );
126 BOOST_CHECK_EQUAL( combined1.AsString(), combined4.AsString() );
127}
128
129
130BOOST_AUTO_TEST_CASE( CombineWithKnownValues )
131{
132 // Test with known UUID strings to verify XOR behavior
133 KIID a( "00000000-0000-0000-0000-000000000001" );
134 KIID b( "00000000-0000-0000-0000-000000000002" );
135
136 KIID combined = KIID::Combine( a, b );
137
138 // XOR of 0x01 and 0x02 = 0x03
139 BOOST_CHECK_EQUAL( combined.AsString(), "00000000-0000-0000-0000-000000000003" );
140
141 // Combining with self should give all zeros
142 KIID selfCombined = KIID::Combine( a, a );
143 BOOST_CHECK_EQUAL( selfCombined.AsString(), "00000000-0000-0000-0000-000000000000" );
144}
145
146
147BOOST_AUTO_TEST_CASE( CombineUniqueness )
148{
149 // Generate several random KIIDs and verify all combinations are unique.
150 // XOR is commutative, so Combine(a,b) == Combine(b,a). We only count
151 // unique unordered pairs, which gives n*(n-1)/2 combinations.
152 std::vector<KIID> ids;
153
154 for( int i = 0; i < 10; ++i )
155 ids.push_back( KIID() );
156
157 std::set<wxString> combinations;
158
159 for( size_t i = 0; i < ids.size(); ++i )
160 {
161 for( size_t j = i + 1; j < ids.size(); ++j )
162 {
163 KIID combined = KIID::Combine( ids[i], ids[j] );
164 combinations.insert( combined.AsString() );
165 }
166 }
167
168 // All 45 unique unordered pairs (10*9/2) should produce unique results
169 BOOST_CHECK_EQUAL( combinations.size(), 45 );
170}
171
172
bool EndsWith(const KIID_PATH &aPath) const
Test if aPath from the last path towards the first path.
Definition kiid.cpp:347
Definition kiid.h:49
static void SeedGenerator(unsigned int aSeed)
Re-initialize the UUID generator with a given seed (for testing or QA purposes)
Definition kiid.cpp:306
size_t Hash() const
Definition kiid.cpp:235
wxString AsString() const
Definition kiid.cpp:247
static KIID Combine(const KIID &aFirst, const KIID &aSecond)
Creates a deterministic KIID from two input KIIDs by XORing their underlying UUIDs.
Definition kiid.cpp:289
wxString AsLegacyTimestampString() const
Definition kiid.cpp:259
timestamp_t AsLegacyTimestamp() const
Definition kiid.cpp:222
uint32_t timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition kiid.h:46
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(Seeding)
Definition test_kiid.cpp:28
BOOST_CHECK_EQUAL(result, "25.4")