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

@ -13,7 +13,6 @@ add_executable(JsonVariantTests
createNested.cpp
is.cpp
isnull.cpp
memoryUsage.cpp
misc.cpp
nesting.cpp
nullptr.cpp

View File

@ -6,10 +6,13 @@
#include <stdint.h>
#include <catch.hpp>
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonVariant::clear()") {
JsonDocument doc;
SpyingAllocator allocator;
JsonDocument doc(&allocator);
JsonVariant var = doc.to<JsonVariant>();
SECTION("size goes back to zero") {
@ -28,9 +31,10 @@ TEST_CASE("JsonVariant::clear()") {
SECTION("releases owned string") {
var.set(std::string("hello"));
REQUIRE(doc.memoryUsage() == sizeofString(5));
var.clear();
REQUIRE(doc.memoryUsage() == 0);
REQUIRE(allocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(5))
<< AllocatorLog::Deallocate(sizeofString(5)));
}
}

View File

@ -44,8 +44,6 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(doc1.memoryUsage() == 0);
REQUIRE(doc2.memoryUsage() == 0);
REQUIRE(spyingAllocator.log() == AllocatorLog());
}
@ -56,8 +54,6 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(doc1.memoryUsage() == sizeofString(7));
REQUIRE(doc2.memoryUsage() == sizeofString(7));
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
}
@ -70,8 +66,6 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(doc1.memoryUsage() == sizeofString(7));
REQUIRE(doc2.memoryUsage() == 0);
REQUIRE(doc2.overflowed() == true);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString((7))));
@ -83,8 +77,6 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(doc1.memoryUsage() == sizeofString(7));
REQUIRE(doc2.memoryUsage() == sizeofString(7));
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
}
@ -95,8 +87,6 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(doc1.memoryUsage() == sizeofString(7));
REQUIRE(doc2.memoryUsage() == sizeofString(7));
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
}
@ -108,8 +98,6 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(doc1.memoryUsage() == sizeofString(7));
REQUIRE(doc2.memoryUsage() == sizeofString(7));
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
}
@ -120,8 +108,6 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(doc1.memoryUsage() == sizeofString(7));
REQUIRE(doc2.memoryUsage() == sizeofString(7));
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
}
@ -133,8 +119,6 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(doc1.memoryUsage() == sizeofString(7));
REQUIRE(doc2.memoryUsage() == 0);
REQUIRE(doc2.overflowed() == true);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString((7))));

View File

@ -1,45 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonVariant::memoryUsage()") {
JsonDocument doc;
JsonVariant var = doc.to<JsonVariant>();
SECTION("returns 0 if uninitialized") {
JsonVariant unitialized;
REQUIRE(unitialized.memoryUsage() == 0);
}
SECTION("returns size of object") {
JsonObject obj = var.to<JsonObject>();
obj["hello"] = 42;
REQUIRE(var.memoryUsage() == sizeofObject(1));
}
SECTION("returns size of array") {
JsonArray arr = var.to<JsonArray>();
arr.add(42);
REQUIRE(var.memoryUsage() == sizeofArray(1));
}
SECTION("returns size of owned string") {
var.set(std::string("hello"));
REQUIRE(var.memoryUsage() == sizeofString(5));
REQUIRE(var.memoryUsage() == doc.memoryUsage());
}
SECTION("returns size of raw string") {
var.set(serialized("hello"));
REQUIRE(var.memoryUsage() == sizeofString(5));
REQUIRE(var.memoryUsage() == doc.memoryUsage());
}
}

View File

@ -6,11 +6,14 @@
#include <stdint.h>
#include <catch.hpp>
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonVariant::remove(int)") {
JsonDocument doc;
SpyingAllocator allocator;
JsonDocument doc(&allocator);
SECTION("release top level strings") {
doc.add(std::string("hello"));
@ -19,19 +22,23 @@ TEST_CASE("JsonVariant::remove(int)") {
JsonVariant var = doc.as<JsonVariant>();
REQUIRE(var.as<std::string>() == "[\"hello\",\"hello\",\"world\"]");
REQUIRE(doc.memoryUsage() == sizeofArray(3) + 2 * sizeofString(5));
allocator.clearLog();
var.remove(1);
REQUIRE(var.as<std::string>() == "[\"hello\",\"world\"]");
REQUIRE(doc.memoryUsage() == sizeofArray(3) + 2 * sizeofString(5));
REQUIRE(allocator.log() == AllocatorLog());
allocator.clearLog();
var.remove(1);
REQUIRE(var.as<std::string>() == "[\"hello\"]");
REQUIRE(doc.memoryUsage() == sizeofArray(3) + 1 * sizeofString(5));
REQUIRE(allocator.log() ==
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
allocator.clearLog();
var.remove(0);
REQUIRE(var.as<std::string>() == "[]");
REQUIRE(doc.memoryUsage() == sizeofArray(3));
REQUIRE(allocator.log() ==
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
}
SECTION("release strings in nested array") {
@ -39,11 +46,13 @@ TEST_CASE("JsonVariant::remove(int)") {
JsonVariant var = doc.as<JsonVariant>();
REQUIRE(var.as<std::string>() == "[[\"hello\"]]");
REQUIRE(doc.memoryUsage() == 2 * sizeofArray(1) + sizeofString(5));
allocator.clearLog();
var.remove(0);
REQUIRE(var.as<std::string>() == "[]");
REQUIRE(doc.memoryUsage() == 2 * sizeofArray(1));
REQUIRE(allocator.log() ==
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
}
}

View File

@ -177,34 +177,41 @@ TEST_CASE("JsonVariant::set(JsonDocument)") {
}
TEST_CASE("JsonVariant::set() releases the previous value") {
JsonDocument doc;
SpyingAllocator allocator;
JsonDocument doc(&allocator);
doc["hello"] = std::string("world");
REQUIRE(doc.memoryUsage() == sizeofObject(1) + sizeofString(5));
allocator.clearLog();
JsonVariant v = doc["hello"];
SECTION("int") {
v.set(42);
REQUIRE(doc.memoryUsage() == sizeofObject(1));
REQUIRE(allocator.log() ==
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
}
SECTION("bool") {
v.set(false);
REQUIRE(doc.memoryUsage() == sizeofObject(1));
REQUIRE(allocator.log() ==
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
}
SECTION("const char*") {
v.set("hello");
REQUIRE(doc.memoryUsage() == sizeofObject(1));
REQUIRE(allocator.log() ==
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
}
SECTION("float") {
v.set(1.2);
REQUIRE(doc.memoryUsage() == sizeofObject(1));
REQUIRE(allocator.log() ==
AllocatorLog() << AllocatorLog::Deallocate(sizeofString(5)));
}
SECTION("Serialized<const char*>") {
v.set(serialized("[]"));
REQUIRE(doc.memoryUsage() == sizeofObject(1) + sizeofString(2));
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Deallocate(sizeofString(5))
<< AllocatorLog::Allocate(sizeofString(2)));
}
}