From 3caa0394c31f875a98ce0c73c520b66d66fb9e12 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 31 Dec 2020 11:24:37 +0100 Subject: [PATCH 1/5] Fix whitespace issues in testcases This adds a newline at the end of the file, which is helpful for git diff display. --- test/src/String/test_comparisonFunc.cpp | 2 +- test/src/String/test_concat.cpp | 2 +- test/src/String/test_operators.cpp | 2 +- test/src/String/test_substring.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/src/String/test_comparisonFunc.cpp b/test/src/String/test_comparisonFunc.cpp index 281ba8d7..68e74d61 100644 --- a/test/src/String/test_comparisonFunc.cpp +++ b/test/src/String/test_comparisonFunc.cpp @@ -104,4 +104,4 @@ TEST_CASE ("Testing String::endsWith(const String &)", "[String-endsWith-10]") arduino::String str2("Helo"); REQUIRE(str1.endsWith(str2) == 0); } -} \ No newline at end of file +} diff --git a/test/src/String/test_concat.cpp b/test/src/String/test_concat.cpp index b3f48960..44d7f602 100644 --- a/test/src/String/test_concat.cpp +++ b/test/src/String/test_concat.cpp @@ -99,4 +99,4 @@ TEST_CASE ("Testing String::concat(const __FlashStringHelper *)", "[String-conca arduino::String str1("Hello"); REQUIRE(str1.concat(F(" Arduino")) == 1); REQUIRE(strcmp(str1.c_str(), "Hello Arduino") == 0); -} \ No newline at end of file +} diff --git a/test/src/String/test_operators.cpp b/test/src/String/test_operators.cpp index f0851fbf..ab9be64c 100644 --- a/test/src/String/test_operators.cpp +++ b/test/src/String/test_operators.cpp @@ -153,4 +153,4 @@ TEST_CASE ("Testing & String::operator = (StringSumHelper &&)", "[String-operato arduino::String str1("Arduino"); str1 = static_cast(str+ch); REQUIRE(str1.compareTo("Hello!") == 0); -} \ No newline at end of file +} diff --git a/test/src/String/test_substring.cpp b/test/src/String/test_substring.cpp index 9a17abff..5a8fe7b0 100644 --- a/test/src/String/test_substring.cpp +++ b/test/src/String/test_substring.cpp @@ -35,4 +35,4 @@ TEST_CASE ("Testing String::substring(unsigned int, unsigned int)", "[String-sub arduino::String str2("ello"); REQUIRE(str2.compareTo(str1.substring(9,1)) == 0); } -} \ No newline at end of file +} From 0c8dbd64a5d05e558185d7f85df40e328e23a9af Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Wed, 30 Dec 2020 21:09:31 +0100 Subject: [PATCH 2/5] Properly print Arduino String objects in tests This ensures that checks like: REQUIRE(some_str == "ABC"); Actually print the value of some_str instead of just `1` (which is the String object converted to StringIfHelperType). This is implemented by adding a specialization of the Catch StringMaker template, so this only takes effect when StringPrinter.h is included. This commit includes it in all the String testcases (even ones that do not strictly use it now) for good measure. --- test/src/String/StringPrinter.h | 24 ++++++++++++++++++++ test/src/String/test_String.cpp | 2 ++ test/src/String/test_characterAccessFunc.cpp | 2 ++ test/src/String/test_compareTo.cpp | 2 ++ test/src/String/test_comparisonFunc.cpp | 2 ++ test/src/String/test_concat.cpp | 2 ++ test/src/String/test_indexOf.cpp | 2 ++ test/src/String/test_lastIndexOf.cpp | 2 ++ test/src/String/test_length.cpp | 2 ++ test/src/String/test_operators.cpp | 2 ++ test/src/String/test_remove.cpp | 2 ++ test/src/String/test_replace.cpp | 2 ++ test/src/String/test_substring.cpp | 2 ++ test/src/String/test_toDouble.cpp | 2 ++ test/src/String/test_toFloat.cpp | 2 ++ test/src/String/test_toInt.cpp | 2 ++ test/src/String/test_toLowerCase.cpp | 2 ++ test/src/String/test_toUpperCase.cpp | 2 ++ test/src/String/test_trim.cpp | 2 ++ 19 files changed, 60 insertions(+) create mode 100644 test/src/String/StringPrinter.h diff --git a/test/src/String/StringPrinter.h b/test/src/String/StringPrinter.h new file mode 100644 index 00000000..f338a902 --- /dev/null +++ b/test/src/String/StringPrinter.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +namespace Catch { + /** + * Template specialization that makes sure Catch can properly print + * Arduino Strings when used in comparisons directly. + * + * Note that without this, String objects are printed as 0 and 1, + * because they are implicitly convertible to StringIfHelperType, + * which is a dummy pointer. + */ + template<> + struct StringMaker { + static std::string convert(const arduino::String& str) { + if (str) + return ::Catch::Detail::stringify(std::string(str.c_str(), str.length())); + else + return "{invalid String}"; + } + }; +} // namespace Catch diff --git a/test/src/String/test_String.cpp b/test/src/String/test_String.cpp index 8ff44c0f..ac3fdea2 100644 --- a/test/src/String/test_String.cpp +++ b/test/src/String/test_String.cpp @@ -12,6 +12,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_characterAccessFunc.cpp b/test/src/String/test_characterAccessFunc.cpp index 24b78d86..e2b716d8 100644 --- a/test/src/String/test_characterAccessFunc.cpp +++ b/test/src/String/test_characterAccessFunc.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_compareTo.cpp b/test/src/String/test_compareTo.cpp index cce07ec3..9aef1ee7 100644 --- a/test/src/String/test_compareTo.cpp +++ b/test/src/String/test_compareTo.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_comparisonFunc.cpp b/test/src/String/test_comparisonFunc.cpp index 68e74d61..b903db1d 100644 --- a/test/src/String/test_comparisonFunc.cpp +++ b/test/src/String/test_comparisonFunc.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_concat.cpp b/test/src/String/test_concat.cpp index 44d7f602..5cc6a765 100644 --- a/test/src/String/test_concat.cpp +++ b/test/src/String/test_concat.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_indexOf.cpp b/test/src/String/test_indexOf.cpp index c0c06d88..bcaf65ef 100644 --- a/test/src/String/test_indexOf.cpp +++ b/test/src/String/test_indexOf.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_lastIndexOf.cpp b/test/src/String/test_lastIndexOf.cpp index bee7276e..2ae95f54 100644 --- a/test/src/String/test_lastIndexOf.cpp +++ b/test/src/String/test_lastIndexOf.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_length.cpp b/test/src/String/test_length.cpp index 7cb493a9..3b47faed 100644 --- a/test/src/String/test_length.cpp +++ b/test/src/String/test_length.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_operators.cpp b/test/src/String/test_operators.cpp index ab9be64c..087f7dc5 100644 --- a/test/src/String/test_operators.cpp +++ b/test/src/String/test_operators.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_remove.cpp b/test/src/String/test_remove.cpp index e7f364c8..998a890c 100644 --- a/test/src/String/test_remove.cpp +++ b/test/src/String/test_remove.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_replace.cpp b/test/src/String/test_replace.cpp index 72a5a495..56e0da9e 100644 --- a/test/src/String/test_replace.cpp +++ b/test/src/String/test_replace.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_substring.cpp b/test/src/String/test_substring.cpp index 5a8fe7b0..366f363d 100644 --- a/test/src/String/test_substring.cpp +++ b/test/src/String/test_substring.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_toDouble.cpp b/test/src/String/test_toDouble.cpp index e57b128e..246f25d4 100644 --- a/test/src/String/test_toDouble.cpp +++ b/test/src/String/test_toDouble.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_toFloat.cpp b/test/src/String/test_toFloat.cpp index b7ad7af3..afef02c5 100644 --- a/test/src/String/test_toFloat.cpp +++ b/test/src/String/test_toFloat.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_toInt.cpp b/test/src/String/test_toInt.cpp index 65c2404a..43397b76 100644 --- a/test/src/String/test_toInt.cpp +++ b/test/src/String/test_toInt.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_toLowerCase.cpp b/test/src/String/test_toLowerCase.cpp index 2508c3f8..c8c683eb 100644 --- a/test/src/String/test_toLowerCase.cpp +++ b/test/src/String/test_toLowerCase.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_toUpperCase.cpp b/test/src/String/test_toUpperCase.cpp index 83121a40..c0628352 100644 --- a/test/src/String/test_toUpperCase.cpp +++ b/test/src/String/test_toUpperCase.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/String/test_trim.cpp b/test/src/String/test_trim.cpp index 114cf10b..70d828cd 100644 --- a/test/src/String/test_trim.cpp +++ b/test/src/String/test_trim.cpp @@ -10,6 +10,8 @@ #include +#include "StringPrinter.h" + /************************************************************************************** * TEST CODE **************************************************************************************/ From f7311d936cd3ad67a38a206745f35ac41a0e80d8 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 31 Dec 2020 12:03:51 +0100 Subject: [PATCH 3/5] Add tests for String operator == and != This expands the existing tests for String.equals to also test operator == and !=. These should be equivalent (the operators just call equals), but add tests to ensure this. --- test/src/String/test_comparisonFunc.cpp | 48 +++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/src/String/test_comparisonFunc.cpp b/test/src/String/test_comparisonFunc.cpp index b903db1d..89a7f3be 100644 --- a/test/src/String/test_comparisonFunc.cpp +++ b/test/src/String/test_comparisonFunc.cpp @@ -22,24 +22,72 @@ TEST_CASE ("Testing String::equals(const String &) with exit status PASS", "[Str REQUIRE(str1.equals(str2) == 1); } +TEST_CASE ("Testing String::operator==(const String &) with exit status PASS", "[String-equals-01]") +{ + arduino::String str1("Hello"), str2("Hello"); + REQUIRE(str1 == str2); +} + +TEST_CASE ("Testing String::operator!=(const String &) with exit status FAIL", "[String-equals-01]") +{ + arduino::String str1("Hello"), str2("Hello"); + REQUIRE_FALSE(str1 != str2); +} + TEST_CASE ("Testing String::equals(const String &) with exit status FAIL", "[String-equals-02]") { arduino::String str1("Hello"), str2("World"); REQUIRE(str1.equals(str2) == 0); } +TEST_CASE ("Testing String::operator==(const String &) with exit status FAIL", "[String-equals-02]") +{ + arduino::String str1("Hello"), str2("World"); + REQUIRE_FALSE(str1 == str2); +} + +TEST_CASE ("Testing String::operator !=(const String &) with exit status PASS", "[String-equals-02]") +{ + arduino::String str1("Hello"), str2("World"); + REQUIRE(str1 != str2); +} + TEST_CASE ("Testing String::equals(const char *) with exit status PASS", "[String-equals-03]") { arduino::String str1("Hello"); REQUIRE(str1.equals("Hello") == 1); } +TEST_CASE ("Testing String::operator ==(const char *) with exit status PASS", "[String-equals-03]") +{ + arduino::String str1("Hello"); + REQUIRE(str1 == "Hello"); +} + +TEST_CASE ("Testing String::operator !=(const char *) with exit status FAIL", "[String-equals-03]") +{ + arduino::String str1("Hello"); + REQUIRE_FALSE(str1 != "Hello"); +} + TEST_CASE ("Testing String::equals(const char *) with exit status FAIL", "[String-equals-04]") { arduino::String str1("Hello"); REQUIRE(str1.equals("World") == 0); } +TEST_CASE ("Testing String::operator ==(const char *) with exit status FAIL", "[String-equals-04]") +{ + arduino::String str1("Hello"); + REQUIRE_FALSE(str1 == "World"); +} + +TEST_CASE ("Testing String::operator !=(const char *) with exit status PASS", "[String-equals-04]") +{ + arduino::String str1("Hello"); + REQUIRE(str1 != "World"); +} + TEST_CASE ("Testing String::equalsIgnoreCase(const String &) PASS with NON-empty string", "[String-equalsIgnoreCase-05]") { arduino::String str1("Hello"), str2("Hello"); From a675d5a5cbb7fc6334f2f873819e582ae6437fb3 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 31 Dec 2020 12:26:15 +0100 Subject: [PATCH 4/5] Fix String::compareTo(const char*) for invalid strings When comparing an invalid String object to a non-empty char*, this would erronously return 0 (equal) because of a typo. This bug also masked three incorrect checks in related testcases. In two cases, a String was made invalid and then checked to still contain a value (these were changed to check that the string is invalid) and in one case the wrong string was checked. --- api/String.cpp | 2 +- test/src/String/test_String.cpp | 4 ++-- test/src/String/test_operators.cpp | 12 ++++++++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/api/String.cpp b/api/String.cpp index 37812418..6f14ae13 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -462,7 +462,7 @@ int String::compareTo(const String &s) const int String::compareTo(const char *cstr) const { if (!buffer || !cstr) { - if (cstr && !*cstr) return 0 - *(unsigned char *)cstr; + if (cstr && *cstr) return 0 - *(unsigned char *)cstr; if (buffer && len > 0) return *(unsigned char *)buffer; return 0; } diff --git a/test/src/String/test_String.cpp b/test/src/String/test_String.cpp index ac3fdea2..95e0c902 100644 --- a/test/src/String/test_String.cpp +++ b/test/src/String/test_String.cpp @@ -127,7 +127,7 @@ TEST_CASE ("Testing String(const __FlashStringHelper) constructor() with invalid char *buffer = NULL; arduino::String str1(F(buffer)); - REQUIRE(str1.compareTo("Hello") == 0); + REQUIRE_FALSE(str1); } TEST_CASE ("Testing String(StringSumHelper &&) constructor()", "[String-Ctor-13]") @@ -158,5 +158,5 @@ TEST_CASE ("Testing String(String &&) with move(String &rhs) from larger to smal arduino::String str("Hello"); arduino::String str1("Arduino"); str = static_cast(str1); - REQUIRE(str1.compareTo("Arduino") == 0); + REQUIRE(str.compareTo("Arduino") == 0); } diff --git a/test/src/String/test_operators.cpp b/test/src/String/test_operators.cpp index 087f7dc5..b71638d0 100644 --- a/test/src/String/test_operators.cpp +++ b/test/src/String/test_operators.cpp @@ -121,12 +121,20 @@ TEST_CASE ("Testing & String::operator = (const String &) with invalid buffer of REQUIRE(str1.compareTo(str2) == 0); } -TEST_CASE ("Testing & String::operator = (const char *)", "[String-operator+-14]") +TEST_CASE ("Testing & String::operator = (const char *) with NULL does not leave string unchanged", "[String-operator+-14]") { char *buffer = NULL; arduino::String str("Hello"); str = buffer; - REQUIRE(str.compareTo("Hello") == 0); + REQUIRE(str.compareTo("Hello") != 0); +} + +TEST_CASE ("Testing & String::operator = (const char *) with NULL produces invalid string", "[String-operator+-14]") +{ + char *buffer = NULL; + arduino::String str("Hello"); + str = buffer; + REQUIRE_FALSE(str); } TEST_CASE ("Testing & String::operator = (const String &) with invalid buffer of first string", "[String-operator+-15]") From c64e2c46b6ab37cf79a6733a0c6b692c6ada0f60 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Wed, 30 Dec 2020 21:09:31 +0100 Subject: [PATCH 5/5] 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. --- test/src/String/test_String.cpp | 38 ++++++++++---------- test/src/String/test_characterAccessFunc.cpp | 2 +- test/src/String/test_concat.cpp | 22 ++++++------ test/src/String/test_operators.cpp | 34 +++++++++--------- test/src/String/test_remove.cpp | 10 +++--- test/src/String/test_replace.cpp | 12 +++---- test/src/String/test_substring.cpp | 4 +-- test/src/String/test_toLowerCase.cpp | 2 +- test/src/String/test_toUpperCase.cpp | 2 +- test/src/String/test_trim.cpp | 8 ++--- 10 files changed, 67 insertions(+), 67 deletions(-) diff --git a/test/src/String/test_String.cpp b/test/src/String/test_String.cpp index 95e0c902..461f2d08 100644 --- a/test/src/String/test_String.cpp +++ b/test/src/String/test_String.cpp @@ -22,14 +22,14 @@ 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]") @@ -37,49 +37,49 @@ TEST_CASE ("Testing String(const __FlashStringHelper) constructor()", "[String-C #undef F #define F(string_literal) (reinterpret_cast(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]") @@ -87,17 +87,17 @@ TEST_CASE ("Testing String(float, unsigned char decimalPlaces = 2) constructor() 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"); } } @@ -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"); } } @@ -135,14 +135,14 @@ TEST_CASE ("Testing String(StringSumHelper &&) constructor()", "[String-Ctor-13] arduino::String str("Hello"); char const ch = '!'; arduino::String str1(static_cast(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(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]") @@ -150,7 +150,7 @@ TEST_CASE ("Testing String(String &&) with move(String &rhs) from smaller to lar arduino::String str("Hello"); arduino::String str1("Arduino"); str1 = static_cast(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]") @@ -158,5 +158,5 @@ TEST_CASE ("Testing String(String &&) with move(String &rhs) from larger to smal arduino::String str("Hello"); arduino::String str1("Arduino"); str = static_cast(str1); - REQUIRE(str.compareTo("Arduino") == 0); + REQUIRE(str == "Arduino"); } diff --git a/test/src/String/test_characterAccessFunc.cpp b/test/src/String/test_characterAccessFunc.cpp index e2b716d8..329cca43 100644 --- a/test/src/String/test_characterAccessFunc.cpp +++ b/test/src/String/test_characterAccessFunc.cpp @@ -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]") diff --git a/test/src/String/test_concat.cpp b/test/src/String/test_concat.cpp index 5cc6a765..b7390b64 100644 --- a/test/src/String/test_concat.cpp +++ b/test/src/String/test_concat.cpp @@ -20,14 +20,14 @@ 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]") @@ -35,7 +35,7 @@ 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]") @@ -43,7 +43,7 @@ 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]") @@ -51,7 +51,7 @@ 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]") @@ -59,7 +59,7 @@ 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]") @@ -67,7 +67,7 @@ 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]") @@ -75,7 +75,7 @@ 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]") @@ -83,7 +83,7 @@ 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]") @@ -91,7 +91,7 @@ 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]") @@ -100,5 +100,5 @@ TEST_CASE ("Testing String::concat(const __FlashStringHelper *)", "[String-conca #define F(string_literal) (reinterpret_cast(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"); } diff --git a/test/src/String/test_operators.cpp b/test/src/String/test_operators.cpp index b71638d0..67cb39be 100644 --- a/test/src/String/test_operators.cpp +++ b/test/src/String/test_operators.cpp @@ -21,14 +21,14 @@ TEST_CASE ("Testing String::operator + (const StringSumHelper, const String)", " arduino::String str1("Hello "); arduino::String str2("Arduino"); arduino::String str("Hello Arduino"); - REQUIRE(str.compareTo(str1+str2) == 0); + REQUIRE(str == str1+str2); } TEST_CASE ("Testing String::operator + (const StringSumHelper, const char *)", "[String-operator+-02]") { arduino::String str1("Hello "); arduino::String str("Hello Arduino"); - REQUIRE(str.compareTo(str1+"Arduino") == 0); + REQUIRE(str == str1+"Arduino"); } TEST_CASE ("Testing String::operator + (const StringSumHelper, char)", "[String-operator+-03]") @@ -36,7 +36,7 @@ TEST_CASE ("Testing String::operator + (const StringSumHelper, char)", "[String- arduino::String str1("Hello"); char ch='!'; arduino::String str("Hello!"); - REQUIRE(str.compareTo(str1+ch) == 0); + REQUIRE(str == str1+ch); } TEST_CASE ("Testing String::operator + (const StringSumHelper, unsigned char)", "[String-operator+-04]") @@ -44,14 +44,14 @@ TEST_CASE ("Testing String::operator + (const StringSumHelper, unsigned char)", arduino::String str1("Hello "); unsigned char ch='A'; arduino::String str("Hello 65"); /* ASCII['A'] = 65 */ - REQUIRE(str.compareTo(str1+ch) == 0); + REQUIRE(str == str1+ch); } TEST_CASE ("Testing String::operator + (const StringSumHelper, int)", "[String-operator+-05]") { arduino::String str1("Hello "); arduino::String str("Hello 1"); - REQUIRE(str.compareTo(str1+1) == 0); + REQUIRE(str == str1+1); } TEST_CASE ("Testing String::operator + (unsigned int)", "[String-operator+-06]") @@ -59,7 +59,7 @@ TEST_CASE ("Testing String::operator + (unsigned int)", "[String-operator+-06]") arduino::String str1("Hello "); unsigned int const num = 1; arduino::String str("Hello 1"); - REQUIRE(str.compareTo(str1+num) == 0); + REQUIRE(str == str1+num); } TEST_CASE ("Testing String::operator + (long)", "[String-operator+-07]") @@ -67,7 +67,7 @@ TEST_CASE ("Testing String::operator + (long)", "[String-operator+-07]") arduino::String str1("Hello "); long const num = -1; arduino::String str("Hello -1"); - REQUIRE(str.compareTo(str1+num) == 0); + REQUIRE(str == str1+num); } TEST_CASE ("Testing String::operator + (unsigned long)", "[String-operator+-08]") @@ -75,7 +75,7 @@ TEST_CASE ("Testing String::operator + (unsigned long)", "[String-operator+-08]" arduino::String str1("Hello "); unsigned long const num = 1; arduino::String str("Hello 1"); - REQUIRE(str.compareTo(str1+num) == 0); + REQUIRE(str == str1+num); } TEST_CASE ("Testing String::operator + (float)", "[String-operator+-09]") @@ -83,7 +83,7 @@ TEST_CASE ("Testing String::operator + (float)", "[String-operator+-09]") arduino::String str1("Hello "); float const num = 1.234f; arduino::String str("Hello 1.23"); - REQUIRE(str.compareTo(str1+num) == 0); + REQUIRE(str == str1+num); } TEST_CASE ("Testing String::operator + (double)", "[String-operator+-10]") @@ -91,7 +91,7 @@ TEST_CASE ("Testing String::operator + (double)", "[String-operator+-10]") arduino::String str1("Hello "); double const num = 5.678; arduino::String str("Hello 5.68"); - REQUIRE(str.compareTo(str1+num) == 0); + REQUIRE(str == str1+num); } TEST_CASE ("Testing String::operator + (const __FlashStringHelper *)", "[String-operator+-11]") @@ -100,14 +100,14 @@ TEST_CASE ("Testing String::operator + (const __FlashStringHelper *)", "[String- #define F(string_literal) (reinterpret_cast(PSTR(string_literal))) arduino::String str1("Hello "); arduino::String str("Hello Arduino"); - REQUIRE(str.compareTo(str1+F("Arduino")) == 0); + REQUIRE(str == str1+F("Arduino")); } TEST_CASE ("Testing & String::operator = (StringSumHelper &&rval)", "[String-operator+-12]") { arduino::String str1("Hello "); arduino::String str = (str1+"Arduino"); - REQUIRE(str.compareTo("Hello Arduino") == 0); + REQUIRE(str == "Hello Arduino"); } TEST_CASE ("Testing & String::operator = (const String &) with invalid buffer of second string", "[String-operator+-13]") @@ -118,7 +118,7 @@ TEST_CASE ("Testing & String::operator = (const String &) with invalid buffer of arduino::String str2(buffer2); str1 = str2; - REQUIRE(str1.compareTo(str2) == 0); + REQUIRE(str1 == str2); } TEST_CASE ("Testing & String::operator = (const char *) with NULL does not leave string unchanged", "[String-operator+-14]") @@ -126,7 +126,7 @@ TEST_CASE ("Testing & String::operator = (const char *) with NULL does not leave char *buffer = NULL; arduino::String str("Hello"); str = buffer; - REQUIRE(str.compareTo("Hello") != 0); + REQUIRE(str != "Hello"); } TEST_CASE ("Testing & String::operator = (const char *) with NULL produces invalid string", "[String-operator+-14]") @@ -145,7 +145,7 @@ TEST_CASE ("Testing & String::operator = (const String &) with invalid buffer of arduino::String str2("Hello"); str1 = str2; - REQUIRE(str1.compareTo(str2) == 0); + REQUIRE(str1 == str2); } TEST_CASE ("Testing & String::operator = (String &&)", "[String-operator+-16]") @@ -153,7 +153,7 @@ TEST_CASE ("Testing & String::operator = (String &&)", "[String-operator+-16]") arduino::String str("Hello"); arduino::String str1("Arduino"); str1 = static_cast(str); - REQUIRE(str1.compareTo("Hello") == 0); + REQUIRE(str1 == "Hello"); } TEST_CASE ("Testing & String::operator = (StringSumHelper &&)", "[String-operator+-17]") @@ -162,5 +162,5 @@ TEST_CASE ("Testing & String::operator = (StringSumHelper &&)", "[String-operato char const ch = '!'; arduino::String str1("Arduino"); str1 = static_cast(str+ch); - REQUIRE(str1.compareTo("Hello!") == 0); + REQUIRE(str1 == "Hello!"); } diff --git a/test/src/String/test_remove.cpp b/test/src/String/test_remove.cpp index 998a890c..e8c19536 100644 --- a/test/src/String/test_remove.cpp +++ b/test/src/String/test_remove.cpp @@ -27,14 +27,14 @@ TEST_CASE ("Testing String::remove(index) when index is > string length", "[Stri { arduino::String str("Hello Arduino!"); str.remove(100); - REQUIRE(strcmp(str.c_str(), "Hello Arduino!") == 0); + REQUIRE(str == "Hello Arduino!"); } TEST_CASE ("Testing String::remove(index) when index is < string length", "[String-remove-03]") { arduino::String str("Hello Arduino!"); str.remove(5); - REQUIRE(strcmp(str.c_str(), "Hello") == 0); + REQUIRE(str == "Hello"); } TEST_CASE ("Testing String::remove(index,count) when string is empty", "[String-remove-04]") @@ -48,19 +48,19 @@ TEST_CASE ("Testing String::remove(index,count) when index is > string length", { arduino::String str("Hello Arduino!"); str.remove(100, 10); - REQUIRE(strcmp(str.c_str(), "Hello Arduino!") == 0); + REQUIRE(str == "Hello Arduino!"); } TEST_CASE ("Testing String::remove(index,count) when index is < string length && count is > remaining length", "[String-remove-06]") { arduino::String str("Hello Arduino!"); str.remove(5, 100); - REQUIRE(strcmp(str.c_str(), "Hello") == 0); + REQUIRE(str == "Hello"); } TEST_CASE ("Testing String::remove(index,count) when index is < string length && count is < remaining length", "[String-remove-07]") { arduino::String str("Hello Arduino!"); str.remove(5, 1); - REQUIRE(strcmp(str.c_str(), "HelloArduino!") == 0); + REQUIRE(str == "HelloArduino!"); } diff --git a/test/src/String/test_replace.cpp b/test/src/String/test_replace.cpp index 56e0da9e..62fd5e8f 100644 --- a/test/src/String/test_replace.cpp +++ b/test/src/String/test_replace.cpp @@ -27,7 +27,7 @@ TEST_CASE ("Testing String::replace(char, char) when string contains elements != { arduino::String str("Hello Arduino!"); str.replace('Z', '0'); - REQUIRE(strcmp(str.c_str(), "Hello Arduino!") == 0); + REQUIRE(str == "Hello Arduino!"); } TEST_CASE ("Testing String::replace(char, char) when string contains elements = 'find'", "[String-replace-03]") @@ -36,33 +36,33 @@ TEST_CASE ("Testing String::replace(char, char) when string contains elements = str.replace('o', '0'); str.replace('e', '3'); str.replace('i', '1'); - REQUIRE(strcmp(str.c_str(), "H3ll0 Ardu1n0!") == 0); + REQUIRE(str == "H3ll0 Ardu1n0!"); } TEST_CASE ("Testing String::replace(String, String) when string does not constain subtr 'find'", "[String-replace-04]") { arduino::String str("Hello Arduino!"); str.replace(arduino::String("Zulu"), arduino::String("11")); - REQUIRE(strcmp(str.c_str(), "Hello Arduino!") == 0); + REQUIRE(str == "Hello Arduino!"); } TEST_CASE ("Testing String::replace(String, String) when string constains subtr 'find'", "[String-replace-05]") { arduino::String str("Hello Arduino!"); str.replace(arduino::String("ll"), arduino::String("11")); - REQUIRE(strcmp(str.c_str(), "He11o Arduino!") == 0); + REQUIRE(str == "He11o Arduino!"); } TEST_CASE ("Testing String::replace(String, String) substr 'find' larger than 'replace'", "[String-replace-06]") { arduino::String str("Hello Arduino!"); str.replace(arduino::String("llo"), arduino::String("11")); - REQUIRE(strcmp(str.c_str(), "He11 Arduino!") == 0); + REQUIRE(str == "He11 Arduino!"); } TEST_CASE ("Testing String::replace(String, String) substr 'find' smaller than 'replace'", "[String-replace-07]") { arduino::String str("Hello Arduino!"); str.replace(arduino::String("ll"), arduino::String("111")); - REQUIRE(strcmp(str.c_str(), "He111o Arduino!") == 0); + REQUIRE(str == "He111o Arduino!"); } diff --git a/test/src/String/test_substring.cpp b/test/src/String/test_substring.cpp index 366f363d..8fa43086 100644 --- a/test/src/String/test_substring.cpp +++ b/test/src/String/test_substring.cpp @@ -28,13 +28,13 @@ TEST_CASE ("Testing String::substring(unsigned int, unsigned int)", "[String-sub { arduino::String str1("Hello"); arduino::String str2("ello"); - REQUIRE(str2.compareTo(str1.substring(1,9)) == 0); + REQUIRE(str2 == str1.substring(1,9)); } WHEN ("left higher than right") { arduino::String str1("Hello"); arduino::String str2("ello"); - REQUIRE(str2.compareTo(str1.substring(9,1)) == 0); + REQUIRE(str2 == str1.substring(9,1)); } } diff --git a/test/src/String/test_toLowerCase.cpp b/test/src/String/test_toLowerCase.cpp index c8c683eb..1ff81e91 100644 --- a/test/src/String/test_toLowerCase.cpp +++ b/test/src/String/test_toLowerCase.cpp @@ -20,5 +20,5 @@ TEST_CASE ("Testing String::toLowerCase", "[String-toLowerCase-01]") { arduino::String str("HELLO ARDUINO"); str.toLowerCase(); - REQUIRE(strcmp(str.c_str(), "hello arduino") == 0); + REQUIRE(str == "hello arduino"); } diff --git a/test/src/String/test_toUpperCase.cpp b/test/src/String/test_toUpperCase.cpp index c0628352..b8ae6aaf 100644 --- a/test/src/String/test_toUpperCase.cpp +++ b/test/src/String/test_toUpperCase.cpp @@ -20,5 +20,5 @@ TEST_CASE ("Testing String::toUpperCase", "[String-toUpperCase-01]") { arduino::String str("hello arduino"); str.toUpperCase(); - REQUIRE(strcmp(str.c_str(), "HELLO ARDUINO") == 0); + REQUIRE(str == "HELLO ARDUINO"); } diff --git a/test/src/String/test_trim.cpp b/test/src/String/test_trim.cpp index 70d828cd..0328e3fc 100644 --- a/test/src/String/test_trim.cpp +++ b/test/src/String/test_trim.cpp @@ -20,26 +20,26 @@ TEST_CASE ("Testing String::trim with space at the beginning", "[String-trim-01] { arduino::String str(" hello"); str.trim(); - REQUIRE(strcmp(str.c_str(), "hello") == 0); + REQUIRE(str == "hello"); } TEST_CASE ("Testing String::trim with space at the end", "[String-trim-02]") { arduino::String str("hello "); str.trim(); - REQUIRE(strcmp(str.c_str(), "hello") == 0); + REQUIRE(str == "hello"); } TEST_CASE ("Testing String::trim with space at both beginng and end", "[String-trim-03]") { arduino::String str(" hello "); str.trim(); - REQUIRE(strcmp(str.c_str(), "hello") == 0); + REQUIRE(str == "hello"); } TEST_CASE ("Testing String::trim with space in the middle", "[String-trim-04]") { arduino::String str("Hello Arduino!"); str.trim(); - REQUIRE(strcmp(str.c_str(), "Hello Arduino!") == 0); + REQUIRE(str == "Hello Arduino!"); }