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/. */
7 #include "mozilla/DefineEnum.h"
9 // Sanity test for MOZ_DEFINE_ENUM.
11 MOZ_DEFINE_ENUM(TestEnum1
, (EnumeratorA
, EnumeratorB
, EnumeratorC
));
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");
19 // Sanity test for MOZ_DEFINE_ENUM_CLASS.
21 MOZ_DEFINE_ENUM_CLASS(TestEnum2
, (A
, B
, C
));
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");
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
35 // Sanity test for MOZ_DEFINE_ENUM[_CLASS]_AT_CLASS_SCOPE.
39 MOZ_DEFINE_ENUM_AT_CLASS_SCOPE(
46 MOZ_DEFINE_ENUM_CLASS_AT_CLASS_SCOPE(
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");
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");
67 // Test that MOZ_DEFINE_ENUM doesn't allow giving enumerators initializers.
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
))
76 // Nothing to do here, all tests are static_asserts.