Skip to content

Commit

Permalink
[core][utest] fix Print/test_print.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
mysterywolf committed Jan 16, 2025
1 parent cbe8ed9 commit b47dfdf
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 67 deletions.
3 changes: 2 additions & 1 deletion core/utest/Print/mock/PrintMock.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class PrintMock : public Print
{
public:
String _str;
size_t write(uint8_t b) { _str += static_cast<char>(b); return 1;};
void flush() { _str = String(); }
size_t write(uint8_t b) { _str += static_cast<char>(b); return 1; };
void mock_setWriteError() { setWriteError(); }
void mock_setWriteError(int err) { setWriteError(err); }
};
Expand Down
110 changes: 59 additions & 51 deletions core/utest/Print/test_print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ TEST_CASE ("Print::print(char)", "[Print-print-01]")
PrintMock mock;

mock.print('A');

REQUIRE(mock._str == "A");
mock.flush();
}

TEST_CASE ("Print::print(const String &)", "[Print-print-02]")
Expand All @@ -23,8 +23,8 @@ TEST_CASE ("Print::print(const String &)", "[Print-print-02]")
String const str("Test String");

mock.print(str);

REQUIRE(mock._str == "Test String");
mock.flush();
}

TEST_CASE ("Print::print(const char str[])", "[Print-print-03]")
Expand All @@ -33,8 +33,8 @@ TEST_CASE ("Print::print(const char str[])", "[Print-print-03]")
const char str[] = "Test String";

mock.print(str);

REQUIRE(mock._str == "Test String");
mock.flush();
}

TEST_CASE ("Print::print(int, int = DEC|HEX|OCT|BIN)", "[Print-print-04]")
Expand All @@ -43,58 +43,66 @@ TEST_CASE ("Print::print(int, int = DEC|HEX|OCT|BIN)", "[Print-print-04]")

int const val = -1;

WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "-1"); }
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "FFFFFFFFFFFFFFFF"); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "1777777777777777777777"); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "1111111111111111111111111111111111111111111111111111111111111111"); }
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "-1"); mock.flush(); }
#ifdef ARCH_CPU_64BIT
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "FFFFFFFFFFFFFFFF"); mock.flush(); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "1777777777777777777777"); mock.flush(); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "1111111111111111111111111111111111111111111111111111111111111111"); mock.flush(); }
#else
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "FFFFFFFF"); mock.flush(); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "37777777777"); mock.flush(); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "11111111111111111111111111111111"); mock.flush(); }
#endif /* ARCH_CPU_64BIT */
}

TEST_CASE ("Print::print(unsigned int, int = DEC|HEX|OCT|BIN)", "[Print-print-05]")
{
PrintMock mock;

unsigned int 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"); }
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "17"); mock.flush(); }
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "11"); mock.flush(); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "21"); mock.flush(); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "10001"); mock.flush(); }
}

TEST_CASE ("Print::print(long, int = DEC|HEX|OCT|BIN)", "[Print-print-06]")
{
PrintMock mock;

long const val = -1;

WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "-1"); }
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "FFFFFFFFFFFFFFFF"); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "1777777777777777777777"); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "1111111111111111111111111111111111111111111111111111111111111111"); }
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "-1"); mock.flush(); }
#ifdef ARCH_CPU_64BIT
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "FFFFFFFFFFFFFFFF"); mock.flush(); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "1777777777777777777777"); mock.flush(); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "1111111111111111111111111111111111111111111111111111111111111111"); mock.flush(); }
#else
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "FFFFFFFF"); mock.flush(); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "37777777777"); mock.flush(); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "11111111111111111111111111111111"); mock.flush(); }
#endif /* ARCH_CPU_64BIT */
}

TEST_CASE ("Print::print(unsigned long, int = DEC|HEX|OCT|BIN)", "[Print-print-07]")
{
PrintMock mock;

unsigned 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"); }
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "17"); mock.flush(); }
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "11"); mock.flush(); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "21"); mock.flush(); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "10001"); mock.flush(); }
}

TEST_CASE ("Print::print(long long, int = DEC|HEX|OCT|BIN)", "[Print-print-08]")
{
PrintMock mock;

long long const val = -1;

WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "-1"); }
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "FFFFFFFFFFFFFFFF"); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "1777777777777777777777"); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "1111111111111111111111111111111111111111111111111111111111111111"); }
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "-1"); mock.flush(); }
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "FFFFFFFFFFFFFFFF"); mock.flush(); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "1777777777777777777777"); mock.flush(); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "1111111111111111111111111111111111111111111111111111111111111111"); mock.flush(); }
}

TEST_CASE ("Print::print(unsigned long long, int = DEC|HEX|OCT|BIN)", "[Print-print-09]")
Expand All @@ -105,19 +113,19 @@ TEST_CASE ("Print::print(unsigned long long, int = DEC|HEX|OCT|BIN)", "[Print-pr
{
unsigned long long const val = 0;

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"); }
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "0"); mock.flush(); }
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "0"); mock.flush(); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "0"); mock.flush(); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "0"); mock.flush(); }
}
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"); }
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "17"); mock.flush(); }
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "11"); mock.flush(); }
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "21"); mock.flush(); }
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "10001"); mock.flush(); }
}
}

Expand All @@ -128,28 +136,28 @@ TEST_CASE ("Print::print(double, int = 2)", "[Print-print-10]")
WHEN ("val is a positive floating point value")
{
double const val = 3.1459;
WHEN("digits = 0") { mock.print(val, 0); REQUIRE(mock._str == "3"); }
WHEN("digits = 1") { mock.print(val, 1); REQUIRE(mock._str == "3.1"); }
WHEN("digits = 2 (default)") { mock.print(val); REQUIRE(mock._str == "3.15"); }
WHEN("digits = 3") { mock.print(val, 3); REQUIRE(mock._str == "3.146"); }
WHEN("digits = 4") { mock.print(val, 4); REQUIRE(mock._str == "3.1459"); }
WHEN("digits = 5") { mock.print(val, 5); REQUIRE(mock._str == "3.14590"); }
WHEN("digits = 0") { mock.print(val, 0); REQUIRE(mock._str == "3"); mock.flush(); }
WHEN("digits = 1") { mock.print(val, 1); REQUIRE(mock._str == "3.1"); mock.flush(); }
WHEN("digits = 2 (default)") { mock.print(val); REQUIRE(mock._str == "3.15"); mock.flush(); }
WHEN("digits = 3") { mock.print(val, 3); REQUIRE(mock._str == "3.146"); mock.flush(); }
WHEN("digits = 4") { mock.print(val, 4); REQUIRE(mock._str == "3.1459"); mock.flush(); }
WHEN("digits = 5") { mock.print(val, 5); REQUIRE(mock._str == "3.14590"); mock.flush(); }
}

WHEN ("digits are negative")
{
double const val = 3.1459;
WHEN("digits = -1") { mock.print(val, -1); REQUIRE(mock._str == "3.15"); }
WHEN("digits = -1") { mock.print(val, -1); REQUIRE(mock._str == "3.15"); mock.flush(); }
}

WHEN ("val is a negative floating point value")
{
double const val = -3.1459;
WHEN("digits = 2 (default)") { mock.print(val); REQUIRE(mock._str == "-3.15"); }
WHEN("digits = 2 (default)") { mock.print(val); REQUIRE(mock._str == "-3.15"); mock.flush(); }
}

WHEN ("val is NAN") { mock.print(NAN); REQUIRE(mock._str == "nan"); }
WHEN ("val is INFINITY") { mock.print(INFINITY); REQUIRE(mock._str == "inf"); }
WHEN ("val is NAN") { mock.print(NAN); REQUIRE(mock._str == "nan"); mock.flush(); }
WHEN ("val is INFINITY") { mock.print(INFINITY); REQUIRE(mock._str == "inf"); mock.flush(); }
}

TEST_CASE ("Print::print(Printable)", "[Print-print-11]")
Expand All @@ -160,18 +168,18 @@ TEST_CASE ("Print::print(Printable)", "[Print-print-11]")
printable._i = 1;

mock.print(printable);

REQUIRE(mock._str == "PrintableMock i = 1");
mock.flush();
}

TEST_CASE ("Print::print(unsigned char, int)", "[Print-print-12]")
{
PrintMock mock;

WHEN("DEC") { mock.print('A', DEC); REQUIRE(mock._str == "65"); }
WHEN("HEX") { mock.print('A', HEX); REQUIRE(mock._str == "41"); }
WHEN("OCT") { mock.print('A', OCT); REQUIRE(mock._str == "101"); }
WHEN("BIN") { mock.print('A', BIN); REQUIRE(mock._str == "1000001"); }
WHEN("DEC") { mock.print('A', DEC); REQUIRE(mock._str == "65"); mock.flush(); }
WHEN("HEX") { mock.print('A', HEX); REQUIRE(mock._str == "41"); mock.flush(); }
WHEN("OCT") { mock.print('A', OCT); REQUIRE(mock._str == "101"); mock.flush(); }
WHEN("BIN") { mock.print('A', BIN); REQUIRE(mock._str == "1000001"); mock.flush(); }
}

TEST_CASE ("Testing Print::print(const __FlashStringHelper *)", "[Print-print-13]")
Expand All @@ -181,6 +189,6 @@ TEST_CASE ("Testing Print::print(const __FlashStringHelper *)", "[Print-print-13
PrintMock mock;

mock.print(F("Hello flash string"));

REQUIRE(mock._str == "Hello flash string");
mock.flush();
}
26 changes: 13 additions & 13 deletions core/utest/Print/test_println.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ TEST_CASE ("Print::println(char)", "[Print-println-01]")
PrintMock mock;

mock.println('A');

REQUIRE(mock._str == "A\r\n");
mock.flush();
}

