| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //===-- Unittests for nan -------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/__support/FPUtil/FPBits.h" | ||
| #include "src/math/nan.h" | ||
| #include "test/UnitTest/FPMatcher.h" | ||
| #include "test/UnitTest/Test.h" | ||
| #include <signal.h> | ||
|
|
||
| class LlvmLibcNanTest : public LIBC_NAMESPACE::testing::Test { | ||
| public: | ||
| using StorageType = LIBC_NAMESPACE::fputil::FPBits<double>::StorageType; | ||
|
|
||
| void run_test(const char *input_str, StorageType bits) { | ||
| double result = LIBC_NAMESPACE::nan(input_str); | ||
| auto actual_fp = LIBC_NAMESPACE::fputil::FPBits<double>(result); | ||
| auto expected_fp = LIBC_NAMESPACE::fputil::FPBits<double>(bits); | ||
| EXPECT_EQ(actual_fp.bits, expected_fp.bits); | ||
| }; | ||
| }; | ||
|
|
||
| TEST_F(LlvmLibcNanTest, NCharSeq) { | ||
| run_test("", 0x7ff8000000000000); | ||
| run_test("1234", 0x7ff80000000004d2); | ||
| run_test("0x1234", 0x7ff8000000001234); | ||
| run_test("1a", 0x7ff8000000000000); | ||
| run_test("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_", | ||
| 0x7ff8000000000000); | ||
| run_test("10000000000000000000000000000000000000000000000000", | ||
| 0x7ff8000000000000); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcNanTest, RandomString) { | ||
| run_test(" 1234", 0x7ff8000000000000); | ||
| run_test("-1234", 0x7ff8000000000000); | ||
| run_test("asd&f", 0x7ff8000000000000); | ||
| run_test("123 ", 0x7ff8000000000000); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcNanTest, InvalidInput) { | ||
| EXPECT_DEATH([] { LIBC_NAMESPACE::nan(nullptr); }, WITH_SIGNAL(SIGSEGV)); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| //===-- Unittests for nanf ------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/__support/FPUtil/FPBits.h" | ||
| #include "src/math/nanf.h" | ||
| #include "test/UnitTest/FPMatcher.h" | ||
| #include "test/UnitTest/Test.h" | ||
| #include <signal.h> | ||
|
|
||
| class LlvmLibcNanfTest : public LIBC_NAMESPACE::testing::Test { | ||
| public: | ||
| using StorageType = LIBC_NAMESPACE::fputil::FPBits<float>::StorageType; | ||
|
|
||
| void run_test(const char *input_str, StorageType bits) { | ||
| float result = LIBC_NAMESPACE::nanf(input_str); | ||
| auto actual_fp = LIBC_NAMESPACE::fputil::FPBits<float>(result); | ||
| auto expected_fp = LIBC_NAMESPACE::fputil::FPBits<float>(bits); | ||
| EXPECT_EQ(actual_fp.bits, expected_fp.bits); | ||
| }; | ||
| }; | ||
|
|
||
| TEST_F(LlvmLibcNanfTest, NCharSeq) { | ||
| run_test("", 0x7fc00000); | ||
| run_test("1234", 0x7fc004d2); | ||
| run_test("0x1234", 0x7fc01234); | ||
| run_test("1a", 0x7fc00000); | ||
| run_test("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_", | ||
| 0x7fc00000); | ||
| run_test("10000000000000000000000000000000000000000000000000", 0x7fc00000); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcNanfTest, RandomString) { | ||
| run_test(" 1234", 0x7fc00000); | ||
| run_test("-1234", 0x7fc00000); | ||
| run_test("asd&f", 0x7fc00000); | ||
| run_test("123 ", 0x7fc00000); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcNanfTest, InvalidInput) { | ||
| EXPECT_DEATH([] { LIBC_NAMESPACE::nanf(nullptr); }, WITH_SIGNAL(SIGSEGV)); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| //===-- Unittests for nanl ------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/__support/FPUtil/FPBits.h" | ||
| #include "src/math/nanl.h" | ||
| #include "test/UnitTest/FPMatcher.h" | ||
| #include "test/UnitTest/Test.h" | ||
| #include <signal.h> | ||
|
|
||
| #if defined(LIBC_LONG_DOUBLE_IS_FLOAT64) | ||
| #define SELECT_LONG_DOUBLE(val, _, __) val | ||
| #elif defined(LIBC_LONG_DOUBLE_IS_X86_FLOAT80) | ||
| #define SELECT_LONG_DOUBLE(_, val, __) val | ||
| #else | ||
| #define SELECT_LONG_DOUBLE(_, __, val) val | ||
| #endif | ||
|
|
||
| class LlvmLibcNanlTest : public LIBC_NAMESPACE::testing::Test { | ||
| public: | ||
| using StorageType = LIBC_NAMESPACE::fputil::FPBits<long double>::StorageType; | ||
|
|
||
| void run_test(const char *input_str, StorageType bits) { | ||
| long double result = LIBC_NAMESPACE::nanl(input_str); | ||
| auto actual_fp = LIBC_NAMESPACE::fputil::FPBits<long double>(result); | ||
| auto expected_fp = LIBC_NAMESPACE::fputil::FPBits<long double>(bits); | ||
| EXPECT_EQ(actual_fp.bits, expected_fp.bits); | ||
| }; | ||
| }; | ||
|
|
||
| TEST_F(LlvmLibcNanlTest, NCharSeq) { | ||
| run_test("", | ||
| SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40), | ||
| (UInt128(0x7fff800000000000) << 64))); | ||
| run_test("1234", SELECT_LONG_DOUBLE( | ||
| 0x7ff80000000004d2, | ||
| (UInt128(0x7fffc00000) << 40) + UInt128(0x4d2), | ||
| (UInt128(0x7fff800000000000) << 64) + UInt128(0x4d2))); | ||
| run_test("0x1234", | ||
| SELECT_LONG_DOUBLE(0x7ff8000000001234, | ||
| (UInt128(0x7fffc00000) << 40) + UInt128(0x1234), | ||
| (UInt128(0x7fff800000000000) << 64) + | ||
| UInt128(0x1234))); | ||
| run_test("1a", | ||
| SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40), | ||
| (UInt128(0x7fff800000000000) << 64))); | ||
| run_test("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_", | ||
| SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40), | ||
| (UInt128(0x7fff800000000000) << 64))); | ||
| run_test("10000000000000000000000000000000000000000000000000", | ||
| SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40), | ||
| (UInt128(0x7fff800000000000) << 64))); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcNanlTest, RandomString) { | ||
| StorageType expected = | ||
| SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40), | ||
| (UInt128(0x7fff800000000000) << 64)); | ||
|
|
||
| run_test(" 1234", expected); | ||
| run_test("-1234", expected); | ||
| run_test("asd&f", expected); | ||
| run_test("123 ", expected); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcNanlTest, InvalidInput) { | ||
| EXPECT_DEATH([] { LIBC_NAMESPACE::nanl(nullptr); }, WITH_SIGNAL(SIGSEGV)); | ||
| } |