[libc-commits] [libc] [libc] Fix bugs found when testing with all headers (PR #144049)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Fri Jun 13 10:00:51 PDT 2025


================
@@ -255,3 +255,33 @@ TEST_F(LlvmLibcPrintfConverterTest, OctConversion) {
   ASSERT_STREQ(str, "1234");
   ASSERT_EQ(writer.get_chars_written(), 4);
 }
+
+TEST_F(LlvmLibcPrintfConverterTest, FloatConversionSimple) {
+
+  LIBC_NAMESPACE::printf_core::FormatSection section;
+  section.has_conv = true;
+  section.raw_string = "%f";
+  section.conv_name = 'f';
+  section.conv_val_raw = LIBC_NAMESPACE::cpp::bit_cast<uint64_t>(1.234567);
+  LIBC_NAMESPACE::printf_core::convert(&writer, section);
+
+  wb.buff[wb.buff_cur] = '\0';
+  ASSERT_STREQ(str, "1.234567");
+  ASSERT_EQ(writer.get_chars_written(), 8);
+}
+
+TEST_F(LlvmLibcPrintfConverterTest, FloatConversionPrecisionHigh) {
+
+  LIBC_NAMESPACE::printf_core::FormatSection section;
+  section.has_conv = true;
+  section.raw_string = "%12.3f";
+  section.conv_name = 'f';
+  section.min_width = 12;
+  section.precision = 3;
+  section.conv_val_raw = LIBC_NAMESPACE::cpp::bit_cast<uint64_t>(0.000123);
+  LIBC_NAMESPACE::printf_core::convert(&writer, section);
+
+  wb.buff[wb.buff_cur] = '\0';
+  ASSERT_STREQ(str, "       0.000");
+  ASSERT_EQ(writer.get_chars_written(), 12);
+}
----------------
michaelrj-google wrote:

Having float conversion tests is useful, but currently they're all in `sprintf_test.cpp` because there are flags to turn float conversions on and off. There's special cmake logic to ensure that the sprintf tests also get those flags so they don't fail on targets without floats.

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


More information about the libc-commits mailing list