TestEnumTypeTraits.cpp (5704B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "mozilla/Assertions.h" 7 #include "mozilla/EnumTypeTraits.h" 8 9 #include <cstdint> 10 11 using namespace mozilla; 12 13 /* Feature check for EnumTypeFitsWithin. */ 14 15 #define MAKE_FIXED_EMUM_FOR_TYPE(IntType) \ 16 enum FixedEnumFor_##IntType : IntType{ \ 17 A_##IntType, \ 18 B_##IntType, \ 19 C_##IntType, \ 20 }; 21 22 template <typename EnumType, typename IntType> 23 static void TestShouldFit() { 24 static_assert(EnumTypeFitsWithin<EnumType, IntType>::value, 25 "Should fit within exact/promoted integral type"); 26 } 27 28 template <typename EnumType, typename IntType> 29 static void TestShouldNotFit() { 30 static_assert(!EnumTypeFitsWithin<EnumType, IntType>::value, 31 "Should not fit within"); 32 } 33 34 void TestFitForTypes() { 35 // check for int8_t 36 MAKE_FIXED_EMUM_FOR_TYPE(int8_t); 37 TestShouldFit<FixedEnumFor_int8_t, int8_t>(); 38 TestShouldFit<FixedEnumFor_int8_t, int16_t>(); 39 TestShouldFit<FixedEnumFor_int8_t, int32_t>(); 40 TestShouldFit<FixedEnumFor_int8_t, int64_t>(); 41 42 TestShouldNotFit<FixedEnumFor_int8_t, uint8_t>(); 43 TestShouldNotFit<FixedEnumFor_int8_t, uint16_t>(); 44 TestShouldNotFit<FixedEnumFor_int8_t, uint32_t>(); 45 TestShouldNotFit<FixedEnumFor_int8_t, uint64_t>(); 46 47 // check for uint8_t 48 MAKE_FIXED_EMUM_FOR_TYPE(uint8_t); 49 TestShouldFit<FixedEnumFor_uint8_t, uint8_t>(); 50 TestShouldFit<FixedEnumFor_uint8_t, uint16_t>(); 51 TestShouldFit<FixedEnumFor_uint8_t, uint32_t>(); 52 TestShouldFit<FixedEnumFor_uint8_t, uint64_t>(); 53 54 TestShouldNotFit<FixedEnumFor_uint8_t, int8_t>(); 55 TestShouldFit<FixedEnumFor_uint8_t, int16_t>(); 56 TestShouldFit<FixedEnumFor_uint8_t, int32_t>(); 57 TestShouldFit<FixedEnumFor_uint8_t, int64_t>(); 58 59 // check for int16_t 60 MAKE_FIXED_EMUM_FOR_TYPE(int16_t); 61 TestShouldNotFit<FixedEnumFor_int16_t, int8_t>(); 62 TestShouldFit<FixedEnumFor_int16_t, int16_t>(); 63 TestShouldFit<FixedEnumFor_int16_t, int32_t>(); 64 TestShouldFit<FixedEnumFor_int16_t, int64_t>(); 65 66 TestShouldNotFit<FixedEnumFor_int16_t, uint8_t>(); 67 TestShouldNotFit<FixedEnumFor_int16_t, uint16_t>(); 68 TestShouldNotFit<FixedEnumFor_int16_t, uint32_t>(); 69 TestShouldNotFit<FixedEnumFor_int16_t, uint64_t>(); 70 71 // check for uint16_t 72 MAKE_FIXED_EMUM_FOR_TYPE(uint16_t); 73 TestShouldNotFit<FixedEnumFor_uint16_t, uint8_t>(); 74 TestShouldFit<FixedEnumFor_uint16_t, uint16_t>(); 75 TestShouldFit<FixedEnumFor_uint16_t, uint32_t>(); 76 TestShouldFit<FixedEnumFor_uint16_t, uint64_t>(); 77 78 TestShouldNotFit<FixedEnumFor_uint16_t, int8_t>(); 79 TestShouldNotFit<FixedEnumFor_uint16_t, int16_t>(); 80 TestShouldFit<FixedEnumFor_uint16_t, int32_t>(); 81 TestShouldFit<FixedEnumFor_uint16_t, int64_t>(); 82 83 // check for int32_t 84 MAKE_FIXED_EMUM_FOR_TYPE(int32_t); 85 TestShouldNotFit<FixedEnumFor_int32_t, int8_t>(); 86 TestShouldNotFit<FixedEnumFor_int32_t, int16_t>(); 87 TestShouldFit<FixedEnumFor_int32_t, int32_t>(); 88 TestShouldFit<FixedEnumFor_int32_t, int64_t>(); 89 90 TestShouldNotFit<FixedEnumFor_int32_t, uint8_t>(); 91 TestShouldNotFit<FixedEnumFor_int32_t, uint16_t>(); 92 TestShouldNotFit<FixedEnumFor_int32_t, uint32_t>(); 93 TestShouldNotFit<FixedEnumFor_int32_t, uint64_t>(); 94 95 // check for uint32_t 96 MAKE_FIXED_EMUM_FOR_TYPE(uint32_t); 97 TestShouldNotFit<FixedEnumFor_uint32_t, uint8_t>(); 98 TestShouldNotFit<FixedEnumFor_uint32_t, uint16_t>(); 99 TestShouldFit<FixedEnumFor_uint32_t, uint32_t>(); 100 TestShouldFit<FixedEnumFor_uint32_t, uint64_t>(); 101 102 TestShouldNotFit<FixedEnumFor_uint32_t, int8_t>(); 103 TestShouldNotFit<FixedEnumFor_uint32_t, int16_t>(); 104 TestShouldNotFit<FixedEnumFor_uint32_t, int32_t>(); 105 TestShouldFit<FixedEnumFor_uint32_t, int64_t>(); 106 107 // check for int64_t 108 MAKE_FIXED_EMUM_FOR_TYPE(int64_t); 109 TestShouldNotFit<FixedEnumFor_int64_t, int8_t>(); 110 TestShouldNotFit<FixedEnumFor_int64_t, int16_t>(); 111 TestShouldNotFit<FixedEnumFor_int64_t, int32_t>(); 112 TestShouldFit<FixedEnumFor_int64_t, int64_t>(); 113 114 TestShouldNotFit<FixedEnumFor_int64_t, uint8_t>(); 115 TestShouldNotFit<FixedEnumFor_int64_t, uint16_t>(); 116 TestShouldNotFit<FixedEnumFor_int64_t, uint32_t>(); 117 TestShouldNotFit<FixedEnumFor_int64_t, uint64_t>(); 118 119 // check for uint64_t 120 MAKE_FIXED_EMUM_FOR_TYPE(uint64_t); 121 TestShouldNotFit<FixedEnumFor_uint64_t, uint8_t>(); 122 TestShouldNotFit<FixedEnumFor_uint64_t, uint16_t>(); 123 TestShouldNotFit<FixedEnumFor_uint64_t, uint32_t>(); 124 TestShouldFit<FixedEnumFor_uint64_t, uint64_t>(); 125 126 TestShouldNotFit<FixedEnumFor_uint64_t, int8_t>(); 127 TestShouldNotFit<FixedEnumFor_uint64_t, int16_t>(); 128 TestShouldNotFit<FixedEnumFor_uint64_t, int32_t>(); 129 TestShouldNotFit<FixedEnumFor_uint64_t, int64_t>(); 130 } 131 132 // - 133 134 template <typename T, typename U> 135 static constexpr void AssertSameTypeAndValue(T a, U b) { 136 static_assert(std::is_same_v<T, U>); 137 MOZ_RELEASE_ASSERT(a == b); 138 } 139 140 void TestUnderlyingValue() { 141 enum class Pet : int16_t { Cat, Dog, Fish }; 142 enum class Plant { Flower, Tree, Vine }; 143 144 AssertSameTypeAndValue(UnderlyingValue(Pet::Cat), int16_t(0)); 145 AssertSameTypeAndValue(UnderlyingValue(Pet::Dog), int16_t(1)); 146 AssertSameTypeAndValue(UnderlyingValue(Pet::Fish), int16_t(2)); 147 148 AssertSameTypeAndValue(UnderlyingValue(Plant::Flower), int(0)); 149 AssertSameTypeAndValue(UnderlyingValue(Plant::Tree), int(1)); 150 AssertSameTypeAndValue(UnderlyingValue(Plant::Vine), int(2)); 151 } 152 153 // - 154 155 int main() { 156 TestFitForTypes(); 157 TestUnderlyingValue(); 158 return 0; 159 }