summaryrefslogtreecommitdiff
path: root/test/src/math/ModfTest.h
blob: 84e26db49695d54812a2fa0756ee1c9976d70e74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//===-- Utility class to test floor[f|l] ------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/FPUtil/BasicOperations.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include "utils/MPFRWrapper/MPFRUtils.h"

#include "include/llvm-libc-macros/math-macros.h"

namespace mpfr = LIBC_NAMESPACE::testing::mpfr;

template <typename T> class ModfTest : public LIBC_NAMESPACE::testing::Test {

  DECLARE_SPECIAL_CONSTANTS(T)

public:
  typedef T (*ModfFunc)(T, T *);

  void testSpecialNumbers(ModfFunc func) {
    T integral;

    EXPECT_FP_EQ(zero, func(zero, &integral));
    EXPECT_FP_EQ(integral, zero);
    EXPECT_FP_EQ(neg_zero, func(neg_zero, &integral));
    EXPECT_FP_EQ(integral, neg_zero);

    EXPECT_FP_EQ(zero, func(inf, &integral));
    EXPECT_FP_EQ(inf, integral);
    EXPECT_FP_EQ(neg_zero, func(neg_inf, &integral));
    EXPECT_FP_EQ(neg_inf, integral);

    EXPECT_FP_EQ(aNaN, func(aNaN, &integral));
  }

  void testIntegers(ModfFunc func) {
    T integral;

    EXPECT_FP_EQ(T(0.0), func(T(1.0), &integral));
    EXPECT_FP_EQ(T(1.0), integral);

    EXPECT_FP_EQ(T(-0.0), func(T(-1.0), &integral));
    EXPECT_FP_EQ(T(-1.0), integral);

    EXPECT_FP_EQ(T(0.0), func(T(10.0), &integral));
    EXPECT_FP_EQ(T(10.0), integral);

    EXPECT_FP_EQ(T(-0.0), func(T(-10.0), &integral));
    EXPECT_FP_EQ(T(-10.0), integral);

    EXPECT_FP_EQ(T(0.0), func(T(12345.0), &integral));
    EXPECT_FP_EQ(T(12345.0), integral);

    EXPECT_FP_EQ(T(-0.0), func(T(-12345.0), &integral));
    EXPECT_FP_EQ(T(-12345.0), integral);
  }

  void testFractions(ModfFunc func) {
    T integral;

    EXPECT_FP_EQ(T(0.5), func(T(1.5), &integral));
    EXPECT_FP_EQ(integral, T(1.0));

    EXPECT_FP_EQ(T(-0.5), func(T(-1.5), &integral));
    EXPECT_FP_EQ(integral, T(-1.0));

    EXPECT_FP_EQ(T(0.75), func(T(10.75), &integral));
    EXPECT_FP_EQ(integral, T(10.0));

    EXPECT_FP_EQ(T(-0.75), func(T(-10.75), &integral));
    EXPECT_FP_EQ(integral, T(-10.0));

    EXPECT_FP_EQ(T(0.125), func(T(100.125), &integral));
    EXPECT_FP_EQ(integral, T(100.0));

    EXPECT_FP_EQ(T(-0.125), func(T(-100.125), &integral));
    EXPECT_FP_EQ(integral, T(-100.0));
  }

  void testRange(ModfFunc func) {
    constexpr StorageType COUNT = 100'000;
    constexpr StorageType STEP = STORAGE_MAX / COUNT;
    for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
      T x = FPBits(v).get_val();
      if (isnan(x) || isinf(x) || x == T(0.0))
        continue;

      T integral;
      T frac = func(x, &integral);
      ASSERT_TRUE(LIBC_NAMESPACE::fputil::abs(frac) < 1.0l);
      ASSERT_TRUE(LIBC_NAMESPACE::fputil::trunc(x) == integral);
      ASSERT_TRUE(integral + frac == x);
    }
  }
};

#define LIST_MODF_TESTS(T, func)                                               \
  using LlvmLibcModfTest = ModfTest<T>;                                        \
  TEST_F(LlvmLibcModfTest, SpecialNumbers) { testSpecialNumbers(&func); }      \
  TEST_F(LlvmLibcModfTest, RoundedNubmers) { testIntegers(&func); }            \
  TEST_F(LlvmLibcModfTest, Fractions) { testFractions(&func); }                \
  TEST_F(LlvmLibcModfTest, Range) { testRange(&func); }