Skip to content

Commit 204cf24

Browse files
Add bitset unit tests
1 parent 4f88005 commit 204cf24

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

tests/testEnumUtils.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (C) 2005 - 2023 Settlers Freaks (sf-team at siedler25.org)
2+
//
3+
// SPDX-License-Identifier: GPL-2.0-or-later
4+
5+
#include "s25util/enumUtils.h"
6+
#include <boost/test/unit_test.hpp>
7+
#include <sstream>
8+
9+
enum class InvalidBitset : int
10+
{
11+
};
12+
template<>
13+
struct IsBitset<InvalidBitset> : std::true_type
14+
{};
15+
16+
enum class Bitset : unsigned
17+
{
18+
None,
19+
A = 1 << 0,
20+
B = 1 << 1,
21+
C = 1 << 2
22+
};
23+
MAKE_BITSET_STRONG(Bitset);
24+
25+
// Check type traits
26+
static_assert(IsBitset<InvalidBitset>::value);
27+
static_assert(!IsValidBitset<InvalidBitset>);
28+
29+
static_assert(IsBitset<Bitset>::value);
30+
static_assert(IsValidBitset<Bitset>);
31+
32+
BOOST_AUTO_TEST_SUITE(EnumUtils)
33+
34+
BOOST_AUTO_TEST_CASE(Operators)
35+
{
36+
BOOST_REQUIRE(static_cast<unsigned>(Bitset{}) == 0);
37+
38+
{
39+
Bitset b{};
40+
b = b | Bitset::A;
41+
BOOST_TEST(static_cast<unsigned>(b) == 0x01u);
42+
b |= Bitset::B;
43+
BOOST_TEST(static_cast<unsigned>(b) == 0x03u);
44+
}
45+
46+
{
47+
Bitset b = Bitset::A | Bitset::B | Bitset::C;
48+
b = b & (Bitset::A | Bitset::B);
49+
BOOST_TEST(static_cast<unsigned>(b) == 0x03u);
50+
b &= Bitset::B;
51+
BOOST_TEST(static_cast<unsigned>(b) == 0x02u);
52+
}
53+
}
54+
55+
BOOST_AUTO_TEST_CASE(UtilityFunctions)
56+
{
57+
Bitset b = Bitset::A | Bitset::C;
58+
BOOST_TEST(bitset::isSet(b, Bitset::A));
59+
BOOST_TEST(bitset::isSet(b, Bitset::A | Bitset::C));
60+
BOOST_TEST(!bitset::isSet(b, Bitset::B));
61+
BOOST_TEST(!bitset::isSet(b, Bitset::B | Bitset::C));
62+
63+
b = bitset::set(b, Bitset::B /*, true */);
64+
BOOST_TEST(static_cast<unsigned>(b) == 0x07u);
65+
66+
b = bitset::set(b, Bitset::B, false);
67+
BOOST_TEST(static_cast<unsigned>(b) == 0x05u);
68+
69+
b = bitset::clear(b, Bitset::A);
70+
BOOST_TEST(static_cast<unsigned>(b) == 0x04u);
71+
72+
b = bitset::toggle(b, Bitset::A);
73+
BOOST_TEST(static_cast<unsigned>(b) == 0x05u);
74+
75+
b = bitset::toggle(b, Bitset::A | Bitset::B);
76+
BOOST_TEST(static_cast<unsigned>(b) == 0x06u);
77+
}
78+
79+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)