1 /*
2  * Copyright 2021 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/enum.h>
18 #include <gtest/gtest.h>
19 
20 namespace android::test {
21 
22 // Keep in sync with example usage in header file.
23 namespace {
24 
25 enum class E { A, B, C, F = 5, ftl_last = F };
26 
27 static_assert(ftl::enum_begin_v<E> == E::A);
28 static_assert(ftl::enum_last_v<E> == E::F);
29 static_assert(ftl::enum_size_v<E> == 6);
30 
31 static_assert(ftl::enum_name<E::B>() == "B");
32 static_assert(ftl::enum_name<E::ftl_last>() == "F");
33 static_assert(ftl::enum_name(E::C).value_or("?") == "C");
34 static_assert(ftl::enum_name(E{3}).value_or("?") == "?");
35 
36 static_assert(ftl::enum_name_full<E::B>() == "E::B");
37 static_assert(ftl::enum_name_full<E::ftl_last>() == "E::F");
38 static_assert(ftl::enum_name_full(E::C).value_or("?") == "E::C");
39 static_assert(ftl::enum_name_full(E{3}).value_or("?") == "?");
40 
41 enum class F : std::uint16_t { X = 0b1, Y = 0b10, Z = 0b100 };
42 
43 static_assert(ftl::enum_begin_v<F> == F{0});
44 static_assert(ftl::enum_last_v<F> == F{15});
45 static_assert(ftl::enum_size_v<F> == 16);
46 
47 static_assert(ftl::flag_name(F::Z).value_or("?") == "Z");
48 static_assert(ftl::flag_name(F{0b111}).value_or("?") == "?");
49 
50 // If a scoped enum is unsigned, its implicit range corresponds to its bit indices.
51 enum class Flags : std::uint8_t {
52   kNone = 0,
53   kFlag1 = 0b0000'0010,
54   kFlag4 = 0b0001'0000,
55   kFlag7 = 0b1000'0000,
56   kMask = kFlag1 | kFlag4 | kFlag7,
57   kAll = 0b1111'1111
58 };
59 
60 static_assert(ftl::enum_begin_v<Flags> == Flags{0});
61 static_assert(ftl::enum_last_v<Flags> == Flags{7});
62 static_assert(ftl::enum_size_v<Flags> == 8);
63 
64 static_assert(ftl::enum_name<Flags::kNone>() == "kNone");
65 static_assert(ftl::enum_name<Flags::kFlag4>() == "kFlag4");
66 static_assert(ftl::enum_name<Flags::kFlag7>() == "kFlag7");
67 
68 static_assert(ftl::enum_name_full<Flags::kNone>() == "Flags::kNone");
69 static_assert(ftl::enum_name_full<Flags::kFlag4>() == "Flags::kFlag4");
70 static_assert(ftl::enum_name_full<Flags::kFlag7>() == "Flags::kFlag7");
71 
72 // Though not flags, the enumerators are within the implicit range of bit indices.
73 enum class Planet : std::uint8_t {
74   kMercury,
75   kVenus,
76   kEarth,
77   kMars,
78   kJupiter,
79   kSaturn,
80   kUranus,
81   kNeptune
82 };
83 
84 constexpr Planet kPluto{ftl::to_underlying(Planet::kNeptune) + 1};  // Honorable mention.
85 
86 static_assert(ftl::enum_begin_v<Planet> == Planet::kMercury);
87 static_assert(ftl::enum_last_v<Planet> == Planet::kNeptune);
88 static_assert(ftl::enum_size_v<Planet> == 8);
89 
90 static_assert(ftl::enum_name<Planet::kMercury>() == "kMercury");
91 static_assert(ftl::enum_name<Planet::kSaturn>() == "kSaturn");
92 
93 static_assert(ftl::enum_name_full<Planet::kMercury>() == "Planet::kMercury");
94 static_assert(ftl::enum_name_full<Planet::kSaturn>() == "Planet::kSaturn");
95 
96 // Unscoped enum must define explicit range, even if the underlying type is fixed.
97 enum Temperature : int {
98   kRoom = 20,
99   kFridge = 4,
100   kFreezer = -18,
101 
102   ftl_first = kFreezer,
103   ftl_last = kRoom
104 };
105 
106 static_assert(ftl::enum_begin_v<Temperature> == kFreezer);
107 static_assert(ftl::enum_last_v<Temperature> == kRoom);
108 static_assert(ftl::enum_size_v<Temperature> == 39);
109 
110 static_assert(ftl::enum_name<kFreezer>() == "kFreezer");
111 static_assert(ftl::enum_name<kFridge>() == "kFridge");
112 static_assert(ftl::enum_name<kRoom>() == "kRoom");
113 
114 }  // namespace
115 
TEST(Enum,Range)116 TEST(Enum, Range) {
117   std::string string;
118   for (E v : ftl::enum_range<E>()) {
119     string += ftl::enum_name(v).value_or("?");
120   }
121   EXPECT_EQ(string, "ABC??F");
122 }
123 
TEST(Enum,Name)124 TEST(Enum, Name) {
125   {
126     EXPECT_EQ(ftl::flag_name(Flags::kFlag1), "kFlag1");
127     EXPECT_EQ(ftl::flag_name(Flags::kFlag7), "kFlag7");
128 
129     EXPECT_EQ(ftl::flag_name(Flags::kNone), std::nullopt);
130     EXPECT_EQ(ftl::flag_name(Flags::kMask), std::nullopt);
131     EXPECT_EQ(ftl::flag_name(Flags::kAll), std::nullopt);
132   }
133   {
134     EXPECT_EQ(ftl::enum_name(Planet::kEarth), "kEarth");
135     EXPECT_EQ(ftl::enum_name(Planet::kNeptune), "kNeptune");
136 
137     EXPECT_EQ(ftl::enum_name_full(Planet::kEarth), "Planet::kEarth");
138     EXPECT_EQ(ftl::enum_name_full(Planet::kNeptune), "Planet::kNeptune");
139 
140     EXPECT_EQ(ftl::enum_name(kPluto), std::nullopt);
141     EXPECT_EQ(ftl::enum_name_full(kPluto), std::nullopt);
142   }
143   {
144     EXPECT_EQ(ftl::enum_name(kRoom), "kRoom");
145     EXPECT_EQ(ftl::enum_name(kFridge), "kFridge");
146     EXPECT_EQ(ftl::enum_name(kFreezer), "kFreezer");
147 
148     EXPECT_EQ(ftl::enum_name(kRoom), "kRoom");
149     EXPECT_EQ(ftl::enum_name(kFridge), "kFridge");
150     EXPECT_EQ(ftl::enum_name(kFreezer), "kFreezer");
151 
152     EXPECT_EQ(ftl::enum_name(static_cast<Temperature>(-30)), std::nullopt);
153     EXPECT_EQ(ftl::enum_name(static_cast<Temperature>(0)), std::nullopt);
154     EXPECT_EQ(ftl::enum_name(static_cast<Temperature>(100)), std::nullopt);
155 
156     EXPECT_EQ(ftl::enum_name_full(static_cast<Temperature>(-30)), std::nullopt);
157     EXPECT_EQ(ftl::enum_name_full(static_cast<Temperature>(0)), std::nullopt);
158     EXPECT_EQ(ftl::enum_name_full(static_cast<Temperature>(100)), std::nullopt);
159   }
160 }
161 
TEST(Enum,String)162 TEST(Enum, String) {
163   {
164     EXPECT_EQ(ftl::flag_string(Flags::kFlag1), "kFlag1");
165     EXPECT_EQ(ftl::flag_string(Flags::kFlag7), "kFlag7");
166 
167     EXPECT_EQ(ftl::flag_string(Flags::kNone), "0b0");
168     EXPECT_EQ(ftl::flag_string(Flags::kMask), "0b10010010");
169     EXPECT_EQ(ftl::flag_string(Flags::kAll), "0b11111111");
170 
171     enum class Flags64 : std::uint64_t {
172       kFlag0 = 0b1ull,
173       kFlag63 = 0x8000'0000'0000'0000ull,
174       kMask = kFlag0 | kFlag63
175     };
176 
177     EXPECT_EQ(ftl::flag_string(Flags64::kFlag0), "kFlag0");
178     EXPECT_EQ(ftl::flag_string(Flags64::kFlag63), "kFlag63");
179     EXPECT_EQ(ftl::flag_string(Flags64::kMask), "0x8000000000000001");
180   }
181   {
182     EXPECT_EQ(ftl::enum_string(Planet::kEarth), "kEarth");
183     EXPECT_EQ(ftl::enum_string(Planet::kNeptune), "kNeptune");
184 
185     EXPECT_EQ(ftl::enum_string_full(Planet::kEarth), "Planet::kEarth");
186     EXPECT_EQ(ftl::enum_string_full(Planet::kNeptune), "Planet::kNeptune");
187 
188     EXPECT_EQ(ftl::enum_string(kPluto), "8");
189 
190     EXPECT_EQ(ftl::enum_string_full(kPluto), "8");
191 
192   }
193   {
194     EXPECT_EQ(ftl::enum_string(kRoom), "kRoom");
195     EXPECT_EQ(ftl::enum_string(kFridge), "kFridge");
196     EXPECT_EQ(ftl::enum_string(kFreezer), "kFreezer");
197 
198     EXPECT_EQ(ftl::enum_string_full(kRoom), "20");
199     EXPECT_EQ(ftl::enum_string_full(kFridge), "4");
200     EXPECT_EQ(ftl::enum_string_full(kFreezer), "-18");
201 
202     EXPECT_EQ(ftl::enum_string(static_cast<Temperature>(-30)), "-30");
203     EXPECT_EQ(ftl::enum_string(static_cast<Temperature>(0)), "0");
204     EXPECT_EQ(ftl::enum_string(static_cast<Temperature>(100)), "100");
205 
206     EXPECT_EQ(ftl::enum_string_full(static_cast<Temperature>(-30)), "-30");
207     EXPECT_EQ(ftl::enum_string_full(static_cast<Temperature>(0)), "0");
208     EXPECT_EQ(ftl::enum_string_full(static_cast<Temperature>(100)), "100");
209   }
210 }
211 
212 }  // namespace android::test
213