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

Simplify string comparisons in tests and fix bug in String::compareTo#137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
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
2 changes: 1 addition & 1 deletionapi/String.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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;
}
Expand Down
24 changes: 24 additions & 0 deletionstest/src/String/StringPrinter.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
#pragma once

#include <catch.hpp>
#include <String.h>

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<arduino::String> {
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
42 changes: 22 additions & 20 deletionstest/src/String/test_String.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,6 +12,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand All@@ -20,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@@ -104,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@@ -125,36 +127,36 @@ 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]")
{
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(str1.compareTo("Arduino") ==0);
REQUIRE(str =="Arduino");
}
4 changes: 3 additions & 1 deletiontest/src/String/test_characterAccessFunc.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand All@@ -24,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
2 changes: 2 additions & 0 deletionstest/src/String/test_compareTo.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,8 @@

#include<String.h>

#include"StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
52 changes: 51 additions & 1 deletiontest/src/String/test_comparisonFunc.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand All@@ -20,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");
Expand DownExpand Up@@ -104,4 +154,4 @@ TEST_CASE ("Testing String::endsWith(const String &)", "[String-endsWith-10]")
arduino::String str2("Helo");
REQUIRE(str1.endsWith(str2) == 0);
}
}
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp