summaryrefslogtreecommitdiffstats
path: root/mfbt/tests/TestEnumTypeTraits.cpp
blob: 1065c92a7bfb5271a056dc82ab87f51e6a932191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/Assertions.h"
#include "mozilla/EnumTypeTraits.h"

#include <cstdint>

using namespace mozilla;

/* Feature check for EnumTypeFitsWithin. */

#define MAKE_FIXED_EMUM_FOR_TYPE(IntType) \
  enum FixedEnumFor_##IntType : IntType{  \
      A_##IntType,                        \
      B_##IntType,                        \
      C_##IntType,                        \
  };

template <typename EnumType, typename IntType>
static void TestShouldFit() {
  static_assert(EnumTypeFitsWithin<EnumType, IntType>::value,
                "Should fit within exact/promoted integral type");
}

template <typename EnumType, typename IntType>
static void TestShouldNotFit() {
  static_assert(!EnumTypeFitsWithin<EnumType, IntType>::value,
                "Should not fit within");
}

void TestFitForTypes() {
  // check for int8_t
  MAKE_FIXED_EMUM_FOR_TYPE(int8_t);
  TestShouldFit<FixedEnumFor_int8_t, int8_t>();
  TestShouldFit<FixedEnumFor_int8_t, int16_t>();
  TestShouldFit<FixedEnumFor_int8_t, int32_t>();
  TestShouldFit<FixedEnumFor_int8_t, int64_t>();

  TestShouldNotFit<FixedEnumFor_int8_t, uint8_t>();
  TestShouldNotFit<FixedEnumFor_int8_t, uint16_t>();
  TestShouldNotFit<FixedEnumFor_int8_t, uint32_t>();
  TestShouldNotFit<FixedEnumFor_int8_t, uint64_t>();

  // check for uint8_t
  MAKE_FIXED_EMUM_FOR_TYPE(uint8_t);
  TestShouldFit<FixedEnumFor_uint8_t, uint8_t>();
  TestShouldFit<FixedEnumFor_uint8_t, uint16_t>();
  TestShouldFit<FixedEnumFor_uint8_t, uint32_t>();
  TestShouldFit<FixedEnumFor_uint8_t, uint64_t>();

  TestShouldNotFit<FixedEnumFor_uint8_t, int8_t>();
  TestShouldFit<FixedEnumFor_uint8_t, int16_t>();
  TestShouldFit<FixedEnumFor_uint8_t, int32_t>();
  TestShouldFit<FixedEnumFor_uint8_t, int64_t>();

  // check for int16_t
  MAKE_FIXED_EMUM_FOR_TYPE(int16_t);
  TestShouldNotFit<FixedEnumFor_int16_t, int8_t>();
  TestShouldFit<FixedEnumFor_int16_t, int16_t>();
  TestShouldFit<FixedEnumFor_int16_t, int32_t>();
  TestShouldFit<FixedEnumFor_int16_t, int64_t>();

  TestShouldNotFit<FixedEnumFor_int16_t, uint8_t>();
  TestShouldNotFit<FixedEnumFor_int16_t, uint16_t>();
  TestShouldNotFit<FixedEnumFor_int16_t, uint32_t>();
  TestShouldNotFit<FixedEnumFor_int16_t, uint64_t>();

  // check for uint16_t
  MAKE_FIXED_EMUM_FOR_TYPE(uint16_t);
  TestShouldNotFit<FixedEnumFor_uint16_t, uint8_t>();
  TestShouldFit<FixedEnumFor_uint16_t, uint16_t>();
  TestShouldFit<FixedEnumFor_uint16_t, uint32_t>();
  TestShouldFit<FixedEnumFor_uint16_t, uint64_t>();

  TestShouldNotFit<FixedEnumFor_uint16_t, int8_t>();
  TestShouldNotFit<FixedEnumFor_uint16_t, int16_t>();
  TestShouldFit<FixedEnumFor_uint16_t, int32_t>();
  TestShouldFit<FixedEnumFor_uint16_t, int64_t>();

  // check for int32_t
  MAKE_FIXED_EMUM_FOR_TYPE(int32_t);
  TestShouldNotFit<FixedEnumFor_int32_t, int8_t>();
  TestShouldNotFit<FixedEnumFor_int32_t, int16_t>();
  TestShouldFit<FixedEnumFor_int32_t, int32_t>();
  TestShouldFit<FixedEnumFor_int32_t, int64_t>();

  TestShouldNotFit<FixedEnumFor_int32_t, uint8_t>();
  TestShouldNotFit<FixedEnumFor_int32_t, uint16_t>();
  TestShouldNotFit<FixedEnumFor_int32_t, uint32_t>();
  TestShouldNotFit<FixedEnumFor_int32_t, uint64_t>();

  // check for uint32_t
  MAKE_FIXED_EMUM_FOR_TYPE(uint32_t);
  TestShouldNotFit<FixedEnumFor_uint32_t, uint8_t>();
  TestShouldNotFit<FixedEnumFor_uint32_t, uint16_t>();
  TestShouldFit<FixedEnumFor_uint32_t, uint32_t>();
  TestShouldFit<FixedEnumFor_uint32_t, uint64_t>();

  TestShouldNotFit<FixedEnumFor_uint32_t, int8_t>();
  TestShouldNotFit<FixedEnumFor_uint32_t, int16_t>();
  TestShouldNotFit<FixedEnumFor_uint32_t, int32_t>();
  TestShouldFit<FixedEnumFor_uint32_t, int64_t>();

  // check for int64_t
  MAKE_FIXED_EMUM_FOR_TYPE(int64_t);
  TestShouldNotFit<FixedEnumFor_int64_t, int8_t>();
  TestShouldNotFit<FixedEnumFor_int64_t, int16_t>();
  TestShouldNotFit<FixedEnumFor_int64_t, int32_t>();
  TestShouldFit<FixedEnumFor_int64_t, int64_t>();

  TestShouldNotFit<FixedEnumFor_int64_t, uint8_t>();
  TestShouldNotFit<FixedEnumFor_int64_t, uint16_t>();
  TestShouldNotFit<FixedEnumFor_int64_t, uint32_t>();
  TestShouldNotFit<FixedEnumFor_int64_t, uint64_t>();

  // check for uint64_t
  MAKE_FIXED_EMUM_FOR_TYPE(uint64_t);
  TestShouldNotFit<FixedEnumFor_uint64_t, uint8_t>();
  TestShouldNotFit<FixedEnumFor_uint64_t, uint16_t>();
  TestShouldNotFit<FixedEnumFor_uint64_t, uint32_t>();
  TestShouldFit<FixedEnumFor_uint64_t, uint64_t>();

  TestShouldNotFit<FixedEnumFor_uint64_t, int8_t>();
  TestShouldNotFit<FixedEnumFor_uint64_t, int16_t>();
  TestShouldNotFit<FixedEnumFor_uint64_t, int32_t>();
  TestShouldNotFit<FixedEnumFor_uint64_t, int64_t>();
}

// -

template <typename T, typename U>
static constexpr void AssertSameTypeAndValue(T a, U b) {
  static_assert(std::is_same_v<T, U>);
  MOZ_ASSERT(a == b);
}

void TestUnderlyingValue() {
  enum class Pet : int16_t { Cat, Dog, Fish };
  enum class Plant { Flower, Tree, Vine };

  AssertSameTypeAndValue(UnderlyingValue(Pet::Cat), int16_t(0));
  AssertSameTypeAndValue(UnderlyingValue(Pet::Dog), int16_t(1));
  AssertSameTypeAndValue(UnderlyingValue(Pet::Fish), int16_t(2));

  AssertSameTypeAndValue(UnderlyingValue(Plant::Flower), int(0));
  AssertSameTypeAndValue(UnderlyingValue(Plant::Tree), int(1));
  AssertSameTypeAndValue(UnderlyingValue(Plant::Vine), int(2));
}

// -

int main() {
  TestFitForTypes();
  TestUnderlyingValue();
  return 0;
}