mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-19 09:25:54 +08:00
添加依赖库
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
# ArduinoJson - https://arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2021
|
||||
# MIT License
|
||||
|
||||
add_executable(MiscTests
|
||||
arithmeticCompare.cpp
|
||||
conflicts.cpp
|
||||
deprecated.cpp
|
||||
FloatParts.cpp
|
||||
JsonString.cpp
|
||||
printable.cpp
|
||||
Readers.cpp
|
||||
StringAdapters.cpp
|
||||
StringWriter.cpp
|
||||
TypeTraits.cpp
|
||||
unsigned_char.cpp
|
||||
Utf16.cpp
|
||||
Utf8.cpp
|
||||
version.cpp
|
||||
)
|
||||
|
||||
set_target_properties(MiscTests PROPERTIES UNITY_BUILD OFF)
|
||||
|
||||
add_test(Misc MiscTests)
|
||||
|
||||
set_tests_properties(Misc
|
||||
PROPERTIES
|
||||
LABELS "Catch"
|
||||
)
|
||||
@@ -0,0 +1,44 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Numbers/FloatParts.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("FloatParts<double>") {
|
||||
SECTION("1.7976931348623157E+308") {
|
||||
FloatParts<double> parts(1.7976931348623157E+308);
|
||||
REQUIRE(parts.integral == 1);
|
||||
REQUIRE(parts.decimal == 797693135);
|
||||
REQUIRE(parts.decimalPlaces == 9);
|
||||
REQUIRE(parts.exponent == 308);
|
||||
}
|
||||
|
||||
SECTION("4.94065645841247e-324") {
|
||||
FloatParts<double> parts(4.94065645841247e-324);
|
||||
REQUIRE(parts.integral == 4);
|
||||
REQUIRE(parts.decimal == 940656458);
|
||||
REQUIRE(parts.decimalPlaces == 9);
|
||||
REQUIRE(parts.exponent == -324);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("FloatParts<float>") {
|
||||
SECTION("3.4E+38") {
|
||||
FloatParts<float> parts(3.4E+38f);
|
||||
REQUIRE(parts.integral == 3);
|
||||
REQUIRE(parts.decimal == 4);
|
||||
REQUIRE(parts.decimalPlaces == 1);
|
||||
REQUIRE(parts.exponent == 38);
|
||||
}
|
||||
|
||||
SECTION("1.17549435e−38") {
|
||||
FloatParts<float> parts(1.17549435e-38f);
|
||||
REQUIRE(parts.integral == 1);
|
||||
REQUIRE(parts.decimal == 175494);
|
||||
REQUIRE(parts.decimalPlaces == 6);
|
||||
REQUIRE(parts.exponent == -38);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonString") {
|
||||
SECTION("Default constructor creates a null JsonString") {
|
||||
JsonString s;
|
||||
|
||||
CHECK(s.isNull() == true);
|
||||
CHECK(s.c_str() == 0);
|
||||
CHECK(s.isStatic() == true);
|
||||
}
|
||||
|
||||
SECTION("Compare null with null") {
|
||||
JsonString a, b;
|
||||
|
||||
CHECK(a == b);
|
||||
CHECK_FALSE(a != b);
|
||||
}
|
||||
|
||||
SECTION("Compare null with non-null") {
|
||||
JsonString a(0), b("hello");
|
||||
|
||||
CHECK_FALSE(a == b);
|
||||
CHECK(a != b);
|
||||
}
|
||||
|
||||
SECTION("Compare non-null with null") {
|
||||
JsonString a("hello"), b(0);
|
||||
|
||||
CHECK_FALSE(a == b);
|
||||
CHECK(a != b);
|
||||
}
|
||||
|
||||
SECTION("Compare different strings") {
|
||||
JsonString a("hello"), b("world");
|
||||
|
||||
CHECK_FALSE(a == b);
|
||||
CHECK(a != b);
|
||||
}
|
||||
|
||||
SECTION("Compare identical by pointer") {
|
||||
JsonString a("hello"), b("hello");
|
||||
|
||||
CHECK(a == b);
|
||||
CHECK_FALSE(a != b);
|
||||
}
|
||||
|
||||
SECTION("Compare identical by value") {
|
||||
char s1[] = "hello";
|
||||
char s2[] = "hello";
|
||||
JsonString a(s1), b(s2);
|
||||
|
||||
CHECK(a == b);
|
||||
CHECK_FALSE(a != b);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoJson.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("Reader<std::istringstream>") {
|
||||
SECTION("read()") {
|
||||
std::istringstream src("\x01\xFF");
|
||||
Reader<std::istringstream> reader(src);
|
||||
|
||||
REQUIRE(reader.read() == 0x01);
|
||||
REQUIRE(reader.read() == 0xFF);
|
||||
REQUIRE(reader.read() == -1);
|
||||
}
|
||||
|
||||
SECTION("readBytes() all at once") {
|
||||
std::istringstream src("ABC");
|
||||
Reader<std::istringstream> reader(src);
|
||||
|
||||
char buffer[8] = "abcd";
|
||||
REQUIRE(reader.readBytes(buffer, 4) == 3);
|
||||
|
||||
REQUIRE(buffer[0] == 'A');
|
||||
REQUIRE(buffer[1] == 'B');
|
||||
REQUIRE(buffer[2] == 'C');
|
||||
REQUIRE(buffer[3] == 'd');
|
||||
}
|
||||
|
||||
SECTION("readBytes() in two parts") {
|
||||
std::istringstream src("ABCDEF");
|
||||
Reader<std::istringstream> reader(src);
|
||||
|
||||
char buffer[12] = "abcdefg";
|
||||
REQUIRE(reader.readBytes(buffer, 4) == 4);
|
||||
REQUIRE(reader.readBytes(buffer + 4, 4) == 2);
|
||||
|
||||
REQUIRE(buffer[0] == 'A');
|
||||
REQUIRE(buffer[1] == 'B');
|
||||
REQUIRE(buffer[2] == 'C');
|
||||
REQUIRE(buffer[3] == 'D');
|
||||
REQUIRE(buffer[4] == 'E');
|
||||
REQUIRE(buffer[5] == 'F');
|
||||
REQUIRE(buffer[6] == 'g');
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("BoundedReader<const char*>") {
|
||||
SECTION("read") {
|
||||
BoundedReader<const char*> reader("\x01\xFF", 2);
|
||||
REQUIRE(reader.read() == 0x01);
|
||||
REQUIRE(reader.read() == 0xFF);
|
||||
REQUIRE(reader.read() == -1);
|
||||
REQUIRE(reader.read() == -1);
|
||||
}
|
||||
|
||||
SECTION("readBytes() all at once") {
|
||||
BoundedReader<const char*> reader("ABCD", 3);
|
||||
|
||||
char buffer[8] = "abcd";
|
||||
REQUIRE(reader.readBytes(buffer, 4) == 3);
|
||||
|
||||
REQUIRE(buffer[0] == 'A');
|
||||
REQUIRE(buffer[1] == 'B');
|
||||
REQUIRE(buffer[2] == 'C');
|
||||
REQUIRE(buffer[3] == 'd');
|
||||
}
|
||||
|
||||
SECTION("readBytes() in two parts") {
|
||||
BoundedReader<const char*> reader("ABCDEF", 6);
|
||||
|
||||
char buffer[8] = "abcdefg";
|
||||
REQUIRE(reader.readBytes(buffer, 4) == 4);
|
||||
REQUIRE(reader.readBytes(buffer + 4, 4) == 2);
|
||||
|
||||
REQUIRE(buffer[0] == 'A');
|
||||
REQUIRE(buffer[1] == 'B');
|
||||
REQUIRE(buffer[2] == 'C');
|
||||
REQUIRE(buffer[3] == 'D');
|
||||
REQUIRE(buffer[4] == 'E');
|
||||
REQUIRE(buffer[5] == 'F');
|
||||
REQUIRE(buffer[6] == 'g');
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Reader<const char*>") {
|
||||
SECTION("read()") {
|
||||
Reader<const char*> reader("\x01\xFF\x00\x12");
|
||||
REQUIRE(reader.read() == 0x01);
|
||||
REQUIRE(reader.read() == 0xFF);
|
||||
REQUIRE(reader.read() == 0);
|
||||
REQUIRE(reader.read() == 0x12);
|
||||
}
|
||||
|
||||
SECTION("readBytes() all at once") {
|
||||
Reader<const char*> reader("ABCD");
|
||||
|
||||
char buffer[8] = "abcd";
|
||||
REQUIRE(reader.readBytes(buffer, 3) == 3);
|
||||
|
||||
REQUIRE(buffer[0] == 'A');
|
||||
REQUIRE(buffer[1] == 'B');
|
||||
REQUIRE(buffer[2] == 'C');
|
||||
REQUIRE(buffer[3] == 'd');
|
||||
}
|
||||
|
||||
SECTION("readBytes() in two parts") {
|
||||
Reader<const char*> reader("ABCDEF");
|
||||
|
||||
char buffer[8] = "abcdefg";
|
||||
REQUIRE(reader.readBytes(buffer, 4) == 4);
|
||||
REQUIRE(reader.readBytes(buffer + 4, 2) == 2);
|
||||
|
||||
REQUIRE(buffer[0] == 'A');
|
||||
REQUIRE(buffer[1] == 'B');
|
||||
REQUIRE(buffer[2] == 'C');
|
||||
REQUIRE(buffer[3] == 'D');
|
||||
REQUIRE(buffer[4] == 'E');
|
||||
REQUIRE(buffer[5] == 'F');
|
||||
REQUIRE(buffer[6] == 'g');
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("IteratorReader") {
|
||||
SECTION("read()") {
|
||||
std::string src("\x01\xFF");
|
||||
IteratorReader<std::string::const_iterator> reader(src.begin(), src.end());
|
||||
|
||||
REQUIRE(reader.read() == 0x01);
|
||||
REQUIRE(reader.read() == 0xFF);
|
||||
REQUIRE(reader.read() == -1);
|
||||
}
|
||||
|
||||
SECTION("readBytes() all at once") {
|
||||
std::string src("ABC");
|
||||
IteratorReader<std::string::const_iterator> reader(src.begin(), src.end());
|
||||
|
||||
char buffer[8] = "abcd";
|
||||
REQUIRE(reader.readBytes(buffer, 4) == 3);
|
||||
|
||||
REQUIRE(buffer[0] == 'A');
|
||||
REQUIRE(buffer[1] == 'B');
|
||||
REQUIRE(buffer[2] == 'C');
|
||||
REQUIRE(buffer[3] == 'd');
|
||||
}
|
||||
|
||||
SECTION("readBytes() in two parts") {
|
||||
std::string src("ABCDEF");
|
||||
IteratorReader<std::string::const_iterator> reader(src.begin(), src.end());
|
||||
|
||||
char buffer[12] = "abcdefg";
|
||||
REQUIRE(reader.readBytes(buffer, 4) == 4);
|
||||
REQUIRE(reader.readBytes(buffer + 4, 4) == 2);
|
||||
|
||||
REQUIRE(buffer[0] == 'A');
|
||||
REQUIRE(buffer[1] == 'B');
|
||||
REQUIRE(buffer[2] == 'C');
|
||||
REQUIRE(buffer[3] == 'D');
|
||||
REQUIRE(buffer[4] == 'E');
|
||||
REQUIRE(buffer[5] == 'F');
|
||||
REQUIRE(buffer[6] == 'g');
|
||||
}
|
||||
}
|
||||
|
||||
class StreamStub : public Stream {
|
||||
public:
|
||||
StreamStub(const char* s) : _stream(s) {}
|
||||
|
||||
int read() {
|
||||
return _stream.get();
|
||||
}
|
||||
|
||||
size_t readBytes(char* buffer, size_t length) {
|
||||
_stream.read(buffer, static_cast<std::streamsize>(length));
|
||||
return static_cast<size_t>(_stream.gcount());
|
||||
}
|
||||
|
||||
private:
|
||||
std::istringstream _stream;
|
||||
};
|
||||
|
||||
TEST_CASE("Reader<Stream>") {
|
||||
SECTION("read()") {
|
||||
StreamStub src("\x01\xFF");
|
||||
Reader<StreamStub> reader(src);
|
||||
|
||||
REQUIRE(reader.read() == 0x01);
|
||||
REQUIRE(reader.read() == 0xFF);
|
||||
REQUIRE(reader.read() == -1);
|
||||
}
|
||||
|
||||
SECTION("readBytes() all at once") {
|
||||
StreamStub src("ABC");
|
||||
Reader<StreamStub> reader(src);
|
||||
|
||||
char buffer[8] = "abcd";
|
||||
REQUIRE(reader.readBytes(buffer, 4) == 3);
|
||||
|
||||
REQUIRE(buffer[0] == 'A');
|
||||
REQUIRE(buffer[1] == 'B');
|
||||
REQUIRE(buffer[2] == 'C');
|
||||
REQUIRE(buffer[3] == 'd');
|
||||
}
|
||||
|
||||
SECTION("readBytes() in two parts") {
|
||||
StreamStub src("ABCDEF");
|
||||
Reader<StreamStub> reader(src);
|
||||
|
||||
char buffer[12] = "abcdefg";
|
||||
REQUIRE(reader.readBytes(buffer, 4) == 4);
|
||||
REQUIRE(reader.readBytes(buffer + 4, 4) == 2);
|
||||
|
||||
REQUIRE(buffer[0] == 'A');
|
||||
REQUIRE(buffer[1] == 'B');
|
||||
REQUIRE(buffer[2] == 'C');
|
||||
REQUIRE(buffer[3] == 'D');
|
||||
REQUIRE(buffer[4] == 'E');
|
||||
REQUIRE(buffer[5] == 'F');
|
||||
REQUIRE(buffer[6] == 'g');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#include "custom_string.hpp"
|
||||
#include "progmem_emulation.hpp"
|
||||
#include "weird_strcmp.hpp"
|
||||
|
||||
#include <ArduinoJson/Strings/ArduinoStringAdapter.hpp>
|
||||
#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp>
|
||||
#include <ArduinoJson/Strings/FlashStringAdapter.hpp>
|
||||
#include <ArduinoJson/Strings/SizedRamStringAdapter.hpp>
|
||||
#include <ArduinoJson/Strings/StdStringAdapter.hpp>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("ConstRamStringAdapter") {
|
||||
SECTION("null") {
|
||||
ConstRamStringAdapter adapter(NULL);
|
||||
|
||||
CHECK(adapter.compare("bravo") < 0);
|
||||
CHECK(adapter.compare(NULL) == 0);
|
||||
|
||||
CHECK(adapter.equals(NULL));
|
||||
CHECK_FALSE(adapter.equals("charlie"));
|
||||
|
||||
CHECK(adapter.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("non-null") {
|
||||
ConstRamStringAdapter adapter("bravo");
|
||||
|
||||
CHECK(adapter.compare(NULL) > 0);
|
||||
CHECK(adapter.compare("alpha") > 0);
|
||||
CHECK(adapter.compare("bravo") == 0);
|
||||
CHECK(adapter.compare("charlie") < 0);
|
||||
|
||||
CHECK(adapter.equals("bravo"));
|
||||
CHECK_FALSE(adapter.equals("charlie"));
|
||||
|
||||
CHECK(adapter.size() == 5);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("SizedRamStringAdapter") {
|
||||
SECTION("null") {
|
||||
SizedRamStringAdapter adapter(NULL, 10);
|
||||
|
||||
CHECK(adapter.compare("bravo") < 0);
|
||||
CHECK(adapter.compare(NULL) == 0);
|
||||
|
||||
CHECK(adapter.equals(NULL));
|
||||
CHECK_FALSE(adapter.equals("charlie"));
|
||||
|
||||
CHECK(adapter.size() == 10);
|
||||
}
|
||||
|
||||
SECTION("non-null") {
|
||||
SizedRamStringAdapter adapter("bravo", 5);
|
||||
|
||||
CHECK(adapter.compare(NULL) > 0);
|
||||
CHECK(adapter.compare("alpha") > 0);
|
||||
CHECK(adapter.compare("bravo") == 0);
|
||||
CHECK(adapter.compare("charlie") < 0);
|
||||
|
||||
CHECK(adapter.equals("bravo"));
|
||||
CHECK_FALSE(adapter.equals("charlie"));
|
||||
|
||||
CHECK(adapter.size() == 5);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("FlashStringAdapter") {
|
||||
SECTION("null") {
|
||||
FlashStringAdapter adapter(NULL);
|
||||
|
||||
CHECK(adapter.compare("bravo") < 0);
|
||||
CHECK(adapter.compare(NULL) == 0);
|
||||
|
||||
CHECK(adapter.equals(NULL));
|
||||
CHECK_FALSE(adapter.equals("charlie"));
|
||||
|
||||
CHECK(adapter.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("non-null") {
|
||||
FlashStringAdapter adapter = adaptString(F("bravo"));
|
||||
|
||||
CHECK(adapter.compare(NULL) > 0);
|
||||
CHECK(adapter.compare("alpha") > 0);
|
||||
CHECK(adapter.compare("bravo") == 0);
|
||||
CHECK(adapter.compare("charlie") < 0);
|
||||
|
||||
CHECK(adapter.equals("bravo"));
|
||||
CHECK_FALSE(adapter.equals("charlie"));
|
||||
|
||||
CHECK(adapter.size() == 5);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("std::string") {
|
||||
std::string str("bravo");
|
||||
StdStringAdapter<std::string> adapter = adaptString(str);
|
||||
|
||||
CHECK(adapter.compare(NULL) > 0);
|
||||
CHECK(adapter.compare("alpha") > 0);
|
||||
CHECK(adapter.compare("bravo") == 0);
|
||||
CHECK(adapter.compare("charlie") < 0);
|
||||
|
||||
CHECK(adapter.equals("bravo"));
|
||||
CHECK_FALSE(adapter.equals("charlie"));
|
||||
|
||||
CHECK(adapter.size() == 5);
|
||||
}
|
||||
|
||||
TEST_CASE("Arduino String") {
|
||||
::String str("bravo");
|
||||
ArduinoStringAdapter adapter = adaptString(str);
|
||||
|
||||
CHECK(adapter.compare(NULL) > 0);
|
||||
CHECK(adapter.compare("alpha") > 0);
|
||||
CHECK(adapter.compare("bravo") == 0);
|
||||
CHECK(adapter.compare("charlie") < 0);
|
||||
|
||||
CHECK(adapter.equals("bravo"));
|
||||
CHECK_FALSE(adapter.equals("charlie"));
|
||||
|
||||
CHECK(adapter.size() == 5);
|
||||
}
|
||||
|
||||
TEST_CASE("custom_string") {
|
||||
custom_string str("bravo");
|
||||
StdStringAdapter<custom_string> adapter = adaptString(str);
|
||||
|
||||
CHECK(adapter.compare(NULL) > 0);
|
||||
CHECK(adapter.compare("alpha") > 0);
|
||||
CHECK(adapter.compare("bravo") == 0);
|
||||
CHECK(adapter.compare("charlie") < 0);
|
||||
|
||||
CHECK(adapter.equals("bravo"));
|
||||
CHECK_FALSE(adapter.equals("charlie"));
|
||||
|
||||
CHECK(adapter.size() == 5);
|
||||
}
|
||||
|
||||
TEST_CASE("IsString<T>") {
|
||||
SECTION("std::string") {
|
||||
CHECK(IsString<std::string>::value == true);
|
||||
}
|
||||
|
||||
SECTION("basic_string<wchar_t>") {
|
||||
CHECK(IsString<std::basic_string<wchar_t> >::value == false);
|
||||
}
|
||||
|
||||
SECTION("custom_string") {
|
||||
CHECK(IsString<custom_string>::value == true);
|
||||
}
|
||||
|
||||
SECTION("const __FlashStringHelper*") {
|
||||
CHECK(IsString<const __FlashStringHelper*>::value == true);
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
CHECK(IsString<const char*>::value == true);
|
||||
}
|
||||
|
||||
SECTION("const char[]") {
|
||||
CHECK(IsString<const char[8]>::value == true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
|
||||
#define ARDUINOJSON_STRING_BUFFER_SIZE 5
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
#include "custom_string.hpp"
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
template <typename StringWriter>
|
||||
static size_t print(StringWriter& sb, const char* s) {
|
||||
return sb.write(reinterpret_cast<const uint8_t*>(s), strlen(s));
|
||||
}
|
||||
|
||||
template <typename StringWriter, typename String>
|
||||
void common_tests(StringWriter& sb, const String& output) {
|
||||
SECTION("InitialState") {
|
||||
REQUIRE(std::string("") == output);
|
||||
}
|
||||
|
||||
SECTION("EmptyString") {
|
||||
REQUIRE(0 == print(sb, ""));
|
||||
REQUIRE(std::string("") == output);
|
||||
}
|
||||
|
||||
SECTION("OneString") {
|
||||
REQUIRE(4 == print(sb, "ABCD"));
|
||||
REQUIRE(std::string("ABCD") == output);
|
||||
}
|
||||
|
||||
SECTION("TwoStrings") {
|
||||
REQUIRE(4 == print(sb, "ABCD"));
|
||||
REQUIRE(4 == print(sb, "EFGH"));
|
||||
REQUIRE(std::string("ABCDEFGH") == output);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("StaticStringWriter") {
|
||||
char output[20] = {0};
|
||||
StaticStringWriter sb(output, sizeof(output));
|
||||
|
||||
common_tests(sb, static_cast<const char*>(output));
|
||||
|
||||
SECTION("OverCapacity") {
|
||||
REQUIRE(20 == print(sb, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
|
||||
REQUIRE(0 == print(sb, "ABC"));
|
||||
REQUIRE("ABCDEFGHIJKLMNOPQRST" == std::string(output, 20));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Writer<std::string>") {
|
||||
std::string output;
|
||||
Writer<std::string> sb(output);
|
||||
common_tests(sb, output);
|
||||
}
|
||||
|
||||
TEST_CASE("Writer<String>") {
|
||||
::String output;
|
||||
Writer< ::String> sb(output);
|
||||
|
||||
common_tests(sb, output);
|
||||
|
||||
SECTION("Writes characters to temporary buffer") {
|
||||
// accumulate in buffer
|
||||
sb.write('a');
|
||||
sb.write('b');
|
||||
sb.write('c');
|
||||
REQUIRE(output == "");
|
||||
|
||||
// flush when full
|
||||
sb.write('d');
|
||||
REQUIRE(output == "abcd");
|
||||
|
||||
// flush on destruction
|
||||
sb.write('e');
|
||||
sb.~Writer();
|
||||
REQUIRE(output == "abcde");
|
||||
}
|
||||
|
||||
SECTION("Writes strings to temporary buffer") {
|
||||
// accumulate in buffer
|
||||
print(sb, "abc");
|
||||
REQUIRE(output == "");
|
||||
|
||||
// flush when full, and continue to accumulate
|
||||
print(sb, "de");
|
||||
REQUIRE(output == "abcd");
|
||||
|
||||
// flush on destruction
|
||||
sb.~Writer();
|
||||
REQUIRE(output == "abcde");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Writer<custom_string>") {
|
||||
custom_string output;
|
||||
Writer<custom_string> sb(output);
|
||||
|
||||
REQUIRE(4 == print(sb, "ABCD"));
|
||||
REQUIRE("ABCD" == output);
|
||||
}
|
||||
|
||||
TEST_CASE("IsWriteableString") {
|
||||
SECTION("std::string") {
|
||||
REQUIRE(IsWriteableString<std::string>::value == true);
|
||||
}
|
||||
|
||||
SECTION("custom_string") {
|
||||
REQUIRE(IsWriteableString<custom_string>::value == true);
|
||||
}
|
||||
|
||||
SECTION("basic_string<wchar_t>") {
|
||||
REQUIRE(IsWriteableString<std::basic_string<wchar_t> >::value == false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
class EmptyClass {};
|
||||
enum EmptyEnum {};
|
||||
|
||||
TEST_CASE("Polyfills/type_traits") {
|
||||
SECTION("is_base_of") {
|
||||
REQUIRE_FALSE(
|
||||
static_cast<bool>(is_base_of<std::istream, std::ostringstream>::value));
|
||||
REQUIRE(
|
||||
static_cast<bool>(is_base_of<std::istream, std::istringstream>::value));
|
||||
}
|
||||
|
||||
SECTION("is_array") {
|
||||
REQUIRE_FALSE((is_array<const char*>::value));
|
||||
REQUIRE((is_array<const char[]>::value));
|
||||
REQUIRE((is_array<const char[10]>::value));
|
||||
}
|
||||
|
||||
SECTION("is_const") {
|
||||
CHECK(is_const<char>::value == false);
|
||||
CHECK(is_const<const char>::value == true);
|
||||
}
|
||||
|
||||
SECTION("is_integral") {
|
||||
CHECK(is_integral<double>::value == false);
|
||||
CHECK(is_integral<float>::value == false);
|
||||
|
||||
CHECK(is_integral<bool>::value == true);
|
||||
CHECK(is_integral<char>::value == true);
|
||||
CHECK(is_integral<signed char>::value == true);
|
||||
CHECK(is_integral<signed int>::value == true);
|
||||
CHECK(is_integral<signed long>::value == true);
|
||||
CHECK(is_integral<signed short>::value == true);
|
||||
CHECK(is_integral<unsigned char>::value == true);
|
||||
CHECK(is_integral<unsigned int>::value == true);
|
||||
CHECK(is_integral<unsigned long>::value == true);
|
||||
CHECK(is_integral<unsigned short>::value == true);
|
||||
|
||||
CHECK(is_integral<UInt>::value == true);
|
||||
}
|
||||
|
||||
SECTION("is_signed") {
|
||||
CHECK(is_signed<char>::value == true);
|
||||
CHECK(is_signed<signed char>::value == true);
|
||||
CHECK(is_signed<signed int>::value == true);
|
||||
CHECK(is_signed<signed short>::value == true);
|
||||
CHECK(is_signed<signed long>::value == true);
|
||||
CHECK(is_signed<float>::value == true);
|
||||
CHECK(is_signed<double>::value == true);
|
||||
CHECK(is_signed<bool>::value == false);
|
||||
}
|
||||
|
||||
SECTION("is_unsigned") {
|
||||
CHECK(is_unsigned<unsigned char>::value == true);
|
||||
CHECK(is_unsigned<unsigned int>::value == true);
|
||||
CHECK(is_unsigned<unsigned short>::value == true);
|
||||
CHECK(is_unsigned<unsigned long>::value == true);
|
||||
CHECK(is_unsigned<bool>::value == true);
|
||||
CHECK(is_unsigned<char>::value == false);
|
||||
CHECK(is_unsigned<float>::value == false);
|
||||
CHECK(is_unsigned<double>::value == false);
|
||||
}
|
||||
|
||||
SECTION("is_convertible") {
|
||||
CHECK((is_convertible<short, int>::value == true));
|
||||
CHECK((is_convertible<int, int>::value == true));
|
||||
CHECK((is_convertible<EmptyEnum, int>::value == true));
|
||||
CHECK((is_convertible<int*, int>::value == false));
|
||||
CHECK((is_convertible<EmptyClass, int>::value == false));
|
||||
}
|
||||
|
||||
SECTION("is_class") {
|
||||
CHECK((is_class<int>::value == false));
|
||||
CHECK((is_class<EmptyEnum>::value == false));
|
||||
CHECK((is_class<int*>::value == false));
|
||||
CHECK((is_class<EmptyClass>::value == true));
|
||||
}
|
||||
|
||||
SECTION("is_enum") {
|
||||
CHECK(is_enum<int>::value == false);
|
||||
CHECK(is_enum<EmptyEnum>::value == true);
|
||||
CHECK(is_enum<int*>::value == false);
|
||||
CHECK(is_enum<EmptyClass>::value == false);
|
||||
CHECK(is_enum<bool>::value == false);
|
||||
CHECK(is_enum<double>::value == false);
|
||||
}
|
||||
|
||||
SECTION("IsVisitable") {
|
||||
CHECK(IsVisitable<DeserializationError>::value == false);
|
||||
CHECK(IsVisitable<JsonPair>::value == false);
|
||||
CHECK(IsVisitable<VariantRef>::value == true);
|
||||
CHECK(IsVisitable<VariantConstRef>::value == true);
|
||||
CHECK(IsVisitable<ArrayRef>::value == true);
|
||||
CHECK(IsVisitable<ElementProxy<ArrayRef> >::value == true);
|
||||
CHECK(IsVisitable<ArrayConstRef>::value == true);
|
||||
CHECK(IsVisitable<ObjectRef>::value == true);
|
||||
CHECK((IsVisitable<MemberProxy<ObjectRef, const char*> >::value == true));
|
||||
CHECK(IsVisitable<ObjectConstRef>::value == true);
|
||||
CHECK(IsVisitable<DynamicJsonDocument>::value == true);
|
||||
CHECK(IsVisitable<StaticJsonDocument<10> >::value == true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Json/Utf16.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
static void testUtf16Codepoint(uint16_t codeunit, uint32_t expectedCodepoint) {
|
||||
Utf16::Codepoint cp;
|
||||
REQUIRE(cp.append(codeunit) == true);
|
||||
REQUIRE(cp.value() == expectedCodepoint);
|
||||
}
|
||||
|
||||
static void testUtf16Codepoint(uint16_t codeunit1, uint16_t codeunit2,
|
||||
uint32_t expectedCodepoint) {
|
||||
Utf16::Codepoint cp;
|
||||
REQUIRE(cp.append(codeunit1) == false);
|
||||
REQUIRE(cp.append(codeunit2) == true);
|
||||
REQUIRE(cp.value() == expectedCodepoint);
|
||||
}
|
||||
|
||||
TEST_CASE("Utf16::Codepoint()") {
|
||||
SECTION("U+0000") {
|
||||
testUtf16Codepoint(0x0000, 0x000000);
|
||||
}
|
||||
|
||||
SECTION("U+0001") {
|
||||
testUtf16Codepoint(0x0001, 0x000001);
|
||||
}
|
||||
|
||||
SECTION("U+D7FF") {
|
||||
testUtf16Codepoint(0xD7FF, 0x00D7FF);
|
||||
}
|
||||
|
||||
SECTION("U+E000") {
|
||||
testUtf16Codepoint(0xE000, 0x00E000);
|
||||
}
|
||||
|
||||
SECTION("U+FFFF") {
|
||||
testUtf16Codepoint(0xFFFF, 0x00FFFF);
|
||||
}
|
||||
|
||||
SECTION("U+010000") {
|
||||
testUtf16Codepoint(0xD800, 0xDC00, 0x010000);
|
||||
}
|
||||
|
||||
SECTION("U+010001") {
|
||||
testUtf16Codepoint(0xD800, 0xDC01, 0x010001);
|
||||
}
|
||||
|
||||
SECTION("U+0103FF") {
|
||||
testUtf16Codepoint(0xD800, 0xDFFF, 0x0103FF);
|
||||
}
|
||||
|
||||
SECTION("U+010400") {
|
||||
testUtf16Codepoint(0xD801, 0xDC00, 0x010400);
|
||||
}
|
||||
|
||||
SECTION("U+010400") {
|
||||
testUtf16Codepoint(0xDBFF, 0xDC00, 0x10FC00);
|
||||
}
|
||||
|
||||
SECTION("U+10FFFF") {
|
||||
testUtf16Codepoint(0xDBFF, 0xDFFF, 0x10FFFF);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
static void testCodepoint(uint32_t codepoint, std::string expected) {
|
||||
char buffer[4096];
|
||||
MemoryPool pool(buffer, 4096);
|
||||
StringCopier str(pool);
|
||||
str.startString();
|
||||
|
||||
CAPTURE(codepoint);
|
||||
Utf8::encodeCodepoint(codepoint, str);
|
||||
|
||||
str.append('\0');
|
||||
REQUIRE(str.c_str() == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("Utf8::encodeCodepoint()") {
|
||||
SECTION("U+0000") {
|
||||
testCodepoint(0x0000, "");
|
||||
}
|
||||
|
||||
SECTION("U+0001") {
|
||||
testCodepoint(0x0001, "\x01");
|
||||
}
|
||||
|
||||
SECTION("U+007F") {
|
||||
testCodepoint(0x007F, "\x7f");
|
||||
}
|
||||
|
||||
SECTION("U+0080") {
|
||||
testCodepoint(0x0080, "\xc2\x80");
|
||||
}
|
||||
|
||||
SECTION("U+07FF") {
|
||||
testCodepoint(0x07FF, "\xdf\xbf");
|
||||
}
|
||||
|
||||
SECTION("U+0800") {
|
||||
testCodepoint(0x0800, "\xe0\xa0\x80");
|
||||
}
|
||||
|
||||
SECTION("U+FFFF") {
|
||||
testCodepoint(0xFFFF, "\xef\xbf\xbf");
|
||||
}
|
||||
|
||||
SECTION("U+10000") {
|
||||
testCodepoint(0x10000, "\xf0\x90\x80\x80");
|
||||
}
|
||||
|
||||
SECTION("U+10FFFF") {
|
||||
testCodepoint(0x10FFFF, "\xf4\x8f\xbf\xbf");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Numbers/arithmeticCompare.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("arithmeticCompare()") {
|
||||
SECTION("int vs uint8_t") {
|
||||
CHECK((arithmeticCompare<int, uint8_t>(256, 1) == COMPARE_RESULT_GREATER));
|
||||
CHECK((arithmeticCompare<int, uint8_t>(41, 42) == COMPARE_RESULT_LESS));
|
||||
CHECK((arithmeticCompare<int, uint8_t>(42, 42) == COMPARE_RESULT_EQUAL));
|
||||
CHECK((arithmeticCompare<int, uint8_t>(43, 42) == COMPARE_RESULT_GREATER));
|
||||
}
|
||||
|
||||
SECTION("unsigned vs int") {
|
||||
CHECK((arithmeticCompare<unsigned, int>(0, -1) == COMPARE_RESULT_GREATER));
|
||||
CHECK((arithmeticCompare<unsigned, int>(42, 41) == COMPARE_RESULT_GREATER));
|
||||
CHECK((arithmeticCompare<unsigned, int>(42, 42) == COMPARE_RESULT_EQUAL));
|
||||
CHECK((arithmeticCompare<unsigned, int>(42, 43) == COMPARE_RESULT_LESS));
|
||||
}
|
||||
|
||||
SECTION("float vs int") {
|
||||
CHECK((arithmeticCompare<float, int>(42, 41) == COMPARE_RESULT_GREATER));
|
||||
CHECK((arithmeticCompare<float, int>(42, 42) == COMPARE_RESULT_EQUAL));
|
||||
CHECK((arithmeticCompare<float, int>(42, 43) == COMPARE_RESULT_LESS));
|
||||
}
|
||||
|
||||
SECTION("int vs unsigned") {
|
||||
CHECK((arithmeticCompare<int, unsigned>(-1, 0) == COMPARE_RESULT_LESS));
|
||||
CHECK((arithmeticCompare<int, unsigned>(0, 0) == COMPARE_RESULT_EQUAL));
|
||||
CHECK((arithmeticCompare<int, unsigned>(1, 0) == COMPARE_RESULT_GREATER));
|
||||
CHECK((arithmeticCompare<int, unsigned>(42, 41) == COMPARE_RESULT_GREATER));
|
||||
CHECK((arithmeticCompare<int, unsigned>(42, 42) == COMPARE_RESULT_EQUAL));
|
||||
CHECK((arithmeticCompare<int, unsigned>(42, 43) == COMPARE_RESULT_LESS));
|
||||
}
|
||||
|
||||
SECTION("unsigned vs unsigned") {
|
||||
CHECK((arithmeticCompare<unsigned, unsigned>(42, 41) ==
|
||||
COMPARE_RESULT_GREATER));
|
||||
CHECK((arithmeticCompare<unsigned, unsigned>(42, 42) ==
|
||||
COMPARE_RESULT_EQUAL));
|
||||
CHECK(
|
||||
(arithmeticCompare<unsigned, unsigned>(42, 43) == COMPARE_RESULT_LESS));
|
||||
}
|
||||
|
||||
SECTION("bool vs bool") {
|
||||
CHECK(
|
||||
(arithmeticCompare<bool, bool>(false, false) == COMPARE_RESULT_EQUAL));
|
||||
CHECK((arithmeticCompare<bool, bool>(true, true) == COMPARE_RESULT_EQUAL));
|
||||
CHECK((arithmeticCompare<bool, bool>(false, true) == COMPARE_RESULT_LESS));
|
||||
CHECK(
|
||||
(arithmeticCompare<bool, bool>(true, false) == COMPARE_RESULT_GREATER));
|
||||
}
|
||||
|
||||
SECTION("bool vs int") {
|
||||
CHECK((arithmeticCompare<bool, int>(false, -1) == COMPARE_RESULT_GREATER));
|
||||
CHECK((arithmeticCompare<bool, int>(false, 0) == COMPARE_RESULT_EQUAL));
|
||||
CHECK((arithmeticCompare<bool, int>(false, 1) == COMPARE_RESULT_LESS));
|
||||
CHECK((arithmeticCompare<bool, int>(true, 0) == COMPARE_RESULT_GREATER));
|
||||
CHECK((arithmeticCompare<bool, int>(true, 1) == COMPARE_RESULT_EQUAL));
|
||||
CHECK((arithmeticCompare<bool, int>(true, 2) == COMPARE_RESULT_LESS));
|
||||
}
|
||||
|
||||
SECTION("bool vs int") {
|
||||
CHECK((arithmeticCompare<int, bool>(0, false) == COMPARE_RESULT_EQUAL));
|
||||
CHECK((arithmeticCompare<int, bool>(1, true) == COMPARE_RESULT_EQUAL));
|
||||
CHECK((arithmeticCompare<int, bool>(1, false) == COMPARE_RESULT_GREATER));
|
||||
CHECK((arithmeticCompare<int, bool>(0, true) == COMPARE_RESULT_LESS));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("arithmeticCompareNegateLeft()") {
|
||||
SECTION("unsigned vs int") {
|
||||
CHECK((arithmeticCompareNegateLeft<int>(0, 1) == COMPARE_RESULT_LESS));
|
||||
CHECK((arithmeticCompareNegateLeft<int>(42, -41) == COMPARE_RESULT_LESS));
|
||||
CHECK((arithmeticCompareNegateLeft<int>(42, -42) == COMPARE_RESULT_EQUAL));
|
||||
CHECK(
|
||||
(arithmeticCompareNegateLeft<int>(42, -43) == COMPARE_RESULT_GREATER));
|
||||
}
|
||||
|
||||
SECTION("unsigned vs unsigned") {
|
||||
CHECK(
|
||||
(arithmeticCompareNegateLeft<unsigned>(42, 42) == COMPARE_RESULT_LESS));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("arithmeticCompareNegateRight()") {
|
||||
SECTION("int vs unsigned") {
|
||||
CHECK((arithmeticCompareNegateRight<int>(1, 0) == COMPARE_RESULT_GREATER));
|
||||
CHECK(
|
||||
(arithmeticCompareNegateRight<int>(-41, 42) == COMPARE_RESULT_GREATER));
|
||||
CHECK((arithmeticCompareNegateRight<int>(-42, 42) == COMPARE_RESULT_EQUAL));
|
||||
CHECK((arithmeticCompareNegateRight<int>(-43, 42) == COMPARE_RESULT_LESS));
|
||||
}
|
||||
|
||||
SECTION("unsigned vs unsigned") {
|
||||
CHECK((arithmeticCompareNegateRight<unsigned>(42, 42) ==
|
||||
COMPARE_RESULT_GREATER));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
// Include any header that might use the conflicting macros
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
// All cores
|
||||
#define bit()
|
||||
#define constrain()
|
||||
#define DEFAULT
|
||||
#define DISABLED
|
||||
#define HIGH
|
||||
#define INPUT
|
||||
#define LOW
|
||||
#define max()
|
||||
#define min()
|
||||
#define OUTPUT
|
||||
#define round()
|
||||
#define sq()
|
||||
#define word()
|
||||
#define bitRead()
|
||||
#define bitSet()
|
||||
#define bitClear()
|
||||
#define bitWrite()
|
||||
#define interrupts()
|
||||
#define lowByte()
|
||||
#define highByte()
|
||||
#define DEC
|
||||
#define HEX
|
||||
#define OCT
|
||||
#define BIN
|
||||
#define cbi()
|
||||
#define sbi()
|
||||
|
||||
// ESP8266
|
||||
#define _max()
|
||||
#define _min()
|
||||
|
||||
// Realtek Ameba
|
||||
#define isdigit(c) (((c) >= '0') && ((c) <= '9'))
|
||||
#define isprint(c)
|
||||
#define isxdigit(c)
|
||||
#define isspace(c)
|
||||
#define isupper(c)
|
||||
#define islower(c)
|
||||
#define isalpha(c)
|
||||
|
||||
// issue #839
|
||||
#define BLOCKSIZE
|
||||
#define CAPACITY
|
||||
|
||||
// catch.hpp mutes several warnings, this file also allows to detect them
|
||||
#include "ArduinoJson.h"
|
||||
@@ -0,0 +1,12 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
struct custom_char_traits : std::char_traits<char> {};
|
||||
struct custom_allocator : std::allocator<char> {};
|
||||
typedef std::basic_string<char, custom_char_traits, custom_allocator>
|
||||
custom_string;
|
||||
@@ -0,0 +1,115 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#define ARDUINOJSON_DEPRECATED(msg) // nothing
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("Deprecated features") {
|
||||
StaticJsonDocument<256> doc;
|
||||
const char* s = "hello";
|
||||
doc["s"] = s;
|
||||
doc["c"] = 42;
|
||||
doc["a"].add(s);
|
||||
doc["a"].add(42);
|
||||
|
||||
SECTION("JsonVariant::add(char)") {
|
||||
JsonVariant v = doc.to<JsonVariant>();
|
||||
v.add('*');
|
||||
REQUIRE(v[0] == 42);
|
||||
}
|
||||
|
||||
SECTION("JsonVariant::as<char*>()") {
|
||||
JsonVariant v = doc["s"];
|
||||
REQUIRE(v.as<char*>() == s);
|
||||
}
|
||||
|
||||
SECTION("JsonVariant::as<char>()") {
|
||||
JsonVariant v = doc["c"];
|
||||
REQUIRE(v.as<char>() == '*');
|
||||
}
|
||||
|
||||
SECTION("JsonVariant::is<char*>()") {
|
||||
JsonVariant v = doc["s"];
|
||||
REQUIRE(v.is<char*>() == true);
|
||||
}
|
||||
|
||||
SECTION("JsonVariant::is<char>()") {
|
||||
JsonVariant v = doc["c"];
|
||||
REQUIRE(v.is<char>() == true);
|
||||
}
|
||||
|
||||
SECTION("JsonVariant::set(char)") {
|
||||
JsonVariant v = doc.to<JsonVariant>();
|
||||
v.set('*');
|
||||
REQUIRE(v.as<unsigned char>() == 42);
|
||||
}
|
||||
|
||||
SECTION("JsonVariantConst::as<char*>()") {
|
||||
JsonVariantConst v = doc["s"];
|
||||
REQUIRE(v.as<char*>() == s);
|
||||
}
|
||||
|
||||
SECTION("JsonVariantConst::as<char>()") {
|
||||
JsonVariantConst v = doc["c"];
|
||||
REQUIRE(v.as<char>() == '*');
|
||||
}
|
||||
|
||||
SECTION("JsonVariantConst::is<char*>()") {
|
||||
JsonVariantConst v = doc["s"];
|
||||
REQUIRE(v.is<char*>() == true);
|
||||
}
|
||||
|
||||
SECTION("JsonVariantConst::is<char>()") {
|
||||
JsonVariantConst v = doc["c"];
|
||||
REQUIRE(v.is<char>() == true);
|
||||
}
|
||||
|
||||
SECTION("MemberProxy::as<char*>()") {
|
||||
REQUIRE(doc["s"].as<char*>() == s);
|
||||
}
|
||||
|
||||
SECTION("MemberProxy::as<char>()") {
|
||||
REQUIRE(doc["c"].as<char>() == '*');
|
||||
}
|
||||
|
||||
SECTION("MemberProxy::as<char>()") {
|
||||
doc["x"].set('*');
|
||||
REQUIRE(doc["x"] == 42);
|
||||
}
|
||||
|
||||
SECTION("MemberProxy::is<char*>()") {
|
||||
REQUIRE(doc["s"].is<char*>() == true);
|
||||
REQUIRE(doc["c"].is<char*>() == false);
|
||||
}
|
||||
|
||||
SECTION("MemberProxy::is<char>()") {
|
||||
REQUIRE(doc["c"].is<char>() == true);
|
||||
REQUIRE(doc["s"].is<char>() == false);
|
||||
}
|
||||
|
||||
SECTION("ElementProxy::as<char*>()") {
|
||||
REQUIRE(doc["a"][0].as<char*>() == s);
|
||||
}
|
||||
|
||||
SECTION("ElementProxy::as<char>()") {
|
||||
REQUIRE(doc["a"][1].as<char>() == '*');
|
||||
}
|
||||
|
||||
SECTION("ElementProxy::as<char>()") {
|
||||
doc["a"][0].set('*');
|
||||
REQUIRE(doc["a"][0] == 42);
|
||||
}
|
||||
|
||||
SECTION("ElementProxy::is<char*>()") {
|
||||
REQUIRE(doc["a"][0].is<char*>() == true);
|
||||
REQUIRE(doc["a"][1].is<char*>() == false);
|
||||
}
|
||||
|
||||
SECTION("ElementProxy::is<char>()") {
|
||||
REQUIRE(doc["a"][1].is<char>() == true);
|
||||
REQUIRE(doc["a"][0].is<char>() == false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 1
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
struct PrintOneCharacterAtATime {
|
||||
static size_t printStringTo(const std::string& s, Print& p) {
|
||||
size_t result = 0;
|
||||
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
|
||||
size_t n = p.write(uint8_t(*it));
|
||||
if (n == 0)
|
||||
break;
|
||||
result += n;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
struct PrintAllAtOnce {
|
||||
static size_t printStringTo(const std::string& s, Print& p) {
|
||||
return p.write(s.data(), s.size());
|
||||
}
|
||||
};
|
||||
|
||||
template <typename PrintPolicy>
|
||||
struct PrintableString : public Printable {
|
||||
PrintableString(const char* s) : _str(s), _total(0) {}
|
||||
|
||||
virtual size_t printTo(Print& p) const {
|
||||
size_t result = PrintPolicy::printStringTo(_str, p);
|
||||
_total += result;
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t totalBytesWritten() const {
|
||||
return _total;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string _str;
|
||||
mutable size_t _total;
|
||||
};
|
||||
|
||||
TEST_CASE("Printable") {
|
||||
SECTION("Doesn't overflow") {
|
||||
StaticJsonDocument<8> doc;
|
||||
const char* value = "example"; // == 7 chars
|
||||
|
||||
doc.set(666); // to make sure we override the value
|
||||
|
||||
SECTION("Via Print::write(char)") {
|
||||
PrintableString<PrintOneCharacterAtATime> printable(value);
|
||||
CHECK(doc.set(printable) == true);
|
||||
CHECK(doc.as<std::string>() == value);
|
||||
CHECK(printable.totalBytesWritten() == 7);
|
||||
CHECK(doc.overflowed() == false);
|
||||
CHECK(doc.memoryUsage() == 8);
|
||||
}
|
||||
|
||||
SECTION("Via Print::write(const char* size_t)") {
|
||||
PrintableString<PrintAllAtOnce> printable(value);
|
||||
CHECK(doc.set(printable) == true);
|
||||
CHECK(doc.as<std::string>() == value);
|
||||
CHECK(printable.totalBytesWritten() == 7);
|
||||
CHECK(doc.overflowed() == false);
|
||||
CHECK(doc.memoryUsage() == 8);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Overflows early") {
|
||||
StaticJsonDocument<8> doc;
|
||||
const char* value = "hello world"; // > 8 chars
|
||||
|
||||
doc.set(666); // to make sure we override the value
|
||||
|
||||
SECTION("Via Print::write(char)") {
|
||||
PrintableString<PrintOneCharacterAtATime> printable(value);
|
||||
CHECK(doc.set(printable) == false);
|
||||
CHECK(doc.isNull());
|
||||
CHECK(printable.totalBytesWritten() == 8);
|
||||
CHECK(doc.overflowed() == true);
|
||||
CHECK(doc.memoryUsage() == 0);
|
||||
}
|
||||
|
||||
SECTION("Via Print::write(const char*, size_t)") {
|
||||
PrintableString<PrintAllAtOnce> printable(value);
|
||||
CHECK(doc.set(printable) == false);
|
||||
CHECK(doc.isNull());
|
||||
CHECK(printable.totalBytesWritten() == 0);
|
||||
CHECK(doc.overflowed() == true);
|
||||
CHECK(doc.memoryUsage() == 0);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Overflows adding terminator") {
|
||||
StaticJsonDocument<8> doc;
|
||||
const char* value = "overflow"; // == 8 chars
|
||||
|
||||
doc.set(666); // to make sure we override the value
|
||||
|
||||
SECTION("Via Print::write(char)") {
|
||||
PrintableString<PrintOneCharacterAtATime> printable(value);
|
||||
CHECK(doc.set(printable) == false);
|
||||
CHECK(doc.isNull());
|
||||
CHECK(printable.totalBytesWritten() == 8);
|
||||
CHECK(doc.overflowed() == true);
|
||||
CHECK(doc.memoryUsage() == 0);
|
||||
}
|
||||
|
||||
SECTION("Via Print::write(const char*, size_t)") {
|
||||
PrintableString<PrintAllAtOnce> printable(value);
|
||||
CHECK(doc.set(printable) == false);
|
||||
CHECK(doc.isNull());
|
||||
CHECK(printable.totalBytesWritten() == 0);
|
||||
CHECK(doc.overflowed() == true);
|
||||
CHECK(doc.memoryUsage() == 0);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Null variant") {
|
||||
JsonVariant var;
|
||||
PrintableString<PrintOneCharacterAtATime> printable = "Hello World!";
|
||||
CHECK(var.set(printable) == false);
|
||||
CHECK(var.isNull());
|
||||
CHECK(printable.totalBytesWritten() == 0);
|
||||
}
|
||||
|
||||
SECTION("String deduplication") {
|
||||
StaticJsonDocument<128> doc;
|
||||
doc.add(PrintableString<PrintOneCharacterAtATime>("Hello World!"));
|
||||
doc.add(PrintableString<PrintAllAtOnce>("Hello World!"));
|
||||
REQUIRE(doc.size() == 2);
|
||||
CHECK(doc[0] == "Hello World!");
|
||||
CHECK(doc[1] == "Hello World!");
|
||||
CHECK(doc.memoryUsage() == JSON_ARRAY_SIZE(2) + 13);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#if defined(__clang__)
|
||||
#define CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
#endif
|
||||
|
||||
TEST_CASE("unsigned char[]") {
|
||||
SECTION("deserializeJson()") {
|
||||
unsigned char input[] = "{\"a\":42}";
|
||||
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("deserializeMsgPack()") {
|
||||
unsigned char input[] = "\xDE\x00\x01\xA5Hello\xA5world";
|
||||
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
|
||||
DeserializationError err = deserializeMsgPack(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("serializeMsgPack(unsigned char[])") {
|
||||
unsigned char buffer[32];
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
|
||||
doc["hello"] = "world";
|
||||
|
||||
size_t n = serializeMsgPack(doc, buffer);
|
||||
|
||||
REQUIRE(n == 13);
|
||||
REQUIRE(memcmp(buffer, "\x81\xA5hello\xA5world", 13) == 0);
|
||||
}
|
||||
|
||||
SECTION("serializeMsgPack(unsigned char*)") {
|
||||
unsigned char buffer[32];
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
|
||||
doc["hello"] = "world";
|
||||
|
||||
size_t n = serializeMsgPack(doc, buffer, sizeof(buffer));
|
||||
|
||||
REQUIRE(n == 13);
|
||||
REQUIRE(memcmp(buffer, "\x81\xA5hello\xA5world", 13) == 0);
|
||||
}
|
||||
|
||||
SECTION("serializeJson(unsigned char[])") {
|
||||
unsigned char buffer[32];
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
|
||||
doc["hello"] = "world";
|
||||
|
||||
size_t n = serializeJson(doc, buffer);
|
||||
|
||||
REQUIRE(n == 17);
|
||||
REQUIRE(memcmp(buffer, "{\"hello\":\"world\"}", n) == 0);
|
||||
}
|
||||
|
||||
SECTION("serializeJson(unsigned char*)") {
|
||||
unsigned char buffer[32];
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
|
||||
doc["hello"] = "world";
|
||||
|
||||
size_t n = serializeJson(doc, buffer, sizeof(buffer));
|
||||
|
||||
REQUIRE(n == 17);
|
||||
REQUIRE(memcmp(buffer, "{\"hello\":\"world\"}", n) == 0);
|
||||
}
|
||||
|
||||
SECTION("serializeJsonPretty(unsigned char[])") {
|
||||
unsigned char buffer[32];
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
|
||||
doc["hello"] = "world";
|
||||
|
||||
size_t n = serializeJsonPretty(doc, buffer);
|
||||
|
||||
REQUIRE(n == 24);
|
||||
}
|
||||
|
||||
SECTION("serializeJsonPretty(unsigned char*)") {
|
||||
unsigned char buffer[32];
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
|
||||
doc["hello"] = "world";
|
||||
|
||||
size_t n = serializeJsonPretty(doc, buffer, sizeof(buffer));
|
||||
|
||||
REQUIRE(n == 24);
|
||||
}
|
||||
|
||||
SECTION("JsonVariant") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("set") {
|
||||
unsigned char value[] = "42";
|
||||
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
variant.set(value);
|
||||
|
||||
REQUIRE(42 == variant.as<int>());
|
||||
}
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
SECTION("operator[]") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
JsonVariant variant = doc.as<JsonVariant>();
|
||||
|
||||
REQUIRE(std::string("world") == variant[key]);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
SECTION("operator[] const") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
const JsonVariant variant = doc.as<JsonVariant>();
|
||||
|
||||
REQUIRE(std::string("world") == variant[key]);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("operator==") {
|
||||
unsigned char comparand[] = "hello";
|
||||
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
variant.set("hello");
|
||||
|
||||
REQUIRE(comparand == variant);
|
||||
REQUIRE(variant == comparand);
|
||||
REQUIRE_FALSE(comparand != variant);
|
||||
REQUIRE_FALSE(variant != comparand);
|
||||
}
|
||||
|
||||
SECTION("operator!=") {
|
||||
unsigned char comparand[] = "hello";
|
||||
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
variant.set("world");
|
||||
|
||||
REQUIRE(comparand != variant);
|
||||
REQUIRE(variant != comparand);
|
||||
REQUIRE_FALSE(comparand == variant);
|
||||
REQUIRE_FALSE(variant == comparand);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("JsonObject") {
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
SECTION("operator[]") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj[key] = "world";
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject::operator[] const") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
REQUIRE(std::string("world") == obj[key]);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("containsKey()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
REQUIRE(true == obj.containsKey(key));
|
||||
}
|
||||
|
||||
SECTION("remove()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
obj.remove(key);
|
||||
|
||||
REQUIRE(0 == obj.size());
|
||||
}
|
||||
|
||||
SECTION("createNestedArray()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj.createNestedArray(key);
|
||||
}
|
||||
|
||||
SECTION("createNestedObject()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj.createNestedObject(key);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("MemberProxy") {
|
||||
SECTION("operator=") { // issue #416
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"] = value;
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("set()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"].set(value);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("JsonArray") {
|
||||
SECTION("add()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add(value);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("ElementProxy") {
|
||||
SECTION("set()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
arr[0].set(value);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
}
|
||||
|
||||
SECTION("operator=") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
arr[0] = value;
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/version.hpp>
|
||||
#include <catch.hpp>
|
||||
#include <sstream>
|
||||
|
||||
using Catch::Matchers::StartsWith;
|
||||
|
||||
TEST_CASE("ARDUINOJSON_VERSION") {
|
||||
std::stringstream version;
|
||||
|
||||
version << ARDUINOJSON_VERSION_MAJOR << "." << ARDUINOJSON_VERSION_MINOR
|
||||
<< "." << ARDUINOJSON_VERSION_REVISION;
|
||||
|
||||
REQUIRE_THAT(ARDUINOJSON_VERSION, StartsWith(version.str()));
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <ArduinoJson/Namespace.hpp>
|
||||
|
||||
// Issue #1198: strcmp() implementation that returns a value larger than 8-bit
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
int strcmp(const char* a, const char* b) {
|
||||
int result = ::strcmp(a, b);
|
||||
if (result > 0)
|
||||
return 2147483647;
|
||||
if (result < 0)
|
||||
return -214748364;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int strncmp(const char* a, const char* b, size_t n) {
|
||||
int result = ::strncmp(a, b, n);
|
||||
if (result > 0)
|
||||
return 2147483647;
|
||||
if (result < 0)
|
||||
return -214748364;
|
||||
return 0;
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
Reference in New Issue
Block a user