Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Use == for string comparison in tests
This replaces assertions that previously checked the return value of
strcmp or compareTo, giving the testing framework a bit more information
on the actual comparison happening, so it can show the actual strings
it compares (instead of just the strcmp or compareTo return value).

This changes errors like:

    REQUIRE( strcmp(str.c_str(), "ABC") == 0 )
    with expansion:
    220 == 0

into:

    REQUIRE( str == "ABC" );
    with expansion:
    "XYZ" equals: "ABC"

These changes were done using the following commands:

    sed -i 's/REQUIRE(strcmp(\([^,]*\).c_str(), \([^,]*\).c_str()) == 0)/REQUIRE(\1 == \2)/g' *
    sed -i 's/REQUIRE(strcmp(\([^,]*\).c_str(), \([^,]*\)) == 0)/REQUIRE(\1 == \2)/g' *
    sed -i 's/REQUIRE(\([^.]*\).compareTo(\([^)]*\)) == 0)/REQUIRE(\1 == \2)/g' test_String.cpp test_characterAccessFunc.cpp test_operators.cpp test_substring.cpp

Note that test_compareTo.cpp was excluded, since that actually needs to
test compareTo.

Additionally, two more lines were changed manually (one where the
Arduino string and cstr were reversed, one where compareTo needed to
return non-zero).

Also note that this relies on the operator == defined by String itself,
but since that is subject of its own tests, this should be ok to use
in other tests.
  • Loading branch information
matthijskooijman committed Jan 25, 2021
commit c64e2c46b6ab37cf79a6733a0c6b692c6ada0f60
38 changes: 19 additions & 19 deletions test/src/String/test_String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,82 +22,82 @@ TEST_CASE ("Testing String(const char *) constructor()", "[String-Ctor-01]")
{
char const CSTR[] = "Hello Arduino String Class";
arduino::String str(CSTR);
REQUIRE(strcmp(CSTR, str.c_str()) == 0);
REQUIRE(str == CSTR);
}

TEST_CASE ("Testing String(const String &) constructor()", "[String-Ctor-02]")
{
arduino::String str1("Hello Arduino String class"),
str2(str1);
REQUIRE(strcmp(str1.c_str(), str2.c_str()) == 0);
REQUIRE(str1 == str2);
}

TEST_CASE ("Testing String(const __FlashStringHelper) constructor()", "[String-Ctor-03]")
{
#undef F
#define F(string_literal) (reinterpret_cast<const arduino::__FlashStringHelper *>(PSTR(string_literal)))
arduino::String str1(F("Hello"));
REQUIRE(str1.compareTo("Hello") == 0);
REQUIRE(str1 == "Hello");
}

TEST_CASE ("Testing String(char) constructor()", "[String-Ctor-04]")
{
char const ch = 'A';
arduino::String str(ch);
REQUIRE(strcmp(str.c_str(), "A") == 0);
REQUIRE(str == "A");
}

TEST_CASE ("Testing String(unsigned char, unsigned char base = 10) constructor()", "[String-Ctor-05]")
{
unsigned char const val = 1;
arduino::String str(val);
REQUIRE(strcmp(str.c_str(), "1") == 0);
REQUIRE(str == "1");
}

TEST_CASE ("Testing String(int, unsigned char base = 10) constructor()", "[String-Ctor-06]")
{
int const val = -1;
arduino::String str(val);
REQUIRE(strcmp(str.c_str(), "-1") == 0);
REQUIRE(str == "-1");
}

TEST_CASE ("Testing String(unsigned int, unsigned char base = 10) constructor()", "[String-Ctor-07]")
{
unsigned int const val = 1;
arduino::String str(val);
REQUIRE(strcmp(str.c_str(), "1") == 0);
REQUIRE(str == "1");
}

TEST_CASE ("Testing String(long, unsigned char base = 10) constructor()", "[String-Ctor-08]")
{
long const val = -1;
arduino::String str(val);
REQUIRE(strcmp(str.c_str(), "-1") == 0);
REQUIRE(str == "-1");
}

