1 | //===-- Unittests for fegetround and fesetround ---------------------------===// |
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-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/fenv/fegetround.h" |
10 | #include "src/fenv/fesetround.h" |
11 | |
12 | #include "test/UnitTest/FEnvSafeTest.h" |
13 | #include "test/UnitTest/Test.h" |
14 | |
15 | #include "hdr/fenv_macros.h" |
16 | |
17 | using LlvmLibcRoundingModeTest = LIBC_NAMESPACE::testing::FEnvSafeTest; |
18 | |
19 | TEST_F(LlvmLibcRoundingModeTest, SetAndGet) { |
20 | struct ResetDefaultRoundingMode { |
21 | int original = LIBC_NAMESPACE::fegetround(); |
22 | ~ResetDefaultRoundingMode() { LIBC_NAMESPACE::fesetround(original); } |
23 | } reset; |
24 | |
25 | int s = LIBC_NAMESPACE::fesetround(FE_TONEAREST); |
26 | EXPECT_EQ(s, 0); |
27 | int rm = LIBC_NAMESPACE::fegetround(); |
28 | EXPECT_EQ(rm, FE_TONEAREST); |
29 | |
30 | s = LIBC_NAMESPACE::fesetround(FE_UPWARD); |
31 | EXPECT_EQ(s, 0); |
32 | rm = LIBC_NAMESPACE::fegetround(); |
33 | EXPECT_EQ(rm, FE_UPWARD); |
34 | |
35 | s = LIBC_NAMESPACE::fesetround(FE_DOWNWARD); |
36 | EXPECT_EQ(s, 0); |
37 | rm = LIBC_NAMESPACE::fegetround(); |
38 | EXPECT_EQ(rm, FE_DOWNWARD); |
39 | |
40 | s = LIBC_NAMESPACE::fesetround(FE_TOWARDZERO); |
41 | EXPECT_EQ(s, 0); |
42 | rm = LIBC_NAMESPACE::fegetround(); |
43 | EXPECT_EQ(rm, FE_TOWARDZERO); |
44 | } |
45 | |