summaryrefslogtreecommitdiff
path: root/libc/test/src/fenv/feclearexcept_test.cpp
blob: 71c7e582bf697b4edee51693aa970ebb8f5ee980 (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
//===-- Unittests for feclearexcept with exceptions enabled ---------------===//
//
// 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/fenv/feclearexcept.h"

#include "src/__support/FPUtil/FEnvUtils.h"
#include "utils/UnitTest/Test.h"

#include <fenv.h>
#include <stdint.h>

TEST(LlvmLibcFEnvTest, ClearTest) {
  uint16_t excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
                        FE_UNDERFLOW};
  __llvm_libc::fputil::disableExcept(FE_ALL_EXCEPT);
  __llvm_libc::fputil::clearExcept(FE_ALL_EXCEPT);

  for (uint16_t e : excepts)
    ASSERT_EQ(__llvm_libc::fputil::testExcept(e), 0);

  __llvm_libc::fputil::raiseExcept(FE_ALL_EXCEPT);
  for (uint16_t e : excepts) {
    // We clear one exception and test to verify that it was cleared.
    __llvm_libc::feclearexcept(e);
    ASSERT_EQ(uint16_t(__llvm_libc::fputil::testExcept(FE_ALL_EXCEPT)),
              uint16_t(FE_ALL_EXCEPT & ~e));
    // After clearing, we raise the exception again.
    __llvm_libc::fputil::raiseExcept(e);
  }

  for (uint16_t e1 : excepts) {
    for (uint16_t e2 : excepts) {
      __llvm_libc::feclearexcept(e1 | e2);
      ASSERT_EQ(uint16_t(__llvm_libc::fputil::testExcept(FE_ALL_EXCEPT)),
                uint16_t(FE_ALL_EXCEPT & ~(e1 | e2)));
      __llvm_libc::fputil::raiseExcept(e1 | e2);
    }
  }

  for (uint16_t e1 : excepts) {
    for (uint16_t e2 : excepts) {
      for (uint16_t e3 : excepts) {
        __llvm_libc::feclearexcept(e1 | e2 | e3);
        ASSERT_EQ(uint16_t(__llvm_libc::fputil::testExcept(FE_ALL_EXCEPT)),
                  uint16_t(FE_ALL_EXCEPT & ~(e1 | e2 | e3)));
        __llvm_libc::fputil::raiseExcept(e1 | e2 | e3);
      }
    }
  }

  for (uint16_t e1 : excepts) {
    for (uint16_t e2 : excepts) {
      for (uint16_t e3 : excepts) {
        for (uint16_t e4 : excepts) {
          __llvm_libc::feclearexcept(e1 | e2 | e3 | e4);
          ASSERT_EQ(uint16_t(__llvm_libc::fputil::testExcept(FE_ALL_EXCEPT)),
                    uint16_t(FE_ALL_EXCEPT & ~(e1 | e2 | e3 | e4)));
          __llvm_libc::fputil::raiseExcept(e1 | e2 | e3 | e4);
        }
      }
    }
  }

  for (uint16_t e1 : excepts) {
    for (uint16_t e2 : excepts) {
      for (uint16_t e3 : excepts) {
        for (uint16_t e4 : excepts) {
          for (uint16_t e5 : excepts) {
            __llvm_libc::feclearexcept(e1 | e2 | e3 | e4 | e5);
            ASSERT_EQ(uint16_t(__llvm_libc::fputil::testExcept(FE_ALL_EXCEPT)),
                      uint16_t(FE_ALL_EXCEPT & ~(e1 | e2 | e3 | e4 | e5)));
            __llvm_libc::fputil::raiseExcept(e1 | e2 | e3 | e4 | e5);
          }
        }
      }
    }
  }
}