[yaml2obj/obj2yaml] - Add support for .stack_sizes sections.
[llvm-complete.git] / unittests / Support / MathExtrasTest.cpp
blob01c83c9e14d3f7250af93ff88d562446d7f5c0fc
1 //===- unittests/Support/MathExtrasTest.cpp - math utils tests ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/MathExtras.h"
10 #include "gtest/gtest.h"
12 using namespace llvm;
14 namespace {
16 TEST(MathExtras, countTrailingZeros) {
17 uint8_t Z8 = 0;
18 uint16_t Z16 = 0;
19 uint32_t Z32 = 0;
20 uint64_t Z64 = 0;
21 EXPECT_EQ(8u, countTrailingZeros(Z8));
22 EXPECT_EQ(16u, countTrailingZeros(Z16));
23 EXPECT_EQ(32u, countTrailingZeros(Z32));
24 EXPECT_EQ(64u, countTrailingZeros(Z64));
26 uint8_t NZ8 = 42;
27 uint16_t NZ16 = 42;
28 uint32_t NZ32 = 42;
29 uint64_t NZ64 = 42;
30 EXPECT_EQ(1u, countTrailingZeros(NZ8));
31 EXPECT_EQ(1u, countTrailingZeros(NZ16));
32 EXPECT_EQ(1u, countTrailingZeros(NZ32));
33 EXPECT_EQ(1u, countTrailingZeros(NZ64));
36 TEST(MathExtras, countLeadingZeros) {
37 uint8_t Z8 = 0;
38 uint16_t Z16 = 0;
39 uint32_t Z32 = 0;
40 uint64_t Z64 = 0;
41 EXPECT_EQ(8u, countLeadingZeros(Z8));
42 EXPECT_EQ(16u, countLeadingZeros(Z16));
43 EXPECT_EQ(32u, countLeadingZeros(Z32));
44 EXPECT_EQ(64u, countLeadingZeros(Z64));
46 uint8_t NZ8 = 42;
47 uint16_t NZ16 = 42;
48 uint32_t NZ32 = 42;
49 uint64_t NZ64 = 42;
50 EXPECT_EQ(2u, countLeadingZeros(NZ8));
51 EXPECT_EQ(10u, countLeadingZeros(NZ16));
52 EXPECT_EQ(26u, countLeadingZeros(NZ32));
53 EXPECT_EQ(58u, countLeadingZeros(NZ64));
55 EXPECT_EQ(8u, countLeadingZeros(0x00F000FFu));
56 EXPECT_EQ(8u, countLeadingZeros(0x00F12345u));
57 for (unsigned i = 0; i <= 30; ++i) {
58 EXPECT_EQ(31 - i, countLeadingZeros(1u << i));
61 EXPECT_EQ(8u, countLeadingZeros(0x00F1234500F12345ULL));
62 EXPECT_EQ(1u, countLeadingZeros(1ULL << 62));
63 for (unsigned i = 0; i <= 62; ++i) {
64 EXPECT_EQ(63 - i, countLeadingZeros(1ULL << i));
68 TEST(MathExtras, onesMask) {
69 EXPECT_EQ(0U, maskLeadingOnes<uint8_t>(0));
70 EXPECT_EQ(0U, maskTrailingOnes<uint8_t>(0));
71 EXPECT_EQ(0U, maskLeadingOnes<uint16_t>(0));
72 EXPECT_EQ(0U, maskTrailingOnes<uint16_t>(0));
73 EXPECT_EQ(0U, maskLeadingOnes<uint32_t>(0));
74 EXPECT_EQ(0U, maskTrailingOnes<uint32_t>(0));
75 EXPECT_EQ(0U, maskLeadingOnes<uint64_t>(0));
76 EXPECT_EQ(0U, maskTrailingOnes<uint64_t>(0));
78 EXPECT_EQ(0x00000003U, maskTrailingOnes<uint32_t>(2U));
79 EXPECT_EQ(0xC0000000U, maskLeadingOnes<uint32_t>(2U));
81 EXPECT_EQ(0x000007FFU, maskTrailingOnes<uint32_t>(11U));
82 EXPECT_EQ(0xFFE00000U, maskLeadingOnes<uint32_t>(11U));
84 EXPECT_EQ(0xFFFFFFFFU, maskTrailingOnes<uint32_t>(32U));
85 EXPECT_EQ(0xFFFFFFFFU, maskLeadingOnes<uint32_t>(32U));
86 EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, maskTrailingOnes<uint64_t>(64U));
87 EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, maskLeadingOnes<uint64_t>(64U));
89 EXPECT_EQ(0x0000FFFFFFFFFFFFULL, maskTrailingOnes<uint64_t>(48U));
90 EXPECT_EQ(0xFFFFFFFFFFFF0000ULL, maskLeadingOnes<uint64_t>(48U));
93 TEST(MathExtras, findFirstSet) {
94 uint8_t Z8 = 0;
95 uint16_t Z16 = 0;
96 uint32_t Z32 = 0;
97 uint64_t Z64 = 0;
98 EXPECT_EQ(0xFFULL, findFirstSet(Z8));
99 EXPECT_EQ(0xFFFFULL, findFirstSet(Z16));
100 EXPECT_EQ(0xFFFFFFFFULL, findFirstSet(Z32));
101 EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, findFirstSet(Z64));
103 uint8_t NZ8 = 42;
104 uint16_t NZ16 = 42;
105 uint32_t NZ32 = 42;
106 uint64_t NZ64 = 42;
107 EXPECT_EQ(1u, findFirstSet(NZ8));
108 EXPECT_EQ(1u, findFirstSet(NZ16));
109 EXPECT_EQ(1u, findFirstSet(NZ32));
110 EXPECT_EQ(1u, findFirstSet(NZ64));
113 TEST(MathExtras, findLastSet) {
114 uint8_t Z8 = 0;
115 uint16_t Z16 = 0;
116 uint32_t Z32 = 0;
117 uint64_t Z64 = 0;
118 EXPECT_EQ(0xFFULL, findLastSet(Z8));
119 EXPECT_EQ(0xFFFFULL, findLastSet(Z16));
120 EXPECT_EQ(0xFFFFFFFFULL, findLastSet(Z32));
121 EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, findLastSet(Z64));
123 uint8_t NZ8 = 42;
124 uint16_t NZ16 = 42;
125 uint32_t NZ32 = 42;
126 uint64_t NZ64 = 42;
127 EXPECT_EQ(5u, findLastSet(NZ8));
128 EXPECT_EQ(5u, findLastSet(NZ16));
129 EXPECT_EQ(5u, findLastSet(NZ32));
130 EXPECT_EQ(5u, findLastSet(NZ64));
133 TEST(MathExtras, isIntN) {
134 EXPECT_TRUE(isIntN(16, 32767));
135 EXPECT_FALSE(isIntN(16, 32768));
138 TEST(MathExtras, isUIntN) {
139 EXPECT_TRUE(isUIntN(16, 65535));
140 EXPECT_FALSE(isUIntN(16, 65536));
141 EXPECT_TRUE(isUIntN(1, 0));
142 EXPECT_TRUE(isUIntN(6, 63));
145 TEST(MathExtras, maxIntN) {
146 EXPECT_EQ(32767, maxIntN(16));
147 EXPECT_EQ(2147483647, maxIntN(32));
148 EXPECT_EQ(std::numeric_limits<int32_t>::max(), maxIntN(32));
149 EXPECT_EQ(std::numeric_limits<int64_t>::max(), maxIntN(64));
152 TEST(MathExtras, minIntN) {
153 EXPECT_EQ(-32768LL, minIntN(16));
154 EXPECT_EQ(-64LL, minIntN(7));
155 EXPECT_EQ(std::numeric_limits<int32_t>::min(), minIntN(32));
156 EXPECT_EQ(std::numeric_limits<int64_t>::min(), minIntN(64));
159 TEST(MathExtras, maxUIntN) {
160 EXPECT_EQ(0xffffULL, maxUIntN(16));
161 EXPECT_EQ(0xffffffffULL, maxUIntN(32));
162 EXPECT_EQ(0xffffffffffffffffULL, maxUIntN(64));
163 EXPECT_EQ(1ULL, maxUIntN(1));
164 EXPECT_EQ(0x0fULL, maxUIntN(4));
167 TEST(MathExtras, reverseBits) {
168 uint8_t NZ8 = 42;
169 uint16_t NZ16 = 42;
170 uint32_t NZ32 = 42;
171 uint64_t NZ64 = 42;
172 EXPECT_EQ(0x54ULL, reverseBits(NZ8));
173 EXPECT_EQ(0x5400ULL, reverseBits(NZ16));
174 EXPECT_EQ(0x54000000ULL, reverseBits(NZ32));
175 EXPECT_EQ(0x5400000000000000ULL, reverseBits(NZ64));
178 TEST(MathExtras, isPowerOf2_32) {
179 EXPECT_FALSE(isPowerOf2_32(0));
180 EXPECT_TRUE(isPowerOf2_32(1 << 6));
181 EXPECT_TRUE(isPowerOf2_32(1 << 12));
182 EXPECT_FALSE(isPowerOf2_32((1 << 19) + 3));
183 EXPECT_FALSE(isPowerOf2_32(0xABCDEF0));
186 TEST(MathExtras, isPowerOf2_64) {
187 EXPECT_FALSE(isPowerOf2_64(0));
188 EXPECT_TRUE(isPowerOf2_64(1LL << 46));
189 EXPECT_TRUE(isPowerOf2_64(1LL << 12));
190 EXPECT_FALSE(isPowerOf2_64((1LL << 53) + 3));
191 EXPECT_FALSE(isPowerOf2_64(0xABCDEF0ABCDEF0LL));
194 TEST(MathExtras, PowerOf2Ceil) {
195 EXPECT_EQ(0U, PowerOf2Ceil(0U));
196 EXPECT_EQ(8U, PowerOf2Ceil(8U));
197 EXPECT_EQ(8U, PowerOf2Ceil(7U));
200 TEST(MathExtras, PowerOf2Floor) {
201 EXPECT_EQ(0U, PowerOf2Floor(0U));
202 EXPECT_EQ(8U, PowerOf2Floor(8U));
203 EXPECT_EQ(4U, PowerOf2Floor(7U));
206 TEST(MathExtras, ByteSwap_32) {
207 EXPECT_EQ(0x44332211u, ByteSwap_32(0x11223344));
208 EXPECT_EQ(0xDDCCBBAAu, ByteSwap_32(0xAABBCCDD));
211 TEST(MathExtras, ByteSwap_64) {
212 EXPECT_EQ(0x8877665544332211ULL, ByteSwap_64(0x1122334455667788LL));
213 EXPECT_EQ(0x1100FFEEDDCCBBAAULL, ByteSwap_64(0xAABBCCDDEEFF0011LL));
216 TEST(MathExtras, countLeadingOnes) {
217 for (int i = 30; i >= 0; --i) {
218 // Start with all ones and unset some bit.
219 EXPECT_EQ(31u - i, countLeadingOnes(0xFFFFFFFF ^ (1 << i)));
221 for (int i = 62; i >= 0; --i) {
222 // Start with all ones and unset some bit.
223 EXPECT_EQ(63u - i, countLeadingOnes(0xFFFFFFFFFFFFFFFFULL ^ (1LL << i)));
225 for (int i = 30; i >= 0; --i) {
226 // Start with all ones and unset some bit.
227 EXPECT_EQ(31u - i, countLeadingOnes(0xFFFFFFFF ^ (1 << i)));
231 TEST(MathExtras, FloatBits) {
232 static const float kValue = 5632.34f;
233 EXPECT_FLOAT_EQ(kValue, BitsToFloat(FloatToBits(kValue)));
236 TEST(MathExtras, DoubleBits) {
237 static const double kValue = 87987234.983498;
238 EXPECT_DOUBLE_EQ(kValue, BitsToDouble(DoubleToBits(kValue)));
241 TEST(MathExtras, MinAlign) {
242 EXPECT_EQ(1u, MinAlign(2, 3));
243 EXPECT_EQ(2u, MinAlign(2, 4));
244 EXPECT_EQ(1u, MinAlign(17, 64));
245 EXPECT_EQ(256u, MinAlign(256, 512));
248 TEST(MathExtras, NextPowerOf2) {
249 EXPECT_EQ(4u, NextPowerOf2(3));
250 EXPECT_EQ(16u, NextPowerOf2(15));
251 EXPECT_EQ(256u, NextPowerOf2(128));
254 TEST(MathExtras, alignTo) {
255 EXPECT_EQ(8u, alignTo(5, 8));
256 EXPECT_EQ(24u, alignTo(17, 8));
257 EXPECT_EQ(0u, alignTo(~0LL, 8));
259 EXPECT_EQ(7u, alignTo(5, 8, 7));
260 EXPECT_EQ(17u, alignTo(17, 8, 1));
261 EXPECT_EQ(3u, alignTo(~0LL, 8, 3));
262 EXPECT_EQ(552u, alignTo(321, 255, 42));
265 template<typename T>
266 void SaturatingAddTestHelper()
268 const T Max = std::numeric_limits<T>::max();
269 bool ResultOverflowed;
271 EXPECT_EQ(T(3), SaturatingAdd(T(1), T(2)));
272 EXPECT_EQ(T(3), SaturatingAdd(T(1), T(2), &ResultOverflowed));
273 EXPECT_FALSE(ResultOverflowed);
275 EXPECT_EQ(Max, SaturatingAdd(Max, T(1)));
276 EXPECT_EQ(Max, SaturatingAdd(Max, T(1), &ResultOverflowed));
277 EXPECT_TRUE(ResultOverflowed);
279 EXPECT_EQ(Max, SaturatingAdd(T(1), T(Max - 1)));
280 EXPECT_EQ(Max, SaturatingAdd(T(1), T(Max - 1), &ResultOverflowed));
281 EXPECT_FALSE(ResultOverflowed);
283 EXPECT_EQ(Max, SaturatingAdd(T(1), Max));
284 EXPECT_EQ(Max, SaturatingAdd(T(1), Max, &ResultOverflowed));
285 EXPECT_TRUE(ResultOverflowed);
287 EXPECT_EQ(Max, SaturatingAdd(Max, Max));
288 EXPECT_EQ(Max, SaturatingAdd(Max, Max, &ResultOverflowed));
289 EXPECT_TRUE(ResultOverflowed);
292 TEST(MathExtras, SaturatingAdd) {
293 SaturatingAddTestHelper<uint8_t>();
294 SaturatingAddTestHelper<uint16_t>();
295 SaturatingAddTestHelper<uint32_t>();
296 SaturatingAddTestHelper<uint64_t>();
299 template<typename T>
300 void SaturatingMultiplyTestHelper()
302 const T Max = std::numeric_limits<T>::max();
303 bool ResultOverflowed;
305 // Test basic multiplication.
306 EXPECT_EQ(T(6), SaturatingMultiply(T(2), T(3)));
307 EXPECT_EQ(T(6), SaturatingMultiply(T(2), T(3), &ResultOverflowed));
308 EXPECT_FALSE(ResultOverflowed);
310 EXPECT_EQ(T(6), SaturatingMultiply(T(3), T(2)));
311 EXPECT_EQ(T(6), SaturatingMultiply(T(3), T(2), &ResultOverflowed));
312 EXPECT_FALSE(ResultOverflowed);
314 // Test multiplication by zero.
315 EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(0)));
316 EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(0), &ResultOverflowed));
317 EXPECT_FALSE(ResultOverflowed);
319 EXPECT_EQ(T(0), SaturatingMultiply(T(1), T(0)));
320 EXPECT_EQ(T(0), SaturatingMultiply(T(1), T(0), &ResultOverflowed));
321 EXPECT_FALSE(ResultOverflowed);
323 EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(1)));
324 EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(1), &ResultOverflowed));
325 EXPECT_FALSE(ResultOverflowed);
327 EXPECT_EQ(T(0), SaturatingMultiply(Max, T(0)));
328 EXPECT_EQ(T(0), SaturatingMultiply(Max, T(0), &ResultOverflowed));
329 EXPECT_FALSE(ResultOverflowed);
331 EXPECT_EQ(T(0), SaturatingMultiply(T(0), Max));
332 EXPECT_EQ(T(0), SaturatingMultiply(T(0), Max, &ResultOverflowed));
333 EXPECT_FALSE(ResultOverflowed);
335 // Test multiplication by maximum value.
336 EXPECT_EQ(Max, SaturatingMultiply(Max, T(2)));
337 EXPECT_EQ(Max, SaturatingMultiply(Max, T(2), &ResultOverflowed));
338 EXPECT_TRUE(ResultOverflowed);
340 EXPECT_EQ(Max, SaturatingMultiply(T(2), Max));
341 EXPECT_EQ(Max, SaturatingMultiply(T(2), Max, &ResultOverflowed));
342 EXPECT_TRUE(ResultOverflowed);
344 EXPECT_EQ(Max, SaturatingMultiply(Max, Max));
345 EXPECT_EQ(Max, SaturatingMultiply(Max, Max, &ResultOverflowed));
346 EXPECT_TRUE(ResultOverflowed);
348 // Test interesting boundary conditions for algorithm -
349 // ((1 << A) - 1) * ((1 << B) + K) for K in [-1, 0, 1]
350 // and A + B == std::numeric_limits<T>::digits.
351 // We expect overflow iff A > B and K = 1.
352 const int Digits = std::numeric_limits<T>::digits;
353 for (int A = 1, B = Digits - 1; B >= 1; ++A, --B) {
354 for (int K = -1; K <= 1; ++K) {
355 T X = (T(1) << A) - T(1);
356 T Y = (T(1) << B) + K;
357 bool OverflowExpected = A > B && K == 1;
359 if(OverflowExpected) {
360 EXPECT_EQ(Max, SaturatingMultiply(X, Y));
361 EXPECT_EQ(Max, SaturatingMultiply(X, Y, &ResultOverflowed));
362 EXPECT_TRUE(ResultOverflowed);
363 } else {
364 EXPECT_EQ(X * Y, SaturatingMultiply(X, Y));
365 EXPECT_EQ(X * Y, SaturatingMultiply(X, Y, &ResultOverflowed));
366 EXPECT_FALSE(ResultOverflowed);
372 TEST(MathExtras, SaturatingMultiply) {
373 SaturatingMultiplyTestHelper<uint8_t>();
374 SaturatingMultiplyTestHelper<uint16_t>();
375 SaturatingMultiplyTestHelper<uint32_t>();
376 SaturatingMultiplyTestHelper<uint64_t>();
379 template<typename T>
380 void SaturatingMultiplyAddTestHelper()
382 const T Max = std::numeric_limits<T>::max();
383 bool ResultOverflowed;
385 // Test basic multiply-add.
386 EXPECT_EQ(T(16), SaturatingMultiplyAdd(T(2), T(3), T(10)));
387 EXPECT_EQ(T(16), SaturatingMultiplyAdd(T(2), T(3), T(10), &ResultOverflowed));
388 EXPECT_FALSE(ResultOverflowed);
390 // Test multiply overflows, add doesn't overflow
391 EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, T(0), &ResultOverflowed));
392 EXPECT_TRUE(ResultOverflowed);
394 // Test multiply doesn't overflow, add overflows
395 EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), T(1), Max, &ResultOverflowed));
396 EXPECT_TRUE(ResultOverflowed);
398 // Test multiply-add with Max as operand
399 EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), T(1), Max, &ResultOverflowed));
400 EXPECT_TRUE(ResultOverflowed);
402 EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), Max, T(1), &ResultOverflowed));
403 EXPECT_TRUE(ResultOverflowed);
405 EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, T(1), &ResultOverflowed));
406 EXPECT_TRUE(ResultOverflowed);
408 EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, Max, &ResultOverflowed));
409 EXPECT_TRUE(ResultOverflowed);
411 // Test multiply-add with 0 as operand
412 EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(1), T(0), &ResultOverflowed));
413 EXPECT_FALSE(ResultOverflowed);
415 EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(0), T(1), &ResultOverflowed));
416 EXPECT_FALSE(ResultOverflowed);
418 EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(0), T(0), T(1), &ResultOverflowed));
419 EXPECT_FALSE(ResultOverflowed);
421 EXPECT_EQ(T(0), SaturatingMultiplyAdd(T(0), T(0), T(0), &ResultOverflowed));
422 EXPECT_FALSE(ResultOverflowed);
426 TEST(MathExtras, SaturatingMultiplyAdd) {
427 SaturatingMultiplyAddTestHelper<uint8_t>();
428 SaturatingMultiplyAddTestHelper<uint16_t>();
429 SaturatingMultiplyAddTestHelper<uint32_t>();
430 SaturatingMultiplyAddTestHelper<uint64_t>();
433 TEST(MathExtras, IsShiftedUInt) {
434 EXPECT_TRUE((isShiftedUInt<1, 0>(0)));
435 EXPECT_TRUE((isShiftedUInt<1, 0>(1)));
436 EXPECT_FALSE((isShiftedUInt<1, 0>(2)));
437 EXPECT_FALSE((isShiftedUInt<1, 0>(3)));
438 EXPECT_FALSE((isShiftedUInt<1, 0>(0x8000000000000000)));
439 EXPECT_TRUE((isShiftedUInt<1, 63>(0x8000000000000000)));
440 EXPECT_TRUE((isShiftedUInt<2, 62>(0xC000000000000000)));
441 EXPECT_FALSE((isShiftedUInt<2, 62>(0xE000000000000000)));
443 // 0x201 is ten bits long and has a 1 in the MSB and LSB.
444 EXPECT_TRUE((isShiftedUInt<10, 5>(uint64_t(0x201) << 5)));
445 EXPECT_FALSE((isShiftedUInt<10, 5>(uint64_t(0x201) << 4)));
446 EXPECT_FALSE((isShiftedUInt<10, 5>(uint64_t(0x201) << 6)));
449 TEST(MathExtras, IsShiftedInt) {
450 EXPECT_TRUE((isShiftedInt<1, 0>(0)));
451 EXPECT_TRUE((isShiftedInt<1, 0>(-1)));
452 EXPECT_FALSE((isShiftedInt<1, 0>(2)));
453 EXPECT_FALSE((isShiftedInt<1, 0>(3)));
454 EXPECT_FALSE((isShiftedInt<1, 0>(0x8000000000000000)));
455 EXPECT_TRUE((isShiftedInt<1, 63>(0x8000000000000000)));
456 EXPECT_TRUE((isShiftedInt<2, 62>(0xC000000000000000)));
457 EXPECT_FALSE((isShiftedInt<2, 62>(0xE000000000000000)));
459 // 0x201 is ten bits long and has a 1 in the MSB and LSB.
460 EXPECT_TRUE((isShiftedInt<11, 5>(int64_t(0x201) << 5)));
461 EXPECT_FALSE((isShiftedInt<11, 5>(int64_t(0x201) << 3)));
462 EXPECT_FALSE((isShiftedInt<11, 5>(int64_t(0x201) << 6)));
463 EXPECT_TRUE((isShiftedInt<11, 5>(-(int64_t(0x201) << 5))));
464 EXPECT_FALSE((isShiftedInt<11, 5>(-(int64_t(0x201) << 3))));
465 EXPECT_FALSE((isShiftedInt<11, 5>(-(int64_t(0x201) << 6))));
467 EXPECT_TRUE((isShiftedInt<6, 10>(-(int64_t(1) << 15))));
468 EXPECT_FALSE((isShiftedInt<6, 10>(int64_t(1) << 15)));
471 template <typename T>
472 class OverflowTest : public ::testing::Test { };
474 using OverflowTestTypes = ::testing::Types<signed char, short, int, long,
475 long long>;
477 TYPED_TEST_CASE(OverflowTest, OverflowTestTypes);
479 TYPED_TEST(OverflowTest, AddNoOverflow) {
480 TypeParam Result;
481 EXPECT_FALSE(AddOverflow<TypeParam>(1, 2, Result));
482 EXPECT_EQ(Result, TypeParam(3));
485 TYPED_TEST(OverflowTest, AddOverflowToNegative) {
486 TypeParam Result;
487 auto MaxValue = std::numeric_limits<TypeParam>::max();
488 EXPECT_TRUE(AddOverflow<TypeParam>(MaxValue, MaxValue, Result));
489 EXPECT_EQ(Result, TypeParam(-2));
492 TYPED_TEST(OverflowTest, AddOverflowToMin) {
493 TypeParam Result;
494 auto MaxValue = std::numeric_limits<TypeParam>::max();
495 EXPECT_TRUE(AddOverflow<TypeParam>(MaxValue, TypeParam(1), Result));
496 EXPECT_EQ(Result, std::numeric_limits<TypeParam>::min());
499 TYPED_TEST(OverflowTest, AddOverflowToZero) {
500 TypeParam Result;
501 auto MinValue = std::numeric_limits<TypeParam>::min();
502 EXPECT_TRUE(AddOverflow<TypeParam>(MinValue, MinValue, Result));
503 EXPECT_EQ(Result, TypeParam(0));
506 TYPED_TEST(OverflowTest, AddOverflowToMax) {
507 TypeParam Result;
508 auto MinValue = std::numeric_limits<TypeParam>::min();
509 EXPECT_TRUE(AddOverflow<TypeParam>(MinValue, TypeParam(-1), Result));
510 EXPECT_EQ(Result, std::numeric_limits<TypeParam>::max());
513 TYPED_TEST(OverflowTest, SubNoOverflow) {
514 TypeParam Result;
515 EXPECT_FALSE(SubOverflow<TypeParam>(1, 2, Result));
516 EXPECT_EQ(Result, TypeParam(-1));
519 TYPED_TEST(OverflowTest, SubOverflowToMax) {
520 TypeParam Result;
521 auto MinValue = std::numeric_limits<TypeParam>::min();
522 EXPECT_TRUE(SubOverflow<TypeParam>(0, MinValue, Result));
523 EXPECT_EQ(Result, MinValue);
526 TYPED_TEST(OverflowTest, SubOverflowToMin) {
527 TypeParam Result;
528 auto MinValue = std::numeric_limits<TypeParam>::min();
529 EXPECT_TRUE(SubOverflow<TypeParam>(0, MinValue, Result));
530 EXPECT_EQ(Result, MinValue);
533 TYPED_TEST(OverflowTest, SubOverflowToNegative) {
534 TypeParam Result;
535 auto MaxValue = std::numeric_limits<TypeParam>::max();
536 auto MinValue = std::numeric_limits<TypeParam>::min();
537 EXPECT_TRUE(SubOverflow<TypeParam>(MaxValue, MinValue, Result));
538 EXPECT_EQ(Result, TypeParam(-1));
541 TYPED_TEST(OverflowTest, SubOverflowToPositive) {
542 TypeParam Result;
543 auto MaxValue = std::numeric_limits<TypeParam>::max();
544 auto MinValue = std::numeric_limits<TypeParam>::min();
545 EXPECT_TRUE(SubOverflow<TypeParam>(MinValue, MaxValue, Result));
546 EXPECT_EQ(Result, TypeParam(1));
549 TYPED_TEST(OverflowTest, MulNoOverflow) {
550 TypeParam Result;
551 EXPECT_FALSE(MulOverflow<TypeParam>(1, 2, Result));
552 EXPECT_EQ(Result, 2);
553 EXPECT_FALSE(MulOverflow<TypeParam>(-1, 3, Result));
554 EXPECT_EQ(Result, -3);
555 EXPECT_FALSE(MulOverflow<TypeParam>(4, -2, Result));
556 EXPECT_EQ(Result, -8);
557 EXPECT_FALSE(MulOverflow<TypeParam>(-6, -5, Result));
558 EXPECT_EQ(Result, 30);
561 TYPED_TEST(OverflowTest, MulNoOverflowToMax) {
562 TypeParam Result;
563 auto MaxValue = std::numeric_limits<TypeParam>::max();
564 auto MinValue = std::numeric_limits<TypeParam>::min();
565 EXPECT_FALSE(MulOverflow<TypeParam>(MinValue + 1, -1, Result));
566 EXPECT_EQ(Result, MaxValue);
569 TYPED_TEST(OverflowTest, MulOverflowToMin) {
570 TypeParam Result;
571 auto MinValue = std::numeric_limits<TypeParam>::min();
572 EXPECT_TRUE(MulOverflow<TypeParam>(MinValue, -1, Result));
573 EXPECT_EQ(Result, MinValue);
576 TYPED_TEST(OverflowTest, MulOverflowMax) {
577 TypeParam Result;
578 auto MinValue = std::numeric_limits<TypeParam>::min();
579 auto MaxValue = std::numeric_limits<TypeParam>::max();
580 EXPECT_TRUE(MulOverflow<TypeParam>(MinValue, MinValue, Result));
581 EXPECT_EQ(Result, 0);
582 EXPECT_TRUE(MulOverflow<TypeParam>(MaxValue, MaxValue, Result));
583 EXPECT_EQ(Result, 1);
586 TYPED_TEST(OverflowTest, MulResultZero) {
587 TypeParam Result;
588 EXPECT_FALSE(MulOverflow<TypeParam>(4, 0, Result));
589 EXPECT_EQ(Result, TypeParam(0));
590 EXPECT_FALSE(MulOverflow<TypeParam>(-5, 0, Result));
591 EXPECT_EQ(Result, TypeParam(0));
592 EXPECT_FALSE(MulOverflow<TypeParam>(0, 5, Result));
593 EXPECT_EQ(Result, TypeParam(0));
594 EXPECT_FALSE(MulOverflow<TypeParam>(0, -5, Result));
595 EXPECT_EQ(Result, TypeParam(0));
598 } // namespace