1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
13 #include <type_traits>
15 #include "test_macros.h"
18 struct correct_size_int
20 typedef typename
std::conditional
<sizeof(T
) < sizeof(int), int, T
>::type type
;
23 template <class Source
, class Result
>
30 ASSERT_SAME_TYPE(decltype(std::abs(neg_val
)), Result
);
32 assert(std::abs(neg_val
) == res
);
33 assert(std::abs(pos_val
) == res
);
38 long long int big_value
= std::numeric_limits
<long long int>::max(); // a value too big for ints to store
39 long long int negative_big_value
= -big_value
;
40 assert(std::abs(negative_big_value
) == big_value
); // make sure it doesn't get casted to a smaller type
43 // The following is helpful to keep in mind:
44 // 1byte == char <= short <= int <= long <= long long
48 // On some systems char is unsigned.
49 // If that is the case, we should just test signed char twice.
50 typedef std::conditional
<
51 std::is_signed
<char>::value
, char, signed char
54 // All types less than or equal to and not greater than int are promoted to int.
55 test_abs
<short int, int>();
56 test_abs
<SignedChar
, int>();
57 test_abs
<signed char, int>();
59 // These three calls have specific overloads:
61 test_abs
<long int, long int>();
62 test_abs
<long long int, long long int>();
64 // Here there is no guarantee that int is larger than int8_t so we
65 // use a helper type trait to conditional test against int.
66 test_abs
<std::int8_t, correct_size_int
<std::int8_t>::type
>();
67 test_abs
<std::int16_t, correct_size_int
<std::int16_t>::type
>();
68 test_abs
<std::int32_t, correct_size_int
<std::int32_t>::type
>();
69 test_abs
<std::int64_t, correct_size_int
<std::int64_t>::type
>();
71 test_abs
<long double, long double>();
72 test_abs
<double, double>();
73 test_abs
<float, float>();