1 | //===-- Unittests for nanf128 ---------------------------------------------===// |
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/__support/FPUtil/FPBits.h" |
10 | #include "src/__support/uint128.h" |
11 | #include "src/math/nanf128.h" |
12 | #include "test/UnitTest/FEnvSafeTest.h" |
13 | #include "test/UnitTest/FPMatcher.h" |
14 | #include "test/UnitTest/Test.h" |
15 | |
16 | class LlvmLibcNanf128Test : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
17 | public: |
18 | using FPBits128 = LIBC_NAMESPACE::fputil::FPBits<float128>; |
19 | using StorageType = FPBits128::StorageType; |
20 | |
21 | const UInt128 QUIET_NAN = FPBits128::quiet_nan().uintval(); |
22 | const UInt128 ONE = UInt128(1); |
23 | |
24 | void run_test(const char *input_str, StorageType bits) { |
25 | float128 result = LIBC_NAMESPACE::nanf128(arg: input_str); |
26 | auto actual_fp = FPBits128(result); |
27 | auto expected_fp = FPBits128(bits); |
28 | EXPECT_EQ(actual_fp.uintval(), expected_fp.uintval()); |
29 | }; |
30 | }; |
31 | |
32 | TEST_F(LlvmLibcNanf128Test, NCharSeq) { |
33 | run_test(input_str: "" , bits: QUIET_NAN); |
34 | run_test(input_str: "1234" , bits: QUIET_NAN | 1234); |
35 | run_test(input_str: "0x1234" , bits: QUIET_NAN | 0x1234); |
36 | run_test(input_str: "2417851639229258349412352" , bits: QUIET_NAN | (ONE << 81)); |
37 | run_test(input_str: "0x200000000000000000000" , bits: QUIET_NAN | (ONE << 81)); |
38 | run_test(input_str: "10384593717069655257060992658440191" , |
39 | bits: QUIET_NAN | FPBits128::SIG_MASK); |
40 | run_test(input_str: "0x1ffffffffffffffffffffffffffff" , bits: QUIET_NAN | FPBits128::SIG_MASK); |
41 | run_test(input_str: "10384593717069655257060992658440192" , bits: QUIET_NAN); |
42 | run_test(input_str: "0x20000000000000000000000000000" , bits: QUIET_NAN); |
43 | run_test(input_str: "1a" , bits: QUIET_NAN); |
44 | run_test(input_str: "10000000000000000000000000000000000000000000000000" , bits: QUIET_NAN); |
45 | } |
46 | |
47 | TEST_F(LlvmLibcNanf128Test, RandomString) { |
48 | run_test(input_str: " 1234" , bits: QUIET_NAN); |
49 | run_test(input_str: "-1234" , bits: QUIET_NAN); |
50 | run_test(input_str: "asd&f" , bits: QUIET_NAN); |
51 | run_test(input_str: "123 " , bits: QUIET_NAN); |
52 | run_test(input_str: "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_" , |
53 | bits: QUIET_NAN); |
54 | } |
55 | |
56 | #ifndef LIBC_HAVE_ADDRESS_SANITIZER |
57 | #include <signal.h> |
58 | TEST_F(LlvmLibcNanf128Test, InvalidInput) { |
59 | EXPECT_DEATH([] { LIBC_NAMESPACE::nanf128(nullptr); }, WITH_SIGNAL(SIGSEGV)); |
60 | } |
61 | #endif // LIBC_HAVE_ADDRESS_SANITIZER |
62 | |