|  | /* | 
|  | * Copyright 2021 The Android Open Source Project | 
|  | * | 
|  | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | * you may not use this file except in compliance with the License. | 
|  | * You may obtain a copy of the License at | 
|  | * | 
|  | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | * | 
|  | * Unless required by applicable law or agreed to in writing, software | 
|  | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | * See the License for the specific language governing permissions and | 
|  | * limitations under the License. | 
|  | */ | 
|  |  | 
|  | #include <ftl/enum.h> | 
|  | #include <gtest/gtest.h> | 
|  |  | 
|  | namespace android::test { | 
|  |  | 
|  | // Keep in sync with example usage in header file. | 
|  | namespace { | 
|  |  | 
|  | enum class E { A, B, C, F = 5, ftl_last = F }; | 
|  |  | 
|  | static_assert(ftl::enum_begin_v<E> == E::A); | 
|  | static_assert(ftl::enum_last_v<E> == E::F); | 
|  | static_assert(ftl::enum_size_v<E> == 6); | 
|  |  | 
|  | static_assert(ftl::enum_name<E::B>() == "B"); | 
|  | static_assert(ftl::enum_name<E::ftl_last>() == "F"); | 
|  | static_assert(ftl::enum_name(E::C).value_or("?") == "C"); | 
|  | static_assert(ftl::enum_name(E{3}).value_or("?") == "?"); | 
|  |  | 
|  | enum class F : std::uint16_t { X = 0b1, Y = 0b10, Z = 0b100 }; | 
|  |  | 
|  | static_assert(ftl::enum_begin_v<F> == F{0}); | 
|  | static_assert(ftl::enum_last_v<F> == F{15}); | 
|  | static_assert(ftl::enum_size_v<F> == 16); | 
|  |  | 
|  | static_assert(ftl::flag_name(F::Z).value_or("?") == "Z"); | 
|  | static_assert(ftl::flag_name(F{0b111}).value_or("?") == "?"); | 
|  |  | 
|  | // If a scoped enum is unsigned, its implicit range corresponds to its bit indices. | 
|  | enum class Flags : std::uint8_t { | 
|  | kNone = 0, | 
|  | kFlag1 = 0b0000'0010, | 
|  | kFlag4 = 0b0001'0000, | 
|  | kFlag7 = 0b1000'0000, | 
|  | kMask = kFlag1 | kFlag4 | kFlag7, | 
|  | kAll = 0b1111'1111 | 
|  | }; | 
|  |  | 
|  | static_assert(ftl::enum_begin_v<Flags> == Flags{0}); | 
|  | static_assert(ftl::enum_last_v<Flags> == Flags{7}); | 
|  | static_assert(ftl::enum_size_v<Flags> == 8); | 
|  |  | 
|  | static_assert(ftl::enum_name<Flags::kNone>() == "kNone"); | 
|  | static_assert(ftl::enum_name<Flags::kFlag4>() == "kFlag4"); | 
|  | static_assert(ftl::enum_name<Flags::kFlag7>() == "kFlag7"); | 
|  |  | 
|  | // Though not flags, the enumerators are within the implicit range of bit indices. | 
|  | enum class Planet : std::uint8_t { | 
|  | kMercury, | 
|  | kVenus, | 
|  | kEarth, | 
|  | kMars, | 
|  | kJupiter, | 
|  | kSaturn, | 
|  | kUranus, | 
|  | kNeptune | 
|  | }; | 
|  |  | 
|  | constexpr Planet kPluto{ftl::enum_cast(Planet::kNeptune) + 1};  // Honorable mention. | 
|  |  | 
|  | static_assert(ftl::enum_begin_v<Planet> == Planet::kMercury); | 
|  | static_assert(ftl::enum_last_v<Planet> == Planet::kNeptune); | 
|  | static_assert(ftl::enum_size_v<Planet> == 8); | 
|  |  | 
|  | static_assert(ftl::enum_name<Planet::kMercury>() == "kMercury"); | 
|  | static_assert(ftl::enum_name<Planet::kSaturn>() == "kSaturn"); | 
|  |  | 
|  | // Unscoped enum must define explicit range, even if the underlying type is fixed. | 
|  | enum Temperature : int { | 
|  | kRoom = 20, | 
|  | kFridge = 4, | 
|  | kFreezer = -18, | 
|  |  | 
|  | ftl_first = kFreezer, | 
|  | ftl_last = kRoom | 
|  | }; | 
|  |  | 
|  | static_assert(ftl::enum_begin_v<Temperature> == kFreezer); | 
|  | static_assert(ftl::enum_last_v<Temperature> == kRoom); | 
|  | static_assert(ftl::enum_size_v<Temperature> == 39); | 
|  |  | 
|  | static_assert(ftl::enum_name<kFreezer>() == "kFreezer"); | 
|  | static_assert(ftl::enum_name<kFridge>() == "kFridge"); | 
|  | static_assert(ftl::enum_name<kRoom>() == "kRoom"); | 
|  |  | 
|  | }  // namespace | 
|  |  | 
|  | TEST(Enum, Range) { | 
|  | std::string string; | 
|  | for (E v : ftl::enum_range<E>()) { | 
|  | string += ftl::enum_name(v).value_or("?"); | 
|  | } | 
|  | EXPECT_EQ(string, "ABC??F"); | 
|  | } | 
|  |  | 
|  | TEST(Enum, Name) { | 
|  | { | 
|  | EXPECT_EQ(ftl::flag_name(Flags::kFlag1), "kFlag1"); | 
|  | EXPECT_EQ(ftl::flag_name(Flags::kFlag7), "kFlag7"); | 
|  |  | 
|  | EXPECT_EQ(ftl::flag_name(Flags::kNone), std::nullopt); | 
|  | EXPECT_EQ(ftl::flag_name(Flags::kMask), std::nullopt); | 
|  | EXPECT_EQ(ftl::flag_name(Flags::kAll), std::nullopt); | 
|  | } | 
|  | { | 
|  | EXPECT_EQ(ftl::enum_name(Planet::kEarth), "kEarth"); | 
|  | EXPECT_EQ(ftl::enum_name(Planet::kNeptune), "kNeptune"); | 
|  |  | 
|  | EXPECT_EQ(ftl::enum_name(kPluto), std::nullopt); | 
|  | } | 
|  | { | 
|  | EXPECT_EQ(ftl::enum_name(kRoom), "kRoom"); | 
|  | EXPECT_EQ(ftl::enum_name(kFridge), "kFridge"); | 
|  | EXPECT_EQ(ftl::enum_name(kFreezer), "kFreezer"); | 
|  |  | 
|  | EXPECT_EQ(ftl::enum_name(static_cast<Temperature>(-30)), std::nullopt); | 
|  | EXPECT_EQ(ftl::enum_name(static_cast<Temperature>(0)), std::nullopt); | 
|  | EXPECT_EQ(ftl::enum_name(static_cast<Temperature>(100)), std::nullopt); | 
|  | } | 
|  | } | 
|  |  | 
|  | TEST(Enum, String) { | 
|  | { | 
|  | EXPECT_EQ(ftl::flag_string(Flags::kFlag1), "kFlag1"); | 
|  | EXPECT_EQ(ftl::flag_string(Flags::kFlag7), "kFlag7"); | 
|  |  | 
|  | EXPECT_EQ(ftl::flag_string(Flags::kNone), "0b0"); | 
|  | EXPECT_EQ(ftl::flag_string(Flags::kMask), "0b10010010"); | 
|  | EXPECT_EQ(ftl::flag_string(Flags::kAll), "0b11111111"); | 
|  | } | 
|  | { | 
|  | EXPECT_EQ(ftl::enum_string(Planet::kEarth), "kEarth"); | 
|  | EXPECT_EQ(ftl::enum_string(Planet::kNeptune), "kNeptune"); | 
|  |  | 
|  | EXPECT_EQ(ftl::enum_string(kPluto), "8"); | 
|  | } | 
|  | { | 
|  | EXPECT_EQ(ftl::enum_string(kRoom), "kRoom"); | 
|  | EXPECT_EQ(ftl::enum_string(kFridge), "kFridge"); | 
|  | EXPECT_EQ(ftl::enum_string(kFreezer), "kFreezer"); | 
|  |  | 
|  | EXPECT_EQ(ftl::enum_string(static_cast<Temperature>(-30)), "-30"); | 
|  | EXPECT_EQ(ftl::enum_string(static_cast<Temperature>(0)), "0"); | 
|  | EXPECT_EQ(ftl::enum_string(static_cast<Temperature>(100)), "100"); | 
|  | } | 
|  | } | 
|  |  | 
|  | }  // namespace android::test |