[libc-commits] [libc] [libc] Move strfrom* test cases into each respective unit test. (PR #218474)

Alexey Samsonov via libc-commits libc-commits at lists.llvm.org
Mon Aug 24 12:55:12 PDT 2026


================
@@ -6,8 +6,168 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "StrfromTest.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/properties/architectures.h"
 #include "src/stdlib/strfroml.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-STRFROM_TEST(long double, Strfroml, LIBC_NAMESPACE::strfroml)
+#define EXPECT_STREQ_LEN(str_size_needed, actual_str, expected_str)            \
+  EXPECT_EQ(str_size_needed, static_cast<int>(sizeof(expected_str) - 1));      \
+  EXPECT_STREQ(actual_str, expected_str);
+
+struct LlvmLibcStrfromlTest : LIBC_NAMESPACE::testing::ErrnoCheckingTest {};
+
+TEST_F(LlvmLibcStrfromlTest, DecimalFormat) {
+  char buff[45];
+  int result;
+
+  result = LIBC_NAMESPACE::strfroml(buff, 40, "%f", 1.0L);
+  EXPECT_STREQ_LEN(result, buff, "1.000000");
+
+  result = LIBC_NAMESPACE::strfroml(buff, 10, "%.f", -2.5L);
+  EXPECT_STREQ_LEN(result, buff, "-2");
+}
+
+TEST_F(LlvmLibcStrfromlTest, HexExponentFormat) {
+  char buff[55];
+  int result;
+
+  result = LIBC_NAMESPACE::strfroml(buff, 50, "%a", 0.1L);
+#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
+  EXPECT_STREQ_LEN(result, buff, "0xc.ccccccccccccccdp-7");
+#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
+  EXPECT_STREQ_LEN(result, buff, "0x1.999999999999ap-4");
+#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
+  EXPECT_STREQ_LEN(result, buff, "0x1.999999999999999999999999999ap-4");
+#endif
+
+  result = LIBC_NAMESPACE::strfroml(buff, 20, "%.1a", 0.1L);
+#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
+  EXPECT_STREQ_LEN(result, buff, "0xc.dp-7");
+#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
+  EXPECT_STREQ_LEN(result, buff, "0x1.ap-4");
+#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
+  EXPECT_STREQ_LEN(result, buff, "0x1.ap-4");
+#endif
+
+  result = LIBC_NAMESPACE::strfroml(buff, 50, "%a", 1.0e1000L);
+#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
+  EXPECT_STREQ_LEN(result, buff, "0xf.38db1f9dd3dac05p+3318");
+#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
+  EXPECT_STREQ_LEN(result, buff, "inf");
+#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
+  EXPECT_STREQ_LEN(result, buff, "0x1.e71b63f3ba7b580af1a52d2a7379p+3321");
+#endif
+
+  result = LIBC_NAMESPACE::strfroml(buff, 50, "%a", 1.0e-1000L);
+#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
+  EXPECT_STREQ_LEN(result, buff, "0x8.68a9188a89e1467p-3325");
+#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
+  EXPECT_STREQ_LEN(result, buff, "0x0p+0");
+#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
+  EXPECT_STREQ_LEN(result, buff, "0x1.0d152311513c28ce202627c06ec2p-3322");
+#endif
+
+  result =
+      LIBC_NAMESPACE::strfroml(buff, 50, "%.1a", 0xf.fffffffffffffffp16380L);
+#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
+  EXPECT_STREQ_LEN(result, buff, "0x1.0p+16384");
+#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
+  EXPECT_STREQ_LEN(result, buff, "inf");
+#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
+  EXPECT_STREQ_LEN(result, buff, "0x2.0p+16383");
+#endif
+}
+
+TEST_F(LlvmLibcStrfromlTest, DecimalExponentFormat) {
+  // Mark as maybe_unused to silence unused variable
----------------
vonosmas wrote:

Can we just move the declarations under the ifdef below and remove maybe_unused? Same below.

https://github.com/llvm/llvm-project/pull/218474


More information about the libc-commits mailing list