Skip to content

Commit

Permalink
Merge pull request #203 from arduino/fix-178
Browse files Browse the repository at this point in the history
Fix of empty string printed in case of a 64-bit unsigned integer.
  • Loading branch information
aentinger authored Sep 4, 2023
2 parents b052976 + 754aa9b commit 267bab1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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

0 comments on commit 267bab1

Please sign in to comment.