KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_hashtables.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 <
hashtables.h
>
22
23
BOOST_AUTO_TEST_SUITE
( HashCombine )
24
25
26
BOOST_AUTO_TEST_CASE
( KnownVectors )
27
{
28
// Pinned results of the Boost hash_combine mixing step. The formula must
29
// stay stable because diff-engine summary hashes that are persisted depend
30
// on it. Computed directly from h ^ ( v + 0x9e3779b9 + (h<<6) + (h>>2) ).
31
BOOST_CHECK_EQUAL
(
KiHashCombine
( 0u, 0u ),
static_cast<
std::size_t
>
( 0x9e3779b9u ) );
32
33
// h == 0 short-circuits the shifts, so the result is just v + 0x9e3779b9.
34
BOOST_CHECK_EQUAL
(
KiHashCombine
( 0u, 1u ),
static_cast<
std::size_t
>
( 0x9e3779bau ) );
35
36
// Spot-check a non-zero seed against the longhand formula.
37
{
38
const
std::size_t
seed
= 0x12345678u;
39
const
std::size_t value = 0xdeadbeefu;
40
const
std::size_t
expected
=
41
seed
^ ( value + 0x9e3779b9 + (
seed
<< 6 ) + (
seed
>> 2 ) );
42
BOOST_CHECK_EQUAL
(
KiHashCombine
(
seed
, value ),
expected
);
43
}
44
}
45
46
47
BOOST_AUTO_TEST_CASE
( Deterministic )
48
{
49
BOOST_CHECK_EQUAL
(
KiHashCombine
( 42u, 99u ),
KiHashCombine
( 42u, 99u ) );
50
}
51
52
53
BOOST_AUTO_TEST_CASE
( OrderSensitive )
54
{
55
// Folding (a then b) differs from (b then a) for distinct operands.
56
const
std::size_t a = 7u;
57
const
std::size_t b = 13u;
58
59
BOOST_CHECK(
KiHashCombine
(
KiHashCombine
( 0u, a ), b )
60
!=
KiHashCombine
(
KiHashCombine
( 0u, b ), a ) );
61
}
62
63
64
BOOST_AUTO_TEST_SUITE_END
()
hashtables.h
KiHashCombine
std::size_t KiHashCombine(std::size_t aSeed, std::size_t aValue)
Fold aValue into the running hash aSeed using the well-known Boost hash_combine mixing step.
Definition
hashtables.h:42
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
Definition
test_api_enums.cpp:71
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(KnownVectors)
Definition
test_hashtables.cpp:26
expected
VECTOR3I expected(15, 30, 45)
seed
const uint32_t seed
Definition
test_pip_benchmark.cpp:765
BOOST_CHECK_EQUAL
BOOST_CHECK_EQUAL(result, "25.4")
src
qa
tests
common
test_hashtables.cpp
Generated on Fri Jun 26 2026 00:05:43 for KiCad PCB EDA Suite by
1.13.2