TEST_CASE ("Testing String(unsigned long, unsigned char base = 10) constructor()", "[String-Ctor-09]")
{
unsigned long const val = 1;
arduino::String str(val);
REQUIRE(strcmp(str.c_str(), "1") == 0);
REQUIRE(str == "1");
}

TEST_CASE ("Testing String(float, unsigned char decimalPlaces = 2) constructor()", "[String-Ctor-10]")
{
WHEN ("String::String (some float value)")
{
arduino::String str(1.234f);
REQUIRE(strcmp(str.c_str(), "1.23") == 0);
REQUIRE(str == "1.23");
}
WHEN ("String::String (FLT_MAX)")
{
arduino::String str(FLT_MAX);
REQUIRE(strcmp(str.c_str(), "340282346638528859811704183484516925440.00") == 0);
REQUIRE(str == "340282346638528859811704183484516925440.00");
}
WHEN ("String::String (-FLT_MAX)")
{
arduino::String str(-FLT_MAX);
REQUIRE(strcmp(str.c_str(), "-340282346638528859811704183484516925440.00") == 0);
REQUIRE(str == "-340282346638528859811704183484516925440.00");
}
}

Expand All @@ -106,17 +106,17 @@ TEST_CASE ("Testing String(double, unsigned char decimalPlaces = 2) constructor(
WHEN ("String::String (some double value)")
{
arduino::String str(5.678);
REQUIRE(strcmp(str.c_str(), "5.68") == 0);
REQUIRE(str == "5.68");
}
WHEN ("String::String (DBL_MAX)")
{
arduino::String str(DBL_MAX);
REQUIRE(strcmp(str.c_str(), "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00") == 0);
REQUIRE(str == "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00");
}
WHEN ("String::String (-DBL_MAX)")
{
arduino::String str(-DBL_MAX);
REQUIRE(strcmp(str.c_str(), "-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00") == 0);
REQUIRE(str == "-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00");
}
}

Expand All @@ -135,28 +135,28 @@ TEST_CASE ("Testing String(StringSumHelper &&) constructor()", "[String-Ctor-13]
arduino::String str("Hello");
char const ch = '!';
arduino::String str1(static_cast<arduino::StringSumHelper&&>(str+ch));
REQUIRE(str1.compareTo("Hello!") == 0);
REQUIRE(str1 == "Hello!");
}

TEST_CASE ("Testing String(String &&) constructor()", "[String-Ctor-14]")
{
arduino::String str("Hello");
arduino::String str1(static_cast<arduino::String&&>(str));
REQUIRE(str1.compareTo("Hello") == 0);
REQUIRE(str1 == "Hello");
}

TEST_CASE ("Testing String(String &&) with move(String &rhs) from smaller to larger buffer", "[String-Ctor-15]")
{
arduino::String str("Hello");
arduino::String str1("Arduino");
str1 = static_cast<arduino::String&&>(str);
REQUIRE(str1.compareTo("Hello") == 0);
REQUIRE(str1 == "Hello");
}

