forked from bblanchon/ArduinoJson
Tests: use a consistent naming convention for allocators
This commit is contained in:
@ -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)));
|
||||
}
|
||||
|
Reference in New Issue
Block a user