• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 enum class F : std::uint16_t { X = 0b1, Y = 0b10, Z = 0b100 };
37 
38 static_assert(ftl::enum_begin_v<F> == F{0});
39 static_assert(ftl::enum_last_v<F> == F{15});
40 static_assert(ftl::enum_size_v<F> == 16);
41 
42 static_assert(ftl::flag_name(F::Z).value_or("?") == "Z");
43 static_assert(ftl::flag_name(F{0b111}).value_or("?") == "?");
44 
45 // If a scoped enum is unsigned, its implicit range corresponds to its bit indices.
46 enum class Flags : std::uint8_t {
47   kNone = 0,
48   kFlag1 = 0b0000'0010,
49   kFlag4 = 0b0001'0000,
50   kFlag7 = 0b1000'0000,
51   kMask = kFlag1 | kFlag4 | kFlag7,
52   kAll = 0b1111'1111
53 };
54 
55 static_assert(ftl::enum_begin_v<Flags> == Flags{0});
56 static_assert(ftl::enum_last_v<Flags> == Flags{7});
57 static_assert(ftl::enum_size_v<Flags> == 8);
58 
59 static_assert(ftl::enum_name<Flags::kNone>() == "kNone");
60 static_assert(ftl::enum_name<Flags::kFlag4>() == "kFlag4");
61 static_assert(ftl::enum_name<Flags::kFlag7>() == "kFlag7");
62 
63 // Though not flags, the enumerators are within the implicit range of bit indices.
64 enum class Planet : std::uint8_t {
65   kMercury,
66   kVenus,
67   kEarth,
68   kMars,
69   kJupiter,
70   kSaturn,
71   kUranus,
72   kNeptune
73 };
74 
75 constexpr Planet kPluto{ftl::to_underlying(Planet::kNeptune) + 1};  // Honorable mention.
76 
77 static_assert(ftl::enum_begin_v<Planet> == Planet::kMercury);
78 static_assert(ftl::enum_last_v<Planet> == Planet::kNeptune);
79 static_assert(ftl::enum_size_v<Planet> == 8);
80 
81 static_assert(ftl::enum_name<Planet::kMercury>() == "kMercury");
82 static_assert(ftl::enum_name<Planet::kSaturn>() == "kSaturn");
83 
84 // Unscoped enum must define explicit range, even if the underlying type is fixed.
85 enum Temperature : int {
86   kRoom = 20,
87   kFridge = 4,
88   kFreezer = -18,
89 
90   ftl_first = kFreezer,
91   ftl_last = kRoom
92 };
93 
94 static_assert(ftl::enum_begin_v<Temperature> == kFreezer);
95 static_assert(ftl::enum_last_v<Temperature> == kRoom);
96 static_assert(ftl::enum_size_v<Temperature> == 39);
97 
98 static_assert(ftl::enum_name<kFreezer>() == "kFreezer");
99 static_assert(ftl::enum_name<kFridge>() == "kFridge");
100 static_assert(ftl::enum_name<kRoom>() == "kRoom");
101 
102 }  // namespace
103 
TEST(Enum,Range)104 TEST(Enum, Range) {
105   std::string string;
106   for (E v : ftl::enum_range<E>()) {
107     string += ftl::enum_name(v).value_or("?");
108   }
109   EXPECT_EQ(string, "ABC??F");
110 }
111 
TEST(Enum,Name)112 TEST(Enum, Name) {
113   {
114     EXPECT_EQ(ftl::flag_name(Flags::kFlag1), "kFlag1");
115     EXPECT_EQ(ftl::flag_name(Flags::kFlag7), "kFlag7");
116 
117     EXPECT_EQ(ftl::flag_name(Flags::kNone), std::nullopt);
118     EXPECT_EQ(ftl::flag_name(Flags::kMask), std::nullopt);
119     EXPECT_EQ(ftl::flag_name(Flags::kAll), std::nullopt);
120   }
121   {
122     EXPECT_EQ(ftl::enum_name(Planet::kEarth), "kEarth");
123     EXPECT_EQ(ftl::enum_name(Planet::kNeptune), "kNeptune");
124 
125     EXPECT_EQ(ftl::enum_name(kPluto), std::nullopt);
126   }
127   {
128     EXPECT_EQ(ftl::enum_name(kRoom), "kRoom");
129     EXPECT_EQ(ftl::enum_name(kFridge), "kFridge");
130     EXPECT_EQ(ftl::enum_name(kFreezer), "kFreezer");
131 
132     EXPECT_EQ(ftl::enum_name(static_cast<Temperature>(-30)), std::nullopt);
133     EXPECT_EQ(ftl::enum_name(static_cast<Temperature>(0)), std::nullopt);
134     EXPECT_EQ(ftl::enum_name(static_cast<Temperature>(100)), std::nullopt);
135   }
136 }
137 
TEST(Enum,String)138 TEST(Enum, String) {
139   {
140     EXPECT_EQ(ftl::flag_string(Flags::kFlag1), "kFlag1");
141     EXPECT_EQ(ftl::flag_string(Flags::kFlag7), "kFlag7");
142 
143     EXPECT_EQ(ftl::flag_string(Flags::kNone), "0b0");
144     EXPECT_EQ(ftl::flag_string(Flags::kMask), "0b10010010");
145     EXPECT_EQ(ftl::flag_string(Flags::kAll), "0b11111111");
146 
147     enum class Flags64 : std::uint64_t {
148       kFlag0 = 0b1ull,
149       kFlag63 = 0x8000'0000'0000'0000ull,
150       kMask = kFlag0 | kFlag63
151     };
152 
153     EXPECT_EQ(ftl::flag_string(Flags64::kFlag0), "kFlag0");
154     EXPECT_EQ(ftl::flag_string(Flags64::kFlag63), "kFlag63");
155     EXPECT_EQ(ftl::flag_string(Flags64::kMask), "0x8000000000000001");
156   }
157   {
158     EXPECT_EQ(ftl::enum_string(Planet::kEarth), "kEarth");
159     EXPECT_EQ(ftl::enum_string(Planet::kNeptune), "kNeptune");
160 
161     EXPECT_EQ(ftl::enum_string(kPluto), "8");
162   }
163   {
164     EXPECT_EQ(ftl::enum_string(kRoom), "kRoom");
165     EXPECT_EQ(ftl::enum_string(kFridge), "kFridge");
166     EXPECT_EQ(ftl::enum_string(kFreezer), "kFreezer");
167 
168     EXPECT_EQ(ftl::enum_string(static_cast<Temperature>(-30)), "-30");
169     EXPECT_EQ(ftl::enum_string(static_cast<Temperature>(0)), "0");
170     EXPECT_EQ(ftl::enum_string(static_cast<Temperature>(100)), "100");
171   }
172 }
173 
174 }  // namespace android::test
175