TEST_CASE ("Testing String(String &&) with move(String &rhs) from larger to smaller buffer", "[String-Ctor-16]")
{
arduino::String str("Hello");
arduino::String str1("Arduino");
str = static_cast<arduino::String&&>(str1);
REQUIRE(str.compareTo("Arduino") == 0);
REQUIRE(str == "Arduino");
}
2 changes: 1 addition & 1 deletion test/src/String/test_characterAccessFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TEST_CASE ("Testing String::setCharAt(unsigned int, char )", "[String-setCharAt-
{
arduino::String str1("Hello");
str1.setCharAt(1, 'a');
REQUIRE(str1.compareTo("Hallo") == 0);
REQUIRE(str1 == "Hallo");
}

TEST_CASE ("Testing String::getBytes(unsigned char, unsigned int, unsigned int)", "[String-getBytes-02]")
Expand Down
22 changes: 11 additions & 11 deletions test/src/String/test_concat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,78 +20,78 @@ TEST_CASE ("Testing String::concat(const String &)", "[String-concat-01]")
{
arduino::String str1("Hello "), str2("Arduino!");
REQUIRE(str1.concat(str2) == 1);
REQUIRE(strcmp(str1.c_str(), "Hello Arduino!") == 0);
REQUIRE(str1 == "Hello Arduino!");
}

TEST_CASE ("Testing String::concat(const char *)", "[String-concat-02]")
{
arduino::String str("Hello ");
REQUIRE(str.concat("Arduino!") == 1);
REQUIRE(strcmp(str.c_str(), "Hello Arduino!") == 0);
REQUIRE(str == "Hello Arduino!");
}

TEST_CASE ("Testing String::concat(char)", "[String-concat-03]")
{
arduino::String str("Hello ");
char const c = 'A';
REQUIRE(str.concat(c) == 1);
REQUIRE(strcmp(str.c_str(), "Hello A") == 0);
REQUIRE(str == "Hello A");
}

TEST_CASE ("Testing String::concat(unsigned char)", "[String-concat-04]")
{
arduino::String str("Hello ");
unsigned char const c = 'A';
REQUIRE(str.concat(c) == 1);
REQUIRE(strcmp(str.c_str(), "Hello 65") == 0); /* ASCII['A'] = 65 */
REQUIRE(str == "Hello 65"); /* ASCII['A'] = 65 */
}

TEST_CASE ("Testing String::concat(int)", "[String-concat-05]")
{
arduino::String str("Hello ");
int const num = -1;
REQUIRE(str.concat(num) == 1);
REQUIRE(strcmp(str.c_str(), "Hello -1") == 0);
REQUIRE(str == "Hello -1");
}

TEST_CASE ("Testing String::concat(unsigned int)", "[String-concat-06]")
{
arduino::String str("Hello ");
unsigned int const num = 1;
REQUIRE(str.concat(num) == 1);
REQUIRE(strcmp(str.c_str(), "Hello 1") == 0);
REQUIRE(str == "Hello 1");
}

TEST_CASE ("Testing String::concat(long)", "[String-concat-07]")
{
arduino::String str("Hello ");
long const num = -1;
REQUIRE(str.concat(num) == 1);
REQUIRE(strcmp(str.c_str(), "Hello -1") == 0);
REQUIRE(str == "Hello -1");
}

TEST_CASE ("Testing String::concat(unsigned long)", "[String-concat-08]")
{
arduino::String str("Hello ");
unsigned long const num = 1;
REQUIRE(str.concat(num) == 1);
REQUIRE(strcmp(str.c_str(), "Hello 1") == 0);
REQUIRE(str == "Hello 1");
}

TEST_CASE ("Testing String::concat(float)", "[String-concat-09]")
{
arduino::String str("Hello ");
float const num = 1.234f;
REQUIRE(str.concat(num) == 1);
REQUIRE(strcmp(str.c_str(), "Hello 1.23") == 0);
REQUIRE(str == "Hello 1.23");
}

TEST_CASE ("Testing String::concat(double)", "[String-concat-10]")
{
arduino::String str("Hello ");
double const num = 5.678;
REQUIRE(str.concat(num) == 1);
REQUIRE(strcmp(str.c_str(), "Hello 5.68") == 0);
REQUIRE(str == "Hello 5.68");
}

TEST_CASE ("Testing String::concat(const __FlashStringHelper *)", "[String-concat-11]")
Expand All @@ -100,5 +100,5 @@ TEST_CASE ("Testing String::concat(const __FlashStringHelper *)", "[String-conca
#define F(string_literal) (reinterpret_cast<const arduino::__FlashStringHelper *>(PSTR(string_literal)))
arduino::String str1("Hello");
REQUIRE(str1.concat(F(" Arduino")) == 1);
REQUIRE(strcmp(str1.c_str(), "Hello Arduino") == 0);
REQUIRE(str1 == "Hello Arduino");
}
Loading