xref: /llvm-project/libc/test/src/math/smoke/nanl_test.cpp (revision 8e79ade49d68c49aeb8ba008b59f559b86d22765)
1 //===-- Unittests for nanl ------------------------------------------------===//
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/math/nanl.h"
13 #include "test/UnitTest/FEnvSafeTest.h"
14 #include "test/UnitTest/FPMatcher.h"
15 #include "test/UnitTest/Test.h"
16 
17 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
18 #define SELECT_LONG_DOUBLE(val, _, __) val
19 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
20 #define SELECT_LONG_DOUBLE(_, val, __) val
21 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
22 #define SELECT_LONG_DOUBLE(_, __, val) val
23 #else
24 #error "Unknown long double type"
25 #endif
26 
27 class LlvmLibcNanlTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
28 public:
29   using StorageType = LIBC_NAMESPACE::fputil::FPBits<long double>::StorageType;
30 
31   void run_test(const char *input_str, StorageType bits) {
32     long double result = LIBC_NAMESPACE::nanl(input_str);
33     auto actual_fp = LIBC_NAMESPACE::fputil::FPBits<long double>(result);
34     auto expected_fp = LIBC_NAMESPACE::fputil::FPBits<long double>(bits);
35     EXPECT_EQ(actual_fp.uintval(), expected_fp.uintval());
36   }
37 };
38 
39 TEST_F(LlvmLibcNanlTest, NCharSeq) {
40   run_test("",
41            SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40),
42                               (UInt128(0x7fff800000000000) << 64)));
43   run_test("1234", SELECT_LONG_DOUBLE(
44                        0x7ff80000000004d2,
45                        (UInt128(0x7fffc00000) << 40) + UInt128(0x4d2),
46                        (UInt128(0x7fff800000000000) << 64) + UInt128(0x4d2)));
47   run_test("0x1234",
48            SELECT_LONG_DOUBLE(0x7ff8000000001234,
49                               (UInt128(0x7fffc00000) << 40) + UInt128(0x1234),
50                               (UInt128(0x7fff800000000000) << 64) +
51                                   UInt128(0x1234)));
52   run_test("1a",
53            SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40),
54                               (UInt128(0x7fff800000000000) << 64)));
55   run_test("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_",
56            SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40),
57                               (UInt128(0x7fff800000000000) << 64)));
58   run_test("10000000000000000000000000000000000000000000000000",
59            SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40),
60                               (UInt128(0x7fff800000000000) << 64)));
61 }
62 
63 TEST_F(LlvmLibcNanlTest, RandomString) {
64   StorageType expected =
65       SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40),
66                          (UInt128(0x7fff800000000000) << 64));
67 
68   run_test(" 1234", expected);
69   run_test("-1234", expected);
70   run_test("asd&f", expected);
71   run_test("123 ", expected);
72 }
73 
74 #if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
75 TEST_F(LlvmLibcNanlTest, InvalidInput) {
76   EXPECT_DEATH([] { LIBC_NAMESPACE::nanl(nullptr); });
77 }
78 #endif // LIBC_HAS_ADDRESS_SANITIZER
79