Skip to content

Commit ae0e616

Browse files
committed
chore: add smoke tests for nanbf16 math function
Signed-off-by: Krishna Pandey <[email protected]>
1 parent ea9625e commit ae0e616

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

libc/test/src/math/smoke/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3515,6 +3515,22 @@ add_fp_unittest(
35153515
UNIT_TEST_ONLY
35163516
)
35173517

3518+
add_fp_unittest(
3519+
nanbf16_test
3520+
SUITE
3521+
libc-math-smoke-tests
3522+
SRCS
3523+
nanbf16_test.cpp
3524+
DEPENDS
3525+
libc.hdr.signal_macros
3526+
libc.src.math.nanbf16
3527+
libc.src.__support.FPUtil.bfloat16
3528+
libc.src.__support.FPUtil.fp_bits
3529+
# FIXME: The nan tests currently have death tests, which aren't supported for
3530+
# hermetic tests.
3531+
UNIT_TEST_ONLY
3532+
)
3533+
35183534
add_fp_unittest(
35193535
nearbyint_test
35203536
SUITE
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===-- Unittests for nanbf16 ---------------------------------------------===//
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/FPUtil/bfloat16.h"
12+
#include "src/math/nanbf16.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<bfloat16>::StorageType;
20+
21+
void run_test(const char *input_str, StorageType bits) {
22+
bfloat16 result = LIBC_NAMESPACE::nanbf16(input_str);
23+
auto actual_fp = LIBC_NAMESPACE::fputil::FPBits<bfloat16>(result);
24+
auto expected_fp = LIBC_NAMESPACE::fputil::FPBits<bfloat16>(bits);
25+
EXPECT_EQ(actual_fp.uintval(), expected_fp.uintval());
26+
}
27+
};
28+
29+
TEST_F(LlvmLibcNanf16Test, NCharSeq) {
30+
run_test("", 0x7fc0);
31+
32+
// 0x7fc0 + 0x1f (31) = 0x7cdf
33+
run_test("31", 0x7fdf);
34+
35+
// 0x7fc0 + 0x15 = 0x7fd5
36+
run_test("0x15", 0x7fd5);
37+
38+
run_test("1a", 0x7fc0);
39+
run_test("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_",
40+
0x7fc0);
41+
run_test("10000000000000000000000000000", 0x7fc0);
42+
}
43+
44+
TEST_F(LlvmLibcNanf16Test, RandomString) {
45+
run_test(" 1234", 0x7fc0);
46+
run_test("-1234", 0x7fc0);
47+
run_test("asd&f", 0x7fc0);
48+
run_test("123 ", 0x7fc0);
49+
}
50+
51+
#if defined(LIBC_ADD_NULL_CHECKS)
52+
TEST_F(LlvmLibcNanf16Test, InvalidInput) {
53+
EXPECT_DEATH([] { LIBC_NAMESPACE::nanbf16(nullptr); }, WITH_SIGNAL(-1));
54+
}
55+
#endif // LIBC_ADD_NULL_CHECKS

0 commit comments

Comments
 (0)