Update to ArduinoJson 7.0.4
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// Copyright © 2014-2024, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <Arduino.h>
|
||||
@@ -8,6 +8,10 @@
|
||||
#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 1
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
|
||||
struct PrintOneCharacterAtATime {
|
||||
static size_t printStringTo(const std::string& s, Print& p) {
|
||||
size_t result = 0;
|
||||
@@ -29,27 +33,28 @@ struct PrintAllAtOnce {
|
||||
|
||||
template <typename PrintPolicy>
|
||||
struct PrintableString : public Printable {
|
||||
PrintableString(const char* s) : _str(s), _total(0) {}
|
||||
PrintableString(const char* s) : str_(s), total_(0) {}
|
||||
|
||||
virtual size_t printTo(Print& p) const {
|
||||
size_t result = PrintPolicy::printStringTo(_str, p);
|
||||
_total += result;
|
||||
size_t result = PrintPolicy::printStringTo(str_, p);
|
||||
total_ += result;
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t totalBytesWritten() const {
|
||||
return _total;
|
||||
return total_;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string _str;
|
||||
mutable size_t _total;
|
||||
std::string str_;
|
||||
mutable size_t total_;
|
||||
};
|
||||
|
||||
TEST_CASE("Printable") {
|
||||
SECTION("Doesn't overflow") {
|
||||
StaticJsonDocument<8> doc;
|
||||
const char* value = "example"; // == 7 chars
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
const char* value = "example";
|
||||
|
||||
doc.set(666); // to make sure we override the value
|
||||
|
||||
@@ -59,8 +64,11 @@ TEST_CASE("Printable") {
|
||||
CHECK(doc.as<std::string>() == value);
|
||||
CHECK(printable.totalBytesWritten() == 7);
|
||||
CHECK(doc.overflowed() == false);
|
||||
CHECK(doc.memoryUsage() == 8);
|
||||
CHECK(doc.as<JsonVariant>().memoryUsage() == 8);
|
||||
CHECK(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("example")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("Via Print::write(const char* size_t)") {
|
||||
@@ -69,58 +77,90 @@ TEST_CASE("Printable") {
|
||||
CHECK(doc.as<std::string>() == value);
|
||||
CHECK(printable.totalBytesWritten() == 7);
|
||||
CHECK(doc.overflowed() == false);
|
||||
CHECK(doc.memoryUsage() == 8);
|
||||
CHECK(doc.as<JsonVariant>().memoryUsage() == 8);
|
||||
CHECK(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("example")),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Overflows early") {
|
||||
StaticJsonDocument<8> doc;
|
||||
const char* value = "hello world"; // > 8 chars
|
||||
SECTION("First allocation fails") {
|
||||
SpyingAllocator spy(FailingAllocator::instance());
|
||||
JsonDocument doc(&spy);
|
||||
const char* value = "hello world";
|
||||
|
||||
doc.set(666); // to make sure we override the value
|
||||
|
||||
SECTION("Via Print::write(char)") {
|
||||
PrintableString<PrintOneCharacterAtATime> printable(value);
|
||||
CHECK(doc.set(printable) == false);
|
||||
|
||||
bool success = doc.set(printable);
|
||||
|
||||
CHECK(success == false);
|
||||
CHECK(doc.isNull());
|
||||
CHECK(printable.totalBytesWritten() == 8);
|
||||
CHECK(printable.totalBytesWritten() == 0);
|
||||
CHECK(doc.overflowed() == true);
|
||||
CHECK(doc.memoryUsage() == 0);
|
||||
CHECK(spy.log() == AllocatorLog{
|
||||
AllocateFail(sizeofStringBuffer()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("Via Print::write(const char*, size_t)") {
|
||||
PrintableString<PrintAllAtOnce> printable(value);
|
||||
CHECK(doc.set(printable) == false);
|
||||
|
||||
bool success = doc.set(printable);
|
||||
|
||||
CHECK(success == false);
|
||||
CHECK(doc.isNull());
|
||||
CHECK(printable.totalBytesWritten() == 0);
|
||||
CHECK(doc.overflowed() == true);
|
||||
CHECK(doc.memoryUsage() == 0);
|
||||
CHECK(spy.log() == AllocatorLog{
|
||||
AllocateFail(sizeofStringBuffer()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Overflows adding terminator") {
|
||||
StaticJsonDocument<8> doc;
|
||||
const char* value = "overflow"; // == 8 chars
|
||||
SECTION("Reallocation fails") {
|
||||
TimebombAllocator timebomb(1);
|
||||
SpyingAllocator spy(&timebomb);
|
||||
JsonDocument doc(&spy);
|
||||
const char* value = "Lorem ipsum dolor sit amet, cons"; // > 31 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);
|
||||
|
||||
bool success = doc.set(printable);
|
||||
|
||||
CHECK(success == false);
|
||||
CHECK(doc.isNull());
|
||||
CHECK(printable.totalBytesWritten() == 8);
|
||||
CHECK(printable.totalBytesWritten() == 31);
|
||||
CHECK(doc.overflowed() == true);
|
||||
CHECK(doc.memoryUsage() == 0);
|
||||
CHECK(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
ReallocateFail(sizeofStringBuffer(), sizeofStringBuffer(2)),
|
||||
Deallocate(sizeofStringBuffer()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("Via Print::write(const char*, size_t)") {
|
||||
PrintableString<PrintAllAtOnce> printable(value);
|
||||
CHECK(doc.set(printable) == false);
|
||||
|
||||
bool success = doc.set(printable);
|
||||
|
||||
CHECK(success == false);
|
||||
CHECK(doc.isNull());
|
||||
CHECK(printable.totalBytesWritten() == 0);
|
||||
CHECK(printable.totalBytesWritten() == 31);
|
||||
CHECK(doc.overflowed() == true);
|
||||
CHECK(doc.memoryUsage() == 0);
|
||||
CHECK(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
ReallocateFail(sizeofStringBuffer(), sizeofStringBuffer(2)),
|
||||
Deallocate(sizeofStringBuffer()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,12 +173,20 @@ TEST_CASE("Printable") {
|
||||
}
|
||||
|
||||
SECTION("String deduplication") {
|
||||
StaticJsonDocument<128> doc;
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
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);
|
||||
CHECK(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("Hello World!")),
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Deallocate(sizeofStringBuffer()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user