1 // Copyright 2019 Google LLC
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 "runtime/cpp/emboss_cpp_types.h"
16
17 #include "gtest/gtest.h"
18
19 namespace emboss {
20 namespace support {
21 namespace test {
22
TEST(FloatTypes,Types)23 TEST(FloatTypes, Types) {
24 EXPECT_EQ(32U / CHAR_BIT, sizeof(FloatType<32>::Type));
25 EXPECT_EQ(64U / CHAR_BIT, sizeof(FloatType<64>::Type));
26 EXPECT_EQ(32U / CHAR_BIT, sizeof(FloatType<32>::UIntType));
27 EXPECT_EQ(64U / CHAR_BIT, sizeof(FloatType<64>::UIntType));
28 EXPECT_TRUE(::std::is_floating_point<FloatType<32>::Type>::value);
29 EXPECT_TRUE(::std::is_floating_point<FloatType<64>::Type>::value);
30 EXPECT_TRUE(
31 (::std::is_same<FloatType<32>::UIntType, ::std::uint32_t>::value));
32 EXPECT_TRUE(
33 (::std::is_same<FloatType<64>::UIntType, ::std::uint64_t>::value));
34 }
35
TEST(LeastWidthInteger,Types)36 TEST(LeastWidthInteger, Types) {
37 EXPECT_TRUE(
38 (::std::is_same<LeastWidthInteger<1>::Unsigned, ::std::uint8_t>::value));
39 EXPECT_TRUE(
40 (::std::is_same<LeastWidthInteger<1>::Signed, ::std::int8_t>::value));
41 EXPECT_TRUE(
42 (::std::is_same<LeastWidthInteger<4>::Unsigned, ::std::uint8_t>::value));
43 EXPECT_TRUE(
44 (::std::is_same<LeastWidthInteger<4>::Signed, ::std::int8_t>::value));
45 EXPECT_TRUE(
46 (::std::is_same<LeastWidthInteger<7>::Unsigned, ::std::uint8_t>::value));
47 EXPECT_TRUE(
48 (::std::is_same<LeastWidthInteger<7>::Signed, ::std::int8_t>::value));
49 EXPECT_TRUE(
50 (::std::is_same<LeastWidthInteger<8>::Unsigned, ::std::uint8_t>::value));
51 EXPECT_TRUE(
52 (::std::is_same<LeastWidthInteger<8>::Signed, ::std::int8_t>::value));
53 EXPECT_TRUE(
54 (::std::is_same<LeastWidthInteger<9>::Unsigned, ::std::uint16_t>::value));
55 EXPECT_TRUE(
56 (::std::is_same<LeastWidthInteger<9>::Signed, ::std::int16_t>::value));
57 EXPECT_TRUE((
58 ::std::is_same<LeastWidthInteger<12>::Unsigned, ::std::uint16_t>::value));
59 EXPECT_TRUE(
60 (::std::is_same<LeastWidthInteger<12>::Signed, ::std::int16_t>::value));
61 EXPECT_TRUE((
62 ::std::is_same<LeastWidthInteger<15>::Unsigned, ::std::uint16_t>::value));
63 EXPECT_TRUE(
64 (::std::is_same<LeastWidthInteger<15>::Signed, ::std::int16_t>::value));
65 EXPECT_TRUE((
66 ::std::is_same<LeastWidthInteger<16>::Unsigned, ::std::uint16_t>::value));
67 EXPECT_TRUE(
68 (::std::is_same<LeastWidthInteger<16>::Signed, ::std::int16_t>::value));
69 EXPECT_TRUE((
70 ::std::is_same<LeastWidthInteger<17>::Unsigned, ::std::uint32_t>::value));
71 EXPECT_TRUE(
72 (::std::is_same<LeastWidthInteger<17>::Signed, ::std::int32_t>::value));
73 EXPECT_TRUE((
74 ::std::is_same<LeastWidthInteger<28>::Unsigned, ::std::uint32_t>::value));
75 EXPECT_TRUE(
76 (::std::is_same<LeastWidthInteger<28>::Signed, ::std::int32_t>::value));
77 EXPECT_TRUE((
78 ::std::is_same<LeastWidthInteger<31>::Unsigned, ::std::uint32_t>::value));
79 EXPECT_TRUE(
80 (::std::is_same<LeastWidthInteger<31>::Signed, ::std::int32_t>::value));
81 EXPECT_TRUE((
82 ::std::is_same<LeastWidthInteger<32>::Unsigned, ::std::uint32_t>::value));
83 EXPECT_TRUE(
84 (::std::is_same<LeastWidthInteger<32>::Signed, ::std::int32_t>::value));
85 EXPECT_TRUE((
86 ::std::is_same<LeastWidthInteger<33>::Unsigned, ::std::uint64_t>::value));
87 EXPECT_TRUE(
88 (::std::is_same<LeastWidthInteger<33>::Signed, ::std::int64_t>::value));
89 EXPECT_TRUE((
90 ::std::is_same<LeastWidthInteger<60>::Unsigned, ::std::uint64_t>::value));
91 EXPECT_TRUE(
92 (::std::is_same<LeastWidthInteger<60>::Signed, ::std::int64_t>::value));
93 EXPECT_TRUE((
94 ::std::is_same<LeastWidthInteger<63>::Unsigned, ::std::uint64_t>::value));
95 EXPECT_TRUE(
96 (::std::is_same<LeastWidthInteger<63>::Signed, ::std::int64_t>::value));
97 EXPECT_TRUE((
98 ::std::is_same<LeastWidthInteger<64>::Unsigned, ::std::uint64_t>::value));
99 EXPECT_TRUE(
100 (::std::is_same<LeastWidthInteger<64>::Signed, ::std::int64_t>::value));
101 }
102
TEST(IsAliasSafe,CharTypes)103 TEST(IsAliasSafe, CharTypes) {
104 EXPECT_TRUE(IsAliasSafe<char>::value);
105 EXPECT_TRUE(IsAliasSafe<unsigned char>::value);
106 EXPECT_TRUE(IsAliasSafe<const char>::value);
107 EXPECT_TRUE(IsAliasSafe<const unsigned char>::value);
108 EXPECT_TRUE(IsAliasSafe<volatile char>::value);
109 EXPECT_TRUE(IsAliasSafe<volatile unsigned char>::value);
110 EXPECT_TRUE(IsAliasSafe<const volatile char>::value);
111 EXPECT_TRUE(IsAliasSafe<const volatile unsigned char>::value);
112 #if __cplusplus >= 201703
113 EXPECT_TRUE(IsAliasSafe<::std::byte>::value);
114 EXPECT_TRUE(IsAliasSafe<const ::std::byte>::value);
115 EXPECT_TRUE(IsAliasSafe<volatile ::std::byte>::value);
116 EXPECT_TRUE(IsAliasSafe<const volatile ::std::byte>::value);
117 #endif
118 }
119
TEST(IsAliasSafe,NonCharTypes)120 TEST(IsAliasSafe, NonCharTypes) {
121 struct OneByte {
122 char c;
123 };
124 EXPECT_EQ(1U, sizeof(OneByte));
125 EXPECT_FALSE(IsAliasSafe<int>::value);
126 EXPECT_FALSE(IsAliasSafe<unsigned>::value);
127 EXPECT_FALSE(IsAliasSafe<const int>::value);
128 EXPECT_FALSE(IsAliasSafe<OneByte>::value);
129
130 EXPECT_FALSE(IsAliasSafe<signed char>::value);
131 EXPECT_FALSE(IsAliasSafe<const signed char>::value);
132 EXPECT_FALSE(IsAliasSafe<volatile signed char>::value);
133 EXPECT_FALSE(IsAliasSafe<const volatile signed char>::value);
134 }
135
TEST(AddSourceConst,AddSourceConst)136 TEST(AddSourceConst, AddSourceConst) {
137 EXPECT_TRUE(
138 (::std::is_same<const char,
139 typename AddSourceConst<const int, char>::Type>::value));
140 EXPECT_TRUE(
141 (::std::is_same<
142 const volatile char,
143 typename AddSourceConst<const int, volatile char>::Type>::value));
144 EXPECT_TRUE(
145 (::std::is_same<char, typename AddSourceConst<int, char>::Type>::value));
146 EXPECT_TRUE(
147 (::std::is_same<
148 char, typename AddSourceConst<volatile int, char>::Type>::value));
149 EXPECT_TRUE(
150 (::std::is_same<const char,
151 typename AddSourceConst<int, const char>::Type>::value));
152 EXPECT_TRUE(
153 (::std::is_same<const char, typename AddSourceConst<
154 const int, const char>::Type>::value));
155 }
156
TEST(AddSourceVolatile,AddSourceVolatile)157 TEST(AddSourceVolatile, AddSourceVolatile) {
158 EXPECT_TRUE(
159 (::std::is_same<volatile char, typename AddSourceVolatile<
160 volatile int, char>::Type>::value));
161 EXPECT_TRUE(
162 (::std::is_same<
163 const volatile char,
164 typename AddSourceVolatile<volatile int, const char>::Type>::value));
165 EXPECT_TRUE(
166 (::std::is_same<char,
167 typename AddSourceVolatile<int, char>::Type>::value));
168 EXPECT_TRUE(
169 (::std::is_same<
170 char, typename AddSourceVolatile<const int, char>::Type>::value));
171 EXPECT_TRUE(
172 (::std::is_same<volatile char, typename AddSourceVolatile<
173 int, volatile char>::Type>::value));
174 EXPECT_TRUE(
175 (::std::is_same<volatile char,
176 typename AddSourceVolatile<volatile int,
177 volatile char>::Type>::value));
178 }
179
TEST(AddSourceCV,AddSourceCV)180 TEST(AddSourceCV, AddSourceCV) {
181 EXPECT_TRUE(
182 (::std::is_same<const char,
183 typename AddSourceCV<const int, char>::Type>::value));
184 EXPECT_TRUE(
185 (::std::is_same<volatile char,
186 typename AddSourceCV<volatile int, char>::Type>::value));
187 EXPECT_TRUE((::std::is_same<
188 const volatile char,
189 typename AddSourceCV<volatile int, const char>::Type>::value));
190 EXPECT_TRUE((::std::is_same<
191 const volatile char,
192 typename AddSourceCV<const int, volatile char>::Type>::value));
193 EXPECT_TRUE((::std::is_same<
194 const volatile char,
195 typename AddSourceCV<const volatile int, char>::Type>::value));
196 EXPECT_TRUE(
197 (::std::is_same<char, typename AddSourceCV<int, char>::Type>::value));
198 EXPECT_TRUE(
199 (::std::is_same<volatile char,
200 typename AddSourceCV<int, volatile char>::Type>::value));
201 EXPECT_TRUE(
202 (::std::is_same<
203 volatile char,
204 typename AddSourceCV<volatile int, volatile char>::Type>::value));
205 }
206 } // namespace test
207 } // namespace support
208 } // namespace emboss
209