Skip to content

Commit 6373577

Browse files
authored
[libc] Add inf/nan tests for strfrom*() functions (#86663)
Adds tests for `inf` and `nan` values to the tests for `strfrom*()` functions. Also marks some variables as `[[maybe_unused]]` to silence unused variable warnings.
1 parent 219511a commit 6373577

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

libc/test/src/stdlib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ add_header_library(
174174
StrfromTest.h
175175
DEPENDS
176176
libc.src.__support.CPP.type_traits
177+
libc.src.__support.FPUtil.fp_bits
177178
)
178179

179180
add_libc_test(

libc/test/src/stdlib/StrfromTest.h

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/__support/CPP/type_traits.h"
10+
#include "src/__support/FPUtil/FPBits.h"
1011
#include "test/UnitTest/Test.h"
1112

1213
#define ASSERT_STREQ_LEN(actual_written, actual_str, expected_str) \
@@ -71,8 +72,18 @@ class StrfromTest : public LIBC_NAMESPACE::testing::Test {
7172
written =
7273
func(buff, 37,
7374
"%A simple string with one conversion, should overwrite.", 1.0);
74-
ASSERT_STREQ_LEN(written, buff, is_long_double ? "0X8P-3" : "0X1P+0");
75-
75+
if (is_long_double) {
76+
#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
77+
ASSERT_STREQ_LEN(written, buff, "0X8P-3");
78+
#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
79+
ASSERT_STREQ_LEN(written, buff, "0X1P+0");
80+
#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
81+
ASSERT_STREQ_LEN(written, buff, "0X1P+0");
82+
#endif
83+
} else {
84+
// not long double
85+
ASSERT_STREQ_LEN(written, buff, "0X1P+0");
86+
}
7687
written = func(buff, 74,
7788
"A simple string with one conversion in %A "
7889
"between, writes string as it is",
@@ -105,6 +116,13 @@ class StrfromTest : public LIBC_NAMESPACE::testing::Test {
105116
ASSERT_STREQ(buff, "1.05"); // Make sure that buff has not changed
106117
}
107118

119+
void infNanValues(FunctionT func) {
120+
if (is_double_prec)
121+
doublePrecInfNan(func);
122+
else if (!is_single_prec)
123+
longDoublePrecInfNan(func);
124+
}
125+
108126
void floatDecimalSinglePrec(FunctionT func) {
109127
char buff[70];
110128
int written;
@@ -336,8 +354,10 @@ class StrfromTest : public LIBC_NAMESPACE::testing::Test {
336354
}
337355

338356
void floatDecimalExpLongDoublePrec(FunctionT func) {
339-
char buff[100];
340-
int written;
357+
// Mark as maybe_unused to silence unused variable
358+
// warning when long double is not 80-bit
359+
[[maybe_unused]] char buff[100];
360+
[[maybe_unused]] int written;
341361

342362
#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
343363
written = func(buff, 90, "%.9e", 1000000000500000000.1L);
@@ -399,8 +419,10 @@ class StrfromTest : public LIBC_NAMESPACE::testing::Test {
399419
}
400420

401421
void floatDecimalAutoLongDoublePrec(FunctionT func) {
402-
char buff[100];
403-
int written;
422+
// Mark as maybe_unused to silence unused variable
423+
// warning when long double is not 80-bit
424+
[[maybe_unused]] char buff[100];
425+
[[maybe_unused]] int written;
404426

405427
#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
406428
written = func(buff, 99, "%g", 0xf.fffffffffffffffp+16380L);
@@ -413,6 +435,48 @@ class StrfromTest : public LIBC_NAMESPACE::testing::Test {
413435
ASSERT_STREQ_LEN(written, buff, "1e-99");
414436
#endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
415437
}
438+
439+
void doublePrecInfNan(FunctionT func) {
440+
char buff[15];
441+
int written;
442+
443+
double inf = LIBC_NAMESPACE::fputil::FPBits<double>::inf().get_val();
444+
double nan = LIBC_NAMESPACE::fputil::FPBits<double>::quiet_nan().get_val();
445+
446+
written = func(buff, 10, "%f", inf);
447+
ASSERT_STREQ_LEN(written, buff, "inf");
448+
449+
written = func(buff, 10, "%A", -inf);
450+
ASSERT_STREQ_LEN(written, buff, "-INF");
451+
452+
written = func(buff, 10, "%f", nan);
453+
ASSERT_STREQ_LEN(written, buff, "nan");
454+
455+
written = func(buff, 10, "%A", -nan);
456+
ASSERT_STREQ_LEN(written, buff, "-NAN");
457+
}
458+
459+
void longDoublePrecInfNan(FunctionT func) {
460+
char buff[15];
461+
int written;
462+
463+
long double ld_inf =
464+
LIBC_NAMESPACE::fputil::FPBits<long double>::inf().get_val();
465+
long double ld_nan =
466+
LIBC_NAMESPACE::fputil::FPBits<long double>::quiet_nan().get_val();
467+
468+
written = func(buff, 10, "%f", ld_inf);
469+
ASSERT_STREQ_LEN(written, buff, "inf");
470+
471+
written = func(buff, 10, "%A", -ld_inf);
472+
ASSERT_STREQ_LEN(written, buff, "-INF");
473+
474+
written = func(buff, 10, "%f", ld_nan);
475+
ASSERT_STREQ_LEN(written, buff, "nan");
476+
477+
written = func(buff, 10, "%A", -ld_nan);
478+
ASSERT_STREQ_LEN(written, buff, "-NAN");
479+
}
416480
};
417481

418482
#define STRFROM_TEST(InputType, name, func) \
@@ -432,4 +496,5 @@ class StrfromTest : public LIBC_NAMESPACE::testing::Test {
432496
} \
433497
TEST_F(LlvmLibc##name##Test, InsufficientBufferSize) { \
434498
insufficentBufsize(func); \
435-
}
499+
} \
500+
TEST_F(LlvmLibc##name##Test, InfAndNanValues) { infNanValues(func); }

0 commit comments

Comments
 (0)