comparison C++/Tests/Flags/main.cpp @ 261:0c7bc55e0d36

Add Flags, a small wrapper for creating enum flags
author David Demelier <markand@malikania.fr>
date Thu, 09 Oct 2014 13:28:24 +0200
parents
children
comparison
equal deleted inserted replaced
260:2096edb63a4f 261:0c7bc55e0d36
1 /*
2 * main.cpp -- main test file for Flags
3 *
4 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <cstdint>
20
21 #include <gtest/gtest.h>
22
23 #include <Flags.h>
24
25 enum Standard : uint8_t {
26 Fullscreen = (1 << 0),
27 Audio = (1 << 1)
28 };
29
30 enum class Strong : uint8_t {
31 NoLog = (1 << 0),
32 NoCheck = (1 << 1)
33 };
34
35 /* --------------------------------------------------------
36 * Global operators on standard enum
37 * -------------------------------------------------------- */
38
39 TEST(OperatorsStandard, opand)
40 {
41 Standard s(Fullscreen | Audio);
42
43 ASSERT_EQ(Fullscreen, (s & Fullscreen));
44 ASSERT_EQ(Audio, (s & Audio));
45 }
46
47 TEST(OperatorsStandard, opor)
48 {
49 Standard s(Fullscreen);
50
51 ASSERT_EQ(3, (s | Audio));
52 }
53
54 TEST(OperatorsStandard, opxor)
55 {
56 Standard s(Fullscreen);
57
58 ASSERT_EQ(3, (s ^ Audio));
59 }
60
61 TEST(OperatorsStandard, opnot)
62 {
63 Standard s(Fullscreen);
64
65 ASSERT_EQ(254, ~s);
66 }
67
68 /* --------------------------------------------------------
69 * Global operators on strong enum
70 * -------------------------------------------------------- */
71
72 TEST(OperatorsStrong, opand)
73 {
74 Strong s(Strong::NoLog | Strong::NoCheck);
75
76 ASSERT_EQ(Strong::NoLog, (s & Strong::NoLog));
77 ASSERT_EQ(Strong::NoCheck, (s & Strong::NoCheck));
78 }
79
80 TEST(OperatorsStrong, opor)
81 {
82 Strong s(Strong::NoLog);
83
84 ASSERT_EQ(3, static_cast<int>((s | Strong::NoCheck)));
85 }
86
87 TEST(OperatorsStrong, opxor)
88 {
89 Strong s(Strong::NoLog);
90
91 ASSERT_EQ(3, static_cast<int>((s ^ Strong::NoCheck)));
92 }
93
94 TEST(OperatorsStrong, opnot)
95 {
96 Strong s(Strong::NoLog);
97
98 ASSERT_EQ(254, static_cast<int>(~s));
99 }
100
101 /* --------------------------------------------------------
102 * Flags with standard enums
103 * -------------------------------------------------------- */
104
105 TEST(Standard, construct)
106 {
107 Flags<Standard> s;
108
109 ASSERT_FALSE(s);
110 ASSERT_TRUE(!s);
111
112 Flags <Standard> s2(Fullscreen | Audio);
113
114 ASSERT_TRUE(s2);
115 ASSERT_FALSE(!s2);
116 ASSERT_TRUE(s2 & Fullscreen);
117 ASSERT_TRUE(s2 & Audio);
118 ASSERT_TRUE((s2 & Fullscreen) == Fullscreen);
119 ASSERT_TRUE((s2 & Audio) == Audio);
120 ASSERT_TRUE(s2 == (Audio | Fullscreen));
121 }
122
123 TEST(Standard, addByOne)
124 {
125 Flags<Standard> s;
126
127 ASSERT_FALSE(s);
128 ASSERT_TRUE(!s);
129 ASSERT_TRUE(!(s & Fullscreen));
130 ASSERT_TRUE(!(s & Audio));
131
132 s |= Fullscreen;
133 ASSERT_TRUE(s);
134 ASSERT_FALSE(!s);
135 ASSERT_TRUE(s & Fullscreen);
136 ASSERT_TRUE((s & Fullscreen) == Fullscreen);
137 ASSERT_FALSE(s & Audio);
138 ASSERT_FALSE((s & Audio) == Audio);
139 ASSERT_TRUE(s == Fullscreen);
140
141 s |= Audio;
142 ASSERT_TRUE(s);
143 ASSERT_FALSE(!s);
144 ASSERT_TRUE(s & Fullscreen);
145 ASSERT_TRUE((s & Fullscreen) == Fullscreen);
146 ASSERT_TRUE(s & Audio);
147 ASSERT_TRUE((s & Audio) == Audio);
148 ASSERT_TRUE(s == (Fullscreen | Audio));
149 }
150
151 TEST(Standard, add)
152 {
153 Flags<Standard> s;
154
155 s |= Fullscreen | Audio;
156 ASSERT_TRUE(s & (Fullscreen | Audio));
157 ASSERT_TRUE((s & (Fullscreen | Audio)) == (Fullscreen | Audio));
158 }
159
160 TEST(Standard, removeByOne)
161 {
162 Flags<Standard> s(Fullscreen | Audio);
163
164 s &= ~(Fullscreen);
165 ASSERT_TRUE(s);
166 ASSERT_FALSE(!s);
167 ASSERT_FALSE(s & Fullscreen);
168 ASSERT_FALSE((s & Fullscreen) == Fullscreen);
169 ASSERT_TRUE(s & Audio);
170 ASSERT_TRUE((s & Audio) == Audio);
171
172 s &= ~(Audio);
173 ASSERT_FALSE(s);
174 ASSERT_TRUE(!s);
175 }
176
177 TEST(Standard, remove)
178 {
179 Flags<Standard> s(Fullscreen | Audio);
180
181 s &= ~(Fullscreen | Audio);
182 ASSERT_FALSE(s);
183 ASSERT_TRUE(!s);
184 }
185
186 TEST(Standard, xorAdd)
187 {
188 Flags<Standard> s(Fullscreen | Audio);
189
190 s ^= Audio;
191 ASSERT_TRUE(s & Fullscreen);
192 ASSERT_TRUE((s & Fullscreen) == Fullscreen);
193 ASSERT_FALSE(s & Audio);
194 ASSERT_FALSE((s & Audio) == Audio);
195 }
196
197 /* --------------------------------------------------------
198 * Flags with strong enums
199 * -------------------------------------------------------- */
200
201 TEST(Strong, construct)
202 {
203 Flags<Strong> s;
204
205 ASSERT_FALSE(s);
206 ASSERT_TRUE(!s);
207
208 Flags <Strong> s2(Strong::NoLog | Strong::NoCheck);
209
210 ASSERT_TRUE(s2);
211 ASSERT_FALSE(!s2);
212 ASSERT_TRUE(s2 & Strong::NoLog);
213 ASSERT_TRUE(s2 & Strong::NoCheck);
214 ASSERT_TRUE((s2 & Strong::NoLog) == Strong::NoLog);
215 ASSERT_TRUE((s2 & Strong::NoCheck) == Strong::NoCheck);
216 ASSERT_TRUE(s2 == (Strong::NoLog | Strong::NoCheck));
217 }
218
219 TEST(Strong, addByOne)
220 {
221 Flags<Strong> s;
222
223 ASSERT_FALSE(s);
224 ASSERT_TRUE(!s);
225 ASSERT_TRUE(!(s & Strong::NoLog));
226 ASSERT_TRUE(!(s & Strong::NoCheck));
227
228 s |= Strong::NoLog;
229 ASSERT_TRUE(s);
230 ASSERT_FALSE(!s);
231 ASSERT_TRUE(s & Strong::NoLog);
232 ASSERT_TRUE((s & Strong::NoLog) == Strong::NoLog);
233 ASSERT_FALSE(s & Strong::NoCheck);
234 ASSERT_FALSE((s & Strong::NoCheck) == Strong::NoCheck);
235 ASSERT_TRUE(s == Strong::NoLog);
236
237 s |= Strong::NoCheck;
238 ASSERT_TRUE(s);
239 ASSERT_FALSE(!s);
240 ASSERT_TRUE(s & Strong::NoLog);
241 ASSERT_TRUE((s & Strong::NoLog) == Strong::NoLog);
242 ASSERT_TRUE(s & Strong::NoCheck);
243 ASSERT_TRUE((s & Strong::NoCheck) == Strong::NoCheck);
244 ASSERT_TRUE(s == (Strong::NoLog | Strong::NoCheck));
245 }
246
247 TEST(Strong, add)
248 {
249 Flags<Strong> s;
250
251 s |= Strong::NoLog | Strong::NoCheck;
252 ASSERT_TRUE(s & (Strong::NoLog | Strong::NoCheck));
253 ASSERT_TRUE((s & (Strong::NoLog | Strong::NoCheck)) == (Strong::NoLog | Strong::NoCheck));
254 }
255
256 TEST(Strong, removeByOne)
257 {
258 Flags<Strong> s(Strong::NoLog | Strong::NoCheck);
259
260 s &= ~(Strong::NoLog);
261 ASSERT_TRUE(s);
262 ASSERT_FALSE(!s);
263 ASSERT_FALSE(s & Strong::NoLog);
264 ASSERT_FALSE((s & Strong::NoLog) == Strong::NoLog);
265 ASSERT_TRUE(s & Strong::NoCheck);
266 ASSERT_TRUE((s & Strong::NoCheck) == Strong::NoCheck);
267
268 s &= ~(Strong::NoCheck);
269 ASSERT_FALSE(s);
270 ASSERT_TRUE(!s);
271 }
272
273 TEST(Strong, remove)
274 {
275 Flags<Strong> s(Strong::NoLog | Strong::NoCheck);
276
277 s &= ~(Strong::NoLog | Strong::NoCheck);
278 ASSERT_FALSE(s);
279 ASSERT_TRUE(!s);
280 }
281
282 TEST(Strong, xorAdd)
283 {
284 Flags<Strong> s(Strong::NoLog | Strong::NoCheck);
285
286 s ^= Strong::NoCheck;
287 ASSERT_TRUE(s & Strong::NoLog);
288 ASSERT_TRUE((s & Strong::NoLog) == Strong::NoLog);
289 ASSERT_FALSE(s & Strong::NoCheck);
290 ASSERT_FALSE((s & Strong::NoCheck) == Strong::NoCheck);
291 }
292
293 int main(int argc, char **argv)
294 {
295 testing::InitGoogleTest(&argc, argv);
296
297 return RUN_ALL_TESTS();
298 }