brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.3 KiB · 51aee6e Raw
73 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include <assert.h>10#include <cmath>11#include <cstdint>12#include <limits>13#include <type_traits>14 15#include "test_macros.h"16 17template <class T>18struct correct_size_int {19  typedef typename std::conditional<sizeof(T) < sizeof(int), int, T>::type type;20};21 22template <class Source, class Result>23void test_abs() {24  Source neg_val = -5;25  Source pos_val = 5;26  Result res     = 5;27 28  ASSERT_SAME_TYPE(decltype(std::abs(neg_val)), Result);29 30  assert(std::abs(neg_val) == res);31  assert(std::abs(pos_val) == res);32}33 34void test_big() {35  long long int big_value          = std::numeric_limits<long long int>::max(); // a value too big for ints to store36  long long int negative_big_value = -big_value;37  assert(std::abs(negative_big_value) == big_value); // make sure it doesn't get casted to a smaller type38}39 40// The following is helpful to keep in mind:41// 1byte == char <= short <= int <= long <= long long42 43int main(int, char**) {44  // On some systems char is unsigned.45  // If that is the case, we should just test signed char twice.46  typedef std::conditional< std::is_signed<char>::value, char, signed char >::type SignedChar;47 48  // All types less than or equal to and not greater than int are promoted to int.49  test_abs<short int, int>();50  test_abs<SignedChar, int>();51  test_abs<signed char, int>();52 53  // These three calls have specific overloads:54  test_abs<int, int>();55  test_abs<long int, long int>();56  test_abs<long long int, long long int>();57 58  // Here there is no guarantee that int is larger than int8_t so we59  // use a helper type trait to conditional test against int.60  test_abs<std::int8_t, correct_size_int<std::int8_t>::type>();61  test_abs<std::int16_t, correct_size_int<std::int16_t>::type>();62  test_abs<std::int32_t, correct_size_int<std::int32_t>::type>();63  test_abs<std::int64_t, correct_size_int<std::int64_t>::type>();64 65  test_abs<long double, long double>();66  test_abs<double, double>();67  test_abs<float, float>();68 69  test_big();70 71  return 0;72}73