1 //===-- Unittests for nan -------------------------------------------------===//
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/nan.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 class LlvmLibcNanTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
17 public:
18 using StorageType = LIBC_NAMESPACE::fputil::FPBits<double>::StorageType;
19
run_test(const char * input_str,StorageType bits)20 void run_test(const char *input_str, StorageType bits) {
21 double result = LIBC_NAMESPACE::nan(input_str);
22 auto actual_fp = LIBC_NAMESPACE::fputil::FPBits<double>(result);
23 auto expected_fp = LIBC_NAMESPACE::fputil::FPBits<double>(bits);
24 EXPECT_EQ(actual_fp.uintval(), expected_fp.uintval());
25 };
26 };
27
TEST_F(LlvmLibcNanTest,NCharSeq)28 TEST_F(LlvmLibcNanTest, NCharSeq) {
29 run_test("", 0x7ff8000000000000);
30 run_test("1234", 0x7ff80000000004d2);
31 run_test("0x1234", 0x7ff8000000001234);
32 run_test("1a", 0x7ff8000000000000);
33 run_test("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_",
34 0x7ff8000000000000);
35 run_test("10000000000000000000000000000000000000000000000000",
36 0x7ff8000000000000);
37 }
38
TEST_F(LlvmLibcNanTest,RandomString)39 TEST_F(LlvmLibcNanTest, RandomString) {
40 run_test(" 1234", 0x7ff8000000000000);
41 run_test("-1234", 0x7ff8000000000000);
42 run_test("asd&f", 0x7ff8000000000000);
43 run_test("123 ", 0x7ff8000000000000);
44 }
45
46 #ifndef LIBC_HAVE_ADDRESS_SANITIZER
TEST_F(LlvmLibcNanTest,InvalidInput)47 TEST_F(LlvmLibcNanTest, InvalidInput) {
48 EXPECT_DEATH([] { LIBC_NAMESPACE::nan(nullptr); }, WITH_SIGNAL(SIGSEGV));
49 }
50 #endif // LIBC_HAVE_ADDRESS_SANITIZER
51