Skip to content
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

Fix of empty string printed in case of a 64-bit unsigned integer. #203

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t i = 0;
uint8_t innerLoops = 0;

// Special case workaround https://github.com/arduino/ArduinoCore-API/issues/178
if (n64 == 0) {
write('0');
return 1;
}

// prevent crash if called with base == 1
if (base < 2) base = 10;

Expand Down
22 changes: 17 additions & 5 deletions test/src/Print/test_print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,24 @@ TEST_CASE ("Print::print(unsigned long long, int = DEC|HEX|OCT|BIN)", "[Print-pr
{
PrintMock mock;

unsigned long long const val = 17;
GIVEN("a value of zero ...")
{
unsigned long long const val = 0;

WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "17"); }
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "11"); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "21"); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "10001"); }
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "0"); }
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "0"); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "0"); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "0"); }
}
GIVEN("a non-zero value ...")
{
unsigned long long const val = 17;

WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "17"); }
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "11"); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "21"); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "10001"); }
}
}

TEST_CASE ("Print::print(double, int = 2)", "[Print-print-10]")
Expand Down
Loading