1 | //===-- FlagsTest.cpp -----------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "gtest/gtest.h" |
10 | |
11 | #include "lldb/Utility/Flags.h" |
12 | |
13 | using namespace lldb_private; |
14 | |
15 | enum DummyFlags { |
16 | eFlag0 = 1 << 0, |
17 | eFlag1 = 1 << 1, |
18 | eFlag2 = 1 << 2, |
19 | eAllFlags = (eFlag0 | eFlag1 | eFlag2) |
20 | }; |
21 | |
22 | TEST(Flags, GetBitSize) { |
23 | Flags f; |
24 | // Methods like ClearCount depend on this specific value, so we test |
25 | // against it here. |
26 | EXPECT_EQ(32U, f.GetBitSize()); |
27 | } |
28 | |
29 | TEST(Flags, Reset) { |
30 | Flags f; |
31 | f.Reset(flags: 0x3); |
32 | EXPECT_EQ(0x3U, f.Get()); |
33 | } |
34 | |
35 | TEST(Flags, Clear) { |
36 | Flags f; |
37 | f.Reset(flags: 0x3); |
38 | EXPECT_EQ(0x3U, f.Get()); |
39 | |
40 | f.Clear(mask: 0x5); |
41 | EXPECT_EQ(0x2U, f.Get()); |
42 | |
43 | f.Clear(); |
44 | EXPECT_EQ(0x0U, f.Get()); |
45 | } |
46 | |
47 | TEST(Flags, AllSet) { |
48 | Flags f; |
49 | |
50 | EXPECT_FALSE(f.AllSet(eFlag0 | eFlag1)); |
51 | |
52 | f.Set(eFlag0); |
53 | EXPECT_FALSE(f.AllSet(eFlag0 | eFlag1)); |
54 | |
55 | f.Set(eFlag1); |
56 | EXPECT_TRUE(f.AllSet(eFlag0 | eFlag1)); |
57 | |
58 | f.Clear(mask: eFlag1); |
59 | EXPECT_FALSE(f.AllSet(eFlag0 | eFlag1)); |
60 | |
61 | f.Clear(mask: eFlag0); |
62 | EXPECT_FALSE(f.AllSet(eFlag0 | eFlag1)); |
63 | } |
64 | |
65 | TEST(Flags, AnySet) { |
66 | Flags f; |
67 | |
68 | EXPECT_FALSE(f.AnySet(eFlag0 | eFlag1)); |
69 | |
70 | f.Set(eFlag0); |
71 | EXPECT_TRUE(f.AnySet(eFlag0 | eFlag1)); |
72 | |
73 | f.Set(eFlag1); |
74 | EXPECT_TRUE(f.AnySet(eFlag0 | eFlag1)); |
75 | |
76 | f.Clear(mask: eFlag1); |
77 | EXPECT_TRUE(f.AnySet(eFlag0 | eFlag1)); |
78 | |
79 | f.Clear(mask: eFlag0); |
80 | EXPECT_FALSE(f.AnySet(eFlag0 | eFlag1)); |
81 | } |
82 | |
83 | TEST(Flags, Test) { |
84 | Flags f; |
85 | |
86 | EXPECT_FALSE(f.Test(eFlag0)); |
87 | EXPECT_FALSE(f.Test(eFlag1)); |
88 | EXPECT_FALSE(f.Test(eFlag2)); |
89 | |
90 | f.Set(eFlag0); |
91 | EXPECT_TRUE(f.Test(eFlag0)); |
92 | EXPECT_FALSE(f.Test(eFlag1)); |
93 | EXPECT_FALSE(f.Test(eFlag2)); |
94 | |
95 | f.Set(eFlag1); |
96 | EXPECT_TRUE(f.Test(eFlag0)); |
97 | EXPECT_TRUE(f.Test(eFlag1)); |
98 | EXPECT_FALSE(f.Test(eFlag2)); |
99 | |
100 | f.Clear(mask: eFlag0); |
101 | EXPECT_FALSE(f.Test(eFlag0)); |
102 | EXPECT_TRUE(f.Test(eFlag1)); |
103 | EXPECT_FALSE(f.Test(eFlag2)); |
104 | |
105 | // FIXME: Should Flags assert on Test(eFlag0 | eFlag1) (more than one bit)? |
106 | } |
107 | |
108 | TEST(Flags, AllClear) { |
109 | Flags f; |
110 | |
111 | EXPECT_TRUE(f.AllClear(eFlag0 | eFlag1)); |
112 | |
113 | f.Set(eFlag0); |
114 | EXPECT_FALSE(f.AllClear(eFlag0 | eFlag1)); |
115 | |
116 | f.Set(eFlag1); |
117 | f.Clear(mask: eFlag0); |
118 | EXPECT_FALSE(f.AllClear(eFlag0 | eFlag1)); |
119 | |
120 | f.Clear(mask: eFlag1); |
121 | EXPECT_TRUE(f.AnyClear(eFlag0 | eFlag1)); |
122 | } |
123 | |
124 | TEST(Flags, AnyClear) { |
125 | Flags f; |
126 | EXPECT_TRUE(f.AnyClear(eFlag0 | eFlag1)); |
127 | |
128 | f.Set(eFlag0); |
129 | EXPECT_TRUE(f.AnyClear(eFlag0 | eFlag1)); |
130 | |
131 | f.Set(eFlag1); |
132 | f.Set(eFlag0); |
133 | EXPECT_FALSE(f.AnyClear(eFlag0 | eFlag1)); |
134 | |
135 | f.Clear(mask: eFlag1); |
136 | EXPECT_TRUE(f.AnyClear(eFlag0 | eFlag1)); |
137 | |
138 | f.Clear(mask: eFlag0); |
139 | EXPECT_TRUE(f.AnyClear(eFlag0 | eFlag1)); |
140 | } |
141 | |
142 | TEST(Flags, IsClear) { |
143 | Flags f; |
144 | |
145 | EXPECT_TRUE(f.IsClear(eFlag0)); |
146 | EXPECT_TRUE(f.IsClear(eFlag1)); |
147 | |
148 | f.Set(eFlag0); |
149 | EXPECT_FALSE(f.IsClear(eFlag0)); |
150 | EXPECT_TRUE(f.IsClear(eFlag1)); |
151 | |
152 | f.Set(eFlag1); |
153 | EXPECT_FALSE(f.IsClear(eFlag0)); |
154 | EXPECT_FALSE(f.IsClear(eFlag1)); |
155 | |
156 | f.Clear(mask: eFlag0); |
157 | EXPECT_TRUE(f.IsClear(eFlag0)); |
158 | EXPECT_FALSE(f.IsClear(eFlag1)); |
159 | |
160 | f.Clear(mask: eFlag1); |
161 | EXPECT_TRUE(f.IsClear(eFlag0)); |
162 | EXPECT_TRUE(f.IsClear(eFlag1)); |
163 | } |
164 | |