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