1 // Copyright 2020 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/base/internal/fast_type_id.h"
16
17 #include <cstdint>
18 #include <map>
19 #include <vector>
20
21 #include "gtest/gtest.h"
22
23 namespace {
24 namespace bi = absl::base_internal;
25
26 // NOLINTNEXTLINE
27 #define PRIM_TYPES(A) \
28 A(bool) \
29 A(short) \
30 A(unsigned short) \
31 A(int) \
32 A(unsigned int) \
33 A(long) \
34 A(unsigned long) \
35 A(long long) \
36 A(unsigned long long) \
37 A(float) \
38 A(double) \
39 A(long double)
40
TEST(FastTypeIdTest,PrimitiveTypes)41 TEST(FastTypeIdTest, PrimitiveTypes) {
42 bi::FastTypeIdType type_ids[] = {
43 #define A(T) bi::FastTypeId<T>(),
44 PRIM_TYPES(A)
45 #undef A
46 #define A(T) bi::FastTypeId<const T>(),
47 PRIM_TYPES(A)
48 #undef A
49 #define A(T) bi::FastTypeId<volatile T>(),
50 PRIM_TYPES(A)
51 #undef A
52 #define A(T) bi::FastTypeId<const volatile T>(),
53 PRIM_TYPES(A)
54 #undef A
55 };
56 size_t total_type_ids = sizeof(type_ids) / sizeof(bi::FastTypeIdType);
57
58 for (int i = 0; i < total_type_ids; ++i) {
59 EXPECT_EQ(type_ids[i], type_ids[i]);
60 for (int j = 0; j < i; ++j) {
61 EXPECT_NE(type_ids[i], type_ids[j]);
62 }
63 }
64 }
65
66 #define FIXED_WIDTH_TYPES(A) \
67 A(int8_t) \
68 A(uint8_t) \
69 A(int16_t) \
70 A(uint16_t) \
71 A(int32_t) \
72 A(uint32_t) \
73 A(int64_t) \
74 A(uint64_t)
75
TEST(FastTypeIdTest,FixedWidthTypes)76 TEST(FastTypeIdTest, FixedWidthTypes) {
77 bi::FastTypeIdType type_ids[] = {
78 #define A(T) bi::FastTypeId<T>(),
79 FIXED_WIDTH_TYPES(A)
80 #undef A
81 #define A(T) bi::FastTypeId<const T>(),
82 FIXED_WIDTH_TYPES(A)
83 #undef A
84 #define A(T) bi::FastTypeId<volatile T>(),
85 FIXED_WIDTH_TYPES(A)
86 #undef A
87 #define A(T) bi::FastTypeId<const volatile T>(),
88 FIXED_WIDTH_TYPES(A)
89 #undef A
90 };
91 size_t total_type_ids = sizeof(type_ids) / sizeof(bi::FastTypeIdType);
92
93 for (int i = 0; i < total_type_ids; ++i) {
94 EXPECT_EQ(type_ids[i], type_ids[i]);
95 for (int j = 0; j < i; ++j) {
96 EXPECT_NE(type_ids[i], type_ids[j]);
97 }
98 }
99 }
100
TEST(FastTypeIdTest,AliasTypes)101 TEST(FastTypeIdTest, AliasTypes) {
102 using int_alias = int;
103 EXPECT_EQ(bi::FastTypeId<int_alias>(), bi::FastTypeId<int>());
104 }
105
TEST(FastTypeIdTest,TemplateSpecializations)106 TEST(FastTypeIdTest, TemplateSpecializations) {
107 EXPECT_NE(bi::FastTypeId<std::vector<int>>(),
108 bi::FastTypeId<std::vector<long>>());
109
110 EXPECT_NE((bi::FastTypeId<std::map<int, float>>()),
111 (bi::FastTypeId<std::map<int, double>>()));
112 }
113
114 struct Base {};
115 struct Derived : Base {};
116 struct PDerived : private Base {};
117
TEST(FastTypeIdTest,Inheritance)118 TEST(FastTypeIdTest, Inheritance) {
119 EXPECT_NE(bi::FastTypeId<Base>(), bi::FastTypeId<Derived>());
120 EXPECT_NE(bi::FastTypeId<Base>(), bi::FastTypeId<PDerived>());
121 }
122
123 } // namespace
124