forked from Return-To-The-Roots/libutil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestEnumUtils.cpp
More file actions
83 lines (67 loc) · 2.06 KB
/
testEnumUtils.cpp
File metadata and controls
83 lines (67 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (C) 2005 - 2023 Settlers Freaks (sf-team at siedler25.org)
//
// SPDX-License-Identifier: GPL-2.0-or-later
#include "s25util/enumUtils.h"
#include <boost/test/unit_test.hpp>
#include <sstream>
enum class InvalidBitset : int
{
};
template<>
struct IsBitset<InvalidBitset> : std::true_type
{};
enum class Bitset : unsigned
{
None,
A = 1 << 0,
B = 1 << 1,
C = 1 << 2
};
MAKE_BITSET_STRONG(Bitset);
// Check type traits
static_assert(IsBitset<InvalidBitset>::value);
static_assert(!IsValidBitset_v<InvalidBitset>);
static_assert(IsBitset<Bitset>::value);
static_assert(IsValidBitset_v<Bitset>);
BOOST_AUTO_TEST_SUITE(EnumUtils)
BOOST_AUTO_TEST_CASE(Operators)
{
BOOST_REQUIRE(static_cast<unsigned>(Bitset{}) == 0);
{
Bitset b{};
b = b | Bitset::A;
BOOST_TEST(static_cast<unsigned>(b) == 0b001u);
b |= Bitset::B;
BOOST_TEST(static_cast<unsigned>(b) == 0b011u);
(b |= Bitset::A) = Bitset::C;
BOOST_CHECK(b == Bitset::C);
}
{
Bitset b = Bitset::A | Bitset::B | Bitset::C;
b = b & (Bitset::A | Bitset::B);
BOOST_TEST(static_cast<unsigned>(b) == 0b011u);
b &= Bitset::B;
BOOST_TEST(static_cast<unsigned>(b) == 0b010u);
(b &= Bitset::A) = Bitset::C;
BOOST_CHECK(b == Bitset::C);
}
}
BOOST_AUTO_TEST_CASE(UtilityFunctions)
{
Bitset b = Bitset::A | Bitset::C;
BOOST_TEST(bitset::isSet(b, Bitset::A));
BOOST_TEST(bitset::isSet(b, Bitset::A | Bitset::C));
BOOST_TEST(!bitset::isSet(b, Bitset::B));
BOOST_TEST(!bitset::isSet(b, Bitset::B | Bitset::C));
b = bitset::set(b, Bitset::B /*, true */);
BOOST_TEST(static_cast<unsigned>(b) == 0b111u);
b = bitset::set(b, Bitset::B, false);
BOOST_TEST(static_cast<unsigned>(b) == 0b101u);
b = bitset::clear(b, Bitset::A);
BOOST_TEST(static_cast<unsigned>(b) == 0b100u);
b = bitset::toggle(b, Bitset::A);
BOOST_TEST(static_cast<unsigned>(b) == 0b101u);
b = bitset::toggle(b, Bitset::A | Bitset::B);
BOOST_TEST(static_cast<unsigned>(b) == 0b110u);
}
BOOST_AUTO_TEST_SUITE_END()