Skip to content

Commit c64e2c4

Browse files
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.
1 parent a675d5a commit c64e2c4

10 files changed

+67
-67
lines changed

Diff for: test/src/String/test_String.cpp

+19-19
Original file line numberDiff line numberDiff line change
@@ -22,82 +22,82 @@ TEST_CASE ("Testing String(const char *) constructor()", "[String-Ctor-01]")
2222
{
2323
char const CSTR[] = "Hello Arduino String Class";
2424
arduino::String str(CSTR);
25-
REQUIRE(strcmp(CSTR, str.c_str()) == 0);
25+
REQUIRE(str == CSTR);
2626
}
2727

2828
TEST_CASE ("Testing String(const String &) constructor()", "[String-Ctor-02]")
2929
{
3030
arduino::String str1("Hello Arduino String class"),
3131
str2(str1);
32-
REQUIRE(strcmp(str1.c_str(), str2.c_str()) == 0);
32+
REQUIRE(str1 == str2);
3333
}
3434

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

4343
TEST_CASE ("Testing String(char) constructor()", "[String-Ctor-04]")
4444
{
4545
char const ch = 'A';
4646
arduino::String str(ch);
47-
REQUIRE(strcmp(str.c_str(), "A") == 0);
47+
REQUIRE(str == "A");
4848
}
4949

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

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

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

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

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

8585
TEST_CASE ("Testing String(float, unsigned char decimalPlaces = 2) constructor()", "[String-Ctor-10]")
8686
{
8787
WHEN ("String::String (some float value)")
8888
{
8989
arduino::String str(1.234f);
90-
REQUIRE(strcmp(str.c_str(), "1.23") == 0);
90+
REQUIRE(str == "1.23");
9191
}
9292
WHEN ("String::String (FLT_MAX)")
9393
{
9494
arduino::String str(FLT_MAX);
95-
REQUIRE(strcmp(str.c_str(), "340282346638528859811704183484516925440.00") == 0);
95+
REQUIRE(str == "340282346638528859811704183484516925440.00");
9696
}
9797
WHEN ("String::String (-FLT_MAX)")
9898
{
9999
arduino::String str(-FLT_MAX);
100-
REQUIRE(strcmp(str.c_str(), "-340282346638528859811704183484516925440.00") == 0);
100+
REQUIRE(str == "-340282346638528859811704183484516925440.00");
101101
}
102102
}
103103

@@ -106,17 +106,17 @@ TEST_CASE ("Testing String(double, unsigned char decimalPlaces = 2) constructor(
106106
WHEN ("String::String (some double value)")
107107
{
108108
arduino::String str(5.678);
109-
REQUIRE(strcmp(str.c_str(), "5.68") == 0);
109+
REQUIRE(str == "5.68");
110110
}
111111
WHEN ("String::String (DBL_MAX)")
112112
{
113113
arduino::String str(DBL_MAX);
114-
REQUIRE(strcmp(str.c_str(), "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00") == 0);
114+
REQUIRE(str == "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00");
115115
}
116116
WHEN ("String::String (-DBL_MAX)")
117117
{
118118
arduino::String str(-DBL_MAX);
119-
REQUIRE(strcmp(str.c_str(), "-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00") == 0);
119+
REQUIRE(str == "-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00");
120120
}
121121
}
122122

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

141141
TEST_CASE ("Testing String(String &&) constructor()", "[String-Ctor-14]")
142142
{
143143
arduino::String str("Hello");
144144
arduino::String str1(static_cast<arduino::String&&>(str));
145-
REQUIRE(str1.compareTo("Hello") == 0);
145+
REQUIRE(str1 == "Hello");
146146
}
147147

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

156156
TEST_CASE ("Testing String(String &&) with move(String &rhs) from larger to smaller buffer", "[String-Ctor-16]")
157157
{
158158
arduino::String str("Hello");
159159
arduino::String str1("Arduino");
160160
str = static_cast<arduino::String&&>(str1);
161-
REQUIRE(str.compareTo("Arduino") == 0);
161+
REQUIRE(str == "Arduino");
162162
}

