Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd5790a0

Browse files
authored
Merge pull request#137 from matthijskooijman/simplify-string-tests
Simplify string comparisons in tests and fix bug in String::compareTo
2 parents2ca15ad +c64e2c4 commitd5790a0

20 files changed

+190
-74
lines changed

‎api/String.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ int String::compareTo(const String &s) const
462462
intString::compareTo(constchar *cstr)const
463463
{
464464
if (!buffer || !cstr) {
465-
if (cstr &&!*cstr)return0 - *(unsignedchar *)cstr;
465+
if (cstr && *cstr)return0 - *(unsignedchar *)cstr;
466466
if (buffer && len >0)return *(unsignedchar *)buffer;
467467
return0;
468468
}

‎test/src/String/StringPrinter.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include<catch.hpp>
4+
#include<String.h>
5+
6+
namespaceCatch {
7+
/**
8+
* Template specialization that makes sure Catch can properly print
9+
* Arduino Strings when used in comparisons directly.
10+
*
11+
* Note that without this, String objects are printed as 0 and 1,
12+
* because they are implicitly convertible to StringIfHelperType,
13+
* which is a dummy pointer.
14+
*/
15+
template<>
16+
structStringMaker<arduino::String> {
17+
static std::stringconvert(const arduino::String& str) {
18+
if (str)
19+
return ::Catch::Detail::stringify(std::string(str.c_str(), str.length()));
20+
else
21+
return"{invalid String}";
22+
}
23+
};
24+
}// namespace Catch

‎test/src/String/test_String.cpp‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include<String.h>
1414

15+
#include"StringPrinter.h"
16+
1517
/**************************************************************************************
1618
* TEST CODE
1719
**************************************************************************************/
@@ -20,82 +22,82 @@ TEST_CASE ("Testing String(const char *) constructor()", "[String-Ctor-01]")
2022
{
2123
charconst CSTR[] ="Hello Arduino String Class";
2224
arduino::Stringstr(CSTR);
23-
REQUIRE(strcmp(CSTR,str.c_str()) ==0);
25+
REQUIRE(str ==CSTR);
2426
}
2527

2628
TEST_CASE ("Testing String(const String &) constructor()","[String-Ctor-02]")
2729
{
2830
arduino::Stringstr1("Hello Arduino String class"),
2931
str2(str1);
30-
REQUIRE(strcmp(str1.c_str(), str2.c_str())==0);
32+
REQUIRE(str1==str2);
3133
}
3234

3335
TEST_CASE ("Testing String(const __FlashStringHelper) constructor()","[String-Ctor-03]")
3436
{
3537
#undef F
3638
#defineF(string_literal) (reinterpret_cast<const arduino::__FlashStringHelper *>(PSTR(string_literal)))
3739
arduino::Stringstr1(F("Hello"));
38-
REQUIRE(str1.compareTo("Hello") ==0);
40+
REQUIRE(str1 =="Hello");
3941
}
4042

4143
TEST_CASE ("Testing String(char) constructor()","[String-Ctor-04]")
4244
{
4345
charconst ch ='A';
4446
arduino::Stringstr(ch);
45-
REQUIRE(strcmp(str.c_str(),"A")==0);
47+
REQUIRE(str=="A");
4648
}
4749

4850
TEST_CASE ("Testing String(unsigned char, unsigned char base = 10) constructor()","[String-Ctor-05]")
4951
{
5052
unsignedcharconst val =1;
5153
arduino::Stringstr(val);
52-
REQUIRE(strcmp(str.c_str(),"1")==0);
54+
REQUIRE(str=="1");
5355
}
5456

5557
TEST_CASE ("Testing String(int, unsigned char base = 10) constructor()","[String-Ctor-06]")
5658
{
5759
intconst val = -1;
5860
arduino::Stringstr(val);
59-
REQUIRE(strcmp(str.c_str(),"-1") ==0);
61+
REQUIRE(str =="-1");
6062
}
6163

6264
TEST_CASE ("Testing String(unsigned int, unsigned char base = 10) constructor()","[String-Ctor-07]")
6365
{
6466
unsignedintconst val =1;
6567
arduino::Stringstr(val);
66-
REQUIRE(strcmp(str.c_str(),"1")==0);
68+
REQUIRE(str=="1");
6769
}
6870

6971
TEST_CASE ("Testing String(long, unsigned char base = 10) constructor()","[String-Ctor-08]")
7072
{
7173
longconst val = -1;
7274
arduino::Stringstr(val);
73-
REQUIRE(strcmp(str.c_str(),"-1") ==0);
75+
REQUIRE(str =="-1");
7476
}
7577

7678
TEST_CASE ("Testing String(unsigned long, unsigned char base = 10) constructor()","[String-Ctor-09]")
7779
{
7880
unsignedlongconst val =1;
7981
arduino::Stringstr(val);
80-
REQUIRE(strcmp(str.c_str(),"1")==0);
82+
REQUIRE(str=="1");
8183
}
8284

8385
TEST_CASE ("Testing String(float, unsigned char decimalPlaces = 2) constructor()","[String-Ctor-10]")
8486
{
8587
WHEN ("String::String (some float value)")
8688
{
8789
arduino::Stringstr(1.234f);
88-
REQUIRE(strcmp(str.c_str(),"1.23") ==0);
90+
REQUIRE(str =="1.23");
8991
}
9092
WHEN ("String::String (FLT_MAX)")
9193
{
9294
arduino::Stringstr(FLT_MAX);
93-
REQUIRE(strcmp(str.c_str(),"340282346638528859811704183484516925440.00") ==0);
95+
REQUIRE(str =="340282346638528859811704183484516925440.00");
9496
}
9597
WHEN ("String::String (-FLT_MAX)")
9698
{
9799
arduino::Stringstr(-FLT_MAX);
98-
REQUIRE(strcmp(str.c_str(),"-340282346638528859811704183484516925440.00") ==0);
100+
REQUIRE(str =="-340282346638528859811704183484516925440.00");
99101
}
100102
}
101103

@@ -104,17 +106,17 @@ TEST_CASE ("Testing String(double, unsigned char decimalPlaces = 2) constructor(
104106
WHEN ("String::String (some double value)")
105107
{
106108
arduino::Stringstr(5.678);
107-
REQUIRE(strcmp(str.c_str(),"5.68") ==0);
109+
REQUIRE(str =="5.68");
108110
}
109111
WHEN ("String::String (DBL_MAX)")
110112
{
111113
arduino::Stringstr(DBL_MAX);
112-
REQUIRE(strcmp(str.c_str(),"179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00") ==0);
114+
REQUIRE(str =="179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00");
113115
}
114116
WHEN ("String::String (-DBL_MAX)")
115117
{
116118
arduino::Stringstr(-DBL_MAX);
117-
REQUIRE(strcmp(str.c_str(),"-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00") ==0);
119+
REQUIRE(str =="-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00");
118120
}
119121
}
120122

@@ -125,36 +127,36 @@ TEST_CASE ("Testing String(const __FlashStringHelper) constructor() with invalid
125127
char *buffer =NULL;
126128

127129
arduino::Stringstr1(F(buffer));
128-
REQUIRE(str1.compareTo("Hello") ==0);
130+
REQUIRE_FALSE(str1);
129131
}
130132

131133
TEST_CASE ("Testing String(StringSumHelper &&) constructor()","[String-Ctor-13]")
132134
{
133135
arduino::Stringstr("Hello");
134136
charconst ch ='!';
135137
arduino::Stringstr1(static_cast<arduino::StringSumHelper&&>(str+ch));
136-
REQUIRE(str1.compareTo("Hello!") ==0);
138+
REQUIRE(str1 =="Hello!");
137139
}
138140

139141
TEST_CASE ("Testing String(String &&) constructor()","[String-Ctor-14]")
140142
{
141143
arduino::Stringstr("Hello");
142144
arduino::Stringstr1(static_cast<arduino::String&&>(str));
143-
REQUIRE(str1.compareTo("Hello") ==0);
145+
REQUIRE(str1 =="Hello");
144146
}
145147

146148
TEST_CASE ("Testing String(String &&) with move(String &rhs) from smaller to larger buffer","[String-Ctor-15]")
147149
{
148150
arduino::Stringstr("Hello");
149151
arduino::Stringstr1("Arduino");
150152
str1 =static_cast<arduino::String&&>(str);
151-
REQUIRE(str1.compareTo("Hello") ==0);
153+
REQUIRE(str1 =="Hello");
152154
}
153155

154156
TEST_CASE ("Testing String(String &&) with move(String &rhs) from larger to smaller buffer","[String-Ctor-16]")
155157
{
156158
arduino::Stringstr("Hello");
157159
arduino::Stringstr1("Arduino");
158160
str =static_cast<arduino::String&&>(str1);
159-
REQUIRE(str1.compareTo("Arduino") ==0);
161+
REQUIRE(str =="Arduino");
160162
}

‎test/src/String/test_characterAccessFunc.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include<String.h>
1212

13+
#include"StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/
@@ -24,7 +26,7 @@ TEST_CASE ("Testing String::setCharAt(unsigned int, char )", "[String-setCharAt-
2426
{
2527
arduino::Stringstr1("Hello");
2628
str1.setCharAt(1,'a');
27-
REQUIRE(str1.compareTo("Hallo") ==0);
29+
REQUIRE(str1 =="Hallo");
2830
}
2931

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

‎test/src/String/test_compareTo.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include<String.h>
1212

13+
#include"StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

‎test/src/String/test_comparisonFunc.cpp‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include<String.h>
1212

13+
#include"StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/
@@ -20,24 +22,72 @@ TEST_CASE ("Testing String::equals(const String &) with exit status PASS", "[Str
2022
REQUIRE(str1.equals(str2) ==1);
2123
}
2224

25+
TEST_CASE ("Testing String::operator==(const String &) with exit status PASS","[String-equals-01]")
26+
{
27+
arduino::Stringstr1("Hello"),str2("Hello");
28+
REQUIRE(str1 == str2);
29+
}
30+
31+
TEST_CASE ("Testing String::operator!=(const String &) with exit status FAIL","[String-equals-01]")
32+
{
33+
arduino::Stringstr1("Hello"),str2("Hello");
34+
REQUIRE_FALSE(str1 != str2);
35+
}
36+
2337
TEST_CASE ("Testing String::equals(const String &) with exit status FAIL","[String-equals-02]")
2438
{
2539
arduino::Stringstr1("Hello"),str2("World");
2640
REQUIRE(str1.equals(str2) ==0);
2741
}
2842

43+
TEST_CASE ("Testing String::operator==(const String &) with exit status FAIL","[String-equals-02]")
44+
{
45+
arduino::Stringstr1("Hello"),str2("World");
46+
REQUIRE_FALSE(str1 == str2);
47+
}
48+
49+
TEST_CASE ("Testing String::operator !=(const String &) with exit status PASS","[String-equals-02]")
50+
{
51+
arduino::Stringstr1("Hello"),str2("World");
52+
REQUIRE(str1 != str2);
53+
}
54+
2955
TEST_CASE ("Testing String::equals(const char *) with exit status PASS","[String-equals-03]")
3056
{
3157
arduino::Stringstr1("Hello");
3258
REQUIRE(str1.equals("Hello") ==1);
3359
}
3460

61+
TEST_CASE ("Testing String::operator ==(const char *) with exit status PASS","[String-equals-03]")
62+
{
63+
arduino::Stringstr1("Hello");
64+
REQUIRE(str1 =="Hello");
65+
}
66+
67+
TEST_CASE ("Testing String::operator !=(const char *) with exit status FAIL","[String-equals-03]")
68+
{
69+
arduino::Stringstr1("Hello");
70+
REQUIRE_FALSE(str1 !="Hello");
71+
}
72+
3573
TEST_CASE ("Testing String::equals(const char *) with exit status FAIL","[String-equals-04]")
3674
{
3775
arduino::Stringstr1("Hello");
3876
REQUIRE(str1.equals("World") ==0);
3977
}
4078

79+
TEST_CASE ("Testing String::operator ==(const char *) with exit status FAIL","[String-equals-04]")
80+
{
81+
arduino::Stringstr1("Hello");
82+
REQUIRE_FALSE(str1 =="World");
83+
}
84+
85+
TEST_CASE ("Testing String::operator !=(const char *) with exit status PASS","[String-equals-04]")
86+
{
87+
arduino::Stringstr1("Hello");
88+
REQUIRE(str1 !="World");
89+
}
90+
4191
TEST_CASE ("Testing String::equalsIgnoreCase(const String &) PASS with NON-empty string","[String-equalsIgnoreCase-05]")
4292
{
4393
arduino::Stringstr1("Hello"),str2("Hello");
@@ -104,4 +154,4 @@ TEST_CASE ("Testing String::endsWith(const String &)", "[String-endsWith-10]")
104154
arduino::Stringstr2("Helo");
105155
REQUIRE(str1.endsWith(str2) ==0);
106156
}
107-
}
157+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp