[libc-commits] [libc] [libc] clean up sprintf macros and float tests (PR #110759)
via libc-commits
libc-commits at lists.llvm.org
Tue Oct 1 16:06:41 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Michael Jones (michaelrj-google)
<details>
<summary>Changes</summary>
The sprintf tests have a macro named "ASSERT_STREQ_LEN" which was used
in about half of the tests. This patch moves all of the tests which can
to using that macro. This patch also enables long double tests for %e
and %g, since those never got finished. There's still some work to do
enabling long double testing for long doubles other than the intel 80
bit format, but that can land in a followup.
The `#ifdef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION` lines are for a
followup patch.
---
Patch is 62.40 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/110759.diff
1 Files Affected:
- (modified) libc/test/src/stdio/sprintf_test.cpp (+222-631)
``````````diff
diff --git a/libc/test/src/stdio/sprintf_test.cpp b/libc/test/src/stdio/sprintf_test.cpp
index 09a175104d2fe3..e8303ff3416b2f 100644
--- a/libc/test/src/stdio/sprintf_test.cpp
+++ b/libc/test/src/stdio/sprintf_test.cpp
@@ -56,8 +56,7 @@ TEST(LlvmLibcSPrintfTest, SimpleNoConv) {
written =
LIBC_NAMESPACE::sprintf(buff, "A simple string with no conversions.");
- EXPECT_EQ(written, 36);
- ASSERT_STREQ(buff, "A simple string with no conversions.");
+ ASSERT_STREQ_LEN(written, buff, "A simple string with no conversions.");
}
TEST(LlvmLibcSPrintfTest, PercentConv) {
@@ -65,16 +64,13 @@ TEST(LlvmLibcSPrintfTest, PercentConv) {
int written;
written = LIBC_NAMESPACE::sprintf(buff, "%%");
- EXPECT_EQ(written, 1);
- ASSERT_STREQ(buff, "%");
+ ASSERT_STREQ_LEN(written, buff, "%");
written = LIBC_NAMESPACE::sprintf(buff, "abc %% def");
- EXPECT_EQ(written, 9);
- ASSERT_STREQ(buff, "abc % def");
+ ASSERT_STREQ_LEN(written, buff, "abc % def");
written = LIBC_NAMESPACE::sprintf(buff, "%%%%%%");
- EXPECT_EQ(written, 3);
- ASSERT_STREQ(buff, "%%%");
+ ASSERT_STREQ_LEN(written, buff, "%%%");
}
TEST(LlvmLibcSPrintfTest, CharConv) {
@@ -82,16 +78,13 @@ TEST(LlvmLibcSPrintfTest, CharConv) {
int written;
written = LIBC_NAMESPACE::sprintf(buff, "%c", 'a');
- EXPECT_EQ(written, 1);
- ASSERT_STREQ(buff, "a");
+ ASSERT_STREQ_LEN(written, buff, "a");
written = LIBC_NAMESPACE::sprintf(buff, "%3c %-3c", '1', '2');
- EXPECT_EQ(written, 7);
- ASSERT_STREQ(buff, " 1 2 ");
+ ASSERT_STREQ_LEN(written, buff, " 1 2 ");
written = LIBC_NAMESPACE::sprintf(buff, "%*c", 2, '3');
- EXPECT_EQ(written, 2);
- ASSERT_STREQ(buff, " 3");
+ ASSERT_STREQ_LEN(written, buff, " 3");
}
TEST(LlvmLibcSPrintfTest, StringConv) {
@@ -99,27 +92,22 @@ TEST(LlvmLibcSPrintfTest, StringConv) {
int written;
written = LIBC_NAMESPACE::sprintf(buff, "%s", "abcDEF123");
- EXPECT_EQ(written, 9);
- ASSERT_STREQ(buff, "abcDEF123");
+ ASSERT_STREQ_LEN(written, buff, "abcDEF123");
written = LIBC_NAMESPACE::sprintf(buff, "%10s %-10s", "centered", "title");
- EXPECT_EQ(written, 21);
- ASSERT_STREQ(buff, " centered title ");
+ ASSERT_STREQ_LEN(written, buff, " centered title ");
written = LIBC_NAMESPACE::sprintf(buff, "%-5.4s%-4.4s", "words can describe",
"soups most delicious");
- EXPECT_EQ(written, 9);
- ASSERT_STREQ(buff, "word soup");
+ ASSERT_STREQ_LEN(written, buff, "word soup");
written = LIBC_NAMESPACE::sprintf(buff, "%*s %.*s %*.*s", 10, "beginning", 2,
"isn't", 12, 10, "important. Ever.");
- EXPECT_EQ(written, 26);
- ASSERT_STREQ(buff, " beginning is important.");
+ ASSERT_STREQ_LEN(written, buff, " beginning is important.");
#ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
written = LIBC_NAMESPACE::sprintf(buff, "%s", nullptr);
- EXPECT_EQ(written, 6);
- ASSERT_STREQ(buff, "(null)");
+ ASSERT_STREQ_LEN(written, buff, "(null)");
#endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
}
@@ -130,73 +118,57 @@ TEST(LlvmLibcSPrintfTest, IntConv) {
// Basic Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%d", 123);
- EXPECT_EQ(written, 3);
- ASSERT_STREQ(buff, "123");
+ ASSERT_STREQ_LEN(written, buff, "123");
written = LIBC_NAMESPACE::sprintf(buff, "%i", -456);
- EXPECT_EQ(written, 4);
- ASSERT_STREQ(buff, "-456");
+ ASSERT_STREQ_LEN(written, buff, "-456");
// Length Modifier Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%hhu", 257); // 0x101
- EXPECT_EQ(written, 1);
- ASSERT_STREQ(buff, "1");
+ ASSERT_STREQ_LEN(written, buff, "1");
written = LIBC_NAMESPACE::sprintf(buff, "%llu", 18446744073709551615ull);
- EXPECT_EQ(written, 20);
- ASSERT_STREQ(buff, "18446744073709551615"); // ull max
+ ASSERT_STREQ_LEN(written, buff, "18446744073709551615"); // ull max
written = LIBC_NAMESPACE::sprintf(buff, "%u", ~0);
if (sizeof(int) == 4) {
- EXPECT_EQ(written, 10);
- ASSERT_STREQ(buff, "4294967295");
+ ASSERT_STREQ_LEN(written, buff, "4294967295");
}
written = LIBC_NAMESPACE::sprintf(buff, "%tu", ~ptrdiff_t(0));
if (sizeof(ptrdiff_t) == 8) {
- EXPECT_EQ(written, 20);
- ASSERT_STREQ(buff, "18446744073709551615");
+ ASSERT_STREQ_LEN(written, buff, "18446744073709551615");
} else if (sizeof(ptrdiff_t) == 4) {
- EXPECT_EQ(written, 10);
- ASSERT_STREQ(buff, "4294967295");
+ ASSERT_STREQ_LEN(written, buff, "4294967295");
}
written = LIBC_NAMESPACE::sprintf(buff, "%lld", -9223372036854775807ll - 1ll);
- EXPECT_EQ(written, 20);
- ASSERT_STREQ(buff, "-9223372036854775808"); // ll min
+ ASSERT_STREQ_LEN(written, buff, "-9223372036854775808"); // ll min
written = LIBC_NAMESPACE::sprintf(buff, "%w3d", 5807);
- EXPECT_EQ(written, 1);
- ASSERT_STREQ(buff, "7");
+ ASSERT_STREQ_LEN(written, buff, "7");
written = LIBC_NAMESPACE::sprintf(buff, "%w3d", 1);
- EXPECT_EQ(written, 1);
- ASSERT_STREQ(buff, "1");
+ ASSERT_STREQ_LEN(written, buff, "1");
written = LIBC_NAMESPACE::sprintf(buff, "%w64d", 9223372036854775807ll);
- EXPECT_EQ(written, 19);
- ASSERT_STREQ(buff, "9223372036854775807");
+ ASSERT_STREQ_LEN(written, buff, "9223372036854775807");
written = LIBC_NAMESPACE::sprintf(buff, "%w-1d", 5807);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "%w-1d");
+ ASSERT_STREQ_LEN(written, buff, "%w-1d");
written = LIBC_NAMESPACE::sprintf(buff, "%w0d", 5807);
- EXPECT_EQ(written, 4);
- ASSERT_STREQ(buff, "%w0d");
+ ASSERT_STREQ_LEN(written, buff, "%w0d");
written = LIBC_NAMESPACE::sprintf(buff, "%w999d", 9223372036854775807ll);
- EXPECT_EQ(written, 19);
- ASSERT_STREQ(buff, "9223372036854775807");
+ ASSERT_STREQ_LEN(written, buff, "9223372036854775807");
written = LIBC_NAMESPACE::sprintf(buff, "%winvalid%w1d", 5807, 5807);
- EXPECT_EQ(written, 10);
- ASSERT_STREQ(buff, "%winvalid1");
+ ASSERT_STREQ_LEN(written, buff, "%winvalid1");
written = LIBC_NAMESPACE::sprintf(buff, "%w-1d%w1d", 5807, 5807);
- EXPECT_EQ(written, 6);
- ASSERT_STREQ(buff, "%w-1d1");
+ ASSERT_STREQ_LEN(written, buff, "%w-1d1");
char format[64];
char uintmax[128];
@@ -208,157 +180,122 @@ TEST(LlvmLibcSPrintfTest, IntConv) {
ASSERT_STREQ(buff, uintmax);
written = LIBC_NAMESPACE::sprintf(buff, "%w64u", 18446744073709551615ull);
- EXPECT_EQ(written, 20);
- ASSERT_STREQ(buff, "18446744073709551615"); // ull max
+ ASSERT_STREQ_LEN(written, buff, "18446744073709551615"); // ull max
written =
LIBC_NAMESPACE::sprintf(buff, "%w64d", -9223372036854775807ll - 1ll);
- EXPECT_EQ(written, 20);
- ASSERT_STREQ(buff, "-9223372036854775808"); // ll min
+ ASSERT_STREQ_LEN(written, buff, "-9223372036854775808"); // ll min
written = LIBC_NAMESPACE::sprintf(buff, "%wf3d", 5807);
- EXPECT_EQ(written, 1);
- ASSERT_STREQ(buff, "7");
+ ASSERT_STREQ_LEN(written, buff, "7");
written = LIBC_NAMESPACE::sprintf(buff, "%wf3d", 1);
- EXPECT_EQ(written, 1);
- ASSERT_STREQ(buff, "1");
+ ASSERT_STREQ_LEN(written, buff, "1");
written = LIBC_NAMESPACE::sprintf(buff, "%wf64u", 18446744073709551615ull);
- EXPECT_EQ(written, 20);
- ASSERT_STREQ(buff, "18446744073709551615"); // ull max
+ ASSERT_STREQ_LEN(written, buff, "18446744073709551615"); // ull max
written =
LIBC_NAMESPACE::sprintf(buff, "%wf64d", -9223372036854775807ll - 1ll);
- EXPECT_EQ(written, 20);
- ASSERT_STREQ(buff, "-9223372036854775808"); // ll min
+ ASSERT_STREQ_LEN(written, buff, "-9223372036854775808"); // ll min
written = LIBC_NAMESPACE::sprintf(buff, "%wf0d", 5807);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "%wf0d");
+ ASSERT_STREQ_LEN(written, buff, "%wf0d");
written = LIBC_NAMESPACE::sprintf(buff, "%wf-1d", 5807);
- EXPECT_EQ(written, 6);
- ASSERT_STREQ(buff, "%wf-1d");
+ ASSERT_STREQ_LEN(written, buff, "%wf-1d");
written = LIBC_NAMESPACE::sprintf(buff, "%wfinvalid%wf1d", 5807, 5807);
- EXPECT_EQ(written, 11);
- ASSERT_STREQ(buff, "%wfinvalid1");
+ ASSERT_STREQ_LEN(written, buff, "%wfinvalid1");
written = LIBC_NAMESPACE::sprintf(buff, "%wf-1d%wf1d", 5807, 5807);
- EXPECT_EQ(written, 7);
- ASSERT_STREQ(buff, "%wf-1d1");
+ ASSERT_STREQ_LEN(written, buff, "%wf-1d1");
written = LIBC_NAMESPACE::sprintf(buff, "%wf999d", 9223372036854775807ll);
- EXPECT_EQ(written, 19);
- ASSERT_STREQ(buff, "9223372036854775807");
+ ASSERT_STREQ_LEN(written, buff, "9223372036854775807");
// Min Width Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%4d", 789);
- EXPECT_EQ(written, 4);
- ASSERT_STREQ(buff, " 789");
+ ASSERT_STREQ_LEN(written, buff, " 789");
written = LIBC_NAMESPACE::sprintf(buff, "%2d", 987);
- EXPECT_EQ(written, 3);
- ASSERT_STREQ(buff, "987");
+ ASSERT_STREQ_LEN(written, buff, "987");
// Precision Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%d", 0);
- EXPECT_EQ(written, 1);
- ASSERT_STREQ(buff, "0");
+ ASSERT_STREQ_LEN(written, buff, "0");
written = LIBC_NAMESPACE::sprintf(buff, "%.0d", 0);
- EXPECT_EQ(written, 0);
- ASSERT_STREQ(buff, "");
+ ASSERT_STREQ_LEN(written, buff, "");
written = LIBC_NAMESPACE::sprintf(buff, "%.5d", 654);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "00654");
+ ASSERT_STREQ_LEN(written, buff, "00654");
written = LIBC_NAMESPACE::sprintf(buff, "%.5d", -321);
- EXPECT_EQ(written, 6);
- ASSERT_STREQ(buff, "-00321");
+ ASSERT_STREQ_LEN(written, buff, "-00321");
written = LIBC_NAMESPACE::sprintf(buff, "%.2d", 135);
- EXPECT_EQ(written, 3);
- ASSERT_STREQ(buff, "135");
+ ASSERT_STREQ_LEN(written, buff, "135");
// Flag Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%.5d", -321);
- EXPECT_EQ(written, 6);
- ASSERT_STREQ(buff, "-00321");
+ ASSERT_STREQ_LEN(written, buff, "-00321");
written = LIBC_NAMESPACE::sprintf(buff, "%-5d", 246);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "246 ");
+ ASSERT_STREQ_LEN(written, buff, "246 ");
written = LIBC_NAMESPACE::sprintf(buff, "%-5d", -147);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "-147 ");
+ ASSERT_STREQ_LEN(written, buff, "-147 ");
written = LIBC_NAMESPACE::sprintf(buff, "%+d", 258);
- EXPECT_EQ(written, 4);
- ASSERT_STREQ(buff, "+258");
+ ASSERT_STREQ_LEN(written, buff, "+258");
written = LIBC_NAMESPACE::sprintf(buff, "% d", 369);
- EXPECT_EQ(written, 4);
- ASSERT_STREQ(buff, " 369");
+ ASSERT_STREQ_LEN(written, buff, " 369");
written = LIBC_NAMESPACE::sprintf(buff, "%05d", 470);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "00470");
+ ASSERT_STREQ_LEN(written, buff, "00470");
written = LIBC_NAMESPACE::sprintf(buff, "%05d", -581);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "-0581");
+ ASSERT_STREQ_LEN(written, buff, "-0581");
// Combined Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%+ u", 692);
- EXPECT_EQ(written, 3);
- ASSERT_STREQ(buff, "692");
+ ASSERT_STREQ_LEN(written, buff, "692");
written = LIBC_NAMESPACE::sprintf(buff, "%+ -05d", 703);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "+703 ");
+ ASSERT_STREQ_LEN(written, buff, "+703 ");
written = LIBC_NAMESPACE::sprintf(buff, "%7.5d", 814);
- EXPECT_EQ(written, 7);
- ASSERT_STREQ(buff, " 00814");
+ ASSERT_STREQ_LEN(written, buff, " 00814");
written = LIBC_NAMESPACE::sprintf(buff, "%7.5d", -925);
- EXPECT_EQ(written, 7);
- ASSERT_STREQ(buff, " -00925");
+ ASSERT_STREQ_LEN(written, buff, " -00925");
written = LIBC_NAMESPACE::sprintf(buff, "%7.5d", 159);
- EXPECT_EQ(written, 7);
- ASSERT_STREQ(buff, " 00159");
+ ASSERT_STREQ_LEN(written, buff, " 00159");
written = LIBC_NAMESPACE::sprintf(buff, "% -7.5d", 260);
- EXPECT_EQ(written, 7);
- ASSERT_STREQ(buff, " 00260 ");
+ ASSERT_STREQ_LEN(written, buff, " 00260 ");
written = LIBC_NAMESPACE::sprintf(buff, "%5.4d", 10000);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "10000");
+ ASSERT_STREQ_LEN(written, buff, "10000");
// Multiple Conversion Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%10d %-10d", 456, -789);
- EXPECT_EQ(written, 21);
- ASSERT_STREQ(buff, " 456 -789 ");
+ ASSERT_STREQ_LEN(written, buff, " 456 -789 ");
written = LIBC_NAMESPACE::sprintf(buff, "%-5.4d%+.4u", 75, 25);
- EXPECT_EQ(written, 9);
- ASSERT_STREQ(buff, "0075 0025");
+ ASSERT_STREQ_LEN(written, buff, "0075 0025");
written = LIBC_NAMESPACE::sprintf(buff, "% 05hhi %+-0.5llu %-+ 06.3zd",
256 + 127, 68719476736ll, size_t(2));
- EXPECT_EQ(written, 24);
- ASSERT_STREQ(buff, " 0127 68719476736 +002 ");
+ ASSERT_STREQ_LEN(written, buff, " 0127 68719476736 +002 ");
}
TEST(LlvmLibcSPrintfTest, HexConv) {
@@ -368,130 +305,102 @@ TEST(LlvmLibcSPrintfTest, HexConv) {
// Basic Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%x", 0x123a);
- EXPECT_EQ(written, 4);
- ASSERT_STREQ(buff, "123a");
+ ASSERT_STREQ_LEN(written, buff, "123a");
written = LIBC_NAMESPACE::sprintf(buff, "%X", 0x456b);
- EXPECT_EQ(written, 4);
- ASSERT_STREQ(buff, "456B");
+ ASSERT_STREQ_LEN(written, buff, "456B");
// Length Modifier Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%hhx", 0x10001);
- EXPECT_EQ(written, 1);
- ASSERT_STREQ(buff, "1");
+ ASSERT_STREQ_LEN(written, buff, "1");
written = LIBC_NAMESPACE::sprintf(buff, "%llx", 0xffffffffffffffffull);
- EXPECT_EQ(written, 16);
- ASSERT_STREQ(buff, "ffffffffffffffff"); // ull max
+ ASSERT_STREQ_LEN(written, buff, "ffffffffffffffff"); // ull max
written = LIBC_NAMESPACE::sprintf(buff, "%tX", ~ptrdiff_t(0));
if (sizeof(ptrdiff_t) == 8) {
- EXPECT_EQ(written, 16);
- ASSERT_STREQ(buff, "FFFFFFFFFFFFFFFF");
+ ASSERT_STREQ_LEN(written, buff, "FFFFFFFFFFFFFFFF");
} else if (sizeof(ptrdiff_t) == 4) {
- EXPECT_EQ(written, 8);
- ASSERT_STREQ(buff, "FFFFFFFF");
+ ASSERT_STREQ_LEN(written, buff, "FFFFFFFF");
}
// Min Width Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%4x", 0x789);
- EXPECT_EQ(written, 4);
- ASSERT_STREQ(buff, " 789");
+ ASSERT_STREQ_LEN(written, buff, " 789");
written = LIBC_NAMESPACE::sprintf(buff, "%2X", 0x987);
- EXPECT_EQ(written, 3);
- ASSERT_STREQ(buff, "987");
+ ASSERT_STREQ_LEN(written, buff, "987");
// Precision Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%x", 0);
- EXPECT_EQ(written, 1);
- ASSERT_STREQ(buff, "0");
+ ASSERT_STREQ_LEN(written, buff, "0");
written = LIBC_NAMESPACE::sprintf(buff, "%.0x", 0);
- EXPECT_EQ(written, 0);
- ASSERT_STREQ(buff, "");
+ ASSERT_STREQ_LEN(written, buff, "");
written = LIBC_NAMESPACE::sprintf(buff, "%.5x", 0x1F3);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "001f3");
+ ASSERT_STREQ_LEN(written, buff, "001f3");
written = LIBC_NAMESPACE::sprintf(buff, "%.2x", 0x135);
- EXPECT_EQ(written, 3);
- ASSERT_STREQ(buff, "135");
+ ASSERT_STREQ_LEN(written, buff, "135");
// Flag Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%-5x", 0x246);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "246 ");
+ ASSERT_STREQ_LEN(written, buff, "246 ");
written = LIBC_NAMESPACE::sprintf(buff, "%#x", 0xd3f);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "0xd3f");
+ ASSERT_STREQ_LEN(written, buff, "0xd3f");
written = LIBC_NAMESPACE::sprintf(buff, "%#x", 0);
- EXPECT_EQ(written, 1);
- ASSERT_STREQ(buff, "0");
+ ASSERT_STREQ_LEN(written, buff, "0");
written = LIBC_NAMESPACE::sprintf(buff, "%#X", 0xE40);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "0XE40");
+ ASSERT_STREQ_LEN(written, buff, "0XE40");
written = LIBC_NAMESPACE::sprintf(buff, "%05x", 0x470);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "00470");
+ ASSERT_STREQ_LEN(written, buff, "00470");
written = LIBC_NAMESPACE::sprintf(buff, "%0#6x", 0x8c3);
- EXPECT_EQ(written, 6);
- ASSERT_STREQ(buff, "0x08c3");
+ ASSERT_STREQ_LEN(written, buff, "0x08c3");
written = LIBC_NAMESPACE::sprintf(buff, "%-#6x", 0x5f0);
- EXPECT_EQ(written, 6);
- ASSERT_STREQ(buff, "0x5f0 ");
+ ASSERT_STREQ_LEN(written, buff, "0x5f0 ");
// Combined Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%#-07x", 0x703);
- EXPECT_EQ(written, 7);
- ASSERT_STREQ(buff, "0x703 ");
+ ASSERT_STREQ_LEN(written, buff, "0x703 ");
written = LIBC_NAMESPACE::sprintf(buff, "%7.5x", 0x814);
- EXPECT_EQ(written, 7);
- ASSERT_STREQ(buff, " 00814");
+ ASSERT_STREQ_LEN(written, buff, " 00814");
written = LIBC_NAMESPACE::sprintf(buff, "%#9.5X", 0x9d4);
- EXPECT_EQ(written, 9);
- ASSERT_STREQ(buff, " 0X009D4");
+ ASSERT_STREQ_LEN(written, buff, " 0X009D4");
written = LIBC_NAMESPACE::sprintf(buff, "%#.x", 0);
- EXPECT_EQ(written, 0);
- ASSERT_STREQ(buff, "");
+ ASSERT_STREQ_LEN(written, buff, "");
written = LIBC_NAMESPACE::sprintf(buff, "%-7.5x", 0x260);
- EXPECT_EQ(written, 7);
- ASSERT_STREQ(buff, "00260 ");
+ ASSERT_STREQ_LEN(written, buff, "00260 ");
written = LIBC_NAMESPACE::sprintf(buff, "%5.4x", 0x10000);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "10000");
+ ASSERT_STREQ_LEN(written, buff, "10000");
// Multiple Conversion Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%10X %-#10x", 0x45b, 0x789);
- EXPECT_EQ(written, 21);
- ASSERT_STREQ(buff, " 45B 0x789 ");
+ ASSERT_STREQ_LEN(written, buff, " 45B 0x789 ");
written = LIBC_NAMESPACE::sprintf(buff, "%-5.4x%#.4x", 0x75, 0x25);
- EXPECT_EQ(written, 11);
- ASSERT_STREQ(buff, "0075 0x0025");
+ ASSERT_STREQ_LEN(written, buff, "0075 0x0025");
written = LIBC_NAMESPACE::sprintf(buff, "%04hhX %#.5llx %-6.3zX", 256 + 0x7f,
0x1000000000ll, size_t(2));
- EXPECT_EQ(written, 24);
- ASSERT_STREQ(buff, "007F 0x1000000000 002 ");
+ ASSERT_STREQ_LEN(written, buff, "007F 0x1000000000 002 ");
}
TEST(LlvmLibcSPrintfTest, BinConv) {
@@ -501,110 +410,86 @@ TEST(LlvmLibcSPrintfTest, BinConv) {
// Basic Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%b", 42);
- EXPECT_EQ(written, 6);
- ASSERT_STREQ(buff, "101010");
+ ASSERT_STREQ_LEN(written, buff, "101010");
written = LIBC_NAMESPACE::sprintf(buff, "%B", 12081991);
- EXPECT_EQ(written, 24);
- ASSERT_STREQ(buff, "101110000101101101000111");
+ ASSERT_STREQ_LEN(written, buff, "101110000101101101000111");
// Min Width Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%10b", 0b101010);
- EXPECT_EQ(written, 10);
- ASSERT_STREQ(buff, " 101010");
+ ASSERT_STREQ_LEN(written, buff, " 101010");
written = LIBC_NAMESPACE::sprintf(buff, "%2B", 0b101010);
- EXPECT_EQ(written, 6);
- ASSERT_STREQ(buff, "101010");
+ ASSERT_STREQ_LEN(written, buff, "101010");
// Precision Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%b", 0);
- EXPECT_EQ(written, 1);
- ASSERT_STREQ(buff, "0");
+ ASSERT_STREQ_LEN(written, buff, "0");
written = LIBC_NAMESPACE::sprintf(buff, "%.0b", 0);
- EXPECT_EQ(written, 0);
- ASSERT_STREQ(buff, "");
+ ASSERT_STREQ_LEN(written, buff, "");
written = LIBC_NAMESPACE::sprintf(buff, "%.5b", 0b111);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "00111");
+ ASSERT_STREQ_LEN(written, buff, "00111");
written = LIBC_NAMESPACE::sprintf(buff, "%.2b", 0b111);
- EXPECT_EQ(written, 3);
- ASSERT_STREQ(buff, "111");
+ ASSERT_STREQ_LEN(written, buff, "111");
written = LIBC_NAMESPACE::sprintf(buff, "%3b", 0b111);
- EXPECT_EQ(written, 3);
- ASSERT_STREQ(buff, "111");
+ ASSERT_STREQ_LEN(written, buff, "111");
// Flag Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%-5b", 0b111);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "111 ");
+ ASSERT_STREQ_LEN(written, buff, "111 ");
written = LIBC_NAMESPACE::sprintf(buff, "%#b", 0b111);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "0b111");
+ ASSERT_STREQ_LEN(written, buff, "0b111");
written = LIBC_NAMESPACE::sprintf(buff, "%#b", 0);
- EXPECT_EQ(written, 1);
- ASSERT_STREQ(buff, "0");
+ ASSERT_STREQ_LEN(written, buff, "0");
written = LIBC_NAMESPACE::sprintf(buff, "%#B", 0b111);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "0B111");
+ ASSERT_STREQ_LEN(written, buff, "0B111");
written = LIBC_NAMESPACE::sprintf(buff, "%05b", 0b111);
- EXPECT_EQ(written, 5);
- ASSERT_STREQ(buff, "00111");
+ ASSERT_STREQ_LEN(written, buff, "00111");
written = LIBC_NAMESPACE::sprintf(buff, "%0#6b", 0b111);
- EXPECT_EQ(written, 6);
- ASSERT_STREQ(buff, "0b0111");
+ ASSERT_STREQ_L...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/110759
More information about the libc-commits
mailing list