From 4756cd32022a384d93627ce49bc23d6236701f66 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 25 Nov 2020 12:09:27 +0100 Subject: [PATCH 001/131] HardwareSPI needs to be a abstract base class, that is only pure virtual functions. --- api/HardwareSPI.h | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/api/HardwareSPI.h b/api/HardwareSPI.h index a851b937..d6609469 100644 --- a/api/HardwareSPI.h +++ b/api/HardwareSPI.h @@ -104,22 +104,24 @@ const SPISettings DEFAULT_SPI_SETTINGS = SPISettings(); class HardwareSPI { public: - virtual uint8_t transfer(uint8_t data); - virtual uint16_t transfer16(uint16_t data); - virtual void transfer(void *buf, size_t count); + virtual ~HardwareSPI() { } + + virtual uint8_t transfer(uint8_t data) = 0; + virtual uint16_t transfer16(uint16_t data) = 0; + virtual void transfer(void *buf, size_t count) = 0; // Transaction Functions - virtual void usingInterrupt(int interruptNumber); - virtual void notUsingInterrupt(int interruptNumber); - virtual void beginTransaction(SPISettings settings); - virtual void endTransaction(void); + virtual void usingInterrupt(int interruptNumber) = 0; + virtual void notUsingInterrupt(int interruptNumber) = 0; + virtual void beginTransaction(SPISettings settings) = 0; + virtual void endTransaction(void) = 0; // SPI Configuration methods - virtual void attachInterrupt(); - virtual void detachInterrupt(); + virtual void attachInterrupt() = 0; + virtual void detachInterrupt() = 0; - virtual void begin(); - virtual void end(); + virtual void begin() = 0; + virtual void end() = 0; }; // Alias SPIClass to HardwareSPI since it's already the defacto standard for SPI classe name From f3cfa2f267051c5761edc642e64865449431126f Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 9 Dec 2020 10:18:21 +0100 Subject: [PATCH 002/131] Calling String::String(float ...) with +/- FLT_MAX or String::String(double ...) with +/- DBL_MAX results in a stack smashing. --- test/src/String/test_String.cpp | 40 +++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/test/src/String/test_String.cpp b/test/src/String/test_String.cpp index 1c94cb99..8ff44c0f 100644 --- a/test/src/String/test_String.cpp +++ b/test/src/String/test_String.cpp @@ -6,6 +6,8 @@ * INCLUDE **************************************************************************************/ +#include + #include #include @@ -80,16 +82,40 @@ TEST_CASE ("Testing String(unsigned long, unsigned char base = 10) constructor() TEST_CASE ("Testing String(float, unsigned char decimalPlaces = 2) constructor()", "[String-Ctor-10]") { - float const val = 1.234f; - arduino::String str(val); - REQUIRE(strcmp(str.c_str(), "1.23") == 0); + WHEN ("String::String (some float value)") + { + arduino::String str(1.234f); + REQUIRE(strcmp(str.c_str(), "1.23") == 0); + } + WHEN ("String::String (FLT_MAX)") + { + arduino::String str(FLT_MAX); + REQUIRE(strcmp(str.c_str(), "340282346638528859811704183484516925440.00") == 0); + } + WHEN ("String::String (-FLT_MAX)") + { + arduino::String str(-FLT_MAX); + REQUIRE(strcmp(str.c_str(), "-340282346638528859811704183484516925440.00") == 0); + } } TEST_CASE ("Testing String(double, unsigned char decimalPlaces = 2) constructor()", "[String-Ctor-11]") { - double const val = 5.678; - arduino::String str(val); - REQUIRE(strcmp(str.c_str(), "5.68") == 0); + WHEN ("String::String (some double value)") + { + arduino::String str(5.678); + REQUIRE(strcmp(str.c_str(), "5.68") == 0); + } + WHEN ("String::String (DBL_MAX)") + { + arduino::String str(DBL_MAX); + REQUIRE(strcmp(str.c_str(), "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00") == 0); + } + WHEN ("String::String (-DBL_MAX)") + { + arduino::String str(-DBL_MAX); + REQUIRE(strcmp(str.c_str(), "-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00") == 0); + } } TEST_CASE ("Testing String(const __FlashStringHelper) constructor() with invalid buffer", "[String-Ctor-12]") @@ -131,4 +157,4 @@ TEST_CASE ("Testing String(String &&) with move(String &rhs) from larger to smal arduino::String str1("Arduino"); str = static_cast(str1); REQUIRE(str1.compareTo("Arduino") == 0); -} \ No newline at end of file +} From 952d776d88550abd62f899646e14b5aa68f18dd5 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 9 Dec 2020 10:30:12 +0100 Subject: [PATCH 003/131] Increase buffer size in order to avoid buffer overflow when using large floating point numbers --- api/String.cpp | 19 +++++++++++++++---- api/String.h | 3 +++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/api/String.cpp b/api/String.cpp index b96b1005..fd8c87f2 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -23,11 +23,20 @@ #include "itoa.h" #include "deprecated-avr-comp/avr/dtostrf.h" +#include + +namespace arduino { + /*********************************************/ -/* Constructors */ +/* Static Member Initialisation */ /*********************************************/ -namespace arduino { +size_t const String::FLT_MAX_DECIMAL_PLACES; +size_t const String::DBL_MAX_DECIMAL_PLACES; + +/*********************************************/ +/* Constructors */ +/*********************************************/ String::String(const char *cstr) { @@ -111,15 +120,17 @@ String::String(unsigned long value, unsigned char base) String::String(float value, unsigned char decimalPlaces) { + static size_t const FLOAT_BUF_SIZE = FLT_MAX_10_EXP + FLT_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */; init(); - char buf[33]; + char buf[FLOAT_BUF_SIZE]; *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); } String::String(double value, unsigned char decimalPlaces) { + static size_t const DOUBLE_BUF_SIZE = DBL_MAX_10_EXP + DBL_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */; init(); - char buf[33]; + char buf[DOUBLE_BUF_SIZE]; *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); } diff --git a/api/String.h b/api/String.h index 93d5b06b..d8f943eb 100644 --- a/api/String.h +++ b/api/String.h @@ -58,6 +58,9 @@ class String typedef void (String::*StringIfHelperType)() const; void StringIfHelper() const {} + static size_t const FLT_MAX_DECIMAL_PLACES = 10; + static size_t const DBL_MAX_DECIMAL_PLACES = FLT_MAX_DECIMAL_PLACES; + public: // constructors // creates a copy of the initial value. From 3c76ef240e483306d129d137696ea3e04e0dbec0 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 9 Dec 2020 10:36:50 +0100 Subject: [PATCH 004/131] Ensure that no buffer overflow can occur by limiting the number of post-comma digits --- api/String.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api/String.cpp b/api/String.cpp index fd8c87f2..37812418 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -20,6 +20,7 @@ */ #include "String.h" +#include "Common.h" #include "itoa.h" #include "deprecated-avr-comp/avr/dtostrf.h" @@ -123,6 +124,7 @@ String::String(float value, unsigned char decimalPlaces) static size_t const FLOAT_BUF_SIZE = FLT_MAX_10_EXP + FLT_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */; init(); char buf[FLOAT_BUF_SIZE]; + decimalPlaces = min(decimalPlaces, FLT_MAX_DECIMAL_PLACES); *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); } @@ -131,6 +133,7 @@ String::String(double value, unsigned char decimalPlaces) static size_t const DOUBLE_BUF_SIZE = DBL_MAX_10_EXP + DBL_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */; init(); char buf[DOUBLE_BUF_SIZE]; + decimalPlaces = min(decimalPlaces, DBL_MAX_DECIMAL_PLACES); *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); } From 4f194387a356624e8c5f4140016a69f80dc75920 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 10 Dec 2020 10:18:20 +0100 Subject: [PATCH 005/131] Changing return type of 'requestFrom' from uint8_t to size_t allows the function to return the correct amount of bytes read (since internally it's already a size_t which is downcast to a uint8_t upon returning it. (#132) --- api/HardwareI2C.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/HardwareI2C.h b/api/HardwareI2C.h index c107d1a6..4a8e5f98 100644 --- a/api/HardwareI2C.h +++ b/api/HardwareI2C.h @@ -36,8 +36,8 @@ class HardwareI2C : public Stream virtual uint8_t endTransmission(bool stopBit) = 0; virtual uint8_t endTransmission(void) = 0; - virtual uint8_t requestFrom(uint8_t address, size_t len, bool stopBit) = 0; - virtual uint8_t requestFrom(uint8_t address, size_t len) = 0; + virtual size_t requestFrom(uint8_t address, size_t len, bool stopBit) = 0; + virtual size_t requestFrom(uint8_t address, size_t len) = 0; virtual void onReceive(void(*)(int)) = 0; virtual void onRequest(void(*)(void)) = 0; From 129ae52c594de3b52b5ded652a99309543b9da62 Mon Sep 17 00:00:00 2001 From: Edgar Bonet Date: Thu, 10 Dec 2020 20:51:08 +0100 Subject: [PATCH 006/131] Test Stream::parseFloat() with many digits --- test/src/Stream/test_parseFloat.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/src/Stream/test_parseFloat.cpp b/test/src/Stream/test_parseFloat.cpp index 610f96a0..a750efc7 100644 --- a/test/src/Stream/test_parseFloat.cpp +++ b/test/src/Stream/test_parseFloat.cpp @@ -10,6 +10,8 @@ #include +#include + /************************************************************************************** * TEST CODE **************************************************************************************/ @@ -43,6 +45,11 @@ TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = mock << "\r\n\t 12.34"; REQUIRE(mock.parseFloat() == 12.34f); } + WHEN ("A float is provided with too many digits") + { + mock << "3.1415926535897932384"; + REQUIRE(abs(mock.parseFloat() - 3.141592654f) < 8 * FLT_EPSILON); + } } TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_NONE, char ignore = NO_IGNORE_CHAR)", "[Stream-parseFloat-02]") From 8eb4013bb230670948ea015189c6775a51665d33 Mon Sep 17 00:00:00 2001 From: Edgar Bonet Date: Fri, 11 Dec 2020 10:14:17 +0100 Subject: [PATCH 007/131] Use Approx() macro for fuzzy comparison --- test/src/Stream/test_parseFloat.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/Stream/test_parseFloat.cpp b/test/src/Stream/test_parseFloat.cpp index a750efc7..64a49c98 100644 --- a/test/src/Stream/test_parseFloat.cpp +++ b/test/src/Stream/test_parseFloat.cpp @@ -48,7 +48,7 @@ TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = WHEN ("A float is provided with too many digits") { mock << "3.1415926535897932384"; - REQUIRE(abs(mock.parseFloat() - 3.141592654f) < 8 * FLT_EPSILON); + REQUIRE(mock.parseFloat() == Approx(3.141592654f)); } } From 5445db39345f62acbdf51c3835ea9c0b512fb5dc Mon Sep 17 00:00:00 2001 From: Edgar Bonet Date: Fri, 11 Dec 2020 14:16:22 +0100 Subject: [PATCH 008/131] Test Stream::parseFloat() with a large number --- test/src/Stream/test_parseFloat.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/src/Stream/test_parseFloat.cpp b/test/src/Stream/test_parseFloat.cpp index 64a49c98..0317b99b 100644 --- a/test/src/Stream/test_parseFloat.cpp +++ b/test/src/Stream/test_parseFloat.cpp @@ -45,11 +45,16 @@ TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = mock << "\r\n\t 12.34"; REQUIRE(mock.parseFloat() == 12.34f); } - WHEN ("A float is provided with too many digits") + WHEN ("A float is provided with too many digits after the decimal point") { mock << "3.1415926535897932384"; REQUIRE(mock.parseFloat() == Approx(3.141592654f)); } + WHEN ("A float is larger than LONG_MAX") + { + mock << "602200000000000000000000.00"; + REQUIRE(mock.parseFloat() == Approx(6.022e23f)); + } } TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_NONE, char ignore = NO_IGNORE_CHAR)", "[Stream-parseFloat-02]") From fae13e5f5324295118cf57104c69c3b087f7d64b Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 14 Dec 2020 10:11:31 +0100 Subject: [PATCH 009/131] Exchanging the type of 'value' from 'long' to 'double' prevents overflow when parsing float values. However, we still need to ensure against too large values contained in streams. This should be possible because the maximum length of a float value pre-comma is known to be 38 digits (FLT_MAX_10_EXP). --- api/Stream.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/api/Stream.cpp b/api/Stream.cpp index a5d8b076..6eb1bdf2 100644 --- a/api/Stream.cpp +++ b/api/Stream.cpp @@ -24,6 +24,7 @@ #include "Common.h" #include "Stream.h" +#include #define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait @@ -162,9 +163,9 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore) { bool isNegative = false; bool isFraction = false; - long value = 0; + double value = 0.0; int c; - float fraction = 1.0; + unsigned int digits_post_comma = 0; c = peekNextDigit(lookahead, true); // ignore non numeric leading characters @@ -181,7 +182,7 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore) else if(c >= '0' && c <= '9') { // is c a digit? value = value * 10 + c - '0'; if(isFraction) - fraction *= 0.1; + digits_post_comma++; } read(); // consume the character we got with peek c = timedPeek(); @@ -190,10 +191,11 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore) if(isNegative) value = -value; + if(isFraction) - return value * fraction; - else - return value; + value /= pow(10, digits_post_comma); + + return value; } // read characters from stream into buffer From 1266b08d30bc4cd71952504dd9830fedd40ac68f Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 14 Dec 2020 10:23:58 +0100 Subject: [PATCH 010/131] Replacing computational expensive pow call with result of accumulated multiplication. --- api/Stream.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/api/Stream.cpp b/api/Stream.cpp index 6eb1bdf2..506e2a7f 100644 --- a/api/Stream.cpp +++ b/api/Stream.cpp @@ -24,7 +24,6 @@ #include "Common.h" #include "Stream.h" -#include #define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait @@ -165,7 +164,7 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore) bool isFraction = false; double value = 0.0; int c; - unsigned int digits_post_comma = 0; + double fraction = 1.0; c = peekNextDigit(lookahead, true); // ignore non numeric leading characters @@ -182,7 +181,7 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore) else if(c >= '0' && c <= '9') { // is c a digit? value = value * 10 + c - '0'; if(isFraction) - digits_post_comma++; + fraction *= 0.1; } read(); // consume the character we got with peek c = timedPeek(); @@ -193,7 +192,7 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore) value = -value; if(isFraction) - value /= pow(10, digits_post_comma); + value *= fraction; return value; } From 2ca15ad8d39ccd79b2afe5a90c828e6e20d53028 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 14 Dec 2020 17:06:00 +0100 Subject: [PATCH 011/131] Release v1.2.0 --- api/ArduinoAPI.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/ArduinoAPI.h b/api/ArduinoAPI.h index fe998604..699df1dd 100644 --- a/api/ArduinoAPI.h +++ b/api/ArduinoAPI.h @@ -20,8 +20,8 @@ #ifndef ARDUINO_API_H #define ARDUINO_API_H -// version 1.1.0 -#define ARDUINO_API_VERSION 10100 +// version 1.2.0 +#define ARDUINO_API_VERSION 10200 #include "Binary.h" From 3caa0394c31f875a98ce0c73c520b66d66fb9e12 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 31 Dec 2020 11:24:37 +0100 Subject: [PATCH 012/131] 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 013/131] 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 014/131] 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 015/131] 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 016/131] 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!"); } From 184dd3e812fe16c66c5bf3a78626c1a47bb900e8 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Sun, 16 Mar 2014 09:55:11 +0100 Subject: [PATCH 017/131] Remove strlen in calls like String::concat(s, strlen(s)) Instead of calling strlen in a dozen places, instead just call String::concat(s), which will then call strlen. This shrinks the code size of these calls significantly, the StringAppendOperator example on the Arduino Uno shrinks by 72 bytes. This change does incur a slight runtime cost, because there is one extra function call. --- api/String.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/api/String.cpp b/api/String.cpp index 6f14ae13..6f3e45f6 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -307,49 +307,49 @@ unsigned char String::concat(unsigned char num) { char buf[1 + 3 * sizeof(unsigned char)]; itoa(num, buf, 10); - return concat(buf, strlen(buf)); + return concat(buf); } unsigned char String::concat(int num) { char buf[2 + 3 * sizeof(int)]; itoa(num, buf, 10); - return concat(buf, strlen(buf)); + return concat(buf); } unsigned char String::concat(unsigned int num) { char buf[1 + 3 * sizeof(unsigned int)]; utoa(num, buf, 10); - return concat(buf, strlen(buf)); + return concat(buf); } unsigned char String::concat(long num) { char buf[2 + 3 * sizeof(long)]; ltoa(num, buf, 10); - return concat(buf, strlen(buf)); + return concat(buf); } unsigned char String::concat(unsigned long num) { char buf[1 + 3 * sizeof(unsigned long)]; ultoa(num, buf, 10); - return concat(buf, strlen(buf)); + return concat(buf); } unsigned char String::concat(float num) { char buf[20]; char* string = dtostrf(num, 4, 2, buf); - return concat(string, strlen(string)); + return concat(string); } unsigned char String::concat(double num) { char buf[20]; char* string = dtostrf(num, 4, 2, buf); - return concat(string, strlen(string)); + return concat(string); } unsigned char String::concat(const __FlashStringHelper * str) @@ -378,7 +378,7 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs) StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr) { StringSumHelper &a = const_cast(lhs); - if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate(); + if (!cstr || !a.concat(cstr)) a.invalidate(); return a; } From 59dd9950ff797a803fb449983ae9fb3e1c436406 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Sun, 16 Mar 2014 09:58:47 +0100 Subject: [PATCH 018/131] Make string concat, copy and move work on non-terminated strings Previously, these methods took a nul-terminated string and appended it to the current buffer. The length argument (or length of the passed String object) was used to allocate memory, but was not used when actually copying the string. This meant that: - If the passed length was too short, or the string passed in was not nul-terminated, the buffer would overflow. - If the string passed in contained embedded nul characters (i.e, among the first length characters), any characters after the embedded nul would not be copied (but len would be updated to indicate they were). In practice, neither of the above would occur, since the length passed is always the known length of the string, usually as returned by strlen. However, to make this method public, and to allow using this concat method to pass in strings that are not nul-terminated, it should be changed to be more robust. This commit changes the method to use memcpy instead of strcpy, copying exactly the number of bytes passed in. For the current calls to this method, which pass a nul-terminated string, without embedded nul characters and a correct length, this behaviour should not change. However, this concat method can now also be used in the two cases mentioned above. Non-nul-terminated strings now work as expected and for strings with embedded newlines the entire string is copied as-is, instead of leaving uninitialized memory after the embedded nul byte. Note that a lot of operations will still only see the string up to the embedded nul byte, but that's not really fixable unless we reimplement functions like strcmp. --- api/String.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/String.cpp b/api/String.cpp index 6f3e45f6..db980897 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -192,7 +192,7 @@ String & String::copy(const char *cstr, unsigned int length) return *this; } len = length; - strcpy(buffer, cstr); + memcpy(buffer, cstr, length); return *this; } @@ -212,7 +212,7 @@ void String::move(String &rhs) { if (buffer) { if (rhs && capacity >= rhs.len) { - strcpy(buffer, rhs.buffer); + memcpy(buffer, rhs.buffer, rhs.len); len = rhs.len; rhs.len = 0; return; @@ -284,7 +284,7 @@ unsigned char String::concat(const char *cstr, unsigned int length) if (!cstr) return 0; if (length == 0) return 1; if (!reserve(newlen)) return 0; - strcpy(buffer + len, cstr); + memcpy(buffer + len, cstr, length); len = newlen; return 1; } From 81a2d51b212c1ea8594065b34de1abd711cdcc42 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Sun, 16 Mar 2014 12:27:00 +0100 Subject: [PATCH 019/131] Simplify String::concat(char) Now concat(const char*, unsigned int) no longer requires a nul-terminated string, we can simplify the concat(char) method to just pass the address of the single character instead of having to create buffer with nul-termination. --- api/String.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/api/String.cpp b/api/String.cpp index db980897..5bf5862a 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -297,10 +297,7 @@ unsigned char String::concat(const char *cstr) unsigned char String::concat(char c) { - char buf[2]; - buf[0] = c; - buf[1] = 0; - return concat(buf, 1); + return concat(&c, 1); } unsigned char String::concat(unsigned char num) From 3b88acac8ee00fc57bc3328e68823aa53088f891 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Sun, 16 Mar 2014 09:25:17 +0100 Subject: [PATCH 020/131] Make String::concat(const char *, unsigned int) public This method is useful when receiving data from external sources that pass an explicit length instead of a NUL-terminated string. --- api/String.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/String.h b/api/String.h index d8f943eb..37842379 100644 --- a/api/String.h +++ b/api/String.h @@ -109,6 +109,7 @@ class String // concatenation is considered unsucessful. unsigned char concat(const String &str); unsigned char concat(const char *cstr); + unsigned char concat(const char *cstr, unsigned int length); unsigned char concat(char c); unsigned char concat(unsigned char num); unsigned char concat(int num); @@ -225,7 +226,6 @@ class String void init(void); void invalidate(void); unsigned char changeBuffer(unsigned int maxStrLen); - unsigned char concat(const char *cstr, unsigned int length); // copy and move String & copy(const char *cstr, unsigned int length); From 84314888a8a73b643469c5609beed6d21d1c037a Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Sun, 16 Mar 2014 10:37:38 +0100 Subject: [PATCH 021/131] Add String(char *, unsigned) constructor This constructor allows converting a buffer containing a non-nul-terminated string to a String object, by explicitely passing the length. --- api/String.cpp | 6 ++++++ api/String.h | 1 + 2 files changed, 7 insertions(+) diff --git a/api/String.cpp b/api/String.cpp index 5bf5862a..fc8a196e 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -45,6 +45,12 @@ String::String(const char *cstr) if (cstr) copy(cstr, strlen(cstr)); } +String::String(const char *cstr, unsigned int length) +{ + init(); + if (cstr) copy(cstr, length); +} + String::String(const String &value) { init(); diff --git a/api/String.h b/api/String.h index 37842379..c0a8ce2d 100644 --- a/api/String.h +++ b/api/String.h @@ -68,6 +68,7 @@ class String // fails, the string will be marked as invalid (i.e. "if (s)" will // be false). String(const char *cstr = ""); + String(const char *cstr, unsigned int length); String(const String &str); String(const __FlashStringHelper *str); #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) From dd236bfd2c62ed083aaa1b1229d71e85fd92f7a9 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Sun, 16 Mar 2014 12:01:53 +0100 Subject: [PATCH 022/131] Don't mess with the original in String::substring Before, substring would (temporarily) add a \0 in the original string at the end of the substring, so the substring could be copied into a new String object. However, now that the String::copy() method can work with non-nul-terminated strings (by passing an explicit length), this trick is not needed and we can just call the copy method instead. --- api/String.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/api/String.cpp b/api/String.cpp index fc8a196e..b67e99c8 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -632,10 +632,7 @@ String String::substring(unsigned int left, unsigned int right) const String out; if (left >= len) return out; if (right > len) right = len; - char temp = buffer[right]; // save the replaced character - buffer[right] = '\0'; - out = buffer + left; // pointer arithmetic - buffer[right] = temp; //restore character + out.copy(buffer + left, right - left); return out; } From eaab14db09073afc20bcf26bd9b7448a6a1309b0 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Mon, 17 Mar 2014 11:24:58 +0100 Subject: [PATCH 023/131] Add String::concat(const uint8_t *, unsigned int) version This just calls the char* version, but allows calling the method with a uint8_t* as well (which is not uncommon for buffers). --- api/String.h | 1 + 1 file changed, 1 insertion(+) diff --git a/api/String.h b/api/String.h index c0a8ce2d..2034c1d2 100644 --- a/api/String.h +++ b/api/String.h @@ -111,6 +111,7 @@ class String unsigned char concat(const String &str); unsigned char concat(const char *cstr); unsigned char concat(const char *cstr, unsigned int length); + unsigned char concat(const uint8_t *cstr, unsigned int length) {return concat((const char*)cstr, length);} unsigned char concat(char c); unsigned char concat(unsigned char num); unsigned char concat(int num); From 0d83f1afc3367037dbde5323c2abd0ae1bd2c583 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 4 Aug 2015 10:08:09 +0200 Subject: [PATCH 024/131] Add String(const uint8_t *, unsigned int) constructor This allows creating a String from a uint8_t[] or uint8_t* as well, without having to add explicit casts. --- api/String.h | 1 + 1 file changed, 1 insertion(+) diff --git a/api/String.h b/api/String.h index 2034c1d2..a3d0d9e8 100644 --- a/api/String.h +++ b/api/String.h @@ -69,6 +69,7 @@ class String // be false). String(const char *cstr = ""); String(const char *cstr, unsigned int length); + String(const uint8_t *cstr, unsigned int length) : String((const char*)cstr, length) {} String(const String &str); String(const __FlashStringHelper *str); #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) From d6fd84fc7e42703a541de9b0837c1f6e78f97bae Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 31 Dec 2020 13:29:04 +0100 Subject: [PATCH 025/131] fixup! Make string concat, copy and move work on non-terminated strings --- api/String.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api/String.cpp b/api/String.cpp index b67e99c8..1b6d4b28 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -199,6 +199,7 @@ String & String::copy(const char *cstr, unsigned int length) } len = length; memcpy(buffer, cstr, length); + buffer[len] = '\0'; return *this; } @@ -220,6 +221,7 @@ void String::move(String &rhs) if (rhs && capacity >= rhs.len) { memcpy(buffer, rhs.buffer, rhs.len); len = rhs.len; + buffer[len] = '\0'; rhs.len = 0; return; } else { @@ -292,6 +294,7 @@ unsigned char String::concat(const char *cstr, unsigned int length) if (!reserve(newlen)) return 0; memcpy(buffer + len, cstr, length); len = newlen; + buffer[len] = '\0'; return 1; } From 91c5e2985335c6cf88c5977cbbc311c292bbf7b8 Mon Sep 17 00:00:00 2001 From: Edgar Bonet Date: Mon, 25 Jan 2021 13:03:20 +0100 Subject: [PATCH 026/131] Test Stream::parseFloat() with a huge number of digits Give a 311-digit number to Stream::parseFloat(). This makes the local variable `value' overflow to infinity. With so many digits, the number cannot be parsed into an integer, not even into an integer stored as a `double'. Note that 40 digits would be enough to unveil this issue on AVR. --- test/src/Stream/test_parseFloat.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/Stream/test_parseFloat.cpp b/test/src/Stream/test_parseFloat.cpp index 0317b99b..19d2ce86 100644 --- a/test/src/Stream/test_parseFloat.cpp +++ b/test/src/Stream/test_parseFloat.cpp @@ -47,7 +47,7 @@ TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = } WHEN ("A float is provided with too many digits after the decimal point") { - mock << "3.1415926535897932384"; + mock << "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870064"; REQUIRE(mock.parseFloat() == Approx(3.141592654f)); } WHEN ("A float is larger than LONG_MAX") From 61975114e344a74bba4aa643fd6dd246175fde98 Mon Sep 17 00:00:00 2001 From: Edgar Bonet Date: Mon, 25 Jan 2021 13:06:35 +0100 Subject: [PATCH 027/131] Avoid overflowing parseFloat()'s internal 'value' If more than 309 digits are provided to Stream::parseFloat() (more than 39 on AVR), the internal variable 'value' would overflow to infinity. We avoid this by not storing the parsed number as an integer-in-a-float. --- api/Stream.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/api/Stream.cpp b/api/Stream.cpp index 506e2a7f..f6f9bda6 100644 --- a/api/Stream.cpp +++ b/api/Stream.cpp @@ -179,9 +179,12 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore) else if (c == '.') isFraction = true; else if(c >= '0' && c <= '9') { // is c a digit? - value = value * 10 + c - '0'; - if(isFraction) - fraction *= 0.1; + if(isFraction) { + fraction *= 0.1; + value = value + fraction * (c - '0'); + } else { + value = value * 10 + c - '0'; + } } read(); // consume the character we got with peek c = timedPeek(); @@ -191,9 +194,6 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore) if(isNegative) value = -value; - if(isFraction) - value *= fraction; - return value; } From bed3b20837ffbf40331a63c0530e439c0d060830 Mon Sep 17 00:00:00 2001 From: per1234 Date: Mon, 1 Feb 2021 23:56:27 -0800 Subject: [PATCH 028/131] Add issue templates A menu of issue categories will be presented when the user begins the issue creation process. The issue will be prefilled with prompts for the specific information needed for each issue type. If none of the issue types are applicable, the user can click the "Open a blank issue" link at the bottom of the issue template chooser page to get the previous issue creation behavior. The default organization level security policy is linked to for people wanting to report a security vulnerability. Links are provided to the Arduino Language Reference, Arduino Forum, and Arduino Developers Mailing list to redirect support requests. References: - https://docs.github.com/en/github/building-a-strong-community/about-issue-and-pull-request-templates - https://docs.github.com/en/github/building-a-strong-community/creating-a-pull-request-template-for-your-repository - https://docs.github.com/en/github/building-a-strong-community/configuring-issue-templates-for-your-repository#configuring-the-template-chooser --- .github/ISSUE_TEMPLATE/api-deprecation.md | 25 ++++++++++++++++++ .github/ISSUE_TEMPLATE/api-improvement.md | 24 +++++++++++++++++ .github/ISSUE_TEMPLATE/bug_report.md | 29 +++++++++++++++++++++ .github/ISSUE_TEMPLATE/config.yml | 10 +++++++ .github/ISSUE_TEMPLATE/new-api-component.md | 15 +++++++++++ .github/ISSUE_TEMPLATE/other-enhancement.md | 22 ++++++++++++++++ 6 files changed, 125 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/api-deprecation.md create mode 100644 .github/ISSUE_TEMPLATE/api-improvement.md create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/config.yml create mode 100644 .github/ISSUE_TEMPLATE/new-api-component.md create mode 100644 .github/ISSUE_TEMPLATE/other-enhancement.md diff --git a/.github/ISSUE_TEMPLATE/api-deprecation.md b/.github/ISSUE_TEMPLATE/api-deprecation.md new file mode 100644 index 00000000..74e4c785 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/api-deprecation.md @@ -0,0 +1,25 @@ +--- +name: API deprecation +about: Suggest the deprecation of an API component defined by ArduinoCore-API. +title: "" +labels: enhancement +assignees: "" +--- + +### API component + + + +### Description + + + + +### Replacement API component + + + + +### Additional information + + diff --git a/.github/ISSUE_TEMPLATE/api-improvement.md b/.github/ISSUE_TEMPLATE/api-improvement.md new file mode 100644 index 00000000..1cfa43bb --- /dev/null +++ b/.github/ISSUE_TEMPLATE/api-improvement.md @@ -0,0 +1,24 @@ +--- +name: API improvement +about: Suggest an improvement to an existing API component. +title: "" +labels: enhancement +assignees: "" +--- + +### API component + + + +### Description + + + +### Is this a breaking change? + + + + +### Additional information + + diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 00000000..cc410ae0 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,29 @@ +--- +name: Bug report +about: Report problems with the code in this repository. +title: "" +labels: bug +assignees: "" +--- + +### Description + + + +### Environment + +- Boards platform name: +- Boards platform version (as shown in Boards Manager): +- ArduinoCore-API version (if you manually installed it): + +### Current behavior + + + +### Expected behavior + + + +### Additional information + + diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 00000000..af023790 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,10 @@ +contact_links: + - name: Learn about the Arduino language + url: https://www.arduino.cc/reference/en + about: User documentation is available at the Arduino language reference. + - name: Support request + url: https://forum.arduino.cc/ + about: We can help you out on the Arduino Forum! + - name: Discuss ArduinoCore-API development + url: https://groups.google.com/a/arduino.cc/g/developers + about: Arduino Developers Mailing List diff --git a/.github/ISSUE_TEMPLATE/new-api-component.md b/.github/ISSUE_TEMPLATE/new-api-component.md new file mode 100644 index 00000000..15991f46 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/new-api-component.md @@ -0,0 +1,15 @@ +--- +name: New API component +about: Suggest the addition of a new API component to ArduinoCore-API. +title: "" +labels: enhancement +assignees: "" +--- + +### Description + + + +### Additional information + + diff --git a/.github/ISSUE_TEMPLATE/other-enhancement.md b/.github/ISSUE_TEMPLATE/other-enhancement.md new file mode 100644 index 00000000..c72cea49 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/other-enhancement.md @@ -0,0 +1,22 @@ +--- +name: Other enhancement +about: + Suggest an improvement for this project that doesn't fit in the specific categories + above. +title: "" +labels: enhancement +assignees: "" +--- + +### Description + + + +### Is this a breaking change? + + + + +### Additional information + + From db53d3d51879ec2253ed4288a926213b75cff633 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 29 Mar 2021 11:50:29 +0200 Subject: [PATCH 029/131] Remove forceful "using namespace arduino" from headers First step to fix https://github.com/arduino/ArduinoCore-samd/issues/606 --- api/Print.h | 2 +- api/String.h | 3 +++ api/Udp.h | 4 +--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/api/Print.h b/api/Print.h index a2b982da..fc30b45b 100644 --- a/api/Print.h +++ b/api/Print.h @@ -93,4 +93,4 @@ class Print }; } -using namespace arduino; \ No newline at end of file +using arduino::Print; \ No newline at end of file diff --git a/api/String.h b/api/String.h index a3d0d9e8..3f34493b 100644 --- a/api/String.h +++ b/api/String.h @@ -255,5 +255,8 @@ class StringSumHelper : public String } // namespace arduino +using arduino::__FlashStringHelper; +using arduino::String; + #endif // __cplusplus #endif // __ARDUINO_STRINGS__ diff --git a/api/Udp.h b/api/Udp.h index 53f89f9a..68a71e85 100644 --- a/api/Udp.h +++ b/api/Udp.h @@ -87,6 +87,4 @@ class UDP : public Stream { uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; }; -} - -using namespace arduino; +} \ No newline at end of file From f66d9dc4bdb6c31f92a1c321b6df14c88ec8c693 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Tue, 6 Apr 2021 15:39:04 +0200 Subject: [PATCH 030/131] Fix CI without explicit Arduino.h include --- test/src/Stream/test_parseFloat.cpp | 2 ++ test/src/Stream/test_parseInt.cpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/test/src/Stream/test_parseFloat.cpp b/test/src/Stream/test_parseFloat.cpp index 19d2ce86..a9fba616 100644 --- a/test/src/Stream/test_parseFloat.cpp +++ b/test/src/Stream/test_parseFloat.cpp @@ -12,6 +12,8 @@ #include +using namespace arduino; + /************************************************************************************** * TEST CODE **************************************************************************************/ diff --git a/test/src/Stream/test_parseInt.cpp b/test/src/Stream/test_parseInt.cpp index b5b60610..c79ed447 100644 --- a/test/src/Stream/test_parseInt.cpp +++ b/test/src/Stream/test_parseInt.cpp @@ -14,6 +14,8 @@ * TEST CODE **************************************************************************************/ +using namespace arduino; + TEST_CASE ("Testing parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR)", "[Stream-parseInt-01]") { StreamMock mock; From e481f1f29e3b9ec2bfdd4c82de44036fe034d848 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Tue, 6 Apr 2021 15:49:17 +0200 Subject: [PATCH 031/131] Release v1.3.0 --- api/ArduinoAPI.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/ArduinoAPI.h b/api/ArduinoAPI.h index 699df1dd..d954afa0 100644 --- a/api/ArduinoAPI.h +++ b/api/ArduinoAPI.h @@ -20,8 +20,8 @@ #ifndef ARDUINO_API_H #define ARDUINO_API_H -// version 1.2.0 -#define ARDUINO_API_VERSION 10200 +// version 1.3.0 +#define ARDUINO_API_VERSION 10300 #include "Binary.h" From e2d2f205094ffa46b7dd31e38a93785960de8604 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Wed, 7 Apr 2021 14:01:36 +0200 Subject: [PATCH 032/131] Bring most commonly used classes to default namespace --- api/IPAddress.h | 2 ++ api/Stream.h | 4 +++- api/Udp.h | 4 +++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/api/IPAddress.h b/api/IPAddress.h index 1771f8e1..8f6ca540 100644 --- a/api/IPAddress.h +++ b/api/IPAddress.h @@ -83,3 +83,5 @@ class IPAddress : public Printable { extern const IPAddress INADDR_NONE; } + +using arduino::IPAddress; \ No newline at end of file diff --git a/api/Stream.h b/api/Stream.h index bf4261a5..11aaa208 100644 --- a/api/Stream.h +++ b/api/Stream.h @@ -128,4 +128,6 @@ class Stream : public Print #undef NO_IGNORE_CHAR -} \ No newline at end of file +} + +using arduino::Stream; \ No newline at end of file diff --git a/api/Udp.h b/api/Udp.h index 68a71e85..4cfe2ac5 100644 --- a/api/Udp.h +++ b/api/Udp.h @@ -87,4 +87,6 @@ class UDP : public Stream { uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; }; -} \ No newline at end of file +} + +using arduino::UDP; \ No newline at end of file From 9668aa626d6f1f3427b88aed06175bc312a1ce84 Mon Sep 17 00:00:00 2001 From: Ahti Legonkov Date: Thu, 15 Nov 2018 23:16:49 +0200 Subject: [PATCH 033/131] Make String move constructor move instead of copy. The move constructor String::String(String&&) and String::operator=(String&&) now perform move instead of copy. Remove String(StringSumHelper&&) constructor because having it makes no sense: String(String&&) takes care of it - you can pass either String&& or StringSumHelper&& to this constructor. StringSumHelper is derived from String and has no data members other than those inherited from String. Even if it did have some extra data members, truncation would have to happen during move, and normally that is something you don't want. --- api/String.cpp | 48 ++++++++++++++++++------------------------------ api/String.h | 2 -- 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/api/String.cpp b/api/String.cpp index 1b6d4b28..6cf244ac 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -65,14 +65,13 @@ String::String(const __FlashStringHelper *pstr) #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) String::String(String &&rval) + : buffer(rval.buffer) + , capacity(rval.capacity) + , len(rval.len) { - init(); - move(rval); -} -String::String(StringSumHelper &&rval) -{ - init(); - move(rval); + rval.buffer = NULL; + rval.capacity = 0; + rval.len = 0; } #endif @@ -217,23 +216,18 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length) #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) void String::move(String &rhs) { - if (buffer) { - if (rhs && capacity >= rhs.len) { - memcpy(buffer, rhs.buffer, rhs.len); - len = rhs.len; - buffer[len] = '\0'; - rhs.len = 0; - return; - } else { - free(buffer); - } + if (this != &rhs) + { + free(buffer); + + buffer = rhs.buffer; + len = rhs.len; + capacity = rhs.capacity; + + rhs.buffer = NULL; + rhs.len = 0; + rhs.capacity = 0; } - buffer = rhs.buffer; - capacity = rhs.capacity; - len = rhs.len; - rhs.buffer = NULL; - rhs.capacity = 0; - rhs.len = 0; } #endif @@ -250,13 +244,7 @@ String & String::operator = (const String &rhs) #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) String & String::operator = (String &&rval) { - if (this != &rval) move(rval); - return *this; -} - -String & String::operator = (StringSumHelper &&rval) -{ - if (this != &rval) move(rval); + move(rval); return *this; } #endif diff --git a/api/String.h b/api/String.h index 3f34493b..c4d11bcc 100644 --- a/api/String.h +++ b/api/String.h @@ -74,7 +74,6 @@ class String String(const __FlashStringHelper *str); #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) String(String &&rval); - String(StringSumHelper &&rval); #endif explicit String(char c); explicit String(unsigned char, unsigned char base=10); @@ -101,7 +100,6 @@ class String String & operator = (const __FlashStringHelper *str); #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) String & operator = (String &&rval); - String & operator = (StringSumHelper &&rval); #endif // concatenate (works w/ built-in types) From 41599a304f19ddf52707a5062012651d250acd7b Mon Sep 17 00:00:00 2001 From: Ahti Legonkov Date: Tue, 20 Apr 2021 23:49:46 +0300 Subject: [PATCH 034/131] Add tests for String move --- test/CMakeLists.txt | 1 + test/src/String/test_move.cpp | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 test/src/String/test_move.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 65e15c47..f731b577 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -66,6 +66,7 @@ set(TEST_SRCS src/String/test_indexOf.cpp src/String/test_lastIndexOf.cpp src/String/test_length.cpp + src/String/test_move.cpp src/String/test_remove.cpp src/String/test_replace.cpp src/String/test_String.cpp diff --git a/test/src/String/test_move.cpp b/test/src/String/test_move.cpp new file mode 100644 index 00000000..d37630c4 --- /dev/null +++ b/test/src/String/test_move.cpp @@ -0,0 +1,37 @@ +#include + +#include + +#include "StringPrinter.h" + +#include + +TEST_CASE("Testing String move constructor", "[String-move-01]") +{ + arduino::String a("src"); + char const* const a_str = a.c_str(); + arduino::String b(std::move(a)); + REQUIRE(a.length() == 0); + REQUIRE(a.c_str() == nullptr); + REQUIRE(b.c_str() == a_str); + REQUIRE(b.length() == 3); +} + +TEST_CASE("Testing String move assignment", "[String-move-02]") +{ + arduino::String a("src"); + char const* const a_str = a.c_str(); + arduino::String b; + b = std::move(a); + REQUIRE(a.length() == 0); + REQUIRE(a.c_str() == nullptr); + REQUIRE(b == arduino::String("src")); + REQUIRE(b.c_str() == a_str); +} + +TEST_CASE("Testing String move self assignment", "[String-move-03]") +{ + arduino::String a("src"); + a = std::move(a); + REQUIRE(a == "src"); +} From a010db7849f4071ba8a149eca4e07518f4fe9b38 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sat, 15 May 2021 23:52:09 +0300 Subject: [PATCH 035/131] Bool as return value for logical operations --- api/String.cpp | 82 ++++++++++++++++++++++++------------------------ api/String.h | 84 +++++++++++++++++++++++++------------------------- 2 files changed, 83 insertions(+), 83 deletions(-) diff --git a/api/String.cpp b/api/String.cpp index 1b6d4b28..77b8240a 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -166,25 +166,25 @@ void String::invalidate(void) capacity = len = 0; } -unsigned char String::reserve(unsigned int size) +bool String::reserve(unsigned int size) { if (buffer && capacity >= size) return 1; if (changeBuffer(size)) { if (len == 0) buffer[0] = 0; - return 1; + return true; } - return 0; + return false; } -unsigned char String::changeBuffer(unsigned int maxStrLen) +bool String::changeBuffer(unsigned int maxStrLen) { char *newbuffer = (char *)realloc(buffer, maxStrLen + 1); if (newbuffer) { buffer = newbuffer; capacity = maxStrLen; - return 1; + return true; } - return 0; + return false; } /*********************************************/ @@ -281,93 +281,93 @@ String & String::operator = (const __FlashStringHelper *pstr) /* concat */ /*********************************************/ -unsigned char String::concat(const String &s) +bool String::concat(const String &s) { return concat(s.buffer, s.len); } -unsigned char String::concat(const char *cstr, unsigned int length) +bool String::concat(const char *cstr, unsigned int length) { unsigned int newlen = len + length; - if (!cstr) return 0; - if (length == 0) return 1; - if (!reserve(newlen)) return 0; + if (!cstr) return false; + if (length == 0) return true; + if (!reserve(newlen)) return false; memcpy(buffer + len, cstr, length); len = newlen; buffer[len] = '\0'; - return 1; + return true; } -unsigned char String::concat(const char *cstr) +bool String::concat(const char *cstr) { - if (!cstr) return 0; + if (!cstr) return false; return concat(cstr, strlen(cstr)); } -unsigned char String::concat(char c) +bool String::concat(char c) { return concat(&c, 1); } -unsigned char String::concat(unsigned char num) +bool String::concat(unsigned char num) { char buf[1 + 3 * sizeof(unsigned char)]; itoa(num, buf, 10); return concat(buf); } -unsigned char String::concat(int num) +bool String::concat(int num) { char buf[2 + 3 * sizeof(int)]; itoa(num, buf, 10); return concat(buf); } -unsigned char String::concat(unsigned int num) +bool String::concat(unsigned int num) { char buf[1 + 3 * sizeof(unsigned int)]; utoa(num, buf, 10); return concat(buf); } -unsigned char String::concat(long num) +bool String::concat(long num) { char buf[2 + 3 * sizeof(long)]; ltoa(num, buf, 10); return concat(buf); } -unsigned char String::concat(unsigned long num) +bool String::concat(unsigned long num) { char buf[1 + 3 * sizeof(unsigned long)]; ultoa(num, buf, 10); return concat(buf); } -unsigned char String::concat(float num) +bool String::concat(float num) { char buf[20]; char* string = dtostrf(num, 4, 2, buf); return concat(string); } -unsigned char String::concat(double num) +bool String::concat(double num) { char buf[20]; char* string = dtostrf(num, 4, 2, buf); return concat(string); } -unsigned char String::concat(const __FlashStringHelper * str) +bool String::concat(const __FlashStringHelper * str) { - if (!str) return 0; + if (!str) return false; int length = strlen_P((const char *) str); - if (length == 0) return 1; + if (length == 0) return true; unsigned int newlen = len + length; - if (!reserve(newlen)) return 0; + if (!reserve(newlen)) return false; strcpy_P(buffer + len, (const char *) str); len = newlen; - return 1; + return true; } /*********************************************/ @@ -475,46 +475,46 @@ int String::compareTo(const char *cstr) const return strcmp(buffer, cstr); } -unsigned char String::equals(const String &s2) const +bool String::equals(const String &s2) const { return (len == s2.len && compareTo(s2) == 0); } -unsigned char String::equals(const char *cstr) const +bool String::equals(const char *cstr) const { if (len == 0) return (cstr == NULL || *cstr == 0); if (cstr == NULL) return buffer[0] == 0; return strcmp(buffer, cstr) == 0; } -unsigned char String::equalsIgnoreCase( const String &s2 ) const +bool String::equalsIgnoreCase( const String &s2 ) const { - if (this == &s2) return 1; - if (len != s2.len) return 0; - if (len == 0) return 1; + if (this == &s2) return true; + if (len != s2.len) return false; + if (len == 0) return true; const char *p1 = buffer; const char *p2 = s2.buffer; while (*p1) { - if (tolower(*p1++) != tolower(*p2++)) return 0; + if (tolower(*p1++) != tolower(*p2++)) return false; } - return 1; + return true; } -unsigned char String::startsWith( const String &s2 ) const +bool String::startsWith( const String &s2 ) const { - if (len < s2.len) return 0; + if (len < s2.len) return false; return startsWith(s2, 0); } -unsigned char String::startsWith( const String &s2, unsigned int offset ) const +bool String::startsWith( const String &s2, unsigned int offset ) const { - if (offset > len - s2.len || !buffer || !s2.buffer) return 0; + if (offset > len - s2.len || !buffer || !s2.buffer) return false; return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0; } -unsigned char String::endsWith( const String &s2 ) const +bool String::endsWith( const String &s2 ) const { - if ( len < s2.len || !buffer || !s2.buffer) return 0; + if ( len < s2.len || !buffer || !s2.buffer) return false; return strcmp(&buffer[len - s2.len], s2.buffer) == 0; } diff --git a/api/String.h b/api/String.h index 3f34493b..9360c090 100644 --- a/api/String.h +++ b/api/String.h @@ -90,7 +90,7 @@ class String // return true on success, false on failure (in which case, the string // is left unchanged). reserve(0), if successful, will validate an // invalid string (i.e., "if (s)" will be true afterwards) - unsigned char reserve(unsigned int size); + bool reserve(unsigned int size); inline unsigned int length(void) const {return len;} // creates a copy of the assigned value. if the value is null or @@ -109,19 +109,19 @@ class String // returns true on success, false on failure (in which case, the string // is left unchanged). if the argument is null or invalid, the // concatenation is considered unsucessful. - unsigned char concat(const String &str); - unsigned char concat(const char *cstr); - unsigned char concat(const char *cstr, unsigned int length); - unsigned char concat(const uint8_t *cstr, unsigned int length) {return concat((const char*)cstr, length);} - unsigned char concat(char c); - unsigned char concat(unsigned char num); - unsigned char concat(int num); - unsigned char concat(unsigned int num); - unsigned char concat(long num); - unsigned char concat(unsigned long num); - unsigned char concat(float num); - unsigned char concat(double num); - unsigned char concat(const __FlashStringHelper * str); + bool concat(const String &str); + bool concat(const char *cstr); + bool concat(const char *cstr, unsigned int length); + bool concat(const uint8_t *cstr, unsigned int length) {return concat((const char*)cstr, length);} + bool concat(char c); + bool concat(unsigned char num); + bool concat(int num); + bool concat(unsigned int num); + bool concat(long num); + bool concat(unsigned long num); + bool concat(float num); + bool concat(double num); + bool concat(const __FlashStringHelper * str); // if there's not enough memory for the concatenated value, the string // will be left unchanged (but this isn't signalled in any way) @@ -153,33 +153,33 @@ class String operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } int compareTo(const String &s) const; int compareTo(const char *cstr) const; - unsigned char equals(const String &s) const; - unsigned char equals(const char *cstr) const; - - friend unsigned char operator == (const String &a, const String &b) { return a.equals(b); } - friend unsigned char operator == (const String &a, const char *b) { return a.equals(b); } - friend unsigned char operator == (const char *a, const String &b) { return b == a; } - friend unsigned char operator < (const String &a, const String &b) { return a.compareTo(b) < 0; } - friend unsigned char operator < (const String &a, const char *b) { return a.compareTo(b) < 0; } - friend unsigned char operator < (const char *a, const String &b) { return b.compareTo(a) > 0; } - - friend unsigned char operator != (const String &a, const String &b) { return !(a == b); } - friend unsigned char operator != (const String &a, const char *b) { return !(a == b); } - friend unsigned char operator != (const char *a, const String &b) { return !(a == b); } - friend unsigned char operator > (const String &a, const String &b) { return b < a; } - friend unsigned char operator > (const String &a, const char *b) { return b < a; } - friend unsigned char operator > (const char *a, const String &b) { return b < a; } - friend unsigned char operator <= (const String &a, const String &b) { return !(b < a); } - friend unsigned char operator <= (const String &a, const char *b) { return !(b < a); } - friend unsigned char operator <= (const char *a, const String &b) { return !(b < a); } - friend unsigned char operator >= (const String &a, const String &b) { return !(a < b); } - friend unsigned char operator >= (const String &a, const char *b) { return !(a < b); } - friend unsigned char operator >= (const char *a, const String &b) { return !(a < b); } - - unsigned char equalsIgnoreCase(const String &s) const; - unsigned char startsWith( const String &prefix) const; - unsigned char startsWith(const String &prefix, unsigned int offset) const; - unsigned char endsWith(const String &suffix) const; + bool equals(const String &s) const; + bool equals(const char *cstr) const; + + friend bool operator == (const String &a, const String &b) { return a.equals(b); } + friend bool operator == (const String &a, const char *b) { return a.equals(b); } + friend bool operator == (const char *a, const String &b) { return b == a; } + friend bool operator < (const String &a, const String &b) { return a.compareTo(b) < 0; } + friend bool operator < (const String &a, const char *b) { return a.compareTo(b) < 0; } + friend bool operator < (const char *a, const String &b) { return b.compareTo(a) > 0; } + + friend bool operator != (const String &a, const String &b) { return !(a == b); } + friend bool operator != (const String &a, const char *b) { return !(a == b); } + friend bool operator != (const char *a, const String &b) { return !(a == b); } + friend bool operator > (const String &a, const String &b) { return b < a; } + friend bool operator > (const String &a, const char *b) { return b < a; } + friend bool operator > (const char *a, const String &b) { return b < a; } + friend bool operator <= (const String &a, const String &b) { return !(b < a); } + friend bool operator <= (const String &a, const char *b) { return !(b < a); } + friend bool operator <= (const char *a, const String &b) { return !(b < a); } + friend bool operator >= (const String &a, const String &b) { return !(a < b); } + friend bool operator >= (const String &a, const char *b) { return !(a < b); } + friend bool operator >= (const char *a, const String &b) { return !(a < b); } + + bool equalsIgnoreCase(const String &s) const; + bool startsWith( const String &prefix) const; + bool startsWith(const String &prefix, unsigned int offset) const; + bool endsWith(const String &suffix) const; // character acccess char charAt(unsigned int index) const; @@ -228,7 +228,7 @@ class String protected: void init(void); void invalidate(void); - unsigned char changeBuffer(unsigned int maxStrLen); + bool changeBuffer(unsigned int maxStrLen); // copy and move String & copy(const char *cstr, unsigned int length); From a98cc4625db5e8f70393d1508de07b6672c91dba Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 28 May 2021 08:29:07 -0700 Subject: [PATCH 036/131] Fix minor misspellings in comments Codespell detected a few minor typos in the API headers. Fix their spelling. --- api/Common.h | 2 +- api/HardwareSPI.h | 2 +- api/IPAddress.h | 2 +- api/Print.h | 2 +- api/Stream.h | 2 +- api/String.h | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/api/Common.h b/api/Common.h index 6e12a74c..0bcc46bb 100644 --- a/api/Common.h +++ b/api/Common.h @@ -85,7 +85,7 @@ int atexit(void (*func)()) __attribute__((weak)); int main() __attribute__((weak)); #ifdef EXTENDED_PIN_MODE -// Platforms who wnat to declare more than 256 pins need to define EXTENDED_PIN_MODE globally +// Platforms who want to declare more than 256 pins need to define EXTENDED_PIN_MODE globally typedef uint32_t pin_size_t; #else typedef uint8_t pin_size_t; diff --git a/api/HardwareSPI.h b/api/HardwareSPI.h index d6609469..8b8affd0 100644 --- a/api/HardwareSPI.h +++ b/api/HardwareSPI.h @@ -124,7 +124,7 @@ class HardwareSPI virtual void end() = 0; }; -// Alias SPIClass to HardwareSPI since it's already the defacto standard for SPI classe name +// Alias SPIClass to HardwareSPI since it's already the defacto standard for SPI class name typedef HardwareSPI SPIClass; } diff --git a/api/IPAddress.h b/api/IPAddress.h index 8f6ca540..d70783ca 100644 --- a/api/IPAddress.h +++ b/api/IPAddress.h @@ -23,7 +23,7 @@ #include "Printable.h" #include "String.h" -// forward declartions of global name space friend classes +// forward declarations of global name space friend classes class EthernetClass; class DhcpClass; class DNSClient; diff --git a/api/Print.h b/api/Print.h index fc30b45b..5a19fe78 100644 --- a/api/Print.h +++ b/api/Print.h @@ -57,7 +57,7 @@ class Print } // default to zero, meaning "a single write may block" - // should be overriden by subclasses with buffering + // should be overridden by subclasses with buffering virtual int availableForWrite() { return 0; } size_t print(const __FlashStringHelper *); diff --git a/api/Stream.h b/api/Stream.h index 11aaa208..e81c71ba 100644 --- a/api/Stream.h +++ b/api/Stream.h @@ -24,7 +24,7 @@ #include #include "Print.h" -// compatability macros for testing +// compatibility macros for testing /* #define getInt() parseInt() #define getInt(ignore) parseInt(ignore) diff --git a/api/String.h b/api/String.h index 54232f83..03ecf442 100644 --- a/api/String.h +++ b/api/String.h @@ -106,7 +106,7 @@ class String // returns true on success, false on failure (in which case, the string // is left unchanged). if the argument is null or invalid, the - // concatenation is considered unsucessful. + // concatenation is considered unsuccessful. bool concat(const String &str); bool concat(const char *cstr); bool concat(const char *cstr, unsigned int length); @@ -179,7 +179,7 @@ class String bool startsWith(const String &prefix, unsigned int offset) const; bool endsWith(const String &suffix) const; - // character acccess + // character access char charAt(unsigned int index) const; void setCharAt(unsigned int index, char c); char operator [] (unsigned int index) const; From 8d54873a0a39a2060668d0ce52474b81d6341f51 Mon Sep 17 00:00:00 2001 From: per1234 Date: Sun, 30 May 2021 23:03:13 -0700 Subject: [PATCH 037/131] Add CI workflow to check for commonly misspelled words On every push, pull request, and periodically, use the codespell-project/actions-codespell action to check for commonly misspelled words. In the event of a false positive, the problematic word should be added, in all lowercase, to the ignore-words-list field of ./.codespellrc. Regardless of the case of the word in the false positive, it must be in all lowercase in the ignore list. The ignore list is comma-separated with no spaces. --- .codespellrc | 7 +++++++ .github/workflows/spell-check.yml | 22 ++++++++++++++++++++++ README.md | 1 + 3 files changed, 30 insertions(+) create mode 100644 .codespellrc create mode 100644 .github/workflows/spell-check.yml diff --git a/.codespellrc b/.codespellrc new file mode 100644 index 00000000..8d12eef4 --- /dev/null +++ b/.codespellrc @@ -0,0 +1,7 @@ +# See: https://github.com/codespell-project/codespell#using-a-config-file +[codespell] +# In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here: +ignore-words-list = hel +check-filenames = +check-hidden = +skip = ./.git,./test/external diff --git a/.github/workflows/spell-check.yml b/.github/workflows/spell-check.yml new file mode 100644 index 00000000..01bee879 --- /dev/null +++ b/.github/workflows/spell-check.yml @@ -0,0 +1,22 @@ +name: Spell Check + +# See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows +on: + push: + pull_request: + schedule: + # Run every Tuesday at 8 AM UTC to catch new misspelling detections resulting from dictionary updates. + - cron: "0 8 * * TUE" + workflow_dispatch: + repository_dispatch: + +jobs: + spellcheck: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Spell check + uses: codespell-project/actions-codespell@master diff --git a/README.md b/README.md index 9dcec8e6..dcfa340a 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ [![Unit Tests](https://github.com/arduino/ArduinoCore-API/workflows/Unit%20Tests/badge.svg)](https://github.com/arduino/ArduinoCore-API/actions?workflow=Unit+Tests) [![codecov](https://codecov.io/gh/arduino/ArduinoCore-API/branch/master/graph/badge.svg)](https://codecov.io/gh/arduino/ArduinoCore-API) +[![Spell Check status](https://github.com/arduino/ArduinoCore-API/actions/workflows/spell-check.yml/badge.svg)](https://github.com/arduino/ArduinoCore-API/actions/workflows/spell-check.yml) This repository hosts the hardware independent layer of Arduino core. From 14defce07cb0935ea12ce342140037d14df513f7 Mon Sep 17 00:00:00 2001 From: per1234 Date: Sun, 30 May 2021 23:40:39 -0700 Subject: [PATCH 038/131] Correct typos in comments and documentation --- .github/workflows/unit-tests.yml | 2 +- README.md | 4 ++-- api/IPAddress.cpp | 2 +- api/Udp.h | 4 ++-- api/deprecated-avr-comp/avr/interrupt.h | 2 +- api/deprecated/Client.h | 2 +- api/deprecated/HardwareSerial.h | 2 +- api/deprecated/IPAddress.h | 2 +- api/deprecated/Print.h | 2 +- api/deprecated/Printable.h | 2 +- api/deprecated/Server.h | 2 +- api/deprecated/Stream.h | 2 +- api/deprecated/Udp.h | 2 +- api/deprecated/WString.h | 2 +- test/src/Stream/test_readBytes.cpp | 4 ++-- test/src/String/test_replace.cpp | 4 ++-- test/src/String/test_trim.cpp | 2 +- 17 files changed, 21 insertions(+), 21 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 490c28e5..f72d58b1 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -2,7 +2,7 @@ name: Unit Tests on: pull_request: - # Only run workflow if a file in these paths are modified + # Only run workflow if a file in these paths is modified paths: - ".github/workflows/unit-tests.yml" - "test/**" diff --git a/README.md b/README.md index dcfa340a..6dc97369 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ This repository hosts the hardware independent layer of Arduino core. All Arduino official cores are being ported to the new structure so they take advantage of this single repo. Including this repo in your existing Arduino core will allow the language to grow and include new features. -For backwards compatibility, every revision of this repo will increase `ARDUINO_API_VERSION` define. +For backwards compatibility, every revision of this repo will increase the `ARDUINO_API_VERSION` define. Some cores have been ported to the new structure, for example: * megaAVR (https://github.com/arduino/ArduinoCore-megaAVR) @@ -21,7 +21,7 @@ These repositories **don't** contain the needed `api` subfolder; to "complete" t ### Porting tips -In the future, core apis will be updated independently from the core, so all the compatible cores will seamlessly adopt new features. +In the future, core APIs will be updated independently from the core, so all the compatible cores will seamlessly adopt new features. This requires support from all the IDEs, so in the meantime we suggest to release the core by copying a snapshot of this `api` folder. The most elegant and effective solution is to develop the core with `api` symlinked and produce the distributable archive by telling `tar` to follow symlinks. diff --git a/api/IPAddress.cpp b/api/IPAddress.cpp index 5a757545..5cf62d5e 100644 --- a/api/IPAddress.cpp +++ b/api/IPAddress.cpp @@ -66,7 +66,7 @@ bool IPAddress::fromString(const char *address) else if (c == '.') { if (dots == 3) { - // Too much dots (there must be 3 dots) + // Too many dots (there must be 3 dots) return false; } if (acc < 0) { diff --git a/api/Udp.h b/api/Udp.h index 4cfe2ac5..20117a67 100644 --- a/api/Udp.h +++ b/api/Udp.h @@ -48,10 +48,10 @@ class UDP : public Stream { // Sending UDP packets - // Start building up a packet to send to the remote host specific in ip and port + // Start building up a packet to send to the remote host specified in ip and port // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port virtual int beginPacket(IPAddress ip, uint16_t port) =0; - // Start building up a packet to send to the remote host specific in host and port + // Start building up a packet to send to the remote host specified in host and port // Returns 1 if successful, 0 if there was a problem resolving the hostname or port virtual int beginPacket(const char *host, uint16_t port) =0; // Finish off this packet and send it diff --git a/api/deprecated-avr-comp/avr/interrupt.h b/api/deprecated-avr-comp/avr/interrupt.h index 950509dd..a2873cf9 100644 --- a/api/deprecated-avr-comp/avr/interrupt.h +++ b/api/deprecated-avr-comp/avr/interrupt.h @@ -19,5 +19,5 @@ /* Empty file. This file is here to allow compatibility with sketches (made for AVR) - that includes + that include */ diff --git a/api/deprecated/Client.h b/api/deprecated/Client.h index 5f8d4be9..84981c71 100644 --- a/api/deprecated/Client.h +++ b/api/deprecated/Client.h @@ -18,7 +18,7 @@ // including Client.h is deprecated, for all future projects use Arduino.h instead -// This include is added for compatibility, it will be remove on the next +// This include is added for compatibility, it will be removed on the next // major release of the API #include "../Client.h" diff --git a/api/deprecated/HardwareSerial.h b/api/deprecated/HardwareSerial.h index 19d77c78..0ed7d7a6 100644 --- a/api/deprecated/HardwareSerial.h +++ b/api/deprecated/HardwareSerial.h @@ -18,7 +18,7 @@ // including HardwareSerial.h is deprecated, for all future projects use Arduino.h instead -// This include is added for compatibility, it will be remove on the next +// This include is added for compatibility, it will be removed on the next // major release of the API #include "../HardwareSerial.h" diff --git a/api/deprecated/IPAddress.h b/api/deprecated/IPAddress.h index bf7fb7f0..4c03574d 100644 --- a/api/deprecated/IPAddress.h +++ b/api/deprecated/IPAddress.h @@ -18,7 +18,7 @@ // including IPAddress.h is deprecated, for all future projects use Arduino.h instead -// This include is added for compatibility, it will be remove on the next +// This include is added for compatibility, it will be removed on the next // major release of the API #include "../IPAddress.h" diff --git a/api/deprecated/Print.h b/api/deprecated/Print.h index 2ac088d8..a0d1bd14 100644 --- a/api/deprecated/Print.h +++ b/api/deprecated/Print.h @@ -18,7 +18,7 @@ // including Print.h is deprecated, for all future projects use Arduino.h instead -// This include is added for compatibility, it will be remove on the next +// This include is added for compatibility, it will be removed on the next // major release of the API #include "../Print.h" diff --git a/api/deprecated/Printable.h b/api/deprecated/Printable.h index bd721264..b6888c11 100644 --- a/api/deprecated/Printable.h +++ b/api/deprecated/Printable.h @@ -18,7 +18,7 @@ // including Printable.h is deprecated, for all future projects use Arduino.h instead -// This include is added for compatibility, it will be remove on the next +// This include is added for compatibility, it will be removed on the next // major release of the API #include "../Printable.h" diff --git a/api/deprecated/Server.h b/api/deprecated/Server.h index f3b02c16..65b544bb 100644 --- a/api/deprecated/Server.h +++ b/api/deprecated/Server.h @@ -18,7 +18,7 @@ // including Server.h is deprecated, for all future projects use Arduino.h instead -// This include is added for compatibility, it will be remove on the next +// This include is added for compatibility, it will be removed on the next // major release of the API #include "../Server.h" diff --git a/api/deprecated/Stream.h b/api/deprecated/Stream.h index aa5dc8e0..4118ccb7 100644 --- a/api/deprecated/Stream.h +++ b/api/deprecated/Stream.h @@ -18,7 +18,7 @@ // including Stream.h is deprecated, for all future projects use Arduino.h instead -// This include is added for compatibility, it will be remove on the next +// This include is added for compatibility, it will be removed on the next // major release of the API #include "../Stream.h" diff --git a/api/deprecated/Udp.h b/api/deprecated/Udp.h index 56980603..f03a64b4 100644 --- a/api/deprecated/Udp.h +++ b/api/deprecated/Udp.h @@ -18,7 +18,7 @@ // including Udp.h is deprecated, for all future projects use Arduino.h instead -// This include is added for compatibility, it will be remove on the next +// This include is added for compatibility, it will be removed on the next // major release of the API #include "../Udp.h" diff --git a/api/deprecated/WString.h b/api/deprecated/WString.h index 072323e0..1a3a79fc 100644 --- a/api/deprecated/WString.h +++ b/api/deprecated/WString.h @@ -18,7 +18,7 @@ // including WString.h is deprecated, for all future projects use Arduino.h instead -// This include is added for compatibility, it will be remove on the next +// This include is added for compatibility, it will be removed on the next // major release of the API #include "../String.h" diff --git a/test/src/Stream/test_readBytes.cpp b/test/src/Stream/test_readBytes.cpp index 9ffdfe1d..65466cc8 100644 --- a/test/src/Stream/test_readBytes.cpp +++ b/test/src/Stream/test_readBytes.cpp @@ -25,7 +25,7 @@ TEST_CASE ("Testing readBytes(char *buffer, size_t length)", "[Stream-readBytes- REQUIRE(mock.readBytes(buf, sizeof(buf)) == 0); } - WHEN ("the stream contains less data then we want to read") + WHEN ("the stream contains less data than we want to read") { char buf[32] = {0}; char const str[] = "some stream content"; @@ -36,7 +36,7 @@ TEST_CASE ("Testing readBytes(char *buffer, size_t length)", "[Stream-readBytes- REQUIRE(mock.readString() == arduino::String("")); } - WHEN ("the stream contains more data then we want to read") + WHEN ("the stream contains more data than we want to read") { char buf[5] = {0}; mock << "some stream content"; diff --git a/test/src/String/test_replace.cpp b/test/src/String/test_replace.cpp index 62fd5e8f..5d46fafb 100644 --- a/test/src/String/test_replace.cpp +++ b/test/src/String/test_replace.cpp @@ -39,14 +39,14 @@ TEST_CASE ("Testing String::replace(char, char) when string contains elements = REQUIRE(str == "H3ll0 Ardu1n0!"); } -TEST_CASE ("Testing String::replace(String, String) when string does not constain subtr 'find'", "[String-replace-04]") +TEST_CASE ("Testing String::replace(String, String) when string does not contain substr 'find'", "[String-replace-04]") { arduino::String str("Hello Arduino!"); str.replace(arduino::String("Zulu"), arduino::String("11")); REQUIRE(str == "Hello Arduino!"); } -TEST_CASE ("Testing String::replace(String, String) when string constains subtr 'find'", "[String-replace-05]") +TEST_CASE ("Testing String::replace(String, String) when string contains substr 'find'", "[String-replace-05]") { arduino::String str("Hello Arduino!"); str.replace(arduino::String("ll"), arduino::String("11")); diff --git a/test/src/String/test_trim.cpp b/test/src/String/test_trim.cpp index 0328e3fc..539d6568 100644 --- a/test/src/String/test_trim.cpp +++ b/test/src/String/test_trim.cpp @@ -30,7 +30,7 @@ TEST_CASE ("Testing String::trim with space at the end", "[String-trim-02]") REQUIRE(str == "hello"); } -TEST_CASE ("Testing String::trim with space at both beginng and end", "[String-trim-03]") +TEST_CASE ("Testing String::trim with space at both beginning and end", "[String-trim-03]") { arduino::String str(" hello "); str.trim(); From 5e2880e27cd35f215f7d0feb08952ca608582b5c Mon Sep 17 00:00:00 2001 From: Paul Melnikov Date: Wed, 25 Aug 2021 16:39:07 +0700 Subject: [PATCH 039/131] Fix bitWrite with parenthesis Current version does not wrap arguments in "()", so works incorrectly when used like this: ``` bitWrite(var, bit, cond?1:0); ``` --- api/Common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/Common.h b/api/Common.h index 0bcc46bb..1fd19300 100644 --- a/api/Common.h +++ b/api/Common.h @@ -65,7 +65,7 @@ typedef void (*voidFuncPtrParam)(void*); #define bitSet(value, bit) ((value) |= (1UL << (bit))) #define bitClear(value, bit) ((value) &= ~(1UL << (bit))) #define bitToggle(value, bit) ((value) ^= (1UL << (bit))) -#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit)) +#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit))) #ifndef bit #define bit(b) (1UL << (b)) From df98331bf41cd3b47ae78a156449cac18cca67f7 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Wed, 22 Sep 2021 15:36:41 -0700 Subject: [PATCH 040/131] Add a new 'OUTPUT_OPENDRAIN' pinmode for platforms. Not every MCU offers open drain pins, but some do. This change means that they'll no longer have to play games to implement an API-compatible pinMode() function that offers open drain. Fixes #155 If you're reading this commit message because you need to add another pin mode to this enum, the hacky trivial workaround is to do something like this in your core: ``` // This typedef is used to extend the PinMode typedef enum // in the ArduinoAPI, since they don't have contants typedef enum { INPUT_ANALOG = 99 , // We assume that the Arduino core will never have 99 PinModes OUTPUT_OPEN_DRAIN // It'd be cleaner to be able to count the size of that enum } PinModeExtension; ``` Because PinMode is an enum and not a proper type, the compiler will let the user pass either a PinMode or a PinModeExtension to your pinMode() function, even though the signature is `void pinMode(pin_size_t ulPin, PinMode ulMode)` --- api/Common.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/api/Common.h b/api/Common.h index 1fd19300..c40a35a8 100644 --- a/api/Common.h +++ b/api/Common.h @@ -16,10 +16,11 @@ typedef enum { } PinStatus; typedef enum { - INPUT = 0x0, - OUTPUT = 0x1, - INPUT_PULLUP = 0x2, - INPUT_PULLDOWN = 0x3, + INPUT = 0x0, + OUTPUT = 0x1, + INPUT_PULLUP = 0x2, + INPUT_PULLDOWN = 0x3, + OUTPUT_OPENDRAIN = 0x4, } PinMode; typedef enum { From 3a6537ee13375326c9485ff51a4d8351bb41c3ba Mon Sep 17 00:00:00 2001 From: Gavin Uberti Date: Wed, 8 Jun 2022 16:23:10 -0400 Subject: [PATCH 041/131] Import stdbool before typedef --- api/Common.h | 1 + 1 file changed, 1 insertion(+) diff --git a/api/Common.h b/api/Common.h index c40a35a8..44fe95e8 100644 --- a/api/Common.h +++ b/api/Common.h @@ -1,5 +1,6 @@ #pragma once #include +#include #ifdef __cplusplus extern "C"{ From 2506d49bfd55a9c68469c75b0c6e74d36b78873c Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 7 Jul 2022 07:57:09 +0200 Subject: [PATCH 042/131] Fix: Providing documentation for various cores how to manually integrate ArduinoCore-API --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 6dc97369..c366d5a2 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,8 @@ Example command: ``` tar --exclude='*.git*' -cjhvf $yourcore-$version.tar.bz2 $yourcore/ ``` + +Documentation how integrate with a Arduino core (which is necessary if you do not download the Arduino core via the board manager) can be found here: +* [ArduinoCore-megaavr](https://github.com/arduino/ArduinoCore-megaavr#developing) +* [ArduinoCore-mbed](https://github.com/arduino/ArduinoCore-mbed#clone-the-repository-in-sketchbookhardwarearduino-git) +* [ArduinoCore-samd](https://github.com/arduino/ArduinoCore-samd/#developing) From f87008ac24b98b067a17c02b6c1f853c1b739cd1 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 7 Jul 2022 08:12:31 +0200 Subject: [PATCH 043/131] Fix: ArduinoCore-samd:master has already been ported to using ArduinoCore-API (#164) The link in the README was stil pointing to a development branch. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6dc97369..34cac95d 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Some cores have been ported to the new structure, for example: * megaAVR (https://github.com/arduino/ArduinoCore-megaAVR) * nRF52-mbedos (https://github.com/arduino/ArduinoCore-nRF528x-mbedos) * classic AVR (https://github.com/arduino/ArduinoCore-avr/tree/api) -* SAMD (https://github.com/arduino/ArduinoCore-samd/tree/api) +* SAMD (https://github.com/arduino/ArduinoCore-samd) These repositories **don't** contain the needed `api` subfolder; to "complete" the core you need to copy or symlink the `api` folder from this repo to the target's `cores/arduino` folder. From ee040ed7eec2dd08455585858ed58c39a750cc9d Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 8 Jul 2022 07:06:49 +0200 Subject: [PATCH 044/131] Update README.md (#166) --- README.md | 61 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 34cac95d..c4a02de0 100644 --- a/README.md +++ b/README.md @@ -4,28 +4,59 @@ [![codecov](https://codecov.io/gh/arduino/ArduinoCore-API/branch/master/graph/badge.svg)](https://codecov.io/gh/arduino/ArduinoCore-API) [![Spell Check status](https://github.com/arduino/ArduinoCore-API/actions/workflows/spell-check.yml/badge.svg)](https://github.com/arduino/ArduinoCore-API/actions/workflows/spell-check.yml) -This repository hosts the hardware independent layer of Arduino core. +This repository hosts the hardware independent layer of Arduino core. In other words it contains the abstract definition of the Arduino core API, consisting of hardware-independent header files that are then included and implemented by the various platform-specific cores. -All Arduino official cores are being ported to the new structure so they take advantage of this single repo. +Having a single place where the Arduino API is defined means that there is no longer a String implementation within every Arduino core (a String module within ArduinoCore-avr, a String module within ArduinoCore-samd, a String module within ArduinoCore-megaavr …) but rather one String implementation within ArduinoCore-API which all other cores utilise. This has the pleasant side effects that bugs fixed or features added within the ArduinoCore-API String implementation are automatically propagated to all cores utilizing ArduinoCore-API. -Including this repo in your existing Arduino core will allow the language to grow and include new features. -For backwards compatibility, every revision of this repo will increase the `ARDUINO_API_VERSION` define. +As of now, the following official cores are utilising ArduinoCore-API: -Some cores have been ported to the new structure, for example: -* megaAVR (https://github.com/arduino/ArduinoCore-megaAVR) -* nRF52-mbedos (https://github.com/arduino/ArduinoCore-nRF528x-mbedos) -* classic AVR (https://github.com/arduino/ArduinoCore-avr/tree/api) -* SAMD (https://github.com/arduino/ArduinoCore-samd) +* [megaavr](https://github.com/arduino/ArduinoCore-megaAVR) +* [mbed](https://github.com/arduino/ArduinoCore-mbed) +* [samd](https://github.com/arduino/ArduinoCore-samd) -These repositories **don't** contain the needed `api` subfolder; to "complete" the core you need to copy or symlink the `api` folder from this repo to the target's `cores/arduino` folder. +There's an ongoing effort to port the others, while maintainers of third-party cores are strongly invited to follow the same route in order to stay up-to-date with the new language features. For backwards compatibility, every revision of this repo will increase the `ARDUINO_API_VERSION` define. -### Porting tips +## Documentation -In the future, core APIs will be updated independently from the core, so all the compatible cores will seamlessly adopt new features. -This requires support from all the IDEs, so in the meantime we suggest to release the core by copying a snapshot of this `api` folder. +The Arduino API is documented in the official [language reference](https://www.arduino.cc/reference/en/), whose sources are located in [this repository](https://github.com/arduino/reference-en) and are open to contributions from the community. + +## Support + +This repository is not directly usable by final users. If you need assistance with Arduino, see the [Help Center](https://support.arduino.cc/) and browse the [forum](https://forum.arduino.cc). + +## Development + +### Bugs & Issues + +If you want to report an issue with this core, you can submit it to the [issue tracker](https://github.com/arduino/ArduinoCore-API/issues) of this repository. Some rules apply: + +* If your issue is about a specific hardware platform, report it to its repository. This one is only about discussing the generic API. +* Before posting, please check if the same problem has been already reported by someone else to avoid duplicates. +* Remember to include as much detail as you can about your hardware set-up, code and steps for reproducing the issue. Make sure you're using an original Arduino board. + +### Contributions + +Contributions are always welcome! You can submit them directly to this repository as Pull Requests. Please provide a detailed description of the problem you're trying to solve. We also appreciate any help in testing issues and patches contributed by other users. + +### Unit testing + +This repository includes a test suite that covers most of the API and that is designed to run on generic hardware, thus not requiring a development board. We call this _host-based unit-testing_. In order to test the features that are only defined but not implemented in this repository, mock implementations are included. + +Please help us improve the coverage of the test suite! + +### Implementing ArduinoCore-API + +In order to compile a core which is implementing ArduinoCore-API you'll need to copy/symlink the `api` directory to the target's `cores/arduino` directory as part of your development and release workflow. The most elegant and effective solution is to develop your core with `api` symlinked and produce the distributable archive by telling `tar` to follow symlinks. Example: -The most elegant and effective solution is to develop the core with `api` symlinked and produce the distributable archive by telling `tar` to follow symlinks. -Example command: ``` tar --exclude='*.git*' -cjhvf $yourcore-$version.tar.bz2 $yourcore/ ``` + +## Donations + +This open source code is maintained by Arduino with the help of the community. We invest a considerable amount of time in testing code, optimizing it and introducing new features. Please consider [donating](https://www.arduino.cc/en/donate/) or [sponsoring](https://github.com/sponsors/arduino) to support our work, as well as [buying original Arduino boards](https://store.arduino.cc) which is the best way to make sure our effort can continue in the long term. + +## License and credits + +This code is licensed under the terms of the GNU LGPL 2.1. If you have questions about licensing please contact us at [license@arduino.cc](mailto:license@arduino.cc). + From d44bc9c6c5891c93cb3ee9a475913364d4f41379 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 8 Aug 2022 09:46:03 +0200 Subject: [PATCH 045/131] Update README.md Co-authored-by: per1234 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c366d5a2..b5e5c037 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Example command: tar --exclude='*.git*' -cjhvf $yourcore-$version.tar.bz2 $yourcore/ ``` -Documentation how integrate with a Arduino core (which is necessary if you do not download the Arduino core via the board manager) can be found here: +Documentation for how to integrate with a Arduino core (which is necessary if you do not download the Arduino core via the Boards Manager) can be found here: * [ArduinoCore-megaavr](https://github.com/arduino/ArduinoCore-megaavr#developing) * [ArduinoCore-mbed](https://github.com/arduino/ArduinoCore-mbed#clone-the-repository-in-sketchbookhardwarearduino-git) * [ArduinoCore-samd](https://github.com/arduino/ArduinoCore-samd/#developing) From 71773a562c4f6d733b4d767e3cb6ab1b35f519e2 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Sat, 13 Aug 2022 20:32:50 +1000 Subject: [PATCH 046/131] Add IPv6 support; all backwards compatible (IPv4) tests pass --- api/IPAddress.cpp | 203 ++++++++++++++++++++++++++++++++++++++++++---- api/IPAddress.h | 53 +++++++++--- 2 files changed, 226 insertions(+), 30 deletions(-) diff --git a/api/IPAddress.cpp b/api/IPAddress.cpp index 5cf62d5e..2ed66688 100644 --- a/api/IPAddress.cpp +++ b/api/IPAddress.cpp @@ -22,30 +22,61 @@ using namespace arduino; -IPAddress::IPAddress() +IPAddress::IPAddress() : IPAddress(IPv4) {} + +IPAddress::IPAddress(IPType ip_type) { - _address.dword = 0; + _type = ip_type; + memset(_address.bytes, 0, sizeof(_address.bytes)); } IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) { - _address.bytes[0] = first_octet; - _address.bytes[1] = second_octet; - _address.bytes[2] = third_octet; - _address.bytes[3] = fourth_octet; + _type = IPv4; + memset(_address.bytes, 0, sizeof(_address.bytes) - sizeof(uint32_t)); + _address.bytes[12] = first_octet; + _address.bytes[13] = second_octet; + _address.bytes[14] = third_octet; + _address.bytes[15] = fourth_octet; } IPAddress::IPAddress(uint32_t address) { - _address.dword = address; + // IPv4 only + _type = IPv4; + memset(_address.bytes, 0, sizeof(_address.bytes) - sizeof(uint32_t)); + _address.dword[3] = address; + + // NOTE on conversion/comparison and uint32_t: + // These conversions are host platform dependent. + // There is a defined integer representation of IPv4 addresses, + // based on network byte order (will be the value on big endian systems), + // e.g. http://2398766798 is the same as http://142.250.70.206, + // However on little endian systems the octets 0x83, 0xFA, 0x46, 0xCE, + // in that order, will form the integer (uint32_t) 3460758158 . } -IPAddress::IPAddress(const uint8_t *address) +IPAddress::IPAddress(const uint8_t *address) : IPAddress(IPv4, address) {} + +IPAddress::IPAddress(IPType ip_type, const uint8_t *address) { - memcpy(_address.bytes, address, sizeof(_address.bytes)); + _type = ip_type; + if (ip_type == IPv4) { + memset(_address.bytes, 0, sizeof(_address.bytes) - sizeof(uint32_t)); + memcpy(&_address.bytes[12], address, sizeof(uint32_t)); + } else { + memcpy(_address.bytes, address, sizeof(_address.bytes)); + } +} + +bool IPAddress::fromString(const char *address) { + if (!fromString4(address)) { + return fromString6(address); + } + return true; } -bool IPAddress::fromString(const char *address) +bool IPAddress::fromString4(const char *address) { // TODO: add support for "a", "a.b", "a.b.c" formats @@ -73,7 +104,7 @@ bool IPAddress::fromString(const char *address) /* No value between dots, e.g. '1..' */ return false; } - _address.bytes[dots++] = acc; + _address.bytes[12 + dots++] = acc; acc = -1; } else @@ -91,37 +122,175 @@ bool IPAddress::fromString(const char *address) /* No value between dots, e.g. '1..' */ return false; } - _address.bytes[3] = acc; + memset(_address.bytes, 0, sizeof(_address.bytes) - sizeof(uint32_t)); + _address.bytes[15] = acc; + _type = IPv4; + return true; +} + +bool IPAddress::fromString6(const char *address) { + uint32_t acc = 0; // Accumulator + int dots = 0, doubledots = -1; + + while (*address) + { + char c = tolower(*address++); + if (isalnum(c)) { + if (c >= 'a') + c -= 'a' - '0' - 10; + acc = acc * 16 + (c - '0'); + if (acc > 0xffff) + // Value out of range + return false; + } + else if (c == ':') { + if (*address == ':') { + if (doubledots >= 0) + // :: allowed once + return false; + // remember location + doubledots = dots + !!acc; + address++; + } + if (dots == 7) + // too many separators + return false; + _address.bytes[dots] = acc >> 2; + _address.bytes[dots + 1] = acc & 0xff; + dots++; + acc = 0; + } + else + // Invalid char + return false; + } + + if (doubledots == -1 && dots != 7) + // Too few separators + return false; + _address.bytes[dots] = acc >> 2; + _address.bytes[dots + 1] = acc & 0xff; + dots++; + + if (doubledots != -1) { + for (int i = dots * 2 - doubledots * 2 - 1; i >= 0; i--) + _address.bytes[16 - dots * 2 + doubledots * 2 + i] = _address.bytes[doubledots * 2 + i]; + for (int i = doubledots * 2; i < 16 - dots * 2 + doubledots * 2; i++) + _address.bytes[i] = 0; + } + + _type = IPv6; return true; } IPAddress& IPAddress::operator=(const uint8_t *address) { - memcpy(_address.bytes, address, sizeof(_address.bytes)); + // IPv4 only conversion from byte pointer + _type = IPv4; + memset(_address.bytes, 0, sizeof(_address.bytes) - sizeof(uint32_t)); + memcpy(&_address.bytes[12], address, sizeof(uint32_t)); return *this; } IPAddress& IPAddress::operator=(uint32_t address) { - _address.dword = address; + // IPv4 conversion + // See note on conversion/comparison and uint32_t + _type = IPv4; + _address.dword[0] = 0; + _address.dword[1] = 0; + _address.dword[2] = 0; + _address.dword[3] = address; return *this; } +bool IPAddress::operator==(const IPAddress& addr) const { + return (addr._type == _type) + && (memcmp(addr._address.bytes, _address.bytes, sizeof(_address.bytes)) == 0); +}; + bool IPAddress::operator==(const uint8_t* addr) const { - return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; + // IPv4 only comparison to byte pointer + // Can't support IPv6 as we know our type, but not the length of the pointer + return _type == IPv4 && memcmp(addr, &_address.bytes[12], sizeof(uint32_t)) == 0; } +uint8_t IPAddress::operator[](int index) const { + if (_type == IPv4) { + return _address.bytes[index + 12]; + } + return _address.bytes[index]; +}; + +uint8_t& IPAddress::operator[](int index) { + if (_type == IPv4) { + return _address.bytes[index + 12]; + } + return _address.bytes[index]; +}; + size_t IPAddress::printTo(Print& p) const { size_t n = 0; + + if (_type == IPv6) { + // IPv6 IETF canonical format: left-most longest run of all zero fields, lower case + int8_t longest_start = -1; + int8_t longest_length = 0; + int8_t current_start = -1; + int8_t current_length = 0; + for (int8_t f = 0; f < 8; f++) { + if (_address.bytes[f * 2] == 0 && _address.bytes[f * 2 + 1] == 0) { + if (current_start == -1) { + current_start = f; + current_length = 0; + } else { + current_length++; + } + if (current_length > longest_length) { + longest_start = current_start; + longest_length = current_length; + } + } else { + current_start = -1; + } + } + for (int f = 0; f < 8; f++) { + if (f < longest_start || f >= longest_start + longest_length) { + uint8_t c1 = _address.bytes[f * 2] >> 1; + uint8_t c2 = _address.bytes[f * 2] & 0xf; + uint8_t c3 = _address.bytes[f * 2 + 1] >> 1; + uint8_t c4 = _address.bytes[f * 2 + 1] & 0xf; + if (c1 > 0) { + n += p.print(c1 < 10 ? '0' + c1 : 'a' + c1 - 10); + } + if (c1 > 0 || c2 > 0) { + n += p.print(c2 < 10 ? '0' + c2 : 'a' + c2 - 10); + } + if (c1 > 0 || c2 > 0 || c3 > 0) { + n += p.print(c3 < 10 ? '0' + c3 : 'a' + c3 - 10); + } + n += p.print(c4 < 10 ? '0' + c4 : 'a' + c4 - 10); + if (f < 7) { + n += p.print(':'); + } + } else if (f == longest_start) { + n += p.print(':'); + } + } + return n; + } + + // IPv4 for (int i =0; i < 3; i++) { - n += p.print(_address.bytes[i], DEC); + n += p.print(_address.bytes[12 + i], DEC); n += p.print('.'); } - n += p.print(_address.bytes[3], DEC); + n += p.print(_address.bytes[15], DEC); return n; } +const IPAddress arduino::IN6ADDR_ANY(IPv6); const IPAddress arduino::INADDR_NONE(0,0,0,0); diff --git a/api/IPAddress.h b/api/IPAddress.h index d70783ca..c7c73f2d 100644 --- a/api/IPAddress.h +++ b/api/IPAddress.h @@ -32,46 +32,68 @@ namespace arduino { // A class to make it easier to handle and pass around IP addresses +enum IPType { + IPv4, + IPv6 +}; + class IPAddress : public Printable { private: union { - uint8_t bytes[4]; // IPv4 address - uint32_t dword; + uint8_t bytes[16]; + uint32_t dword[4]; } _address; + IPType _type; // Access the raw byte array containing the address. Because this returns a pointer // to the internal structure rather than a copy of the address this function should only // be used when you know that the usage of the returned uint8_t* will be transient and not // stored. - uint8_t* raw_address() { return _address.bytes; }; + // IPv4 only (for friends) + uint8_t* raw_address() { + if (_type == IPv4) { + return &_address.bytes[12]; + } + return nullptr; + }; + uint8_t* raw_bytes() { return _address.bytes; } public: // Constructors - IPAddress(); + IPAddress(); // IPv4 + IPAddress(IPType ip_type); IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); - IPAddress(uint32_t address); - IPAddress(const uint8_t *address); + IPAddress(uint32_t address); // IPv4 only; see implementation note + IPAddress(const uint8_t *address); // IPv4 + IPAddress(IPType ip_type, const uint8_t *address); bool fromString(const char *address); bool fromString(const String &address) { return fromString(address.c_str()); } - // Overloaded cast operator to allow IPAddress objects to be used where a pointer - // to a four-byte uint8_t array is expected - operator uint32_t() const { return _address.dword; }; - bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; }; - bool operator!=(const IPAddress& addr) const { return _address.dword != addr._address.dword; }; + // Overloaded cast operator to allow IPAddress objects to be used where a uint32_t is expected + // IPv4 only; see implementation note + operator uint32_t() const { return _type == IPv4 ? _address.dword[3] : 0; }; + + bool operator==(const IPAddress& addr) const; + bool operator!=(const IPAddress& addr) const { return !(*this == addr); }; + + // IPv4 only; we don't know the length of the pointer bool operator==(const uint8_t* addr) const; // Overloaded index operator to allow getting and setting individual octets of the address - uint8_t operator[](int index) const { return _address.bytes[index]; }; - uint8_t& operator[](int index) { return _address.bytes[index]; }; + uint8_t operator[](int index) const; + uint8_t& operator[](int index); // Overloaded copy operators to allow initialisation of IPAddress objects from other types + // IPv4 only IPAddress& operator=(const uint8_t *address); + // IPv4 only; see implementation note IPAddress& operator=(uint32_t address); virtual size_t printTo(Print& p) const; + IPType type() { return _type; } + friend class UDP; friend class Client; friend class Server; @@ -79,8 +101,13 @@ class IPAddress : public Printable { friend ::EthernetClass; friend ::DhcpClass; friend ::DNSClient; + +protected: + bool fromString4(const char *address); + bool fromString6(const char *address); }; +extern const IPAddress IN6ADDR_ANY; extern const IPAddress INADDR_NONE; } From d7d6fe72aa9830919344b319df3dd129d22712f5 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 00:09:52 +1000 Subject: [PATCH 047/131] Add 16 octet constructor --- api/IPAddress.h | 1 + 1 file changed, 1 insertion(+) diff --git a/api/IPAddress.h b/api/IPAddress.h index c7c73f2d..3bffafaf 100644 --- a/api/IPAddress.h +++ b/api/IPAddress.h @@ -63,6 +63,7 @@ class IPAddress : public Printable { IPAddress(); // IPv4 IPAddress(IPType ip_type); IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); + IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5, uint8_t o6, uint8_t o7, uint8_t o8, uint8_t o9, uint8_t o10, uint8_t o11, uint8_t o12, uint8_t o13, uint8_t o14, uint8_t o15, uint8_t o16); IPAddress(uint32_t address); // IPv4 only; see implementation note IPAddress(const uint8_t *address); // IPv4 IPAddress(IPType ip_type, const uint8_t *address); From 5706879dd16064331ab4bbd560b750ed45fbcb67 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 00:10:15 +1000 Subject: [PATCH 048/131] Add IPv6 tests, and fix issues --- api/IPAddress.cpp | 68 +++- test/CMakeLists.txt | 3 + test/src/IPAddress/test_IPAddress6.cpp | 85 +++++ test/src/IPAddress/test_fromString6.cpp | 395 ++++++++++++++++++++++++ test/src/IPAddress/test_printTo6.cpp | 137 ++++++++ 5 files changed, 672 insertions(+), 16 deletions(-) create mode 100644 test/src/IPAddress/test_IPAddress6.cpp create mode 100644 test/src/IPAddress/test_fromString6.cpp create mode 100644 test/src/IPAddress/test_printTo6.cpp diff --git a/api/IPAddress.cpp b/api/IPAddress.cpp index 2ed66688..ab14f85a 100644 --- a/api/IPAddress.cpp +++ b/api/IPAddress.cpp @@ -40,6 +40,26 @@ IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_oc _address.bytes[15] = fourth_octet; } +IPAddress::IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5, uint8_t o6, uint8_t o7, uint8_t o8, uint8_t o9, uint8_t o10, uint8_t o11, uint8_t o12, uint8_t o13, uint8_t o14, uint8_t o15, uint8_t o16) { + _type = IPv6; + _address.bytes[0] = o1; + _address.bytes[1] = o2; + _address.bytes[2] = o3; + _address.bytes[3] = o4; + _address.bytes[4] = o5; + _address.bytes[5] = o6; + _address.bytes[6] = o7; + _address.bytes[7] = o8; + _address.bytes[8] = o9; + _address.bytes[9] = o10; + _address.bytes[10] = o11; + _address.bytes[11] = o12; + _address.bytes[12] = o13; + _address.bytes[13] = o14; + _address.bytes[14] = o15; + _address.bytes[15] = o16; +} + IPAddress::IPAddress(uint32_t address) { // IPv4 only @@ -135,7 +155,7 @@ bool IPAddress::fromString6(const char *address) { while (*address) { char c = tolower(*address++); - if (isalnum(c)) { + if (isalnum(c) && c <= 'f') { if (c >= 'a') c -= 'a' - '0' - 10; acc = acc * 16 + (c - '0'); @@ -145,18 +165,26 @@ bool IPAddress::fromString6(const char *address) { } else if (c == ':') { if (*address == ':') { - if (doubledots >= 0) + if (doubledots >= 0) { // :: allowed once return false; + } + if (*address != '\0' && *(address + 1) == ':') { + // ::: not allowed + return false; + } // remember location doubledots = dots + !!acc; address++; + } else if (*address == '\0') { + // can't end with a single colon + return false; } if (dots == 7) // too many separators return false; - _address.bytes[dots] = acc >> 2; - _address.bytes[dots + 1] = acc & 0xff; + _address.bytes[dots * 2] = acc >> 8; + _address.bytes[dots * 2 + 1] = acc & 0xff; dots++; acc = 0; } @@ -165,11 +193,16 @@ bool IPAddress::fromString6(const char *address) { return false; } - if (doubledots == -1 && dots != 7) + if (doubledots == -1 && dots != 7) { // Too few separators return false; - _address.bytes[dots] = acc >> 2; - _address.bytes[dots + 1] = acc & 0xff; + } + if (doubledots > -1 && dots > 6) { + // Too many segments + return false; + } + _address.bytes[dots * 2] = acc >> 8; + _address.bytes[dots * 2 + 1] = acc & 0xff; dots++; if (doubledots != -1) { @@ -235,16 +268,16 @@ size_t IPAddress::printTo(Print& p) const size_t n = 0; if (_type == IPv6) { - // IPv6 IETF canonical format: left-most longest run of all zero fields, lower case + // IPv6 IETF canonical format: compress left-most longest run of two or more zero fields, lower case int8_t longest_start = -1; - int8_t longest_length = 0; + int8_t longest_length = 1; int8_t current_start = -1; int8_t current_length = 0; for (int8_t f = 0; f < 8; f++) { if (_address.bytes[f * 2] == 0 && _address.bytes[f * 2 + 1] == 0) { if (current_start == -1) { current_start = f; - current_length = 0; + current_length = 1; } else { current_length++; } @@ -258,24 +291,27 @@ size_t IPAddress::printTo(Print& p) const } for (int f = 0; f < 8; f++) { if (f < longest_start || f >= longest_start + longest_length) { - uint8_t c1 = _address.bytes[f * 2] >> 1; + uint8_t c1 = _address.bytes[f * 2] >> 4; uint8_t c2 = _address.bytes[f * 2] & 0xf; - uint8_t c3 = _address.bytes[f * 2 + 1] >> 1; + uint8_t c3 = _address.bytes[f * 2 + 1] >> 4; uint8_t c4 = _address.bytes[f * 2 + 1] & 0xf; if (c1 > 0) { - n += p.print(c1 < 10 ? '0' + c1 : 'a' + c1 - 10); + n += p.print((char)(c1 < 10 ? '0' + c1 : 'a' + c1 - 10)); } if (c1 > 0 || c2 > 0) { - n += p.print(c2 < 10 ? '0' + c2 : 'a' + c2 - 10); + n += p.print((char)(c2 < 10 ? '0' + c2 : 'a' + c2 - 10)); } if (c1 > 0 || c2 > 0 || c3 > 0) { - n += p.print(c3 < 10 ? '0' + c3 : 'a' + c3 - 10); + n += p.print((char)(c3 < 10 ? '0' + c3 : 'a' + c3 - 10)); } - n += p.print(c4 < 10 ? '0' + c4 : 'a' + c4 - 10); + n += p.print((char)(c4 < 10 ? '0' + c4 : 'a' + c4 - 10)); if (f < 7) { n += p.print(':'); } } else if (f == longest_start) { + if (longest_start == 0) { + n += p.print(':'); + } n += p.print(':'); } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f731b577..acf0c8cd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -30,11 +30,14 @@ set(TEST_SRCS src/Common/test_max.cpp src/Common/test_min.cpp src/IPAddress/test_fromString.cpp + src/IPAddress/test_fromString6.cpp src/IPAddress/test_IPAddress.cpp + src/IPAddress/test_IPAddress6.cpp src/IPAddress/test_operator_assignment.cpp src/IPAddress/test_operator_comparison.cpp src/IPAddress/test_operator_parentheses.cpp src/IPAddress/test_printTo.cpp + src/IPAddress/test_printTo6.cpp src/Print/test_clearWriteError.cpp src/Print/test_getWriteError.cpp src/Print/test_print.cpp diff --git a/test/src/IPAddress/test_IPAddress6.cpp b/test/src/IPAddress/test_IPAddress6.cpp new file mode 100644 index 00000000..a6941b71 --- /dev/null +++ b/test/src/IPAddress/test_IPAddress6.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include + +/************************************************************************************** + * TEST CODE + **************************************************************************************/ + +TEST_CASE ("Testing IPAddress(type) constructor()", "[IPAddress6-Ctor-01]") +{ + arduino::IPAddress ip (arduino::IPType::IPv6); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0); + REQUIRE(ip[1] == 0); + REQUIRE(ip[2] == 0); + REQUIRE(ip[3] == 0); + REQUIRE(ip[4] == 0); + REQUIRE(ip[5] == 0); + REQUIRE(ip[6] == 0); + REQUIRE(ip[7] == 0); + REQUIRE(ip[8] == 0); + REQUIRE(ip[9] == 0); + REQUIRE(ip[10] == 0); + REQUIRE(ip[11] == 0); + REQUIRE(ip[12] == 0); + REQUIRE(ip[13] == 0); + REQUIRE(ip[14] == 0); + REQUIRE(ip[15] == 0); +} + +TEST_CASE ("Testing IPAddress(o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o) constructor", "[IPAddress-Ctor6-02]") +{ + arduino::IPAddress ip(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0x20); + REQUIRE(ip[1] == 0x01); + REQUIRE(ip[2] == 0xd); + REQUIRE(ip[3] == 0xb8); + REQUIRE(ip[4] == 1); + REQUIRE(ip[5] == 2); + REQUIRE(ip[6] == 3); + REQUIRE(ip[7] == 4); + REQUIRE(ip[8] == 5); + REQUIRE(ip[9] == 6); + REQUIRE(ip[10] == 7); + REQUIRE(ip[11] == 8); + REQUIRE(ip[12] == 9); + REQUIRE(ip[13] == 0xa); + REQUIRE(ip[14] == 0xb); + REQUIRE(ip[15] == 0xc); +} + +TEST_CASE ("Testing IPAddress(type, a *) constructor", "[IPAddress6-Ctor-03]") +{ + uint8_t const ip_addr_array[] = {0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc}; + arduino::IPAddress ip(arduino::IPType::IPv6, ip_addr_array); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0x20); + REQUIRE(ip[1] == 0x01); + REQUIRE(ip[2] == 0xd); + REQUIRE(ip[3] == 0xb8); + REQUIRE(ip[4] == 1); + REQUIRE(ip[5] == 2); + REQUIRE(ip[6] == 3); + REQUIRE(ip[7] == 4); + REQUIRE(ip[8] == 5); + REQUIRE(ip[9] == 6); + REQUIRE(ip[10] == 7); + REQUIRE(ip[11] == 8); + REQUIRE(ip[12] == 9); + REQUIRE(ip[13] == 0xa); + REQUIRE(ip[14] == 0xb); + REQUIRE(ip[15] == 0xc); +} diff --git a/test/src/IPAddress/test_fromString6.cpp b/test/src/IPAddress/test_fromString6.cpp new file mode 100644 index 00000000..d2d29124 --- /dev/null +++ b/test/src/IPAddress/test_fromString6.cpp @@ -0,0 +1,395 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include +#include + +/************************************************************************************** + * TEST CODE + **************************************************************************************/ + +TEST_CASE ("Extract valid IPv6 address 'fromString(const char *)'", "[IPAddress-fromString-01]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString("2001:db8:102:304:506:708:90a:b0c") == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0x20); + REQUIRE(ip[1] == 0x01); + REQUIRE(ip[2] == 0xd); + REQUIRE(ip[3] == 0xb8); + REQUIRE(ip[4] == 1); + REQUIRE(ip[5] == 2); + REQUIRE(ip[6] == 3); + REQUIRE(ip[7] == 4); + REQUIRE(ip[8] == 5); + REQUIRE(ip[9] == 6); + REQUIRE(ip[10] == 7); + REQUIRE(ip[11] == 8); + REQUIRE(ip[12] == 9); + REQUIRE(ip[13] == 0xa); + REQUIRE(ip[14] == 0xb); + REQUIRE(ip[15] == 0xc); +} + +TEST_CASE ("Extract valid IPv6 address 'fromString(const String &)'", "[IPAddress-fromString-02]") +{ + arduino::IPAddress ip; + + arduino::String const ip_addr_str("2001:db8:102:304:506:708:90a:b0c"); + + REQUIRE(ip.fromString(ip_addr_str) == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0x20); + REQUIRE(ip[1] == 0x01); + REQUIRE(ip[2] == 0xd); + REQUIRE(ip[3] == 0xb8); + REQUIRE(ip[4] == 1); + REQUIRE(ip[5] == 2); + REQUIRE(ip[6] == 3); + REQUIRE(ip[7] == 4); + REQUIRE(ip[8] == 5); + REQUIRE(ip[9] == 6); + REQUIRE(ip[10] == 7); + REQUIRE(ip[11] == 8); + REQUIRE(ip[12] == 9); + REQUIRE(ip[13] == 0xa); + REQUIRE(ip[14] == 0xb); + REQUIRE(ip[15] == 0xc); +} + +TEST_CASE ("Extract valid IPv6 any address", "[IPAddress-fromString-03]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString("::") == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0); + REQUIRE(ip[1] == 0); + REQUIRE(ip[2] == 0); + REQUIRE(ip[3] == 0); + REQUIRE(ip[4] == 0); + REQUIRE(ip[5] == 0); + REQUIRE(ip[6] == 0); + REQUIRE(ip[7] == 0); + REQUIRE(ip[8] == 0); + REQUIRE(ip[9] == 0); + REQUIRE(ip[10] == 0); + REQUIRE(ip[11] == 0); + REQUIRE(ip[12] == 0); + REQUIRE(ip[13] == 0); + REQUIRE(ip[14] == 0); + REQUIRE(ip[15] == 0); +} + +TEST_CASE ("Extract valid IPv6 localhost address", "[IPAddress-fromString-04]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString("::1") == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0); + REQUIRE(ip[1] == 0); + REQUIRE(ip[2] == 0); + REQUIRE(ip[3] == 0); + REQUIRE(ip[4] == 0); + REQUIRE(ip[5] == 0); + REQUIRE(ip[6] == 0); + REQUIRE(ip[7] == 0); + REQUIRE(ip[8] == 0); + REQUIRE(ip[9] == 0); + REQUIRE(ip[10] == 0); + REQUIRE(ip[11] == 0); + REQUIRE(ip[12] == 0); + REQUIRE(ip[13] == 0); + REQUIRE(ip[14] == 0); + REQUIRE(ip[15] == 1); +} + +TEST_CASE ("Extract valid IPv6 different length segments", "[IPAddress-fromString-05]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString("abcd:ef1:23:0:4::") == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0xab); + REQUIRE(ip[1] == 0xcd); + REQUIRE(ip[2] == 0xe); + REQUIRE(ip[3] == 0xf1); + REQUIRE(ip[4] == 0); + REQUIRE(ip[5] == 0x23); + REQUIRE(ip[6] == 0); + REQUIRE(ip[7] == 0); + REQUIRE(ip[8] == 0); + REQUIRE(ip[9] == 4); + REQUIRE(ip[10] == 0); + REQUIRE(ip[11] == 0); + REQUIRE(ip[12] == 0); + REQUIRE(ip[13] == 0); + REQUIRE(ip[14] == 0); + REQUIRE(ip[15] == 0); +} + + +TEST_CASE ("Extract valid IPv6 zero start", "[IPAddress-fromString-06]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString("0:2:3:4:5:6:7:8") == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0); + REQUIRE(ip[1] == 0); + REQUIRE(ip[2] == 0); + REQUIRE(ip[3] == 2); + REQUIRE(ip[4] == 0); + REQUIRE(ip[5] == 3); + REQUIRE(ip[6] == 0); + REQUIRE(ip[7] == 4); + REQUIRE(ip[8] == 0); + REQUIRE(ip[9] == 5); + REQUIRE(ip[10] == 0); + REQUIRE(ip[11] == 6); + REQUIRE(ip[12] == 0); + REQUIRE(ip[13] == 7); + REQUIRE(ip[14] == 0); + REQUIRE(ip[15] == 8); +} + + +TEST_CASE ("Extract valid IPv6 zero end", "[IPAddress-fromString-07]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString("1:2:3:4:5:6:7:0") == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0); + REQUIRE(ip[1] == 1); + REQUIRE(ip[2] == 0); + REQUIRE(ip[3] == 2); + REQUIRE(ip[4] == 0); + REQUIRE(ip[5] == 3); + REQUIRE(ip[6] == 0); + REQUIRE(ip[7] == 4); + REQUIRE(ip[8] == 0); + REQUIRE(ip[9] == 5); + REQUIRE(ip[10] == 0); + REQUIRE(ip[11] == 6); + REQUIRE(ip[12] == 0); + REQUIRE(ip[13] == 7); + REQUIRE(ip[14] == 0); + REQUIRE(ip[15] == 0); +} + + +TEST_CASE ("Extract valid IPv6 two zero start", "[IPAddress-fromString-08]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString("::3:4:5:6:7:0") == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0); + REQUIRE(ip[1] == 0); + REQUIRE(ip[2] == 0); + REQUIRE(ip[3] == 0); + REQUIRE(ip[4] == 0); + REQUIRE(ip[5] == 3); + REQUIRE(ip[6] == 0); + REQUIRE(ip[7] == 4); + REQUIRE(ip[8] == 0); + REQUIRE(ip[9] == 5); + REQUIRE(ip[10] == 0); + REQUIRE(ip[11] == 6); + REQUIRE(ip[12] == 0); + REQUIRE(ip[13] == 7); + REQUIRE(ip[14] == 0); + REQUIRE(ip[15] == 0); +} + + +TEST_CASE ("Extract valid IPv6 two zero end", "[IPAddress-fromString-9]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString("0:2:3:4:5:6::") == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0); + REQUIRE(ip[1] == 0); + REQUIRE(ip[2] == 0); + REQUIRE(ip[3] == 2); + REQUIRE(ip[4] == 0); + REQUIRE(ip[5] == 3); + REQUIRE(ip[6] == 0); + REQUIRE(ip[7] == 4); + REQUIRE(ip[8] == 0); + REQUIRE(ip[9] == 5); + REQUIRE(ip[10] == 0); + REQUIRE(ip[11] == 6); + REQUIRE(ip[12] == 0); + REQUIRE(ip[13] == 0); + REQUIRE(ip[14] == 0); + REQUIRE(ip[15] == 0); +} + +// Non-canonical + +TEST_CASE ("Extract valid IPv6 any full long form", "[IPAddress-fromString-10]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString("0:0:0:0:0:0:0:0") == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0); + REQUIRE(ip[1] == 0); + REQUIRE(ip[2] == 0); + REQUIRE(ip[3] == 0); + REQUIRE(ip[4] == 0); + REQUIRE(ip[5] == 0); + REQUIRE(ip[6] == 0); + REQUIRE(ip[7] == 0); + REQUIRE(ip[8] == 0); + REQUIRE(ip[9] == 0); + REQUIRE(ip[10] == 0); + REQUIRE(ip[11] == 0); + REQUIRE(ip[12] == 0); + REQUIRE(ip[13] == 0); + REQUIRE(ip[14] == 0); + REQUIRE(ip[15] == 0); +} + +TEST_CASE ("Extract valid IPv6 upper case", "[IPAddress-fromString-11]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString("2001:DB8:102:304:506:708:90A:B0C") == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0x20); + REQUIRE(ip[1] == 0x01); + REQUIRE(ip[2] == 0xd); + REQUIRE(ip[3] == 0xb8); + REQUIRE(ip[4] == 1); + REQUIRE(ip[5] == 2); + REQUIRE(ip[6] == 3); + REQUIRE(ip[7] == 4); + REQUIRE(ip[8] == 5); + REQUIRE(ip[9] == 6); + REQUIRE(ip[10] == 7); + REQUIRE(ip[11] == 8); + REQUIRE(ip[12] == 9); + REQUIRE(ip[13] == 0xa); + REQUIRE(ip[14] == 0xb); + REQUIRE(ip[15] == 0xc); +} + +TEST_CASE ("Extract valid IPv6 explict start zero", "[IPAddress-fromString-10]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString("0::") == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0); + REQUIRE(ip[1] == 0); + REQUIRE(ip[2] == 0); + REQUIRE(ip[3] == 0); + REQUIRE(ip[4] == 0); + REQUIRE(ip[5] == 0); + REQUIRE(ip[6] == 0); + REQUIRE(ip[7] == 0); + REQUIRE(ip[8] == 0); + REQUIRE(ip[9] == 0); + REQUIRE(ip[10] == 0); + REQUIRE(ip[11] == 0); + REQUIRE(ip[12] == 0); + REQUIRE(ip[13] == 0); + REQUIRE(ip[14] == 0); + REQUIRE(ip[15] == 0); +} + +TEST_CASE ("Extract valid IPv6 explict end zero", "[IPAddress-fromString-10]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString("::0") == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0); + REQUIRE(ip[1] == 0); + REQUIRE(ip[2] == 0); + REQUIRE(ip[3] == 0); + REQUIRE(ip[4] == 0); + REQUIRE(ip[5] == 0); + REQUIRE(ip[6] == 0); + REQUIRE(ip[7] == 0); + REQUIRE(ip[8] == 0); + REQUIRE(ip[9] == 0); + REQUIRE(ip[10] == 0); + REQUIRE(ip[11] == 0); + REQUIRE(ip[12] == 0); + REQUIRE(ip[13] == 0); + REQUIRE(ip[14] == 0); + REQUIRE(ip[15] == 0); +} + +TEST_CASE ("Extract valid IPv6 compression of one group of zero", "[IPAddress-fromString-10]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString("1::3:4:5:6:7:8") == true); + + REQUIRE(ip.type() == arduino::IPType::IPv6); + REQUIRE(ip[0] == 0); + REQUIRE(ip[1] == 1); + REQUIRE(ip[2] == 0); + REQUIRE(ip[3] == 0); + REQUIRE(ip[4] == 0); + REQUIRE(ip[5] == 3); + REQUIRE(ip[6] == 0); + REQUIRE(ip[7] == 4); + REQUIRE(ip[8] == 0); + REQUIRE(ip[9] == 5); + REQUIRE(ip[10] == 0); + REQUIRE(ip[11] == 6); + REQUIRE(ip[12] == 0); + REQUIRE(ip[13] == 7); + REQUIRE(ip[14] == 0); + REQUIRE(ip[15] == 8); +} + +// Invalid cases + +TEST_CASE ("Extract invalid IPv6 address", "[IPAddress-fromString-12]") +{ + arduino::IPAddress ip; + + REQUIRE(ip.fromString(":::") == false); // three colons by self + REQUIRE(ip.fromString("::3:4:5:6::") == false); // two compressions + REQUIRE(ip.fromString("2001:db8:102:10304:506:708:90a:b0c") == false); // 5 character field + REQUIRE(ip.fromString("200x:db8:102:304:506:708:90a:b0c") == false); // invalid character + REQUIRE(ip.fromString("2001:db8:102:304::506:708:90a:b0c") == false); // double colon with 8 other fields (so not a compression) + REQUIRE(ip.fromString("2001:db8:102:304:::708:90a:b0c") == false); // three colons in middle + REQUIRE(ip.fromString("2001:db8:102:304:506:708:90a:b0c:d0e") == false); // 9 fields + REQUIRE(ip.fromString("2001:db8:102:304:506:708:90a:") == false); // missing last group (but has a colon) + REQUIRE(ip.fromString("2001:db8:102:304:506:708:90a") == false); // only seven groups + REQUIRE(ip.fromString("0:0:0:0:0:0:0:0:0") == false); // nine zeros + REQUIRE(ip.fromString("0:0:0:0:0:0:0:0:") == false); // extra colon + REQUIRE(ip.fromString("0:0:0:0:0:0:0:") == false); // missing last group (but has a colon) + REQUIRE(ip.fromString("0:0:0:0:0:0:0") == false); // only seven groups +} diff --git a/test/src/IPAddress/test_printTo6.cpp b/test/src/IPAddress/test_printTo6.cpp new file mode 100644 index 00000000..621008af --- /dev/null +++ b/test/src/IPAddress/test_printTo6.cpp @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include +#include + +/************************************************************************************** + * TEST CODE + **************************************************************************************/ + +TEST_CASE ("Print IPv6", "[IPAddress-printTo6-01]") +{ + PrintMock mock; + arduino::IPAddress ip(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc); + + mock.print(ip); + + REQUIRE(mock._str == "2001:db8:102:304:506:708:90a:b0c"); +} + +TEST_CASE ("Print IPv6 any", "[IPAddress-printTo6-02]") +{ + PrintMock mock; + arduino::IPAddress const ip(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + + mock.print(ip); + + REQUIRE(mock._str == "::"); +} + +TEST_CASE ("Print IPv6 localhost", "[IPAddress-printTo6-03]") +{ + PrintMock mock; + arduino::IPAddress const ip(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1); + + mock.print(ip); + + REQUIRE(mock._str == "::1"); +} + +TEST_CASE ("Print IPv6 different length segments", "[IPAddress-printTo6-04]") +{ + PrintMock mock; + arduino::IPAddress const ip(0xab,0xcd, 0x0e,0xf1, 0x00,0x23, 0,0, 0x00,0x04, 0,0, 0,0, 0,0); + + mock.print(ip); + + REQUIRE(mock._str == "abcd:ef1:23:0:4::"); +} + +TEST_CASE ("Print IPv6 zero longest run end", "[IPAddress-printTo6-05]") +{ + PrintMock mock; + arduino::IPAddress const ip(0,0, 0,1, 0,0, 0,0, 0,2, 0,0, 0,0, 0,0); + + mock.print(ip); + + REQUIRE(mock._str == "0:1:0:0:2::"); +} + +TEST_CASE ("Print IPv6 zero longest run mid", "[IPAddress-printTo6-06]") +{ + PrintMock mock; + arduino::IPAddress const ip(0,0, 0,1, 0,0, 0,0, 0,0, 0,2, 0,0, 0,0); + + mock.print(ip); + + REQUIRE(mock._str == "0:1::2:0:0"); +} + +TEST_CASE ("Print IPv6 start zero", "[IPAddress-printTo6-07]") +{ + PrintMock mock; + arduino::IPAddress const ip(0,0, 0,2, 0,3, 0,4, 0,5, 0,6, 0,7, 0,8); + + mock.print(ip); + + REQUIRE(mock._str == "0:2:3:4:5:6:7:8"); +} + +TEST_CASE ("Print IPv6 ending zero", "[IPAddress-printTo6-08]") +{ + PrintMock mock; + arduino::IPAddress const ip(0,1, 0,2, 0,3, 0,4, 0,5, 0,6, 0,7, 0,0); + + mock.print(ip); + + REQUIRE(mock._str == "1:2:3:4:5:6:7:0"); +} + +TEST_CASE ("Print IPv6 start two zero", "[IPAddress-printTo6-09]") +{ + PrintMock mock; + arduino::IPAddress const ip(0,0, 0,0, 0,3, 0,4, 0,5, 0,6, 0,7, 0,0); + + mock.print(ip); + + REQUIRE(mock._str == "::3:4:5:6:7:0"); +} + +TEST_CASE ("Print IPv6 ending two zero", "[IPAddress-printTo6-10]") +{ + PrintMock mock; + arduino::IPAddress const ip(0,0, 0,2, 0,3, 0,4, 0,5, 0,6, 0,0, 0,0); + + mock.print(ip); + + REQUIRE(mock._str == "0:2:3:4:5:6::"); +} + +TEST_CASE ("Print IPv6 first out of same length", "[IPAddress-printTo6-11]") +{ + PrintMock mock; + arduino::IPAddress const ip(0,1, 0,0, 0,0, 0,4, 0,5, 0,0, 0,0, 0,8); + + mock.print(ip); + + REQUIRE(mock._str == "1::4:5:0:0:8"); +} + + +TEST_CASE ("Print IPv6 single zeros not compressed", "[IPAddress-printTo6-12]") +{ + PrintMock mock; + arduino::IPAddress const ip(0,1, 0,0, 0,3, 0,0, 0,5, 0,0, 0,7, 0,8); + + mock.print(ip); + + REQUIRE(mock._str == "1:0:3:0:5:0:7:8"); +} From 81dfceead6ee814b5b72372c9462d2131e98ef41 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 00:33:16 +1000 Subject: [PATCH 049/131] IPv6 raw address --- api/IPAddress.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/api/IPAddress.h b/api/IPAddress.h index 3bffafaf..64a287d5 100644 --- a/api/IPAddress.h +++ b/api/IPAddress.h @@ -49,14 +49,7 @@ class IPAddress : public Printable { // to the internal structure rather than a copy of the address this function should only // be used when you know that the usage of the returned uint8_t* will be transient and not // stored. - // IPv4 only (for friends) - uint8_t* raw_address() { - if (_type == IPv4) { - return &_address.bytes[12]; - } - return nullptr; - }; - uint8_t* raw_bytes() { return _address.bytes; } + uint8_t* raw_address() { return _type == IPv4 ? &_address.bytes[12] : _address.bytes; } public: // Constructors From a395f2055bc4e2ad6286a4fcd29bd2e4327b2e97 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 00:33:59 +1000 Subject: [PATCH 050/131] IPv6 comparison tests --- api/IPAddress.h | 10 +-- test/CMakeLists.txt | 1 + .../IPAddress/test_operator_comparison6.cpp | 65 +++++++++++++++++++ 3 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 test/src/IPAddress/test_operator_comparison6.cpp diff --git a/api/IPAddress.h b/api/IPAddress.h index 64a287d5..79f05878 100644 --- a/api/IPAddress.h +++ b/api/IPAddress.h @@ -57,7 +57,7 @@ class IPAddress : public Printable { IPAddress(IPType ip_type); IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5, uint8_t o6, uint8_t o7, uint8_t o8, uint8_t o9, uint8_t o10, uint8_t o11, uint8_t o12, uint8_t o13, uint8_t o14, uint8_t o15, uint8_t o16); - IPAddress(uint32_t address); // IPv4 only; see implementation note + IPAddress(uint32_t address); // IPv4; see implementation note IPAddress(const uint8_t *address); // IPv4 IPAddress(IPType ip_type, const uint8_t *address); @@ -65,13 +65,13 @@ class IPAddress : public Printable { bool fromString(const String &address) { return fromString(address.c_str()); } // Overloaded cast operator to allow IPAddress objects to be used where a uint32_t is expected - // IPv4 only; see implementation note + // NOTE: IPv4 only; see implementation note operator uint32_t() const { return _type == IPv4 ? _address.dword[3] : 0; }; bool operator==(const IPAddress& addr) const; bool operator!=(const IPAddress& addr) const { return !(*this == addr); }; - // IPv4 only; we don't know the length of the pointer + // NOTE: IPv4 only; we don't know the length of the pointer bool operator==(const uint8_t* addr) const; // Overloaded index operator to allow getting and setting individual octets of the address @@ -79,9 +79,9 @@ class IPAddress : public Printable { uint8_t& operator[](int index); // Overloaded copy operators to allow initialisation of IPAddress objects from other types - // IPv4 only + // NOTE: IPv4 only IPAddress& operator=(const uint8_t *address); - // IPv4 only; see implementation note + // NOTE: IPv4 only; see implementation note IPAddress& operator=(uint32_t address); virtual size_t printTo(Print& p) const; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index acf0c8cd..e88fe130 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -35,6 +35,7 @@ set(TEST_SRCS src/IPAddress/test_IPAddress6.cpp src/IPAddress/test_operator_assignment.cpp src/IPAddress/test_operator_comparison.cpp + src/IPAddress/test_operator_comparison6.cpp src/IPAddress/test_operator_parentheses.cpp src/IPAddress/test_printTo.cpp src/IPAddress/test_printTo6.cpp diff --git a/test/src/IPAddress/test_operator_comparison6.cpp b/test/src/IPAddress/test_operator_comparison6.cpp new file mode 100644 index 00000000..36c7efbd --- /dev/null +++ b/test/src/IPAddress/test_operator_comparison6.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include + +/************************************************************************************** + * TEST CODE + **************************************************************************************/ + +TEST_CASE ("Testing two basic constructs the same", "[IPAddress6-Operator-==-01]") +{ + arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc), ip2(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc); + REQUIRE((ip1 == ip2) == true); +} + +TEST_CASE ("Testing two addresses different", "[IPAddress-Operator-==-02]") +{ + arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc), ip2(0xfd,0x12, 0x34,0x56, 0x78,0x9a, 0,1, 0,0, 0,0, 0,0, 0,1); + REQUIRE((ip1 == ip2) == false); +} + +TEST_CASE ("Testing not equals different address is true", "[IPAddress-Operator-==-03]") +{ + arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc), ip2(0xfd,0x12, 0x34,0x56, 0x78,0x9a, 0,1, 0,0, 0,0, 0,0, 0,1); + REQUIRE((ip1 != ip2) == true); +} + +TEST_CASE ("Testing not equals same address is false", "[IPAddress-Operator-==-04]") +{ + arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc), ip2(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc); + REQUIRE((ip1 != ip2) == false); +} + +// IPv4 and IPv6 differ based on type (irrespective of bytes) + +TEST_CASE ("Testing IPv4 vs IPv6", "[IPAddress6-Operator-==-05]") +{ + arduino::IPAddress ip1(10, 0, 0, 1), ip2(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc); + REQUIRE((ip1 == ip2) == false); +} + +TEST_CASE ("Testing IPv4 vs IPv6 equivalent IPv4-compatible address (deprecated)", "[IPAddress6-Operator-==-05]") +{ + arduino::IPAddress ip1(10, 0, 0, 1), ip2(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 10,0, 0,1); + REQUIRE((ip1 == ip2) == false); +} + +TEST_CASE ("Testing IPv4 vs IPv6 localhost", "[IPAddress6-Operator-==-05]") +{ + arduino::IPAddress ip1(127, 0, 0, 1), ip2(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 10,0, 0,1); + REQUIRE((ip1 == ip2) == false); +} + +TEST_CASE ("Testing IPv4 equivalent compatible address vs IPv6 localhost", "[IPAddress6-Operator-==-05]") +{ + arduino::IPAddress ip1(0, 0, 0, 1), ip2(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,1); + REQUIRE((ip1 == ip2) == false); +} From 7211accf5a27ff58a11618efd35153d37deb5f1f Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 09:03:58 +1000 Subject: [PATCH 051/131] Spelling typos --- test/src/IPAddress/test_fromString6.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/src/IPAddress/test_fromString6.cpp b/test/src/IPAddress/test_fromString6.cpp index d2d29124..a0f09c73 100644 --- a/test/src/IPAddress/test_fromString6.cpp +++ b/test/src/IPAddress/test_fromString6.cpp @@ -298,7 +298,7 @@ TEST_CASE ("Extract valid IPv6 upper case", "[IPAddress-fromString-11]") REQUIRE(ip[15] == 0xc); } -TEST_CASE ("Extract valid IPv6 explict start zero", "[IPAddress-fromString-10]") +TEST_CASE ("Extract valid IPv6 explicit start zero", "[IPAddress-fromString-10]") { arduino::IPAddress ip; @@ -323,7 +323,7 @@ TEST_CASE ("Extract valid IPv6 explict start zero", "[IPAddress-fromString-10]") REQUIRE(ip[15] == 0); } -TEST_CASE ("Extract valid IPv6 explict end zero", "[IPAddress-fromString-10]") +TEST_CASE ("Extract valid IPv6 explicit end zero", "[IPAddress-fromString-10]") { arduino::IPAddress ip; From 58c8f228ad7775122bffd14ada0df30e9ba99be6 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 09:06:50 +1000 Subject: [PATCH 052/131] Fix indend --- api/IPAddress.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/IPAddress.h b/api/IPAddress.h index 79f05878..6d669195 100644 --- a/api/IPAddress.h +++ b/api/IPAddress.h @@ -40,8 +40,8 @@ enum IPType { class IPAddress : public Printable { private: union { - uint8_t bytes[16]; - uint32_t dword[4]; + uint8_t bytes[16]; + uint32_t dword[4]; } _address; IPType _type; From ac541e8c001fe90f09aef5575b2921a0ff5078ab Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 09:09:27 +1000 Subject: [PATCH 053/131] Move comments --- api/IPAddress.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/api/IPAddress.h b/api/IPAddress.h index 6d669195..c9e90985 100644 --- a/api/IPAddress.h +++ b/api/IPAddress.h @@ -53,12 +53,16 @@ class IPAddress : public Printable { public: // Constructors - IPAddress(); // IPv4 + + // Default IPv4 + IPAddress(); IPAddress(IPType ip_type); IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5, uint8_t o6, uint8_t o7, uint8_t o8, uint8_t o9, uint8_t o10, uint8_t o11, uint8_t o12, uint8_t o13, uint8_t o14, uint8_t o15, uint8_t o16); - IPAddress(uint32_t address); // IPv4; see implementation note - IPAddress(const uint8_t *address); // IPv4 + // IPv4; see implementation note + IPAddress(uint32_t address); + // Default IPv4 + IPAddress(const uint8_t *address); IPAddress(IPType ip_type, const uint8_t *address); bool fromString(const char *address); From fe80caa43b45722b9eff6adc66437e1f3f73edc3 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 09:20:06 +1000 Subject: [PATCH 054/131] Define constants for the IPv4 index --- api/IPAddress.cpp | 44 +++++++++++++++++++++----------------------- api/IPAddress.h | 7 +++++-- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/api/IPAddress.cpp b/api/IPAddress.cpp index ab14f85a..08d55b08 100644 --- a/api/IPAddress.cpp +++ b/api/IPAddress.cpp @@ -33,11 +33,11 @@ IPAddress::IPAddress(IPType ip_type) IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) { _type = IPv4; - memset(_address.bytes, 0, sizeof(_address.bytes) - sizeof(uint32_t)); - _address.bytes[12] = first_octet; - _address.bytes[13] = second_octet; - _address.bytes[14] = third_octet; - _address.bytes[15] = fourth_octet; + memset(_address.bytes, 0, sizeof(_address.bytes)); + _address.bytes[IPADDRESS_V4_BYTES_INDEX] = first_octet; + _address.bytes[IPADDRESS_V4_BYTES_INDEX + 1] = second_octet; + _address.bytes[IPADDRESS_V4_BYTES_INDEX + 2] = third_octet; + _address.bytes[IPADDRESS_V4_BYTES_INDEX + 3] = fourth_octet; } IPAddress::IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5, uint8_t o6, uint8_t o7, uint8_t o8, uint8_t o9, uint8_t o10, uint8_t o11, uint8_t o12, uint8_t o13, uint8_t o14, uint8_t o15, uint8_t o16) { @@ -64,8 +64,8 @@ IPAddress::IPAddress(uint32_t address) { // IPv4 only _type = IPv4; - memset(_address.bytes, 0, sizeof(_address.bytes) - sizeof(uint32_t)); - _address.dword[3] = address; + memset(_address.bytes, 0, sizeof(_address.bytes)); + _address.dword[IPADDRESS_V4_DWORD_INDEX] = address; // NOTE on conversion/comparison and uint32_t: // These conversions are host platform dependent. @@ -82,8 +82,8 @@ IPAddress::IPAddress(IPType ip_type, const uint8_t *address) { _type = ip_type; if (ip_type == IPv4) { - memset(_address.bytes, 0, sizeof(_address.bytes) - sizeof(uint32_t)); - memcpy(&_address.bytes[12], address, sizeof(uint32_t)); + memset(_address.bytes, 0, sizeof(_address.bytes)); + memcpy(&_address.bytes[IPADDRESS_V4_BYTES_INDEX], address, sizeof(uint32_t)); } else { memcpy(_address.bytes, address, sizeof(_address.bytes)); } @@ -103,6 +103,7 @@ bool IPAddress::fromString4(const char *address) int16_t acc = -1; // Accumulator uint8_t dots = 0; + memset(_address.bytes, 0, sizeof(_address.bytes)); while (*address) { char c = *address++; @@ -124,7 +125,7 @@ bool IPAddress::fromString4(const char *address) /* No value between dots, e.g. '1..' */ return false; } - _address.bytes[12 + dots++] = acc; + _address.bytes[IPADDRESS_V4_BYTES_INDEX + dots++] = acc; acc = -1; } else @@ -142,8 +143,7 @@ bool IPAddress::fromString4(const char *address) /* No value between dots, e.g. '1..' */ return false; } - memset(_address.bytes, 0, sizeof(_address.bytes) - sizeof(uint32_t)); - _address.bytes[15] = acc; + _address.bytes[IPADDRESS_V4_BYTES_INDEX + 3] = acc; _type = IPv4; return true; } @@ -220,8 +220,8 @@ IPAddress& IPAddress::operator=(const uint8_t *address) { // IPv4 only conversion from byte pointer _type = IPv4; - memset(_address.bytes, 0, sizeof(_address.bytes) - sizeof(uint32_t)); - memcpy(&_address.bytes[12], address, sizeof(uint32_t)); + memset(_address.bytes, 0, sizeof(_address.bytes)); + memcpy(&_address.bytes[IPADDRESS_V4_BYTES_INDEX], address, sizeof(uint32_t)); return *this; } @@ -230,10 +230,8 @@ IPAddress& IPAddress::operator=(uint32_t address) // IPv4 conversion // See note on conversion/comparison and uint32_t _type = IPv4; - _address.dword[0] = 0; - _address.dword[1] = 0; - _address.dword[2] = 0; - _address.dword[3] = address; + memset(_address.bytes, 0, sizeof(_address.bytes)); + _address.dword[IPADDRESS_V4_DWORD_INDEX] = address; return *this; } @@ -246,19 +244,19 @@ bool IPAddress::operator==(const uint8_t* addr) const { // IPv4 only comparison to byte pointer // Can't support IPv6 as we know our type, but not the length of the pointer - return _type == IPv4 && memcmp(addr, &_address.bytes[12], sizeof(uint32_t)) == 0; + return _type == IPv4 && memcmp(addr, &_address.bytes[IPADDRESS_V4_BYTES_INDEX], sizeof(uint32_t)) == 0; } uint8_t IPAddress::operator[](int index) const { if (_type == IPv4) { - return _address.bytes[index + 12]; + return _address.bytes[IPADDRESS_V4_BYTES_INDEX + index]; } return _address.bytes[index]; }; uint8_t& IPAddress::operator[](int index) { if (_type == IPv4) { - return _address.bytes[index + 12]; + return _address.bytes[IPADDRESS_V4_BYTES_INDEX + index]; } return _address.bytes[index]; }; @@ -321,10 +319,10 @@ size_t IPAddress::printTo(Print& p) const // IPv4 for (int i =0; i < 3; i++) { - n += p.print(_address.bytes[12 + i], DEC); + n += p.print(_address.bytes[IPADDRESS_V4_BYTES_INDEX + i], DEC); n += p.print('.'); } - n += p.print(_address.bytes[15], DEC); + n += p.print(_address.bytes[IPADDRESS_V4_BYTES_INDEX + 3], DEC); return n; } diff --git a/api/IPAddress.h b/api/IPAddress.h index c9e90985..03f2e8e2 100644 --- a/api/IPAddress.h +++ b/api/IPAddress.h @@ -23,6 +23,9 @@ #include "Printable.h" #include "String.h" +#define IPADDRESS_V4_BYTES_INDEX 12 +#define IPADDRESS_V4_DWORD_INDEX 3 + // forward declarations of global name space friend classes class EthernetClass; class DhcpClass; @@ -49,7 +52,7 @@ class IPAddress : public Printable { // to the internal structure rather than a copy of the address this function should only // be used when you know that the usage of the returned uint8_t* will be transient and not // stored. - uint8_t* raw_address() { return _type == IPv4 ? &_address.bytes[12] : _address.bytes; } + uint8_t* raw_address() { return _type == IPv4 ? &_address.bytes[IPADDRESS_V4_BYTES_INDEX] : _address.bytes; } public: // Constructors @@ -70,7 +73,7 @@ class IPAddress : public Printable { // Overloaded cast operator to allow IPAddress objects to be used where a uint32_t is expected // NOTE: IPv4 only; see implementation note - operator uint32_t() const { return _type == IPv4 ? _address.dword[3] : 0; }; + operator uint32_t() const { return _type == IPv4 ? _address.dword[IPADDRESS_V4_DWORD_INDEX] : 0; }; bool operator==(const IPAddress& addr) const; bool operator!=(const IPAddress& addr) const { return !(*this == addr); }; From 79cda869c889d79c677e56aab9dd40b1ea5c963a Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 09:35:11 +1000 Subject: [PATCH 055/131] Fix build warnings / typos --- api/IPAddress.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/IPAddress.cpp b/api/IPAddress.cpp index 08d55b08..f0b13eef 100644 --- a/api/IPAddress.cpp +++ b/api/IPAddress.cpp @@ -238,7 +238,7 @@ IPAddress& IPAddress::operator=(uint32_t address) bool IPAddress::operator==(const IPAddress& addr) const { return (addr._type == _type) && (memcmp(addr._address.bytes, _address.bytes, sizeof(_address.bytes)) == 0); -}; +} bool IPAddress::operator==(const uint8_t* addr) const { @@ -252,14 +252,14 @@ uint8_t IPAddress::operator[](int index) const { return _address.bytes[IPADDRESS_V4_BYTES_INDEX + index]; } return _address.bytes[index]; -}; +} uint8_t& IPAddress::operator[](int index) { if (_type == IPv4) { return _address.bytes[IPADDRESS_V4_BYTES_INDEX + index]; } return _address.bytes[index]; -}; +} size_t IPAddress::printTo(Print& p) const { From 1f6c79b5e3946ba50b6ae4e70d524a8e1e55ce45 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 10:38:31 +1000 Subject: [PATCH 056/131] IPv6 is colons, not dots --- api/IPAddress.cpp | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/api/IPAddress.cpp b/api/IPAddress.cpp index f0b13eef..ee941f6d 100644 --- a/api/IPAddress.cpp +++ b/api/IPAddress.cpp @@ -150,7 +150,7 @@ bool IPAddress::fromString4(const char *address) bool IPAddress::fromString6(const char *address) { uint32_t acc = 0; // Accumulator - int dots = 0, doubledots = -1; + int colons = 0, double_colons = -1; while (*address) { @@ -165,7 +165,7 @@ bool IPAddress::fromString6(const char *address) { } else if (c == ':') { if (*address == ':') { - if (doubledots >= 0) { + if (double_colons >= 0) { // :: allowed once return false; } @@ -174,18 +174,18 @@ bool IPAddress::fromString6(const char *address) { return false; } // remember location - doubledots = dots + !!acc; + double_colons = colons + !!acc; address++; } else if (*address == '\0') { // can't end with a single colon return false; } - if (dots == 7) + if (colons == 7) // too many separators return false; - _address.bytes[dots * 2] = acc >> 8; - _address.bytes[dots * 2 + 1] = acc & 0xff; - dots++; + _address.bytes[colons * 2] = acc >> 8; + _address.bytes[colons * 2 + 1] = acc & 0xff; + colons++; acc = 0; } else @@ -193,22 +193,22 @@ bool IPAddress::fromString6(const char *address) { return false; } - if (doubledots == -1 && dots != 7) { + if (double_colons == -1 && colons != 7) { // Too few separators return false; } - if (doubledots > -1 && dots > 6) { - // Too many segments + if (double_colons > -1 && colons > 6) { + // Too many segments (double colon must be at least one zero field) return false; } - _address.bytes[dots * 2] = acc >> 8; - _address.bytes[dots * 2 + 1] = acc & 0xff; - dots++; - - if (doubledots != -1) { - for (int i = dots * 2 - doubledots * 2 - 1; i >= 0; i--) - _address.bytes[16 - dots * 2 + doubledots * 2 + i] = _address.bytes[doubledots * 2 + i]; - for (int i = doubledots * 2; i < 16 - dots * 2 + doubledots * 2; i++) + _address.bytes[colons * 2] = acc >> 8; + _address.bytes[colons * 2 + 1] = acc & 0xff; + colons++; + + if (double_colons != -1) { + for (int i = colons * 2 - double_colons * 2 - 1; i >= 0; i--) + _address.bytes[16 - colons * 2 + double_colons * 2 + i] = _address.bytes[double_colons * 2 + i]; + for (int i = double_colons * 2; i < 16 - colons * 2 + double_colons * 2; i++) _address.bytes[i] = 0; } From 3f7f18bafc10cca26d186aaa939b6b7e92b6b575 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 10:54:26 +1000 Subject: [PATCH 057/131] Increase test coverage --- test/src/IPAddress/test_operator_comparison6.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/src/IPAddress/test_operator_comparison6.cpp b/test/src/IPAddress/test_operator_comparison6.cpp index 36c7efbd..a5e1b87c 100644 --- a/test/src/IPAddress/test_operator_comparison6.cpp +++ b/test/src/IPAddress/test_operator_comparison6.cpp @@ -63,3 +63,10 @@ TEST_CASE ("Testing IPv4 equivalent compatible address vs IPv6 localhost", "[IPA arduino::IPAddress ip1(0, 0, 0, 1), ip2(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,1); REQUIRE((ip1 == ip2) == false); } + +TEST_CASE ("Testing IPv6 never matches as raw byte sequence assumed to be length 4", "[IPAddress6-Operator-==-06]") +{ + arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc); + uint8_t const ip2[] = {0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc}; + REQUIRE((ip1 == ip2) == false); +} From deabbb85f3a858924cc8dcf1990a7c73282f2ba4 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 10:55:20 +1000 Subject: [PATCH 058/131] Test IPv6 comparison to int32_t (should always not match) --- .../IPAddress/test_operator_parentheses6.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 test/src/IPAddress/test_operator_parentheses6.cpp diff --git a/test/src/IPAddress/test_operator_parentheses6.cpp b/test/src/IPAddress/test_operator_parentheses6.cpp new file mode 100644 index 00000000..5b4740c8 --- /dev/null +++ b/test/src/IPAddress/test_operator_parentheses6.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include + +/************************************************************************************** + * TEST CODE + **************************************************************************************/ + +// These comparisons should always return false, as you can't compare an IPv6 to an int32_t + +TEST_CASE ("Testing implicit cast of IPv6 compatible (little endian) to uint32_t always false", "[IPAddress6-Operator-()-01]") +{ + // On little endian systems, considering only last four octets (ignoring the rest) + arduino::IPAddress ip(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 129,168, 1,2); + uint32_t const val_expected = ip; + uint32_t const val_actual = (129 | (168 << 8) | (1 << 16) | (2 << 24)); + REQUIRE((val_expected == val_actual) == false); +} + +TEST_CASE ("Testing implicit cast of IPv6 full little endian to uint32_t always false", "[IPAddress6-Operator-()-01]") +{ + // On little endian systems (full value) + arduino::IPAddress ip(129,168, 1,2, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0); + uint32_t const val_expected = ip; + uint32_t const val_actual = (129 | (168 << 8) | (1 << 16) | (2 << 24)); + REQUIRE((val_expected == val_actual) == false); +} + +TEST_CASE ("Testing implicit cast of IPv6 to uint32_t always false", "[IPAddress6-Operator-()-01]") +{ + // Actual value of the 128-bit IPv6 address, which is network byte order + arduino::IPAddress ip(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 129,168, 1,2); + uint32_t const val_expected = ip; + uint32_t const val_actual = ((129 << 24) | (168 << 16) | (1 << 8) | 2); + REQUIRE((val_expected == val_actual) == false); +} From 606d3a81ea5abc13697e9bedf525dee3d9a1e0a7 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 10:55:44 +1000 Subject: [PATCH 059/131] Note on IPv4 tests to int32_t --- test/src/IPAddress/test_operator_assignment.cpp | 1 + test/src/IPAddress/test_operator_parentheses.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/test/src/IPAddress/test_operator_assignment.cpp b/test/src/IPAddress/test_operator_assignment.cpp index f7afece2..e9fc8691 100644 --- a/test/src/IPAddress/test_operator_assignment.cpp +++ b/test/src/IPAddress/test_operator_assignment.cpp @@ -29,5 +29,6 @@ TEST_CASE ("Testing IPAddress::operator = (uint32_t a)", "[IPAddress-Operator-=- uint32_t const ip2 = 192 | (168 << 8) | (1 << 16) | (2 << 24); ip1 = ip2; + // NOTE: Only correct on little-endian systems REQUIRE(ip1 == arduino::IPAddress(192,168,1,2)); } diff --git a/test/src/IPAddress/test_operator_parentheses.cpp b/test/src/IPAddress/test_operator_parentheses.cpp index e9f1c0af..27fce3a5 100644 --- a/test/src/IPAddress/test_operator_parentheses.cpp +++ b/test/src/IPAddress/test_operator_parentheses.cpp @@ -19,5 +19,6 @@ TEST_CASE ("Testing IPAddress::operator uint32_t() const", "[IPAddress-Operator- arduino::IPAddress ip(129,168,1,2); uint32_t const val_expected = ip; uint32_t const val_actual = (129 | (168 << 8) | (1 << 16) | (2 << 24)); + // NOTE: Only correct on little-endian systems REQUIRE(val_expected == val_actual); } From bcc5233a9385ed7eb9d0bc23d380e3f9a3b2b65d Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Mon, 15 Aug 2022 10:57:51 +1000 Subject: [PATCH 060/131] Actually include the tests ! --- test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e88fe130..bd0b6821 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -37,6 +37,7 @@ set(TEST_SRCS src/IPAddress/test_operator_comparison.cpp src/IPAddress/test_operator_comparison6.cpp src/IPAddress/test_operator_parentheses.cpp + src/IPAddress/test_operator_parentheses6.cpp src/IPAddress/test_printTo.cpp src/IPAddress/test_printTo6.cpp src/Print/test_clearWriteError.cpp From df3f64a7ed41da11944b9b41bdfe83eeb9e33a96 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Tue, 23 Aug 2022 23:43:27 +1000 Subject: [PATCH 061/131] Local build instructions --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index c4a02de0..0ecb31c2 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,18 @@ This repository includes a test suite that covers most of the API and that is de Please help us improve the coverage of the test suite! +#### To build and run unit tests + +The unit tests are automatically built by GitHub as part of pull request checks (in `.github/workflows/unit-tests.yml`). + +To build and run locally (from the project root): + +``` +cmake -S test -B test/build +make --directory test/build +test/build/bin/test-ArduinoCore-API +``` + ### Implementing ArduinoCore-API In order to compile a core which is implementing ArduinoCore-API you'll need to copy/symlink the `api` directory to the target's `cores/arduino` directory as part of your development and release workflow. The most elegant and effective solution is to develop your core with `api` symlinked and produce the distributable archive by telling `tar` to follow symlinks. Example: From 246852ba523b17f2ee5026ccefbcfab6aea1f7d1 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Tue, 23 Aug 2022 23:52:53 +1000 Subject: [PATCH 062/131] List dependencies --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0ecb31c2..ddb61ad4 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,15 @@ Please help us improve the coverage of the test suite! The unit tests are automatically built by GitHub as part of pull request checks (in `.github/workflows/unit-tests.yml`). -To build and run locally (from the project root): +To build and run locally: + +**Dependencies** + +* CMake +* make +* gcc + +From the project root: ``` cmake -S test -B test/build From 09a0dd46a61d082387aaff23a657280acc41a038 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 24 Aug 2022 07:16:50 +0200 Subject: [PATCH 063/131] Simplfiy build instructions. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ddb61ad4..23569aa3 100644 --- a/README.md +++ b/README.md @@ -59,9 +59,9 @@ To build and run locally: From the project root: ``` -cmake -S test -B test/build -make --directory test/build -test/build/bin/test-ArduinoCore-API +cd test && mkdir build && cd build +cmake .. +make && bin/test-ArduinoCore-API ``` ### Implementing ArduinoCore-API From f2624d1b3244040d9b2698791dccc4724a0ea05f Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 24 Aug 2022 07:21:35 +0200 Subject: [PATCH 064/131] Adding URLs to dependencies, dependency installation procedure for Linux. --- README.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 23569aa3..f8e0ab06 100644 --- a/README.md +++ b/README.md @@ -52,13 +52,18 @@ To build and run locally: **Dependencies** -* CMake -* make -* gcc +* [CMake](https://cmake.org/) +* [GCC](https://gcc.gnu.org/) -From the project root: +On (Ubuntu) Linux run: +```bash +sudo apt-get install build-essential cmake ``` + +From the project root: + +```bash cd test && mkdir build && cd build cmake .. make && bin/test-ArduinoCore-API @@ -68,7 +73,7 @@ make && bin/test-ArduinoCore-API In order to compile a core which is implementing ArduinoCore-API you'll need to copy/symlink the `api` directory to the target's `cores/arduino` directory as part of your development and release workflow. The most elegant and effective solution is to develop your core with `api` symlinked and produce the distributable archive by telling `tar` to follow symlinks. Example: -``` +```bash tar --exclude='*.git*' -cjhvf $yourcore-$version.tar.bz2 $yourcore/ ``` From c6ca79344f9ab0b501a3856368aff0086f712d44 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Tue, 20 Dec 2022 17:25:55 +0100 Subject: [PATCH 065/131] catch: update to 2.13.9 --- test/CMakeLists.txt | 2 +- test/external/catch/v2.13.9/include/catch.hpp | 17970 ++++++++++++++++ 2 files changed, 17971 insertions(+), 1 deletion(-) create mode 100644 test/external/catch/v2.13.9/include/catch.hpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bd0b6821..268e2d6d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -10,7 +10,7 @@ project(test-ArduinoCore-API) include_directories(../api) include_directories(include) -include_directories(external/catch/v2.13.1/include) +include_directories(external/catch/v2.13.9/include) ########################################################################## diff --git a/test/external/catch/v2.13.9/include/catch.hpp b/test/external/catch/v2.13.9/include/catch.hpp new file mode 100644 index 00000000..d2a12427 --- /dev/null +++ b/test/external/catch/v2.13.9/include/catch.hpp @@ -0,0 +1,17970 @@ +/* + * Catch v2.13.9 + * Generated: 2022-04-12 22:37:23.260201 + * ---------------------------------------------------------- + * This file has been merged from multiple headers. Please don't edit it directly + * Copyright (c) 2022 Two Blue Cubes Ltd. All rights reserved. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + */ +#ifndef TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED +#define TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED +// start catch.hpp + + +#define CATCH_VERSION_MAJOR 2 +#define CATCH_VERSION_MINOR 13 +#define CATCH_VERSION_PATCH 9 + +#ifdef __clang__ +# pragma clang system_header +#elif defined __GNUC__ +# pragma GCC system_header +#endif + +// start catch_suppress_warnings.h + +#ifdef __clang__ +# ifdef __ICC // icpc defines the __clang__ macro +# pragma warning(push) +# pragma warning(disable: 161 1682) +# else // __ICC +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wpadded" +# pragma clang diagnostic ignored "-Wswitch-enum" +# pragma clang diagnostic ignored "-Wcovered-switch-default" +# endif +#elif defined __GNUC__ + // Because REQUIREs trigger GCC's -Wparentheses, and because still + // supported version of g++ have only buggy support for _Pragmas, + // Wparentheses have to be suppressed globally. +# pragma GCC diagnostic ignored "-Wparentheses" // See #674 for details + +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wunused-variable" +# pragma GCC diagnostic ignored "-Wpadded" +#endif +// end catch_suppress_warnings.h +#if defined(CATCH_CONFIG_MAIN) || defined(CATCH_CONFIG_RUNNER) +# define CATCH_IMPL +# define CATCH_CONFIG_ALL_PARTS +#endif + +// In the impl file, we want to have access to all parts of the headers +// Can also be used to sanely support PCHs +#if defined(CATCH_CONFIG_ALL_PARTS) +# define CATCH_CONFIG_EXTERNAL_INTERFACES +# if defined(CATCH_CONFIG_DISABLE_MATCHERS) +# undef CATCH_CONFIG_DISABLE_MATCHERS +# endif +# if !defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER) +# define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER +# endif +#endif + +#if !defined(CATCH_CONFIG_IMPL_ONLY) +// start catch_platform.h + +// See e.g.: +// https://opensource.apple.com/source/CarbonHeaders/CarbonHeaders-18.1/TargetConditionals.h.auto.html +#ifdef __APPLE__ +# include +# if (defined(TARGET_OS_OSX) && TARGET_OS_OSX == 1) || \ + (defined(TARGET_OS_MAC) && TARGET_OS_MAC == 1) +# define CATCH_PLATFORM_MAC +# elif (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE == 1) +# define CATCH_PLATFORM_IPHONE +# endif + +#elif defined(linux) || defined(__linux) || defined(__linux__) +# define CATCH_PLATFORM_LINUX + +#elif defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || defined(__MINGW32__) +# define CATCH_PLATFORM_WINDOWS +#endif + +// end catch_platform.h + +#ifdef CATCH_IMPL +# ifndef CLARA_CONFIG_MAIN +# define CLARA_CONFIG_MAIN_NOT_DEFINED +# define CLARA_CONFIG_MAIN +# endif +#endif + +// start catch_user_interfaces.h + +namespace Catch { + unsigned int rngSeed(); +} + +// end catch_user_interfaces.h +// start catch_tag_alias_autoregistrar.h + +// start catch_common.h + +// start catch_compiler_capabilities.h + +// Detect a number of compiler features - by compiler +// The following features are defined: +// +// CATCH_CONFIG_COUNTER : is the __COUNTER__ macro supported? +// CATCH_CONFIG_WINDOWS_SEH : is Windows SEH supported? +// CATCH_CONFIG_POSIX_SIGNALS : are POSIX signals supported? +// CATCH_CONFIG_DISABLE_EXCEPTIONS : Are exceptions enabled? +// **************** +// Note to maintainers: if new toggles are added please document them +// in configuration.md, too +// **************** + +// In general each macro has a _NO_ form +// (e.g. CATCH_CONFIG_NO_POSIX_SIGNALS) which disables the feature. +// Many features, at point of detection, define an _INTERNAL_ macro, so they +// can be combined, en-mass, with the _NO_ forms later. + +#ifdef __cplusplus + +# if (__cplusplus >= 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L) +# define CATCH_CPP14_OR_GREATER +# endif + +# if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) +# define CATCH_CPP17_OR_GREATER +# endif + +#endif + +// Only GCC compiler should be used in this block, so other compilers trying to +// mask themselves as GCC should be ignored. +#if defined(__GNUC__) && !defined(__clang__) && !defined(__ICC) && !defined(__CUDACC__) && !defined(__LCC__) +# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION _Pragma( "GCC diagnostic push" ) +# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION _Pragma( "GCC diagnostic pop" ) + +# define CATCH_INTERNAL_IGNORE_BUT_WARN(...) (void)__builtin_constant_p(__VA_ARGS__) + +#endif + +#if defined(__clang__) + +# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION _Pragma( "clang diagnostic push" ) +# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION _Pragma( "clang diagnostic pop" ) + +// As of this writing, IBM XL's implementation of __builtin_constant_p has a bug +// which results in calls to destructors being emitted for each temporary, +// without a matching initialization. In practice, this can result in something +// like `std::string::~string` being called on an uninitialized value. +// +// For example, this code will likely segfault under IBM XL: +// ``` +// REQUIRE(std::string("12") + "34" == "1234") +// ``` +// +// Therefore, `CATCH_INTERNAL_IGNORE_BUT_WARN` is not implemented. +# if !defined(__ibmxl__) && !defined(__CUDACC__) +# define CATCH_INTERNAL_IGNORE_BUT_WARN(...) (void)__builtin_constant_p(__VA_ARGS__) /* NOLINT(cppcoreguidelines-pro-type-vararg, hicpp-vararg) */ +# endif + +# define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + _Pragma( "clang diagnostic ignored \"-Wexit-time-destructors\"" ) \ + _Pragma( "clang diagnostic ignored \"-Wglobal-constructors\"") + +# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ + _Pragma( "clang diagnostic ignored \"-Wparentheses\"" ) + +# define CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS \ + _Pragma( "clang diagnostic ignored \"-Wunused-variable\"" ) + +# define CATCH_INTERNAL_SUPPRESS_ZERO_VARIADIC_WARNINGS \ + _Pragma( "clang diagnostic ignored \"-Wgnu-zero-variadic-macro-arguments\"" ) + +# define CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS \ + _Pragma( "clang diagnostic ignored \"-Wunused-template\"" ) + +#endif // __clang__ + +//////////////////////////////////////////////////////////////////////////////// +// Assume that non-Windows platforms support posix signals by default +#if !defined(CATCH_PLATFORM_WINDOWS) + #define CATCH_INTERNAL_CONFIG_POSIX_SIGNALS +#endif + +//////////////////////////////////////////////////////////////////////////////// +// We know some environments not to support full POSIX signals +#if defined(__CYGWIN__) || defined(__QNX__) || defined(__EMSCRIPTEN__) || defined(__DJGPP__) + #define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS +#endif + +#ifdef __OS400__ +# define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS +# define CATCH_CONFIG_COLOUR_NONE +#endif + +//////////////////////////////////////////////////////////////////////////////// +// Android somehow still does not support std::to_string +#if defined(__ANDROID__) +# define CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING +# define CATCH_INTERNAL_CONFIG_ANDROID_LOGWRITE +#endif + +//////////////////////////////////////////////////////////////////////////////// +// Not all Windows environments support SEH properly +#if defined(__MINGW32__) +# define CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH +#endif + +//////////////////////////////////////////////////////////////////////////////// +// PS4 +#if defined(__ORBIS__) +# define CATCH_INTERNAL_CONFIG_NO_NEW_CAPTURE +#endif + +//////////////////////////////////////////////////////////////////////////////// +// Cygwin +#ifdef __CYGWIN__ + +// Required for some versions of Cygwin to declare gettimeofday +// see: http://stackoverflow.com/questions/36901803/gettimeofday-not-declared-in-this-scope-cygwin +# define _BSD_SOURCE +// some versions of cygwin (most) do not support std::to_string. Use the libstd check. +// https://gcc.gnu.org/onlinedocs/gcc-4.8.2/libstdc++/api/a01053_source.html line 2812-2813 +# if !((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \ + && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)) + +# define CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING + +# endif +#endif // __CYGWIN__ + +//////////////////////////////////////////////////////////////////////////////// +// Visual C++ +#if defined(_MSC_VER) + +// Universal Windows platform does not support SEH +// Or console colours (or console at all...) +# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) +# define CATCH_CONFIG_COLOUR_NONE +# else +# define CATCH_INTERNAL_CONFIG_WINDOWS_SEH +# endif + +# if !defined(__clang__) // Handle Clang masquerading for msvc + +// MSVC traditional preprocessor needs some workaround for __VA_ARGS__ +// _MSVC_TRADITIONAL == 0 means new conformant preprocessor +// _MSVC_TRADITIONAL == 1 means old traditional non-conformant preprocessor +# if !defined(_MSVC_TRADITIONAL) || (defined(_MSVC_TRADITIONAL) && _MSVC_TRADITIONAL) +# define CATCH_INTERNAL_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +# endif // MSVC_TRADITIONAL + +// Only do this if we're not using clang on Windows, which uses `diagnostic push` & `diagnostic pop` +# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION __pragma( warning(push) ) +# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION __pragma( warning(pop) ) +# endif // __clang__ + +#endif // _MSC_VER + +#if defined(_REENTRANT) || defined(_MSC_VER) +// Enable async processing, as -pthread is specified or no additional linking is required +# define CATCH_INTERNAL_CONFIG_USE_ASYNC +#endif // _MSC_VER + +//////////////////////////////////////////////////////////////////////////////// +// Check if we are compiled with -fno-exceptions or equivalent +#if defined(__EXCEPTIONS) || defined(__cpp_exceptions) || defined(_CPPUNWIND) +# define CATCH_INTERNAL_CONFIG_EXCEPTIONS_ENABLED +#endif + +//////////////////////////////////////////////////////////////////////////////// +// DJGPP +#ifdef __DJGPP__ +# define CATCH_INTERNAL_CONFIG_NO_WCHAR +#endif // __DJGPP__ + +//////////////////////////////////////////////////////////////////////////////// +// Embarcadero C++Build +#if defined(__BORLANDC__) + #define CATCH_INTERNAL_CONFIG_POLYFILL_ISNAN +#endif + +//////////////////////////////////////////////////////////////////////////////// + +// Use of __COUNTER__ is suppressed during code analysis in +// CLion/AppCode 2017.2.x and former, because __COUNTER__ is not properly +// handled by it. +// Otherwise all supported compilers support COUNTER macro, +// but user still might want to turn it off +#if ( !defined(__JETBRAINS_IDE__) || __JETBRAINS_IDE__ >= 20170300L ) + #define CATCH_INTERNAL_CONFIG_COUNTER +#endif + +//////////////////////////////////////////////////////////////////////////////// + +// RTX is a special version of Windows that is real time. +// This means that it is detected as Windows, but does not provide +// the same set of capabilities as real Windows does. +#if defined(UNDER_RTSS) || defined(RTX64_BUILD) + #define CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH + #define CATCH_INTERNAL_CONFIG_NO_ASYNC + #define CATCH_CONFIG_COLOUR_NONE +#endif + +#if !defined(_GLIBCXX_USE_C99_MATH_TR1) +#define CATCH_INTERNAL_CONFIG_GLOBAL_NEXTAFTER +#endif + +// Various stdlib support checks that require __has_include +#if defined(__has_include) + // Check if string_view is available and usable + #if __has_include() && defined(CATCH_CPP17_OR_GREATER) + # define CATCH_INTERNAL_CONFIG_CPP17_STRING_VIEW + #endif + + // Check if optional is available and usable + # if __has_include() && defined(CATCH_CPP17_OR_GREATER) + # define CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL + # endif // __has_include() && defined(CATCH_CPP17_OR_GREATER) + + // Check if byte is available and usable + # if __has_include() && defined(CATCH_CPP17_OR_GREATER) + # include + # if defined(__cpp_lib_byte) && (__cpp_lib_byte > 0) + # define CATCH_INTERNAL_CONFIG_CPP17_BYTE + # endif + # endif // __has_include() && defined(CATCH_CPP17_OR_GREATER) + + // Check if variant is available and usable + # if __has_include() && defined(CATCH_CPP17_OR_GREATER) + # if defined(__clang__) && (__clang_major__ < 8) + // work around clang bug with libstdc++ https://bugs.llvm.org/show_bug.cgi?id=31852 + // fix should be in clang 8, workaround in libstdc++ 8.2 + # include + # if defined(__GLIBCXX__) && defined(_GLIBCXX_RELEASE) && (_GLIBCXX_RELEASE < 9) + # define CATCH_CONFIG_NO_CPP17_VARIANT + # else + # define CATCH_INTERNAL_CONFIG_CPP17_VARIANT + # endif // defined(__GLIBCXX__) && defined(_GLIBCXX_RELEASE) && (_GLIBCXX_RELEASE < 9) + # else + # define CATCH_INTERNAL_CONFIG_CPP17_VARIANT + # endif // defined(__clang__) && (__clang_major__ < 8) + # endif // __has_include() && defined(CATCH_CPP17_OR_GREATER) +#endif // defined(__has_include) + +#if defined(CATCH_INTERNAL_CONFIG_COUNTER) && !defined(CATCH_CONFIG_NO_COUNTER) && !defined(CATCH_CONFIG_COUNTER) +# define CATCH_CONFIG_COUNTER +#endif +#if defined(CATCH_INTERNAL_CONFIG_WINDOWS_SEH) && !defined(CATCH_CONFIG_NO_WINDOWS_SEH) && !defined(CATCH_CONFIG_WINDOWS_SEH) && !defined(CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH) +# define CATCH_CONFIG_WINDOWS_SEH +#endif +// This is set by default, because we assume that unix compilers are posix-signal-compatible by default. +#if defined(CATCH_INTERNAL_CONFIG_POSIX_SIGNALS) && !defined(CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_POSIX_SIGNALS) +# define CATCH_CONFIG_POSIX_SIGNALS +#endif +// This is set by default, because we assume that compilers with no wchar_t support are just rare exceptions. +#if !defined(CATCH_INTERNAL_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_WCHAR) +# define CATCH_CONFIG_WCHAR +#endif + +#if !defined(CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING) && !defined(CATCH_CONFIG_NO_CPP11_TO_STRING) && !defined(CATCH_CONFIG_CPP11_TO_STRING) +# define CATCH_CONFIG_CPP11_TO_STRING +#endif + +#if defined(CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL) && !defined(CATCH_CONFIG_NO_CPP17_OPTIONAL) && !defined(CATCH_CONFIG_CPP17_OPTIONAL) +# define CATCH_CONFIG_CPP17_OPTIONAL +#endif + +#if defined(CATCH_INTERNAL_CONFIG_CPP17_STRING_VIEW) && !defined(CATCH_CONFIG_NO_CPP17_STRING_VIEW) && !defined(CATCH_CONFIG_CPP17_STRING_VIEW) +# define CATCH_CONFIG_CPP17_STRING_VIEW +#endif + +#if defined(CATCH_INTERNAL_CONFIG_CPP17_VARIANT) && !defined(CATCH_CONFIG_NO_CPP17_VARIANT) && !defined(CATCH_CONFIG_CPP17_VARIANT) +# define CATCH_CONFIG_CPP17_VARIANT +#endif + +#if defined(CATCH_INTERNAL_CONFIG_CPP17_BYTE) && !defined(CATCH_CONFIG_NO_CPP17_BYTE) && !defined(CATCH_CONFIG_CPP17_BYTE) +# define CATCH_CONFIG_CPP17_BYTE +#endif + +#if defined(CATCH_CONFIG_EXPERIMENTAL_REDIRECT) +# define CATCH_INTERNAL_CONFIG_NEW_CAPTURE +#endif + +#if defined(CATCH_INTERNAL_CONFIG_NEW_CAPTURE) && !defined(CATCH_INTERNAL_CONFIG_NO_NEW_CAPTURE) && !defined(CATCH_CONFIG_NO_NEW_CAPTURE) && !defined(CATCH_CONFIG_NEW_CAPTURE) +# define CATCH_CONFIG_NEW_CAPTURE +#endif + +#if !defined(CATCH_INTERNAL_CONFIG_EXCEPTIONS_ENABLED) && !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) +# define CATCH_CONFIG_DISABLE_EXCEPTIONS +#endif + +#if defined(CATCH_INTERNAL_CONFIG_POLYFILL_ISNAN) && !defined(CATCH_CONFIG_NO_POLYFILL_ISNAN) && !defined(CATCH_CONFIG_POLYFILL_ISNAN) +# define CATCH_CONFIG_POLYFILL_ISNAN +#endif + +#if defined(CATCH_INTERNAL_CONFIG_USE_ASYNC) && !defined(CATCH_INTERNAL_CONFIG_NO_ASYNC) && !defined(CATCH_CONFIG_NO_USE_ASYNC) && !defined(CATCH_CONFIG_USE_ASYNC) +# define CATCH_CONFIG_USE_ASYNC +#endif + +#if defined(CATCH_INTERNAL_CONFIG_ANDROID_LOGWRITE) && !defined(CATCH_CONFIG_NO_ANDROID_LOGWRITE) && !defined(CATCH_CONFIG_ANDROID_LOGWRITE) +# define CATCH_CONFIG_ANDROID_LOGWRITE +#endif + +#if defined(CATCH_INTERNAL_CONFIG_GLOBAL_NEXTAFTER) && !defined(CATCH_CONFIG_NO_GLOBAL_NEXTAFTER) && !defined(CATCH_CONFIG_GLOBAL_NEXTAFTER) +# define CATCH_CONFIG_GLOBAL_NEXTAFTER +#endif + +// Even if we do not think the compiler has that warning, we still have +// to provide a macro that can be used by the code. +#if !defined(CATCH_INTERNAL_START_WARNINGS_SUPPRESSION) +# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION +#endif +#if !defined(CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION) +# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION +#endif +#if !defined(CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS) +# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS +#endif +#if !defined(CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS) +# define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS +#endif +#if !defined(CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS) +# define CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS +#endif +#if !defined(CATCH_INTERNAL_SUPPRESS_ZERO_VARIADIC_WARNINGS) +# define CATCH_INTERNAL_SUPPRESS_ZERO_VARIADIC_WARNINGS +#endif + +// The goal of this macro is to avoid evaluation of the arguments, but +// still have the compiler warn on problems inside... +#if !defined(CATCH_INTERNAL_IGNORE_BUT_WARN) +# define CATCH_INTERNAL_IGNORE_BUT_WARN(...) +#endif + +#if defined(__APPLE__) && defined(__apple_build_version__) && (__clang_major__ < 10) +# undef CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS +#elif defined(__clang__) && (__clang_major__ < 5) +# undef CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS +#endif + +#if !defined(CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS) +# define CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS +#endif + +#if defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) +#define CATCH_TRY if ((true)) +#define CATCH_CATCH_ALL if ((false)) +#define CATCH_CATCH_ANON(type) if ((false)) +#else +#define CATCH_TRY try +#define CATCH_CATCH_ALL catch (...) +#define CATCH_CATCH_ANON(type) catch (type) +#endif + +#if defined(CATCH_INTERNAL_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR) && !defined(CATCH_CONFIG_NO_TRADITIONAL_MSVC_PREPROCESSOR) && !defined(CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR) +#define CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +#endif + +// end catch_compiler_capabilities.h +#define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) name##line +#define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) +#ifdef CATCH_CONFIG_COUNTER +# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __COUNTER__ ) +#else +# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __LINE__ ) +#endif + +#include +#include +#include + +// We need a dummy global operator<< so we can bring it into Catch namespace later +struct Catch_global_namespace_dummy {}; +std::ostream& operator<<(std::ostream&, Catch_global_namespace_dummy); + +namespace Catch { + + struct CaseSensitive { enum Choice { + Yes, + No + }; }; + + class NonCopyable { + NonCopyable( NonCopyable const& ) = delete; + NonCopyable( NonCopyable && ) = delete; + NonCopyable& operator = ( NonCopyable const& ) = delete; + NonCopyable& operator = ( NonCopyable && ) = delete; + + protected: + NonCopyable(); + virtual ~NonCopyable(); + }; + + struct SourceLineInfo { + + SourceLineInfo() = delete; + SourceLineInfo( char const* _file, std::size_t _line ) noexcept + : file( _file ), + line( _line ) + {} + + SourceLineInfo( SourceLineInfo const& other ) = default; + SourceLineInfo& operator = ( SourceLineInfo const& ) = default; + SourceLineInfo( SourceLineInfo&& ) noexcept = default; + SourceLineInfo& operator = ( SourceLineInfo&& ) noexcept = default; + + bool empty() const noexcept { return file[0] == '\0'; } + bool operator == ( SourceLineInfo const& other ) const noexcept; + bool operator < ( SourceLineInfo const& other ) const noexcept; + + char const* file; + std::size_t line; + }; + + std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ); + + // Bring in operator<< from global namespace into Catch namespace + // This is necessary because the overload of operator<< above makes + // lookup stop at namespace Catch + using ::operator<<; + + // Use this in variadic streaming macros to allow + // >> +StreamEndStop + // as well as + // >> stuff +StreamEndStop + struct StreamEndStop { + std::string operator+() const; + }; + template + T const& operator + ( T const& value, StreamEndStop ) { + return value; + } +} + +#define CATCH_INTERNAL_LINEINFO \ + ::Catch::SourceLineInfo( __FILE__, static_cast( __LINE__ ) ) + +// end catch_common.h +namespace Catch { + + struct RegistrarForTagAliases { + RegistrarForTagAliases( char const* alias, char const* tag, SourceLineInfo const& lineInfo ); + }; + +} // end namespace Catch + +#define CATCH_REGISTER_TAG_ALIAS( alias, spec ) \ + CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::RegistrarForTagAliases INTERNAL_CATCH_UNIQUE_NAME( AutoRegisterTagAlias )( alias, spec, CATCH_INTERNAL_LINEINFO ); } \ + CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION + +// end catch_tag_alias_autoregistrar.h +// start catch_test_registry.h + +// start catch_interfaces_testcase.h + +#include + +namespace Catch { + + class TestSpec; + + struct ITestInvoker { + virtual void invoke () const = 0; + virtual ~ITestInvoker(); + }; + + class TestCase; + struct IConfig; + + struct ITestCaseRegistry { + virtual ~ITestCaseRegistry(); + virtual std::vector const& getAllTests() const = 0; + virtual std::vector const& getAllTestsSorted( IConfig const& config ) const = 0; + }; + + bool isThrowSafe( TestCase const& testCase, IConfig const& config ); + bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ); + std::vector filterTests( std::vector const& testCases, TestSpec const& testSpec, IConfig const& config ); + std::vector const& getAllTestCasesSorted( IConfig const& config ); + +} + +// end catch_interfaces_testcase.h +// start catch_stringref.h + +#include +#include +#include +#include + +namespace Catch { + + /// A non-owning string class (similar to the forthcoming std::string_view) + /// Note that, because a StringRef may be a substring of another string, + /// it may not be null terminated. + class StringRef { + public: + using size_type = std::size_t; + using const_iterator = const char*; + + private: + static constexpr char const* const s_empty = ""; + + char const* m_start = s_empty; + size_type m_size = 0; + + public: // construction + constexpr StringRef() noexcept = default; + + StringRef( char const* rawChars ) noexcept; + + constexpr StringRef( char const* rawChars, size_type size ) noexcept + : m_start( rawChars ), + m_size( size ) + {} + + StringRef( std::string const& stdString ) noexcept + : m_start( stdString.c_str() ), + m_size( stdString.size() ) + {} + + explicit operator std::string() const { + return std::string(m_start, m_size); + } + + public: // operators + auto operator == ( StringRef const& other ) const noexcept -> bool; + auto operator != (StringRef const& other) const noexcept -> bool { + return !(*this == other); + } + + auto operator[] ( size_type index ) const noexcept -> char { + assert(index < m_size); + return m_start[index]; + } + + public: // named queries + constexpr auto empty() const noexcept -> bool { + return m_size == 0; + } + constexpr auto size() const noexcept -> size_type { + return m_size; + } + + // Returns the current start pointer. If the StringRef is not + // null-terminated, throws std::domain_exception + auto c_str() const -> char const*; + + public: // substrings and searches + // Returns a substring of [start, start + length). + // If start + length > size(), then the substring is [start, size()). + // If start > size(), then the substring is empty. + auto substr( size_type start, size_type length ) const noexcept -> StringRef; + + // Returns the current start pointer. May not be null-terminated. + auto data() const noexcept -> char const*; + + constexpr auto isNullTerminated() const noexcept -> bool { + return m_start[m_size] == '\0'; + } + + public: // iterators + constexpr const_iterator begin() const { return m_start; } + constexpr const_iterator end() const { return m_start + m_size; } + }; + + auto operator += ( std::string& lhs, StringRef const& sr ) -> std::string&; + auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&; + + constexpr auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef { + return StringRef( rawChars, size ); + } +} // namespace Catch + +constexpr auto operator "" _catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef { + return Catch::StringRef( rawChars, size ); +} + +// end catch_stringref.h +// start catch_preprocessor.hpp + + +#define CATCH_RECURSION_LEVEL0(...) __VA_ARGS__ +#define CATCH_RECURSION_LEVEL1(...) CATCH_RECURSION_LEVEL0(CATCH_RECURSION_LEVEL0(CATCH_RECURSION_LEVEL0(__VA_ARGS__))) +#define CATCH_RECURSION_LEVEL2(...) CATCH_RECURSION_LEVEL1(CATCH_RECURSION_LEVEL1(CATCH_RECURSION_LEVEL1(__VA_ARGS__))) +#define CATCH_RECURSION_LEVEL3(...) CATCH_RECURSION_LEVEL2(CATCH_RECURSION_LEVEL2(CATCH_RECURSION_LEVEL2(__VA_ARGS__))) +#define CATCH_RECURSION_LEVEL4(...) CATCH_RECURSION_LEVEL3(CATCH_RECURSION_LEVEL3(CATCH_RECURSION_LEVEL3(__VA_ARGS__))) +#define CATCH_RECURSION_LEVEL5(...) CATCH_RECURSION_LEVEL4(CATCH_RECURSION_LEVEL4(CATCH_RECURSION_LEVEL4(__VA_ARGS__))) + +#ifdef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +#define INTERNAL_CATCH_EXPAND_VARGS(...) __VA_ARGS__ +// MSVC needs more evaluations +#define CATCH_RECURSION_LEVEL6(...) CATCH_RECURSION_LEVEL5(CATCH_RECURSION_LEVEL5(CATCH_RECURSION_LEVEL5(__VA_ARGS__))) +#define CATCH_RECURSE(...) CATCH_RECURSION_LEVEL6(CATCH_RECURSION_LEVEL6(__VA_ARGS__)) +#else +#define CATCH_RECURSE(...) CATCH_RECURSION_LEVEL5(__VA_ARGS__) +#endif + +#define CATCH_REC_END(...) +#define CATCH_REC_OUT + +#define CATCH_EMPTY() +#define CATCH_DEFER(id) id CATCH_EMPTY() + +#define CATCH_REC_GET_END2() 0, CATCH_REC_END +#define CATCH_REC_GET_END1(...) CATCH_REC_GET_END2 +#define CATCH_REC_GET_END(...) CATCH_REC_GET_END1 +#define CATCH_REC_NEXT0(test, next, ...) next CATCH_REC_OUT +#define CATCH_REC_NEXT1(test, next) CATCH_DEFER ( CATCH_REC_NEXT0 ) ( test, next, 0) +#define CATCH_REC_NEXT(test, next) CATCH_REC_NEXT1(CATCH_REC_GET_END test, next) + +#define CATCH_REC_LIST0(f, x, peek, ...) , f(x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1) ) ( f, peek, __VA_ARGS__ ) +#define CATCH_REC_LIST1(f, x, peek, ...) , f(x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST0) ) ( f, peek, __VA_ARGS__ ) +#define CATCH_REC_LIST2(f, x, peek, ...) f(x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1) ) ( f, peek, __VA_ARGS__ ) + +#define CATCH_REC_LIST0_UD(f, userdata, x, peek, ...) , f(userdata, x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1_UD) ) ( f, userdata, peek, __VA_ARGS__ ) +#define CATCH_REC_LIST1_UD(f, userdata, x, peek, ...) , f(userdata, x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST0_UD) ) ( f, userdata, peek, __VA_ARGS__ ) +#define CATCH_REC_LIST2_UD(f, userdata, x, peek, ...) f(userdata, x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1_UD) ) ( f, userdata, peek, __VA_ARGS__ ) + +// Applies the function macro `f` to each of the remaining parameters, inserts commas between the results, +// and passes userdata as the first parameter to each invocation, +// e.g. CATCH_REC_LIST_UD(f, x, a, b, c) evaluates to f(x, a), f(x, b), f(x, c) +#define CATCH_REC_LIST_UD(f, userdata, ...) CATCH_RECURSE(CATCH_REC_LIST2_UD(f, userdata, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) + +#define CATCH_REC_LIST(f, ...) CATCH_RECURSE(CATCH_REC_LIST2(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) + +#define INTERNAL_CATCH_EXPAND1(param) INTERNAL_CATCH_EXPAND2(param) +#define INTERNAL_CATCH_EXPAND2(...) INTERNAL_CATCH_NO## __VA_ARGS__ +#define INTERNAL_CATCH_DEF(...) INTERNAL_CATCH_DEF __VA_ARGS__ +#define INTERNAL_CATCH_NOINTERNAL_CATCH_DEF +#define INTERNAL_CATCH_STRINGIZE(...) INTERNAL_CATCH_STRINGIZE2(__VA_ARGS__) +#ifndef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +#define INTERNAL_CATCH_STRINGIZE2(...) #__VA_ARGS__ +#define INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS(param) INTERNAL_CATCH_STRINGIZE(INTERNAL_CATCH_REMOVE_PARENS(param)) +#else +// MSVC is adding extra space and needs another indirection to expand INTERNAL_CATCH_NOINTERNAL_CATCH_DEF +#define INTERNAL_CATCH_STRINGIZE2(...) INTERNAL_CATCH_STRINGIZE3(__VA_ARGS__) +#define INTERNAL_CATCH_STRINGIZE3(...) #__VA_ARGS__ +#define INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS(param) (INTERNAL_CATCH_STRINGIZE(INTERNAL_CATCH_REMOVE_PARENS(param)) + 1) +#endif + +#define INTERNAL_CATCH_MAKE_NAMESPACE2(...) ns_##__VA_ARGS__ +#define INTERNAL_CATCH_MAKE_NAMESPACE(name) INTERNAL_CATCH_MAKE_NAMESPACE2(name) + +#define INTERNAL_CATCH_REMOVE_PARENS(...) INTERNAL_CATCH_EXPAND1(INTERNAL_CATCH_DEF __VA_ARGS__) + +#ifndef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +#define INTERNAL_CATCH_MAKE_TYPE_LIST2(...) decltype(get_wrapper()) +#define INTERNAL_CATCH_MAKE_TYPE_LIST(...) INTERNAL_CATCH_MAKE_TYPE_LIST2(INTERNAL_CATCH_REMOVE_PARENS(__VA_ARGS__)) +#else +#define INTERNAL_CATCH_MAKE_TYPE_LIST2(...) INTERNAL_CATCH_EXPAND_VARGS(decltype(get_wrapper())) +#define INTERNAL_CATCH_MAKE_TYPE_LIST(...) INTERNAL_CATCH_EXPAND_VARGS(INTERNAL_CATCH_MAKE_TYPE_LIST2(INTERNAL_CATCH_REMOVE_PARENS(__VA_ARGS__))) +#endif + +#define INTERNAL_CATCH_MAKE_TYPE_LISTS_FROM_TYPES(...)\ + CATCH_REC_LIST(INTERNAL_CATCH_MAKE_TYPE_LIST,__VA_ARGS__) + +#define INTERNAL_CATCH_REMOVE_PARENS_1_ARG(_0) INTERNAL_CATCH_REMOVE_PARENS(_0) +#define INTERNAL_CATCH_REMOVE_PARENS_2_ARG(_0, _1) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_1_ARG(_1) +#define INTERNAL_CATCH_REMOVE_PARENS_3_ARG(_0, _1, _2) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_2_ARG(_1, _2) +#define INTERNAL_CATCH_REMOVE_PARENS_4_ARG(_0, _1, _2, _3) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_3_ARG(_1, _2, _3) +#define INTERNAL_CATCH_REMOVE_PARENS_5_ARG(_0, _1, _2, _3, _4) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_4_ARG(_1, _2, _3, _4) +#define INTERNAL_CATCH_REMOVE_PARENS_6_ARG(_0, _1, _2, _3, _4, _5) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_5_ARG(_1, _2, _3, _4, _5) +#define INTERNAL_CATCH_REMOVE_PARENS_7_ARG(_0, _1, _2, _3, _4, _5, _6) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_6_ARG(_1, _2, _3, _4, _5, _6) +#define INTERNAL_CATCH_REMOVE_PARENS_8_ARG(_0, _1, _2, _3, _4, _5, _6, _7) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_7_ARG(_1, _2, _3, _4, _5, _6, _7) +#define INTERNAL_CATCH_REMOVE_PARENS_9_ARG(_0, _1, _2, _3, _4, _5, _6, _7, _8) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_8_ARG(_1, _2, _3, _4, _5, _6, _7, _8) +#define INTERNAL_CATCH_REMOVE_PARENS_10_ARG(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_9_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9) +#define INTERNAL_CATCH_REMOVE_PARENS_11_ARG(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_10_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10) + +#define INTERNAL_CATCH_VA_NARGS_IMPL(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N + +#define INTERNAL_CATCH_TYPE_GEN\ + template struct TypeList {};\ + template\ + constexpr auto get_wrapper() noexcept -> TypeList { return {}; }\ + template class...> struct TemplateTypeList{};\ + template class...Cs>\ + constexpr auto get_wrapper() noexcept -> TemplateTypeList { return {}; }\ + template\ + struct append;\ + template\ + struct rewrap;\ + template class, typename...>\ + struct create;\ + template class, typename>\ + struct convert;\ + \ + template \ + struct append { using type = T; };\ + template< template class L1, typename...E1, template class L2, typename...E2, typename...Rest>\ + struct append, L2, Rest...> { using type = typename append, Rest...>::type; };\ + template< template class L1, typename...E1, typename...Rest>\ + struct append, TypeList, Rest...> { using type = L1; };\ + \ + template< template class Container, template class List, typename...elems>\ + struct rewrap, List> { using type = TypeList>; };\ + template< template class Container, template class List, class...Elems, typename...Elements>\ + struct rewrap, List, Elements...> { using type = typename append>, typename rewrap, Elements...>::type>::type; };\ + \ + template