1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <ftl/flags.h>
18 #include <gtest/gtest.h>
19 
20 #include <type_traits>
21 
22 namespace android::test {
23 
24 using ftl::Flags;
25 using namespace ftl::flag_operators;
26 
27 enum class TestFlags : uint8_t { ONE = 0x1, TWO = 0x2, THREE = 0x4 };
28 
TEST(Flags,Test)29 TEST(Flags, Test) {
30     Flags<TestFlags> flags = TestFlags::ONE;
31     ASSERT_TRUE(flags.test(TestFlags::ONE));
32     ASSERT_FALSE(flags.test(TestFlags::TWO));
33     ASSERT_FALSE(flags.test(TestFlags::THREE));
34 }
35 
TEST(Flags,Any)36 TEST(Flags, Any) {
37     Flags<TestFlags> flags = TestFlags::ONE | TestFlags::TWO;
38     ASSERT_TRUE(flags.any());
39     ASSERT_TRUE(flags.any(TestFlags::ONE));
40     ASSERT_TRUE(flags.any(TestFlags::TWO));
41     ASSERT_FALSE(flags.any(TestFlags::THREE));
42     ASSERT_TRUE(flags.any(TestFlags::ONE | TestFlags::TWO));
43     ASSERT_TRUE(flags.any(TestFlags::TWO | TestFlags::THREE));
44     ASSERT_TRUE(flags.any(TestFlags::ONE | TestFlags::THREE));
45     ASSERT_TRUE(flags.any(TestFlags::ONE | TestFlags::TWO | TestFlags::THREE));
46 
47     Flags<TestFlags> emptyFlags;
48     ASSERT_FALSE(emptyFlags.any());
49 }
50 
TEST(Flags,All)51 TEST(Flags, All) {
52     Flags<TestFlags> flags = TestFlags::ONE | TestFlags::TWO;
53     ASSERT_TRUE(flags.all(TestFlags::ONE));
54     ASSERT_TRUE(flags.all(TestFlags::TWO));
55     ASSERT_FALSE(flags.all(TestFlags::THREE));
56     ASSERT_TRUE(flags.all(TestFlags::ONE | TestFlags::TWO));
57     ASSERT_FALSE(flags.all(TestFlags::TWO | TestFlags::THREE));
58     ASSERT_FALSE(flags.all(TestFlags::ONE | TestFlags::THREE));
59     ASSERT_FALSE(flags.all(TestFlags::ONE | TestFlags::TWO | TestFlags::THREE));
60 }
61 
TEST(Flags,DefaultConstructor_hasNoFlagsSet)62 TEST(Flags, DefaultConstructor_hasNoFlagsSet) {
63     Flags<TestFlags> flags;
64     ASSERT_FALSE(flags.any(TestFlags::ONE | TestFlags::TWO | TestFlags::THREE));
65 }
66 
TEST(Flags,NotOperator_onEmptyFlagsSetsAllFlags)67 TEST(Flags, NotOperator_onEmptyFlagsSetsAllFlags) {
68     Flags<TestFlags> flags;
69     flags = ~flags;
70     ASSERT_TRUE(flags.all(TestFlags::ONE | TestFlags::TWO | TestFlags::THREE));
71 }
72 
TEST(Flags,NotOperator_onNonEmptyFlagsInvertsFlags)73 TEST(Flags, NotOperator_onNonEmptyFlagsInvertsFlags) {
74     Flags<TestFlags> flags = TestFlags::TWO;
75     flags = ~flags;
76     ASSERT_TRUE(flags.all(TestFlags::ONE | TestFlags::THREE));
77     ASSERT_FALSE(flags.test(TestFlags::TWO));
78 }
79 
TEST(Flags,OrOperator_withNewFlag)80 TEST(Flags, OrOperator_withNewFlag) {
81     Flags<TestFlags> flags = TestFlags::ONE;
82     Flags<TestFlags> flags2 = flags | TestFlags::TWO;
83     ASSERT_FALSE(flags2.test(TestFlags::THREE));
84     ASSERT_TRUE(flags2.all(TestFlags::ONE | TestFlags::TWO));
85 }
86 
TEST(Flags,OrOperator_withExistingFlag)87 TEST(Flags, OrOperator_withExistingFlag) {
88     Flags<TestFlags> flags = TestFlags::ONE | TestFlags::THREE;
89     Flags<TestFlags> flags2 = flags | TestFlags::THREE;
90     ASSERT_FALSE(flags2.test(TestFlags::TWO));
91     ASSERT_TRUE(flags2.all(TestFlags::ONE | TestFlags::THREE));
92 }
93 
TEST(Flags,OrEqualsOperator_withNewFlag)94 TEST(Flags, OrEqualsOperator_withNewFlag) {
95     Flags<TestFlags> flags;
96     flags |= TestFlags::THREE;
97     ASSERT_TRUE(flags.test(TestFlags::THREE));
98     ASSERT_FALSE(flags.any(TestFlags::ONE | TestFlags::TWO));
99 }
100 
TEST(Flags,OrEqualsOperator_withExistingFlag)101 TEST(Flags, OrEqualsOperator_withExistingFlag) {
102     Flags<TestFlags> flags = TestFlags::ONE | TestFlags::THREE;
103     flags |= TestFlags::THREE;
104     ASSERT_TRUE(flags.all(TestFlags::ONE | TestFlags::THREE));
105     ASSERT_FALSE(flags.test(TestFlags::TWO));
106 }
107 
TEST(Flags,AndOperator_withOneSetFlag)108 TEST(Flags, AndOperator_withOneSetFlag) {
109     Flags<TestFlags> flags = TestFlags::ONE | TestFlags::THREE;
110     Flags<TestFlags> andFlags = flags & TestFlags::THREE;
111     ASSERT_TRUE(andFlags.test(TestFlags::THREE));
112     ASSERT_FALSE(andFlags.any(TestFlags::ONE | TestFlags::TWO));
113 }
114 
TEST(Flags,AndOperator_withMultipleSetFlags)115 TEST(Flags, AndOperator_withMultipleSetFlags) {
116     Flags<TestFlags> flags = TestFlags::ONE | TestFlags::THREE;
117     Flags<TestFlags> andFlags = flags & (TestFlags::ONE | TestFlags::THREE);
118     ASSERT_TRUE(andFlags.all(TestFlags::ONE | TestFlags::THREE));
119     ASSERT_FALSE(andFlags.test(TestFlags::TWO));
120 }
121 
TEST(Flags,AndOperator_withNoSetFlags)122 TEST(Flags, AndOperator_withNoSetFlags) {
123     Flags<TestFlags> flags = TestFlags::ONE | TestFlags::THREE;
124     Flags<TestFlags> andFlags = flags & TestFlags::TWO;
125     ASSERT_FALSE(andFlags.any(TestFlags::ONE | TestFlags::TWO | TestFlags::THREE));
126 }
127 
TEST(Flags,Equality)128 TEST(Flags, Equality) {
129     Flags<TestFlags> flags1 = TestFlags::ONE | TestFlags::TWO;
130     Flags<TestFlags> flags2 = TestFlags::ONE | TestFlags::TWO;
131     ASSERT_EQ(flags1, flags2);
132 }
133 
TEST(Flags,Inequality)134 TEST(Flags, Inequality) {
135     Flags<TestFlags> flags1 = TestFlags::ONE | TestFlags::TWO;
136     Flags<TestFlags> flags2 = TestFlags::ONE | TestFlags::THREE;
137     ASSERT_NE(flags1, flags2);
138 }
139 
TEST(Flags,EqualsOperator)140 TEST(Flags, EqualsOperator) {
141     Flags<TestFlags> flags;
142     flags = TestFlags::ONE;
143     ASSERT_TRUE(flags.test(TestFlags::ONE));
144     ASSERT_FALSE(flags.any(TestFlags::TWO | TestFlags::THREE));
145 }
146 
TEST(Flags,EqualsOperator_DontShareState)147 TEST(Flags, EqualsOperator_DontShareState) {
148     Flags<TestFlags> flags1 = TestFlags::ONE | TestFlags::TWO;
149     Flags<TestFlags> flags2 = flags1;
150     ASSERT_EQ(flags1, flags2);
151 
152     flags1 &= TestFlags::TWO;
153     ASSERT_NE(flags1, flags2);
154 }
155 
TEST(Flags,GetValue)156 TEST(Flags, GetValue) {
157     Flags<TestFlags> flags = TestFlags::ONE | TestFlags::TWO;
158     ASSERT_EQ(flags.get(), 0x3);
159 }
160 
TEST(Flags,String_NoFlags)161 TEST(Flags, String_NoFlags) {
162     Flags<TestFlags> flags;
163     ASSERT_EQ(flags.string(), "0x0");
164 }
165 
TEST(Flags,String_KnownValues)166 TEST(Flags, String_KnownValues) {
167     Flags<TestFlags> flags = TestFlags::ONE | TestFlags::TWO;
168     ASSERT_EQ(flags.string(), "ONE | TWO");
169 }
170 
TEST(Flags,String_UnknownValues)171 TEST(Flags, String_UnknownValues) {
172     auto flags = Flags<TestFlags>(0b1011);
173     ASSERT_EQ(flags.string(), "ONE | TWO | 0b1000");
174 }
175 
TEST(FlagsIterator,IteratesOverAllFlags)176 TEST(FlagsIterator, IteratesOverAllFlags) {
177     Flags<TestFlags> flags1 = TestFlags::ONE | TestFlags::TWO;
178     Flags<TestFlags> flags2;
179     for (TestFlags f : flags1) {
180         flags2 |= f;
181     }
182     ASSERT_EQ(flags2, flags1);
183 }
184 
TEST(FlagsIterator,IteratesInExpectedOrder)185 TEST(FlagsIterator, IteratesInExpectedOrder) {
186     const std::vector<TestFlags> flagOrder = {TestFlags::ONE, TestFlags::TWO};
187     Flags<TestFlags> flags;
188     for (TestFlags f : flagOrder) {
189         flags |= f;
190     }
191 
192     size_t idx = 0;
193     auto iter = flags.begin();
194     while (iter != flags.end() && idx < flagOrder.size()) {
195         // Make sure the order is what we expect
196         ASSERT_EQ(*iter, flagOrder[idx]);
197         iter++;
198         idx++;
199     }
200     ASSERT_EQ(iter, flags.end());
201 }
TEST(FlagsIterator,PostFixIncrement)202 TEST(FlagsIterator, PostFixIncrement) {
203     Flags<TestFlags> flags = TestFlags::ONE | TestFlags::TWO;
204     auto iter = flags.begin();
205     ASSERT_EQ(*(iter++), TestFlags::ONE);
206     ASSERT_EQ(*iter, TestFlags::TWO);
207     ASSERT_EQ(*(iter++), TestFlags::TWO);
208     ASSERT_EQ(iter, flags.end());
209 }
210 
TEST(FlagsIterator,PreFixIncrement)211 TEST(FlagsIterator, PreFixIncrement) {
212     Flags<TestFlags> flags = TestFlags::ONE | TestFlags::TWO;
213     auto iter = flags.begin();
214     ASSERT_EQ(*++iter, TestFlags::TWO);
215     ASSERT_EQ(++iter, flags.end());
216 }
217 
218 } // namespace android::test
219