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