mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:00:12 +01:00
Adds a unit test, and some smaller edits: - Remove the `=` when defining names, in order to change `}` placement by clang-format on uses. - context: https://github.com/carbon-language/carbon-lang/pull/6053#discussion_r2343423178 - I believe with `EnumBase` that keeping the `=` had been a deliberate choice, so this PR is intended to confirm that removing it is okay. - Delete `EnumMaskBase::name` - context: https://github.com/carbon-language/carbon-lang/pull/6053#discussion_r2344233707 - We can't just do nothing because `EnumBase::name` uses indexing that's incompatible with `EnumMaskBase`. - Some small comment cleanups. - Tests don't need to be in the `Carbon` namespace anymore, macros work fine in other namespaces, but it's still the right namespace. - Documentation on `EnumBase::name` seems to be referring to a prior structure, wherein we had a macro defining the function instead of the `Names` array. --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
105 lines
2.8 KiB
C++
105 lines
2.8 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "common/enum_base.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "common/raw_string_ostream.h"
|
|
|
|
namespace Carbon {
|
|
namespace {
|
|
|
|
// These are directly in the Carbon namespace because the defines require it.
|
|
CARBON_DEFINE_RAW_ENUM_CLASS(TestKind, uint8_t) {
|
|
#define CARBON_ENUM_BASE_TEST_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
|
|
#include "common/enum_base_test.def"
|
|
};
|
|
|
|
class TestKind : public CARBON_ENUM_BASE(TestKind) {
|
|
public:
|
|
#define CARBON_ENUM_BASE_TEST_KIND(Name) CARBON_ENUM_CONSTANT_DECL(Name)
|
|
#include "common/enum_base_test.def"
|
|
|
|
using EnumBase::AsInt;
|
|
using EnumBase::FromInt;
|
|
};
|
|
|
|
#define CARBON_ENUM_BASE_TEST_KIND(Name) \
|
|
CARBON_ENUM_CONSTANT_DEFINITION(TestKind, Name)
|
|
#include "common/enum_base_test.def"
|
|
|
|
CARBON_DEFINE_ENUM_CLASS_NAMES(TestKind) {
|
|
#define CARBON_ENUM_BASE_TEST_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
|
|
#include "common/enum_base_test.def"
|
|
};
|
|
|
|
static_assert(sizeof(TestKind) == sizeof(uint8_t),
|
|
"Class size doesn't match enum size!");
|
|
|
|
TEST(EnumBaseTest, NamesAndConstants) {
|
|
EXPECT_EQ("Beep", TestKind::Beep.name());
|
|
EXPECT_EQ("Boop", TestKind::Boop.name());
|
|
EXPECT_EQ("Burr", TestKind::Burr.name());
|
|
}
|
|
|
|
TEST(EnumBaseTest, Printing) {
|
|
RawStringOstream stream;
|
|
|
|
TestKind kind = TestKind::Beep;
|
|
stream << kind << " " << TestKind::Beep;
|
|
kind = TestKind::Boop;
|
|
stream << " " << kind;
|
|
|
|
// Check the streamed results and also make sure we can stream into GoogleTest
|
|
// assertions.
|
|
EXPECT_EQ("Beep Beep Boop", stream.TakeStr()) << "Final kind: " << kind;
|
|
}
|
|
|
|
TEST(EnumBaseTest, Switch) {
|
|
TestKind kind = TestKind::Boop;
|
|
|
|
switch (kind) {
|
|
case TestKind::Beep: {
|
|
FAIL() << "Beep case selected!";
|
|
break;
|
|
}
|
|
case TestKind::Boop: {
|
|
EXPECT_EQ("Boop", kind.name());
|
|
break;
|
|
}
|
|
case TestKind::Burr: {
|
|
FAIL() << "Burr case selected!";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST(EnumBaseTest, Comparison) {
|
|
TestKind kind = TestKind::Beep;
|
|
|
|
// Make sure all the different comparisons work, and also work with
|
|
// GoogleTest expectations.
|
|
EXPECT_EQ(TestKind::Beep, kind);
|
|
EXPECT_NE(TestKind::Boop, kind);
|
|
|
|
// These should also all be constexpr.
|
|
constexpr TestKind Kind2 = TestKind::Beep;
|
|
static_assert(Kind2 == TestKind::Beep);
|
|
static_assert(Kind2 != TestKind::Boop);
|
|
}
|
|
|
|
TEST(EnumBaseTest, IntConversion) {
|
|
EXPECT_EQ(0, TestKind::Beep.AsInt());
|
|
EXPECT_EQ(1, TestKind::Boop.AsInt());
|
|
EXPECT_EQ(2, TestKind::Burr.AsInt());
|
|
|
|
EXPECT_EQ(TestKind::Beep, TestKind::FromInt(0));
|
|
EXPECT_EQ(TestKind::Boop, TestKind::FromInt(1));
|
|
EXPECT_EQ(TestKind::Burr, TestKind::FromInt(2));
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon
|