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