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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
#include <library/cpp/testing/unittest/registar.h>
#include "flags.h"
enum ETestFlag1: ui16 {
Test1 = 1,
Test2 = 2,
Test4 = 4,
Test8 = 8
};
Y_DECLARE_FLAGS(ETest1, ETestFlag1);
Y_DECLARE_OPERATORS_FOR_FLAGS(ETest1);
static_assert(TTypeTraits<ETest1>::IsPod, "flags should be POD type");
enum class ETestFlag2 {
Test1 = 1,
Test2 = 2,
Test4 = 4,
Test8 = 8
};
Y_DECLARE_FLAGS(ETest2, ETestFlag2);
Y_DECLARE_OPERATORS_FOR_FLAGS(ETest2);
namespace {
// won't compile without Y_DECLARE_UNUSED
enum class ETestFlag3 { One = 1,
Two = 2,
Three = 3 };
Y_DECLARE_FLAGS(ETestFlags3, ETestFlag3);
Y_DECLARE_OPERATORS_FOR_FLAGS(ETestFlags3);
}
Y_UNIT_TEST_SUITE(TFlagsTest) {
template <class Enum>
void TestEnum() {
{
auto i = Enum::Test1 | Enum::Test2;
UNIT_ASSERT((std::is_same<decltype(i), TFlags<Enum>>::value));
UNIT_ASSERT((std::is_same<decltype(~i), TFlags<Enum>>::value));
UNIT_ASSERT(!(std::is_same<decltype(i), int>::value));
UNIT_ASSERT_VALUES_EQUAL(sizeof(Enum), sizeof(TFlags<Enum>));
UNIT_ASSERT(i.HasFlag(Enum::Test1));
UNIT_ASSERT(i.HasFlag(Enum::Test4) == false);
UNIT_ASSERT(i.HasFlags(Enum::Test1));
UNIT_ASSERT(i.HasFlags(Enum::Test4) == false);
UNIT_ASSERT(i.HasFlags(Enum::Test1 | Enum::Test4) == false);
UNIT_ASSERT(i.HasAnyOfFlags(Enum::Test1));
UNIT_ASSERT(i.HasAnyOfFlags(Enum::Test4) == false);
UNIT_ASSERT(i.HasAnyOfFlags(Enum::Test1 | Enum::Test4));
i |= Enum::Test4;
i ^= Enum::Test2;
UNIT_ASSERT_EQUAL(i, Enum::Test4 | Enum::Test1);
UNIT_ASSERT_EQUAL(i & Enum::Test1, i & ~Enum::Test4);
UNIT_ASSERT(i & Enum::Test4);
UNIT_ASSERT_UNEQUAL(i, ~i);
UNIT_ASSERT_EQUAL(i, ~~i);
}
{
auto i = Enum::Test1 | Enum::Test2;
i.RemoveFlag(Enum::Test1);
UNIT_ASSERT_EQUAL(i, TFlags<Enum>(Enum::Test2));
}
{
auto i = Enum::Test1 | Enum::Test2;
i.RemoveFlags(Enum::Test1);
UNIT_ASSERT_EQUAL(i, TFlags<Enum>(Enum::Test2));
}
{
auto i = Enum::Test1 | Enum::Test2;
i.RemoveFlags(Enum::Test1 | Enum::Test2);
UNIT_ASSERT_EQUAL(i, TFlags<Enum>());
}
}
Y_UNIT_TEST(TestFlags) {
TestEnum<ETestFlag1>();
TestEnum<ETestFlag2>();
}
Y_UNIT_TEST(TestZero) {
/* This code should simply compile. */
ETest1 f = 0;
f = 0;
f = ETest1(0);
ETest1 ff(0);
ff = 0;
}
Y_UNIT_TEST(TestOutput) {
ETest1 value0 = nullptr, value1 = Test1, value7 = Test1 | Test2 | Test4;
UNIT_ASSERT_VALUES_EQUAL(ToString(value0), "TFlags(0000000000000000)");
UNIT_ASSERT_VALUES_EQUAL(ToString(value1), "TFlags(0000000000000001)");
UNIT_ASSERT_VALUES_EQUAL(ToString(value7), "TFlags(0000000000000111)");
}
Y_UNIT_TEST(TestHash) {
ETest1 value0 = nullptr, value1 = Test1;
THashMap<ETest1, int> hash;
hash[value0] = 0;
hash[value1] = 1;
UNIT_ASSERT_VALUES_EQUAL(hash[value0], 0);
UNIT_ASSERT_VALUES_EQUAL(hash[value1], 1);
}
Y_UNIT_TEST(TestBaseType) {
ui16 goodValue = 7;
auto goodFlags = ETest1::FromBaseType(goodValue);
UNIT_ASSERT(goodFlags& ETestFlag1::Test1);
UNIT_ASSERT(goodFlags& ETestFlag1::Test2);
UNIT_ASSERT(goodFlags& ETestFlag1::Test4);
UNIT_ASSERT_VALUES_EQUAL(goodValue, goodFlags.ToBaseType());
// Passed value is not checked, but preserved as is
ui16 badValue = 1024;
auto badFlags = ETest1::FromBaseType(badValue);
UNIT_ASSERT_VALUES_EQUAL(badValue, badFlags.ToBaseType());
}
}
|