brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.8 KiB · 28c39f4 Raw
92 lines · c
1//===-- Utility class to test idivfx functions ------------------*- C++ -*-===//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 "test/UnitTest/Test.h"10 11#include "src/__support/fixed_point/fx_rep.h"12#include "src/__support/macros/sanitizer.h"13 14#include "hdr/signal_macros.h"15 16template <typename T, typename XType>17class IdivTest : public LIBC_NAMESPACE::testing::Test {18 19  using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<T>;20 21  static constexpr T zero = FXRep::ZERO();22  static constexpr T max = FXRep::MAX();23  static constexpr T min = FXRep::MIN();24  static constexpr T one_half = FXRep::ONE_HALF();25  static constexpr T one_fourth = FXRep::ONE_FOURTH();26 27public:28  typedef XType (*IdivFunc)(T, T);29 30  void testSpecialNumbers(IdivFunc func) {31    constexpr bool is_signed = (FXRep::SIGN_LEN > 0);32    constexpr bool has_integral = (FXRep::INTEGRAL_LEN > 0);33 34    EXPECT_EQ(func(one_half, one_fourth), static_cast<XType>(2));35    EXPECT_EQ(func(one_half, one_half), static_cast<XType>(1));36    EXPECT_EQ(func(one_fourth, one_half), static_cast<XType>(0));37    EXPECT_EQ(func(0.75, 0.25), static_cast<XType>(3));38    EXPECT_EQ(func(0.625, 0.125), static_cast<XType>(5));39 40    if constexpr (is_signed) {41      EXPECT_EQ(func(min, one_half), static_cast<XType>(min) * 2);42    } else {43      EXPECT_EQ(func(min, one_half), static_cast<XType>(0));44    }45 46    if constexpr (has_integral && min <= 7 && max >= 5) {47      EXPECT_EQ(func(6.9, 4.2), static_cast<XType>(1));48      EXPECT_EQ(func(4.2, 6.9), static_cast<XType>(0));49      EXPECT_EQ(func(4.5, 2.2), static_cast<XType>(2));50      EXPECT_EQ(func(2.2, 1.1), static_cast<XType>(2));51      EXPECT_EQ(func(2.25, 1.0), static_cast<XType>(2));52      EXPECT_EQ(func(2.25, 3.0), static_cast<XType>(0));53 54      if constexpr (is_signed) {55        EXPECT_EQ(func(4.2, -6.9), static_cast<XType>(0));56        EXPECT_EQ(func(-6.9, 4.2), static_cast<XType>(-1));57        EXPECT_EQ(func(-2.5, 1.25), static_cast<XType>(-2));58        EXPECT_EQ(func(-2.25, 1.0), static_cast<XType>(-2));59        EXPECT_EQ(func(2.25, -3.0), static_cast<XType>(0));60      }61    }62  }63 64  void testInvalidNumbers(IdivFunc func) {65    constexpr bool has_integral = (FXRep::INTEGRAL_LEN > 0);66 67    EXPECT_DEATH([func] { func(0.5, 0.0); }, WITH_SIGNAL(-1));68    if constexpr (has_integral) {69      EXPECT_DEATH([func] { func(2.5, 0.0); }, WITH_SIGNAL(-1));70    }71  }72};73 74#if defined(LIBC_ADD_NULL_CHECKS)75#define LIST_IDIV_TESTS(Name, T, XType, func)                                  \76  using LlvmLibcIdiv##Name##Test = IdivTest<T, XType>;                         \77  TEST_F(LlvmLibcIdiv##Name##Test, InvalidNumbers) {                           \78    testInvalidNumbers(&func);                                                 \79  }                                                                            \80  TEST_F(LlvmLibcIdiv##Name##Test, SpecialNumbers) {                           \81    testSpecialNumbers(&func);                                                 \82  }                                                                            \83  static_assert(true, "Require semicolon.")84#else85#define LIST_IDIV_TESTS(Name, T, XType, func)                                  \86  using LlvmLibcIdiv##Name##Test = IdivTest<T, XType>;                         \87  TEST_F(LlvmLibcIdiv##Name##Test, SpecialNumbers) {                           \88    testSpecialNumbers(&func);                                                 \89  }                                                                            \90  static_assert(true, "Require semicolon.")91#endif // LIBC_ADD_NULL_CHECKS92