enum_base_test.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "common/enum_base.h"
  5. #include <gtest/gtest.h>
  6. #include "testing/base/test_raw_ostream.h"
  7. namespace Carbon::Testing {
  8. namespace {
  9. // These are directly in the Carbon namespace because the defines require it.
  10. CARBON_DEFINE_RAW_ENUM_CLASS(TestKind, uint8_t) {
  11. #define CARBON_ENUM_BASE_TEST_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  12. #include "common/enum_base_test.def"
  13. };
  14. class TestKind : public CARBON_ENUM_BASE(TestKind) {
  15. public:
  16. #define CARBON_ENUM_BASE_TEST_KIND(Name) CARBON_ENUM_CONSTANT_DECLARATION(Name)
  17. #include "common/enum_base_test.def"
  18. using EnumBase::AsInt;
  19. using EnumBase::FromInt;
  20. };
  21. #define CARBON_ENUM_BASE_TEST_KIND(Name) \
  22. CARBON_ENUM_CONSTANT_DEFINITION(TestKind, Name)
  23. #include "common/enum_base_test.def"
  24. CARBON_DEFINE_ENUM_CLASS_NAMES(TestKind) = {
  25. #define CARBON_ENUM_BASE_TEST_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
  26. #include "common/enum_base_test.def"
  27. };
  28. static_assert(sizeof(TestKind) == sizeof(uint8_t),
  29. "Class size doesn't match enum size!");
  30. TEST(EnumBaseTest, NamesAndConstants) {
  31. EXPECT_EQ("Beep", TestKind::Beep.name());
  32. EXPECT_EQ("Boop", TestKind::Boop.name());
  33. EXPECT_EQ("Burr", TestKind::Burr.name());
  34. }
  35. TEST(EnumBaseTest, Printing) {
  36. TestRawOstream stream;
  37. TestKind kind = TestKind::Beep;
  38. stream << kind << " " << TestKind::Beep;
  39. kind = TestKind::Boop;
  40. stream << " " << kind;
  41. // Check the streamed results and also make sure we can stream into GoogleTest
  42. // assertions.
  43. EXPECT_EQ("Beep Beep Boop", stream.TakeStr()) << "Final kind: " << kind;
  44. }
  45. TEST(EnumBaseTest, Switch) {
  46. TestKind kind = TestKind::Boop;
  47. switch (kind) {
  48. case TestKind::Beep: {
  49. FAIL() << "Beep case selected!";
  50. break;
  51. }
  52. case TestKind::Boop: {
  53. EXPECT_EQ("Boop", kind.name());
  54. break;
  55. }
  56. case TestKind::Burr: {
  57. FAIL() << "Burr case selected!";
  58. break;
  59. }
  60. }
  61. }
  62. TEST(EnumBaseTest, Comparison) {
  63. TestKind kind = TestKind::Beep;
  64. // Make sure all the different comparisons work, and also to work with
  65. // GoogleTest expectations.
  66. EXPECT_EQ(TestKind::Beep, kind);
  67. EXPECT_NE(TestKind::Boop, kind);
  68. EXPECT_LT(kind, TestKind::Boop);
  69. EXPECT_GT(TestKind::Burr, kind);
  70. EXPECT_LE(kind, TestKind::Beep);
  71. EXPECT_GE(TestKind::Beep, kind);
  72. // These should also all be constexpr.
  73. constexpr TestKind Kind2 = TestKind::Beep;
  74. static_assert(Kind2 == TestKind::Beep);
  75. static_assert(Kind2 != TestKind::Boop);
  76. static_assert(Kind2 < TestKind::Boop);
  77. static_assert(!(Kind2 > TestKind::Burr));
  78. static_assert(Kind2 <= TestKind::Beep);
  79. static_assert(!(Kind2 >= TestKind::Burr));
  80. }
  81. TEST(EnumBaseTest, IntConversion) {
  82. EXPECT_EQ(0, TestKind::Beep.AsInt());
  83. EXPECT_EQ(1, TestKind::Boop.AsInt());
  84. EXPECT_EQ(2, TestKind::Burr.AsInt());
  85. EXPECT_EQ(TestKind::Beep, TestKind::FromInt(0));
  86. EXPECT_EQ(TestKind::Boop, TestKind::FromInt(1));
  87. EXPECT_EQ(TestKind::Burr, TestKind::FromInt(2));
  88. }
  89. } // namespace
  90. } // namespace Carbon::Testing