-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Support C23 'b' (binary) modifier in printf #80851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ef3ebe5
[libc] Support C23 'b' (binary) modifier in printf
agentcooper f1672b5
Handle uppercase, refactor num_buf_size, add more tests
agentcooper 097f8a7
Fix array size
agentcooper b320757
Binary does not need uppercase
agentcooper 3142fdb
Fix return statement
agentcooper 76841b1
Fix broken syntax after a merge conflict
agentcooper 722f178
Update test case
agentcooper 8036745
Add precision test
agentcooper 14a2e68
Move binary test to the correct place
agentcooper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -410,6 +410,119 @@ TEST(LlvmLibcSPrintfTest, HexConv) { | |
ASSERT_STREQ(buff, "007F 0x1000000000 002 "); | ||
} | ||
|
||
TEST(LlvmLibcSPrintfTest, BinConv) { | ||
char buff[64]; | ||
int written; | ||
|
||
// Basic Tests. | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%b", 42); | ||
EXPECT_EQ(written, 6); | ||
ASSERT_STREQ(buff, "101010"); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%B", 12081991); | ||
EXPECT_EQ(written, 24); | ||
ASSERT_STREQ(buff, "101110000101101101000111"); | ||
|
||
// Min Width Tests. | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%10b", 0b101010); | ||
EXPECT_EQ(written, 10); | ||
ASSERT_STREQ(buff, " 101010"); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%2B", 0b101010); | ||
EXPECT_EQ(written, 6); | ||
ASSERT_STREQ(buff, "101010"); | ||
|
||
// Precision Tests. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: It would be good to have a test where the precision equals the number of digits, such as ("%3b", 0b111) -> "111" |
||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%b", 0); | ||
EXPECT_EQ(written, 1); | ||
ASSERT_STREQ(buff, "0"); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%.0b", 0); | ||
EXPECT_EQ(written, 0); | ||
ASSERT_STREQ(buff, ""); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%.5b", 0b111); | ||
EXPECT_EQ(written, 5); | ||
ASSERT_STREQ(buff, "00111"); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%.2b", 0b111); | ||
EXPECT_EQ(written, 3); | ||
ASSERT_STREQ(buff, "111"); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%3b", 0b111); | ||
EXPECT_EQ(written, 3); | ||
ASSERT_STREQ(buff, "111"); | ||
|
||
// Flag Tests. | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%-5b", 0b111); | ||
EXPECT_EQ(written, 5); | ||
ASSERT_STREQ(buff, "111 "); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%#b", 0b111); | ||
EXPECT_EQ(written, 5); | ||
ASSERT_STREQ(buff, "0b111"); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%#b", 0); | ||
EXPECT_EQ(written, 1); | ||
ASSERT_STREQ(buff, "0"); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%#B", 0b111); | ||
EXPECT_EQ(written, 5); | ||
ASSERT_STREQ(buff, "0B111"); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%05b", 0b111); | ||
EXPECT_EQ(written, 5); | ||
ASSERT_STREQ(buff, "00111"); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%0#6b", 0b111); | ||
EXPECT_EQ(written, 6); | ||
ASSERT_STREQ(buff, "0b0111"); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%-#6b", 0b111); | ||
EXPECT_EQ(written, 6); | ||
ASSERT_STREQ(buff, "0b111 "); | ||
|
||
// Combined Tests. | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%#-07b", 0b111); | ||
EXPECT_EQ(written, 7); | ||
ASSERT_STREQ(buff, "0b111 "); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%7.5b", 0b111); | ||
EXPECT_EQ(written, 7); | ||
ASSERT_STREQ(buff, " 00111"); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%#9.5B", 0b111); | ||
EXPECT_EQ(written, 9); | ||
ASSERT_STREQ(buff, " 0B00111"); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%#.b", 0); | ||
EXPECT_EQ(written, 0); | ||
ASSERT_STREQ(buff, ""); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%-7.5b", 0b111); | ||
EXPECT_EQ(written, 7); | ||
ASSERT_STREQ(buff, "00111 "); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%5.4b", 0b1111); | ||
EXPECT_EQ(written, 5); | ||
ASSERT_STREQ(buff, " 1111"); | ||
|
||
// Multiple Conversion Tests. | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%10B %-#10b", 0b101, 0b110); | ||
EXPECT_EQ(written, 21); | ||
ASSERT_STREQ(buff, " 101 0b110 "); | ||
|
||
written = LIBC_NAMESPACE::sprintf(buff, "%-5.4b%#.4b", 0b101, 0b110); | ||
EXPECT_EQ(written, 11); | ||
ASSERT_STREQ(buff, "0101 0b0110"); | ||
} | ||
|
||
TEST(LlvmLibcSPrintfTest, PointerConv) { | ||
char buff[64]; | ||
int written; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.