tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestDefineEnum.cpp (3024B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "mozilla/DefineEnum.h"
      8 
      9 // Sanity test for MOZ_DEFINE_ENUM.
     10 
     11 MOZ_DEFINE_ENUM(TestEnum1, (EnumeratorA, EnumeratorB, EnumeratorC));
     12 
     13 static_assert(EnumeratorA == 0, "Unexpected enumerator value");
     14 static_assert(EnumeratorB == 1, "Unexpected enumerator value");
     15 static_assert(EnumeratorC == 2, "Unexpected enumerator value");
     16 static_assert(kHighestTestEnum1 == EnumeratorC, "Incorrect highest value");
     17 static_assert(kTestEnum1Count == 3, "Incorrect enumerator count");
     18 
     19 // Sanity test for MOZ_DEFINE_ENUM_CLASS.
     20 
     21 MOZ_DEFINE_ENUM_CLASS(TestEnum2, (A, B, C));
     22 
     23 static_assert(TestEnum2::A == TestEnum2(0), "Unexpected enumerator value");
     24 static_assert(TestEnum2::B == TestEnum2(1), "Unexpected enumerator value");
     25 static_assert(TestEnum2::C == TestEnum2(2), "Unexpected enumerator value");
     26 static_assert(kHighestTestEnum2 == TestEnum2::C, "Incorrect highest value");
     27 static_assert(kTestEnum2Count == 3, "Incorrect enumerator count");
     28 
     29 // TODO: Test that the _WITH_BASE variants generate enumerators with the
     30 //       correct underlying types. To do this, we need an |UnderlyingType|
     31 //       type trait, which needs compiler support (recent versions of
     32 //       compilers in the GCC family provide an |__underlying_type| builtin
     33 //       for this purpose.
     34 
     35 // Sanity test for MOZ_DEFINE_ENUM[_CLASS]_AT_CLASS_SCOPE.
     36 
     37 struct TestClass {
     38  // clang-format off
     39  MOZ_DEFINE_ENUM_AT_CLASS_SCOPE(
     40    TestEnum3, (
     41      EnumeratorA,
     42      EnumeratorB,
     43      EnumeratorC
     44  ));
     45 
     46  MOZ_DEFINE_ENUM_CLASS_AT_CLASS_SCOPE(
     47    TestEnum4, (
     48      A,
     49      B,
     50      C
     51  ));
     52  // clang-format on
     53 
     54  static_assert(EnumeratorA == 0, "Unexpected enumerator value");
     55  static_assert(EnumeratorB == 1, "Unexpected enumerator value");
     56  static_assert(EnumeratorC == 2, "Unexpected enumerator value");
     57  static_assert(sHighestTestEnum3 == EnumeratorC, "Incorrect highest value");
     58  static_assert(sTestEnum3Count == 3, "Incorrect enumerator count");
     59 
     60  static_assert(TestEnum4::A == TestEnum4(0), "Unexpected enumerator value");
     61  static_assert(TestEnum4::B == TestEnum4(1), "Unexpected enumerator value");
     62  static_assert(TestEnum4::C == TestEnum4(2), "Unexpected enumerator value");
     63  static_assert(sHighestTestEnum4 == TestEnum4::C, "Incorrect highest value");
     64  static_assert(sTestEnum4Count == 3, "Incorrect enumerator count");
     65 };
     66 
     67 // Test that MOZ_DEFINE_ENUM doesn't allow giving enumerators initializers.
     68 
     69 #ifdef CONFIRM_COMPILATION_ERRORS
     70 MOZ_DEFINE_ENUM_CLASS(EnumWithInitializer1, (A = -1, B, C))
     71 MOZ_DEFINE_ENUM_CLASS(EnumWithInitializer2, (A = 1, B, C))
     72 MOZ_DEFINE_ENUM_CLASS(EnumWithInitializer3, (A, B = 6, C))
     73 #endif
     74 
     75 int main() {
     76  // Nothing to do here, all tests are static_asserts.
     77  return 0;
     78 }