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