Remove memoryUsage()

This commit is contained in:
Benoit Blanchon
2023-07-24 17:21:25 +02:00
parent 2fdacb1ca0
commit 00c9d8680a
43 changed files with 1173 additions and 567 deletions

View File

@ -7,61 +7,80 @@
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonObject::set()") {
JsonDocument doc1;
JsonDocument doc2;
SpyingAllocator allocator;
JsonDocument doc1(&allocator);
JsonDocument doc2(&allocator);
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();
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool()));
}
SECTION("copy local string value") {
obj1["hello"] = std::string("world");
allocator.clearLog();
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(5)));
}
SECTION("copy local key") {
obj1[std::string("hello")] = "world";
allocator.clearLog();
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofString(5))
<< AllocatorLog::Allocate(sizeofPool()));
}
SECTION("copy string from deserializeJson()") {
deserializeJson(doc1, "{'hello':'world'}");
allocator.clearLog();
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(allocator.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();
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofString(5))
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(5)));
}
SECTION("should work with JsonObjectConst") {
@ -69,13 +88,12 @@ TEST_CASE("JsonObject::set()") {
obj2.set(static_cast<JsonObjectConst>(obj1));
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
}
SECTION("copy fails in the middle of an object") {
TimebombAllocator allocator(2);
JsonDocument doc3(&allocator);
TimebombAllocator timebomb(2);
JsonDocument doc3(&timebomb);
JsonObject obj3 = doc3.to<JsonObject>();
obj1[std::string("a")] = 1;
@ -88,8 +106,8 @@ TEST_CASE("JsonObject::set()") {
}
SECTION("copy fails in the middle of an array") {
TimebombAllocator allocator(1);
JsonDocument doc3(&allocator);
TimebombAllocator timebomb(1);
JsonDocument doc3(&timebomb);
JsonObject obj3 = doc3.to<JsonObject>();
obj1["hello"][0] = std::string("world");