Diff for: test/src/String/test_characterAccessFunc.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ TEST_CASE ("Testing String::setCharAt(unsigned int, char )", "[String-setCharAt-
2626
{
2727
arduino::String str1("Hello");
2828
str1.setCharAt(1, 'a');
29-
REQUIRE(str1.compareTo("Hallo") == 0);
29+
REQUIRE(str1 == "Hallo");
3030
}
3131

3232
TEST_CASE ("Testing String::getBytes(unsigned char, unsigned int, unsigned int)", "[String-getBytes-02]")

Diff for: test/src/String/test_concat.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -20,78 +20,78 @@ TEST_CASE ("Testing String::concat(const String &)", "[String-concat-01]")
2020
{
2121
arduino::String str1("Hello "), str2("Arduino!");
2222
REQUIRE(str1.concat(str2) == 1);
23-
REQUIRE(strcmp(str1.c_str(), "Hello Arduino!") == 0);
23+
REQUIRE(str1 == "Hello Arduino!");
2424
}
2525

2626
TEST_CASE ("Testing String::concat(const char *)", "[String-concat-02]")
2727
{
2828
arduino::String str("Hello ");
2929
REQUIRE(str.concat("Arduino!") == 1);
30-
REQUIRE(strcmp(str.c_str(), "Hello Arduino!") == 0);
30+
REQUIRE(str == "Hello Arduino!");
3131
}
3232

3333
TEST_CASE ("Testing String::concat(char)", "[String-concat-03]")
3434
{
3535
arduino::String str("Hello ");
3636
char const c = 'A';
3737
REQUIRE(str.concat(c) == 1);
38-
REQUIRE(strcmp(str.c_str(), "Hello A") == 0);
38+
REQUIRE(str == "Hello A");
3939
}
4040

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

4949
TEST_CASE ("Testing String::concat(int)", "[String-concat-05]")
5050
{
5151
arduino::String str("Hello ");
5252
int const num = -1;
5353
REQUIRE(str.concat(num) == 1);
54-
REQUIRE(strcmp(str.c_str(), "Hello -1") == 0);
54+
REQUIRE(str == "Hello -1");
5555
}
5656

5757
TEST_CASE ("Testing String::concat(unsigned int)", "[String-concat-06]")
5858
{
5959
arduino::String str("Hello ");
6060
unsigned int const num = 1;
6161
REQUIRE(str.concat(num) == 1);
62-
REQUIRE(strcmp(str.c_str(), "Hello 1") == 0);
62+
REQUIRE(str == "Hello 1");
6363
}
6464

6565
TEST_CASE ("Testing String::concat(long)", "[String-concat-07]")
6666
{
6767
arduino::String str("Hello ");
6868
long const num = -1;
6969
REQUIRE(str.concat(num) == 1);
70-
REQUIRE(strcmp(str.c_str(), "Hello -1") == 0);
70+
REQUIRE(str == "Hello -1");
7171
}
7272

7373
TEST_CASE ("Testing String::concat(unsigned long)", "[String-concat-08]")
7474
{
7575
arduino::String str("Hello ");
7676
unsigned long const num = 1;
7777
REQUIRE(str.concat(num) == 1);
78-
REQUIRE(strcmp(str.c_str(), "Hello 1") == 0);
78+
REQUIRE(str == "Hello 1");
7979
}
8080

8181
TEST_CASE ("Testing String::concat(float)", "[String-concat-09]")
8282
{
8383
arduino::String str("Hello ");
8484
float const num = 1.234f;
8585
REQUIRE(str.concat(num) == 1);
86-
REQUIRE(strcmp(str.c_str(), "Hello 1.23") == 0);
86+
REQUIRE(str == "Hello 1.23");
8787
}
8888

8989
TEST_CASE ("Testing String::concat(double)", "[String-concat-10]")
9090
{
9191
arduino::String str("Hello ");
9292
double const num = 5.678;
9393
REQUIRE(str.concat(num) == 1);
94-
REQUIRE(strcmp(str.c_str(), "Hello 5.68") == 0);
94+
REQUIRE(str == "Hello 5.68");
9595
}
9696

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

0 commit comments

Comments
 (0)