mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-16 20:12:16 +02:00
Tests: use a consistent naming convention for allocators
This commit is contained in:
@ -13,8 +13,8 @@ using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("string_view") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
|
||||
SECTION("deserializeJson()") {
|
||||
@ -63,10 +63,10 @@ TEST_CASE("string_view") {
|
||||
doc.add(std::string_view("example\0tree", 12));
|
||||
doc.add(std::string_view("example\0tree and a half", 12));
|
||||
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7))
|
||||
<< AllocatorLog::Allocate(sizeofString(12)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7))
|
||||
<< AllocatorLog::Allocate(sizeofString(12)));
|
||||
}
|
||||
|
||||
SECTION("as<std::string_view>()") {
|
||||
|
@ -11,8 +11,8 @@ using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonArray::add()") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("int") {
|
||||
@ -102,49 +102,49 @@ TEST_CASE("JsonArray::add()") {
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
array.add("world");
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("should duplicate char*") {
|
||||
array.add(const_cast<char*>("world"));
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string") {
|
||||
array.add(std::string("world"));
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("should duplicate serialized(const char*)") {
|
||||
array.add(serialized("{}"));
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(2)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(2)));
|
||||
}
|
||||
|
||||
SECTION("should duplicate serialized(char*)") {
|
||||
array.add(serialized(const_cast<char*>("{}")));
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(2)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(2)));
|
||||
}
|
||||
|
||||
SECTION("should duplicate serialized(std::string)") {
|
||||
array.add(serialized(std::string("{}")));
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(2)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(2)));
|
||||
}
|
||||
|
||||
SECTION("should duplicate serialized(std::string)") {
|
||||
array.add(serialized(std::string("\0XX", 3)));
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(3)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(3)));
|
||||
}
|
||||
}
|
||||
|
@ -26,8 +26,8 @@ TEST_CASE("JsonArray::clear()") {
|
||||
}
|
||||
|
||||
SECTION("Removed elements are recycled") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
// fill the pool entirely
|
||||
@ -39,7 +39,7 @@ TEST_CASE("JsonArray::clear()") {
|
||||
for (int i = 0; i < ARDUINOJSON_POOL_CAPACITY; i++)
|
||||
array.add(i);
|
||||
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
}
|
||||
|
@ -91,8 +91,8 @@ TEST_CASE("JsonArray::remove()") {
|
||||
}
|
||||
|
||||
TEST_CASE("Removed elements are recycled") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
// fill the pool entirely
|
||||
@ -105,7 +105,7 @@ TEST_CASE("Removed elements are recycled") {
|
||||
// add one element; it should use the free slot
|
||||
array.add(42);
|
||||
|
||||
REQUIRE(allocator.log() == AllocatorLog() << AllocatorLog::Allocate(
|
||||
sizeofPool()) // only one pool
|
||||
REQUIRE(spy.log() == AllocatorLog() << AllocatorLog::Allocate(
|
||||
sizeofPool()) // only one pool
|
||||
);
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonArray::operator[]") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("Pad with null") {
|
||||
@ -118,22 +118,22 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
array[0] = "world";
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("should duplicate char*") {
|
||||
array[0] = const_cast<char*>("world");
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string") {
|
||||
array[0] = std::string("world");
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("array[0].to<JsonObject>()") {
|
||||
|
@ -11,8 +11,8 @@ using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("deserialize JSON array") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("An empty array") {
|
||||
DeserializationError err = deserializeJson(doc, "[]");
|
||||
@ -254,16 +254,16 @@ TEST_CASE("deserialize JSON array") {
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(arr.size() == 0);
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Deallocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Deallocate(sizeofPool()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("deserialize JSON array under memory constraints") {
|
||||
TimebombAllocator timebomb(100);
|
||||
SpyingAllocator allocator(&timebomb);
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy(&timebomb);
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("empty array requires no allocation") {
|
||||
timebomb.setCountdown(0);
|
||||
@ -307,10 +307,10 @@ TEST_CASE("deserialize JSON array under memory constraints") {
|
||||
SECTION("don't store space characters") {
|
||||
deserializeJson(doc, " [ \"1234567\" ] ");
|
||||
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(
|
||||
sizeofString(31), sizeofString(7)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(7)));
|
||||
}
|
||||
}
|
||||
|
@ -691,9 +691,9 @@ TEST_CASE("Filtering") {
|
||||
for (size_t i = 0; i < sizeof(testCases) / sizeof(testCases[0]); i++) {
|
||||
CAPTURE(i);
|
||||
|
||||
SpyingAllocator allocator;
|
||||
SpyingAllocator spy;
|
||||
JsonDocument filter;
|
||||
JsonDocument doc(&allocator);
|
||||
JsonDocument doc(&spy);
|
||||
TestCase& tc = testCases[i];
|
||||
|
||||
CAPTURE(tc.filter);
|
||||
@ -708,7 +708,7 @@ TEST_CASE("Filtering") {
|
||||
CHECK(doc.as<std::string>() == tc.output);
|
||||
|
||||
doc.shrinkToFit();
|
||||
CHECK(allocator.allocatedBytes() == tc.memoryUsage);
|
||||
CHECK(spy.allocatedBytes() == tc.memoryUsage);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,8 +14,8 @@ using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("deserializeJson(char*)") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
char input[] = "{\"hello\":\"world\"}";
|
||||
|
||||
@ -23,14 +23,14 @@ TEST_CASE("deserializeJson(char*)") {
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(5)));
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeJson(unsigned char*, unsigned int)") { // issue #1897
|
||||
|
@ -12,8 +12,8 @@ using namespace Catch::Matchers;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
|
||||
TEST_CASE("deserializeJson(JsonDocument&)") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("Edge cases") {
|
||||
SECTION("null char*") {
|
||||
@ -117,8 +117,8 @@ TEST_CASE("deserializeJson(JsonDocument&)") {
|
||||
deserializeJson(doc, "{}");
|
||||
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Deallocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Deallocate(sizeofPool()));
|
||||
}
|
||||
}
|
||||
|
@ -11,8 +11,8 @@ using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("deserialize JSON object") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("An empty object") {
|
||||
DeserializationError err = deserializeJson(doc, "{}");
|
||||
@ -284,7 +284,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.as<std::string>() == "{\"a\":2}");
|
||||
REQUIRE(allocator.log() ==
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog()
|
||||
// a
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
@ -331,21 +331,22 @@ TEST_CASE("deserialize JSON object") {
|
||||
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 0);
|
||||
REQUIRE(allocator.log() ==
|
||||
AllocatorLog()
|
||||
// string "hello"
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(5))
|
||||
// pool
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
// string "world"
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(5))
|
||||
// free pool
|
||||
<< AllocatorLog::Deallocate(sizeofPool())
|
||||
// free "hello" and "world"
|
||||
<< AllocatorLog::Deallocate(sizeofString(5))
|
||||
<< AllocatorLog::Deallocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
// string "hello"
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(5))
|
||||
// pool
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
// string "world"
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(5))
|
||||
// free pool
|
||||
<< AllocatorLog::Deallocate(sizeofPool())
|
||||
// free "hello" and "world"
|
||||
<< AllocatorLog::Deallocate(sizeofString(5))
|
||||
<< AllocatorLog::Deallocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("Issue #1335") {
|
||||
@ -356,11 +357,11 @@ TEST_CASE("deserialize JSON object") {
|
||||
}
|
||||
|
||||
TEST_CASE("deserialize JSON object under memory constraints") {
|
||||
TimebombAllocator allocator(1024);
|
||||
JsonDocument doc(&allocator);
|
||||
TimebombAllocator timebomb(1024);
|
||||
JsonDocument doc(&timebomb);
|
||||
|
||||
SECTION("empty object requires no allocation") {
|
||||
allocator.setCountdown(0);
|
||||
timebomb.setCountdown(0);
|
||||
char input[] = "{}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
@ -370,7 +371,7 @@ TEST_CASE("deserialize JSON object under memory constraints") {
|
||||
}
|
||||
|
||||
SECTION("key allocation fails") {
|
||||
allocator.setCountdown(0);
|
||||
timebomb.setCountdown(0);
|
||||
char input[] = "{\"a\":1}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
@ -380,7 +381,7 @@ TEST_CASE("deserialize JSON object under memory constraints") {
|
||||
}
|
||||
|
||||
SECTION("pool allocation fails") {
|
||||
allocator.setCountdown(2);
|
||||
timebomb.setCountdown(2);
|
||||
char input[] = "{\"a\":1}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
@ -390,7 +391,7 @@ TEST_CASE("deserialize JSON object under memory constraints") {
|
||||
}
|
||||
|
||||
SECTION("string allocation fails") {
|
||||
allocator.setCountdown(3);
|
||||
timebomb.setCountdown(3);
|
||||
char input[] = "{\"a\":\"b\"}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
@ -99,98 +99,98 @@ TEST_CASE("Invalid JSON string") {
|
||||
}
|
||||
|
||||
TEST_CASE("Allocation of the key fails") {
|
||||
TimebombAllocator timebombAllocator(0);
|
||||
SpyingAllocator spyingAllocator(&timebombAllocator);
|
||||
JsonDocument doc(&spyingAllocator);
|
||||
TimebombAllocator timebomb(0);
|
||||
SpyingAllocator spy(&timebomb);
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("Quoted string, first member") {
|
||||
REQUIRE(deserializeJson(doc, "{\"example\":1}") ==
|
||||
DeserializationError::NoMemory);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
}
|
||||
|
||||
SECTION("Quoted string, second member") {
|
||||
timebombAllocator.setCountdown(3);
|
||||
timebomb.setCountdown(3);
|
||||
REQUIRE(deserializeJson(doc, "{\"hello\":1,\"world\"}") ==
|
||||
DeserializationError::NoMemory);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
}
|
||||
|
||||
SECTION("Non-Quoted string, first member") {
|
||||
REQUIRE(deserializeJson(doc, "{example:1}") ==
|
||||
DeserializationError::NoMemory);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
}
|
||||
|
||||
SECTION("Non-Quoted string, second member") {
|
||||
timebombAllocator.setCountdown(3);
|
||||
timebomb.setCountdown(3);
|
||||
REQUIRE(deserializeJson(doc, "{hello:1,world}") ==
|
||||
DeserializationError::NoMemory);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("String allocation fails") {
|
||||
SpyingAllocator spyingAllocator(FailingAllocator::instance());
|
||||
JsonDocument doc(&spyingAllocator);
|
||||
SpyingAllocator spy(FailingAllocator::instance());
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("Input is const char*") {
|
||||
REQUIRE(deserializeJson(doc, "\"hello\"") ==
|
||||
DeserializationError::NoMemory);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Deduplicate values") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
deserializeJson(doc, "[\"example\",\"example\"]");
|
||||
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
// pool
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
// string builder
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
// string "example"
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(7))
|
||||
// string builder
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Deallocate(sizeofString(31)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
// pool
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
// string builder
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
// string "example"
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(7))
|
||||
// string builder
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Deallocate(sizeofString(31)));
|
||||
}
|
||||
|
||||
TEST_CASE("Deduplicate keys") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
deserializeJson(doc, "[{\"example\":1},{\"example\":2}]");
|
||||
|
||||
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
CHECK(key1 == key2);
|
||||
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
// pool
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
// string builder
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
// string "example"
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(7))
|
||||
// string builder
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Deallocate(sizeofString(31)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
// pool
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
// string builder
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
// string "example"
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(7))
|
||||
// string builder
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Deallocate(sizeofString(31)));
|
||||
}
|
||||
|
@ -315,8 +315,8 @@ TEST_CASE("MemberProxy::createNestedObject(key)") {
|
||||
}
|
||||
|
||||
TEST_CASE("Deduplicate keys") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("std::string") {
|
||||
doc[0][std::string("example")] = 1;
|
||||
@ -326,9 +326,9 @@ TEST_CASE("Deduplicate keys") {
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
CHECK(key1 == key2);
|
||||
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
}
|
||||
|
||||
SECTION("char*") {
|
||||
@ -340,9 +340,9 @@ TEST_CASE("Deduplicate keys") {
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
CHECK(key1 == key2);
|
||||
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
}
|
||||
|
||||
SECTION("Arduino String") {
|
||||
@ -353,9 +353,9 @@ TEST_CASE("Deduplicate keys") {
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
CHECK(key1 == key2);
|
||||
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
}
|
||||
|
||||
SECTION("Flash string") {
|
||||
@ -366,9 +366,9 @@ TEST_CASE("Deduplicate keys") {
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
CHECK(key1 == key2);
|
||||
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,23 +14,23 @@ using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonDocument::add()") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("integer") {
|
||||
doc.add(42);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[42]");
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
doc.add("hello");
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
@ -38,9 +38,9 @@ TEST_CASE("JsonDocument::add()") {
|
||||
doc.add(std::string("example"));
|
||||
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
}
|
||||
|
||||
SECTION("char*") {
|
||||
@ -49,9 +49,9 @@ TEST_CASE("JsonDocument::add()") {
|
||||
doc.add(value);
|
||||
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
}
|
||||
|
||||
SECTION("Arduino String") {
|
||||
@ -59,9 +59,9 @@ TEST_CASE("JsonDocument::add()") {
|
||||
doc.add(String("example"));
|
||||
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
}
|
||||
|
||||
SECTION("Flash string") {
|
||||
@ -69,8 +69,8 @@ TEST_CASE("JsonDocument::add()") {
|
||||
doc.add(F("example"));
|
||||
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(7)));
|
||||
}
|
||||
}
|
||||
|
@ -8,75 +8,75 @@
|
||||
#include "Allocators.hpp"
|
||||
|
||||
TEST_CASE("JsonDocument::overflowed()") {
|
||||
TimebombAllocator allocator(10);
|
||||
JsonDocument doc(&allocator);
|
||||
TimebombAllocator timebomb(10);
|
||||
JsonDocument doc(&timebomb);
|
||||
|
||||
SECTION("returns false on a fresh object") {
|
||||
allocator.setCountdown(0);
|
||||
timebomb.setCountdown(0);
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true after a failed insertion") {
|
||||
allocator.setCountdown(0);
|
||||
timebomb.setCountdown(0);
|
||||
doc.add(0);
|
||||
CHECK(doc.overflowed() == true);
|
||||
}
|
||||
|
||||
SECTION("returns false after successful insertion") {
|
||||
allocator.setCountdown(2);
|
||||
timebomb.setCountdown(2);
|
||||
doc.add(0);
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true after a failed string copy") {
|
||||
allocator.setCountdown(0);
|
||||
timebomb.setCountdown(0);
|
||||
doc.add(std::string("example"));
|
||||
CHECK(doc.overflowed() == true);
|
||||
}
|
||||
|
||||
SECTION("returns false after a successful string copy") {
|
||||
allocator.setCountdown(3);
|
||||
timebomb.setCountdown(3);
|
||||
doc.add(std::string("example"));
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true after a failed member add") {
|
||||
allocator.setCountdown(0);
|
||||
timebomb.setCountdown(0);
|
||||
doc["example"] = true;
|
||||
CHECK(doc.overflowed() == true);
|
||||
}
|
||||
|
||||
SECTION("returns true after a failed deserialization") {
|
||||
allocator.setCountdown(0);
|
||||
timebomb.setCountdown(0);
|
||||
deserializeJson(doc, "[1, 2]");
|
||||
CHECK(doc.overflowed() == true);
|
||||
}
|
||||
|
||||
SECTION("returns false after a successful deserialization") {
|
||||
allocator.setCountdown(3);
|
||||
timebomb.setCountdown(3);
|
||||
deserializeJson(doc, "[\"example\"]");
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
SECTION("returns false after clear()") {
|
||||
allocator.setCountdown(0);
|
||||
timebomb.setCountdown(0);
|
||||
doc.add(0);
|
||||
doc.clear();
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
SECTION("remains false after shrinkToFit()") {
|
||||
allocator.setCountdown(2);
|
||||
timebomb.setCountdown(2);
|
||||
doc.add(0);
|
||||
allocator.setCountdown(2);
|
||||
timebomb.setCountdown(2);
|
||||
doc.shrinkToFit();
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
SECTION("remains true after shrinkToFit()") {
|
||||
allocator.setCountdown(0);
|
||||
timebomb.setCountdown(0);
|
||||
doc.add(0);
|
||||
allocator.setCountdown(2);
|
||||
timebomb.setCountdown(2);
|
||||
doc.shrinkToFit();
|
||||
CHECK(doc.overflowed() == true);
|
||||
}
|
||||
|
@ -10,77 +10,77 @@
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonObject::set()") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc1(&allocator);
|
||||
JsonDocument doc2(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc1(&spy);
|
||||
JsonDocument doc2(&spy);
|
||||
|
||||
JsonObject obj1 = doc1.to<JsonObject>();
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
|
||||
SECTION("doesn't copy static string in key or value") {
|
||||
obj1["hello"] = "world";
|
||||
allocator.clearLog();
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj2["hello"] == std::string("world"));
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("copy local string value") {
|
||||
obj1["hello"] = std::string("world");
|
||||
allocator.clearLog();
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj2["hello"] == std::string("world"));
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("copy local key") {
|
||||
obj1[std::string("hello")] = "world";
|
||||
allocator.clearLog();
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj2["hello"] == std::string("world"));
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("copy string from deserializeJson()") {
|
||||
deserializeJson(doc1, "{'hello':'world'}");
|
||||
allocator.clearLog();
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj2["hello"] == std::string("world"));
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("copy string from deserializeMsgPack()") {
|
||||
deserializeMsgPack(doc1, "\x81\xA5hello\xA5world");
|
||||
allocator.clearLog();
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj2["hello"] == std::string("world"));
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("should work with JsonObjectConst") {
|
||||
|
@ -11,8 +11,8 @@ using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonObject::operator[]") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("int") {
|
||||
@ -106,65 +106,65 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
obj["hello"] = "world";
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* value") {
|
||||
obj["hello"] = const_cast<char*>("world");
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* key") {
|
||||
obj[const_cast<char*>("hello")] = "world";
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* key&value") {
|
||||
obj[const_cast<char*>("hello")] = const_cast<char*>("world");
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string value") {
|
||||
obj["hello"] = std::string("world");
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string key") {
|
||||
obj[std::string("hello")] = "world";
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string key&value") {
|
||||
obj[std::string("hello")] = std::string("world");
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("should duplicate a non-static JsonString key") {
|
||||
obj[JsonString("hello", JsonString::Copied)] = "world";
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("should not duplicate a static JsonString key") {
|
||||
obj[JsonString("hello", JsonString::Linked)] = "world";
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("should ignore null key") {
|
||||
|
@ -11,8 +11,8 @@
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonVariant::clear()") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
|
||||
SECTION("size goes back to zero") {
|
||||
@ -33,8 +33,8 @@ TEST_CASE("JsonVariant::clear()") {
|
||||
var.set(std::string("hello"));
|
||||
var.clear();
|
||||
|
||||
REQUIRE(allocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Deallocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Deallocate(sizeofString(5)));
|
||||
}
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonVariant::remove(int)") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("release top level strings") {
|
||||
doc.add(std::string("hello"));
|
||||
@ -23,22 +23,22 @@ TEST_CASE("JsonVariant::remove(int)") {
|
||||
JsonVariant var = doc.as<JsonVariant>();
|
||||
REQUIRE(var.as<std::string>() == "[\"hello\",\"hello\",\"world\"]");
|
||||
|
||||
allocator.clearLog();
|
||||
spy.clearLog();
|
||||
var.remove(1);
|
||||
REQUIRE(var.as<std::string>() == "[\"hello\",\"world\"]");
|
||||
REQUIRE(allocator.log() == AllocatorLog());
|
||||
REQUIRE(spy.log() == AllocatorLog());
|
||||
|
||||
allocator.clearLog();
|
||||
spy.clearLog();
|
||||
var.remove(1);
|
||||
REQUIRE(var.as<std::string>() == "[\"hello\"]");
|
||||
REQUIRE(allocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Deallocate(sizeofString(5)));
|
||||
|
||||
allocator.clearLog();
|
||||
spy.clearLog();
|
||||
var.remove(0);
|
||||
REQUIRE(var.as<std::string>() == "[]");
|
||||
REQUIRE(allocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Deallocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("release strings in nested array") {
|
||||
@ -47,12 +47,12 @@ TEST_CASE("JsonVariant::remove(int)") {
|
||||
JsonVariant var = doc.as<JsonVariant>();
|
||||
REQUIRE(var.as<std::string>() == "[[\"hello\"]]");
|
||||
|
||||
allocator.clearLog();
|
||||
spy.clearLog();
|
||||
var.remove(0);
|
||||
|
||||
REQUIRE(var.as<std::string>() == "[]");
|
||||
REQUIRE(allocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Deallocate(sizeofString(5)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -177,41 +177,41 @@ TEST_CASE("JsonVariant::set(JsonDocument)") {
|
||||
}
|
||||
|
||||
TEST_CASE("JsonVariant::set() releases the previous value") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
doc["hello"] = std::string("world");
|
||||
allocator.clearLog();
|
||||
spy.clearLog();
|
||||
|
||||
JsonVariant v = doc["hello"];
|
||||
|
||||
SECTION("int") {
|
||||
v.set(42);
|
||||
REQUIRE(allocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Deallocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("bool") {
|
||||
v.set(false);
|
||||
REQUIRE(allocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Deallocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
v.set("hello");
|
||||
REQUIRE(allocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Deallocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("float") {
|
||||
v.set(1.2);
|
||||
REQUIRE(allocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Deallocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("Serialized<const char*>") {
|
||||
v.set(serialized("[]"));
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Deallocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofString(2)));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Deallocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofString(2)));
|
||||
}
|
||||
}
|
||||
|
@ -53,8 +53,8 @@ struct PrintableString : public Printable {
|
||||
|
||||
TEST_CASE("Printable") {
|
||||
SECTION("Doesn't overflow") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
const char* value = "example";
|
||||
|
||||
doc.set(666); // to make sure we override the value
|
||||
@ -65,10 +65,10 @@ TEST_CASE("Printable") {
|
||||
CHECK(doc.as<std::string>() == value);
|
||||
CHECK(printable.totalBytesWritten() == 7);
|
||||
CHECK(doc.overflowed() == false);
|
||||
CHECK(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(
|
||||
sizeofString(31), sizeofString(7)));
|
||||
CHECK(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(7)));
|
||||
}
|
||||
|
||||
SECTION("Via Print::write(const char* size_t)") {
|
||||
@ -77,16 +77,16 @@ TEST_CASE("Printable") {
|
||||
CHECK(doc.as<std::string>() == value);
|
||||
CHECK(printable.totalBytesWritten() == 7);
|
||||
CHECK(doc.overflowed() == false);
|
||||
CHECK(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(
|
||||
sizeofString(31), sizeofString(7)));
|
||||
CHECK(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(7)));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("First allocation fails") {
|
||||
SpyingAllocator spyingAllocator(FailingAllocator::instance());
|
||||
JsonDocument doc(&spyingAllocator);
|
||||
SpyingAllocator spy(FailingAllocator::instance());
|
||||
JsonDocument doc(&spy);
|
||||
const char* value = "hello world";
|
||||
|
||||
doc.set(666); // to make sure we override the value
|
||||
@ -100,8 +100,8 @@ TEST_CASE("Printable") {
|
||||
CHECK(doc.isNull());
|
||||
CHECK(printable.totalBytesWritten() == 0);
|
||||
CHECK(doc.overflowed() == true);
|
||||
CHECK(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
CHECK(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
}
|
||||
|
||||
SECTION("Via Print::write(const char*, size_t)") {
|
||||
@ -113,15 +113,15 @@ TEST_CASE("Printable") {
|
||||
CHECK(doc.isNull());
|
||||
CHECK(printable.totalBytesWritten() == 0);
|
||||
CHECK(doc.overflowed() == true);
|
||||
CHECK(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
CHECK(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Reallocation fails") {
|
||||
TimebombAllocator timebombAllocator(1);
|
||||
SpyingAllocator spyingAllocator(&timebombAllocator);
|
||||
JsonDocument doc(&spyingAllocator);
|
||||
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
|
||||
@ -135,11 +135,11 @@ TEST_CASE("Printable") {
|
||||
CHECK(doc.isNull());
|
||||
CHECK(printable.totalBytesWritten() == 31);
|
||||
CHECK(doc.overflowed() == true);
|
||||
CHECK(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::ReallocateFail(sizeofString(31),
|
||||
sizeofString(63))
|
||||
<< AllocatorLog::Deallocate(sizeofString(31)));
|
||||
CHECK(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::ReallocateFail(sizeofString(31),
|
||||
sizeofString(63))
|
||||
<< AllocatorLog::Deallocate(sizeofString(31)));
|
||||
}
|
||||
|
||||
SECTION("Via Print::write(const char*, size_t)") {
|
||||
@ -151,11 +151,11 @@ TEST_CASE("Printable") {
|
||||
CHECK(doc.isNull());
|
||||
CHECK(printable.totalBytesWritten() == 31);
|
||||
CHECK(doc.overflowed() == true);
|
||||
CHECK(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::ReallocateFail(sizeofString(31),
|
||||
sizeofString(63))
|
||||
<< AllocatorLog::Deallocate(sizeofString(31)));
|
||||
CHECK(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::ReallocateFail(sizeofString(31),
|
||||
sizeofString(63))
|
||||
<< AllocatorLog::Deallocate(sizeofString(31)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -168,19 +168,19 @@ TEST_CASE("Printable") {
|
||||
}
|
||||
|
||||
SECTION("String deduplication") {
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
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(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(12))
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Deallocate(sizeofString(31)));
|
||||
CHECK(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(12))
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Deallocate(sizeofString(31)));
|
||||
}
|
||||
}
|
||||
|
@ -20,8 +20,8 @@ static void checkValue(const char* input, T expected) {
|
||||
|
||||
static void checkError(size_t timebombCountDown, const char* input,
|
||||
DeserializationError expected) {
|
||||
TimebombAllocator timebombAllocator(timebombCountDown);
|
||||
JsonDocument doc(&timebombAllocator);
|
||||
TimebombAllocator timebomb(timebombCountDown);
|
||||
JsonDocument doc(&timebomb);
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -11,8 +11,7 @@
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("ResourceManager::shrinkToFit()") {
|
||||
TimebombAllocator allocator(100);
|
||||
SpyingAllocator spyingAllocator(&allocator);
|
||||
SpyingAllocator spyingAllocator;
|
||||
ResourceManager resources(&spyingAllocator);
|
||||
|
||||
SECTION("empty") {
|
||||
|
@ -11,15 +11,15 @@
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("ResourceManager::size()") {
|
||||
TimebombAllocator allocator(0);
|
||||
ResourceManager resources(&allocator);
|
||||
TimebombAllocator timebomb(0);
|
||||
ResourceManager resources(&timebomb);
|
||||
|
||||
SECTION("Initial size is 0") {
|
||||
REQUIRE(0 == resources.size());
|
||||
}
|
||||
|
||||
SECTION("Doesn't grow when allocation of second pool fails") {
|
||||
allocator.setCountdown(1);
|
||||
timebomb.setCountdown(1);
|
||||
for (size_t i = 0; i < ARDUINOJSON_POOL_CAPACITY; i++)
|
||||
resources.allocSlot();
|
||||
size_t size = resources.size();
|
||||
|
@ -19,9 +19,9 @@ static void fullPreallocatedPools(ResourceManager& resources) {
|
||||
|
||||
TEST_CASE("ResourceManager::swap()") {
|
||||
SECTION("Both using preallocated pool list") {
|
||||
SpyingAllocator allocator;
|
||||
ResourceManager a(&allocator);
|
||||
ResourceManager b(&allocator);
|
||||
SpyingAllocator spy;
|
||||
ResourceManager a(&spy);
|
||||
ResourceManager b(&spy);
|
||||
|
||||
auto a1 = a.allocSlot();
|
||||
auto b1 = b.allocSlot();
|
||||
@ -31,14 +31,14 @@ TEST_CASE("ResourceManager::swap()") {
|
||||
REQUIRE(a1->data() == b.getSlot(a1.id())->data());
|
||||
REQUIRE(b1->data() == a.getSlot(b1.id())->data());
|
||||
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()) * 2);
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()) * 2);
|
||||
}
|
||||
|
||||
SECTION("Only left using preallocated pool list") {
|
||||
SpyingAllocator allocator;
|
||||
ResourceManager a(&allocator);
|
||||
ResourceManager b(&allocator);
|
||||
SpyingAllocator spy;
|
||||
ResourceManager a(&spy);
|
||||
ResourceManager b(&spy);
|
||||
fullPreallocatedPools(b);
|
||||
|
||||
auto a1 = a.allocSlot();
|
||||
@ -48,19 +48,19 @@ TEST_CASE("ResourceManager::swap()") {
|
||||
REQUIRE(a1->data() == b.getSlot(a1.id())->data());
|
||||
REQUIRE(b1->data() == a.getSlot(b1.id())->data());
|
||||
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()) *
|
||||
(ARDUINOJSON_INITIAL_POOL_COUNT + 1)
|
||||
<< AllocatorLog::Allocate(sizeofPoolList(
|
||||
ARDUINOJSON_INITIAL_POOL_COUNT * 2))
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()) *
|
||||
(ARDUINOJSON_INITIAL_POOL_COUNT + 1)
|
||||
<< AllocatorLog::Allocate(sizeofPoolList(
|
||||
ARDUINOJSON_INITIAL_POOL_COUNT * 2))
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("Only right using preallocated pool list") {
|
||||
SpyingAllocator allocator;
|
||||
ResourceManager a(&allocator);
|
||||
SpyingAllocator spy;
|
||||
ResourceManager a(&spy);
|
||||
fullPreallocatedPools(a);
|
||||
ResourceManager b(&allocator);
|
||||
ResourceManager b(&spy);
|
||||
|
||||
auto a1 = a.allocSlot();
|
||||
auto b1 = b.allocSlot();
|
||||
@ -69,19 +69,19 @@ TEST_CASE("ResourceManager::swap()") {
|
||||
REQUIRE(a1->data() == b.getSlot(a1.id())->data());
|
||||
REQUIRE(b1->data() == a.getSlot(b1.id())->data());
|
||||
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()) *
|
||||
ARDUINOJSON_INITIAL_POOL_COUNT
|
||||
<< AllocatorLog::Allocate(sizeofPoolList(
|
||||
ARDUINOJSON_INITIAL_POOL_COUNT * 2))
|
||||
<< AllocatorLog::Allocate(sizeofPool()) * 2);
|
||||
REQUIRE(spy.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()) *
|
||||
ARDUINOJSON_INITIAL_POOL_COUNT
|
||||
<< AllocatorLog::Allocate(sizeofPoolList(
|
||||
ARDUINOJSON_INITIAL_POOL_COUNT * 2))
|
||||
<< AllocatorLog::Allocate(sizeofPool()) * 2);
|
||||
}
|
||||
|
||||
SECTION("None is using preallocated pool list") {
|
||||
SpyingAllocator allocator;
|
||||
ResourceManager a(&allocator);
|
||||
SpyingAllocator spy;
|
||||
ResourceManager a(&spy);
|
||||
fullPreallocatedPools(a);
|
||||
ResourceManager b(&allocator);
|
||||
ResourceManager b(&spy);
|
||||
fullPreallocatedPools(b);
|
||||
|
||||
auto a1 = a.allocSlot();
|
||||
|
Reference in New Issue
Block a user