TEST_CASE ("Print::println(const String &)", "[Print-println-02]")
Expand All @@ -23,8 +23,8 @@ TEST_CASE ("Print::println(const String &)", "[Print-println-02]")
String const str("Test String");

mock.println(str);

REQUIRE(mock._str == "Test String\r\n");
mock.flush();
}

TEST_CASE ("Print::println(const char str[])", "[Print-println-03]")
Expand All @@ -33,8 +33,8 @@ TEST_CASE ("Print::println(const char str[])", "[Print-println-03]")
const char str[] = "Test String";

mock.println(str);

REQUIRE(mock._str == "Test String\r\n");
mock.flush();
}

TEST_CASE ("Print::println(int, int = DEC (default))", "[Print-println-04]")
Expand All @@ -43,8 +43,8 @@ TEST_CASE ("Print::println(int, int = DEC (default))", "[Print-println-04]")
int const val = -1;

mock.println(val);

REQUIRE(mock._str == "-1\r\n");
mock.flush();
}

TEST_CASE ("Print::println(unsigned int, int = DEC (default))", "[Print-println-05]")
Expand All @@ -53,8 +53,8 @@ TEST_CASE ("Print::println(unsigned int, int = DEC (default))", "[Print-println-
unsigned int const val = 17;

mock.println(val);

REQUIRE(mock._str == "17\r\n");
mock.flush();
}

TEST_CASE ("Print::println(long, int = DEC (default))", "[Print-println-06]")
Expand All @@ -63,8 +63,8 @@ TEST_CASE ("Print::println(long, int = DEC (default))", "[Print-println-06]")
long const val = -1;

mock.println(val);

REQUIRE(mock._str == "-1\r\n");
mock.flush();
}

TEST_CASE ("Print::println(unsigned long, int = DEC (default))", "[Print-println-07]")
Expand All @@ -73,8 +73,8 @@ TEST_CASE ("Print::println(unsigned long, int = DEC (default))", "[Print-println
unsigned long const val = 17;

mock.println(val);

REQUIRE(mock._str == "17\r\n");
mock.flush();
}

TEST_CASE ("Print::println(long long, int = DEC (default))", "[Print-println-08]")
Expand All @@ -83,8 +83,8 @@ TEST_CASE ("Print::println(long long, int = DEC (default))", "[Print-println-08]
long long const val = -1;

mock.println(val);

REQUIRE(mock._str == "-1\r\n");
mock.flush();
}

TEST_CASE ("Print::println(unsigned long long, int = DEC|HEX|OCT|BIN)", "[Print-println-09]")
Expand All @@ -93,8 +93,8 @@ TEST_CASE ("Print::println(unsigned long long, int = DEC|HEX|OCT|BIN)", "[Print-
unsigned long long const val = 17;

mock.println(val);

REQUIRE(mock._str == "17\r\n");
mock.flush();
}

TEST_CASE ("Print::println(double, int = 2)", "[Print-println-10]")
Expand All @@ -103,8 +103,8 @@ TEST_CASE ("Print::println(double, int = 2)", "[Print-println-10]")
double const val = 3.1459;

mock.println(val);

REQUIRE(mock._str == "3.15\r\n");
mock.flush();
}

TEST_CASE ("Print::println(Printable)", "[Print-println-11]")
Expand All @@ -114,17 +114,17 @@ TEST_CASE ("Print::println(Printable)", "[Print-println-11]")
printable._i = 1;

mock.println(printable);

REQUIRE(mock._str == "PrintableMock i = 1\r\n");
mock.flush();
}

TEST_CASE ("Print::println(unsigned char, int base = DEC (default))", "[Print-println-12]")
{
PrintMock mock;

mock.println('A', DEC);

REQUIRE(mock._str == "65\r\n");
mock.flush();
}

TEST_CASE ("Testing Print::println(const __FlashStringHelper *)", "[Print-println-13]")
Expand All @@ -134,6 +134,6 @@ TEST_CASE ("Testing Print::println(const __FlashStringHelper *)", "[Print-printl
PrintMock mock;

mock.println(F("Hello flash string"));

REQUIRE(mock._str == "Hello flash string\r\n");
mock.flush();
}
2 changes: 1 addition & 1 deletion core/utest/TC_Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static void TC_getWriteError(void)

static void TC_print(void)
{
// #include "Print/test_print.cpp"
#include "Print/test_print.cpp"
}

static void TC_println(void)
Expand Down
2 changes: 1 addition & 1 deletion core/utest/catch_compatible.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <utest.h>

#define TEST_CASE(name, tags) LOG_I("[==========] [ TESTCASE ] %s", name);
#define TEST_CASE(name, tags) LOG_I("[==========] %s %s", tags, name);
#define WHEN(name) LOG_I("[==========] [ WHEN ] %s", name);
#define THEN(name) LOG_I("[==========] [ THEN ] %s", name);
#define GIVEN(name) LOG_I("[==========] [ GIVEN ] %s", name);
Expand Down

0 comments on commit b47dfdf

Please sign in to comment.