1//===-- Unittests for nanf ------------------------------------------------===//
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/nanf.h"
11#include "test/UnitTest/FEnvSafeTest.h"
12#include "test/UnitTest/FPMatcher.h"
13#include "test/UnitTest/Test.h"
14#include <signal.h>
15
16class LlvmLibcNanfTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
17public:
18 using StorageType = LIBC_NAMESPACE::fputil::FPBits<float>::StorageType;
19
20 void run_test(const char *input_str, StorageType bits) {
21 float result = LIBC_NAMESPACE::nanf(arg: input_str);
22 auto actual_fp = LIBC_NAMESPACE::fputil::FPBits<float>(result);
23 auto expected_fp = LIBC_NAMESPACE::fputil::FPBits<float>(bits);
24 EXPECT_EQ(actual_fp.uintval(), expected_fp.uintval());
25 };
26};
27
28TEST_F(LlvmLibcNanfTest, NCharSeq) {
29 run_test(input_str: "", bits: 0x7fc00000);
30 run_test(input_str: "1234", bits: 0x7fc004d2);
31 run_test(input_str: "0x1234", bits: 0x7fc01234);
32 run_test(input_str: "1a", bits: 0x7fc00000);
33 run_test(input_str: "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_",
34 bits: 0x7fc00000);
35 run_test(input_str: "10000000000000000000000000000000000000000000000000", bits: 0x7fc00000);
36}
37
38TEST_F(LlvmLibcNanfTest, RandomString) {
39 run_test(input_str: " 1234", bits: 0x7fc00000);
40 run_test(input_str: "-1234", bits: 0x7fc00000);
41 run_test(input_str: "asd&f", bits: 0x7fc00000);
42 run_test(input_str: "123 ", bits: 0x7fc00000);
43}
44
45#ifndef LIBC_HAVE_ADDRESS_SANITIZER
46TEST_F(LlvmLibcNanfTest, InvalidInput) {
47 EXPECT_DEATH([] { LIBC_NAMESPACE::nanf(nullptr); }, WITH_SIGNAL(SIGSEGV));
48}
49#endif // LIBC_HAVE_ADDRESS_SANITIZER
50

source code of libc/test/src/math/smoke/nanf_test.cpp