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

@ -11,7 +11,6 @@ add_executable(JsonArrayTests
equals.cpp
isNull.cpp
iterator.cpp
memoryUsage.cpp
nesting.cpp
remove.cpp
size.cpp

View File

@ -5,11 +5,14 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonArray::add()") {
JsonDocument doc;
SpyingAllocator allocator;
JsonDocument doc(&allocator);
JsonArray array = doc.to<JsonArray>();
SECTION("int") {
@ -99,43 +102,49 @@ TEST_CASE("JsonArray::add()") {
SECTION("should not duplicate const char*") {
array.add("world");
const size_t expectedSize = sizeofArray(1);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool()));
}
SECTION("should duplicate char*") {
array.add(const_cast<char*>("world"));
const size_t expectedSize = sizeofArray(1) + sizeofString(5);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(5)));
}
SECTION("should duplicate std::string") {
array.add(std::string("world"));
const size_t expectedSize = sizeofArray(1) + sizeofString(5);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(5)));
}
SECTION("should duplicate serialized(const char*)") {
array.add(serialized("{}"));
const size_t expectedSize = sizeofArray(1) + sizeofString(2);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(2)));
}
SECTION("should duplicate serialized(char*)") {
array.add(serialized(const_cast<char*>("{}")));
const size_t expectedSize = sizeofArray(1) + sizeofString(2);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(2)));
}
SECTION("should duplicate serialized(std::string)") {
array.add(serialized(std::string("{}")));
const size_t expectedSize = sizeofArray(1) + sizeofString(2);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(2)));
}
SECTION("should duplicate serialized(std::string)") {
array.add(serialized(std::string("\0XX", 3)));
const size_t expectedSize = sizeofArray(1) + sizeofString(3);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(3)));
}
}

View File

@ -1,46 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonArray::memoryUsage()") {
JsonDocument doc;
JsonArray arr = doc.to<JsonArray>();
SECTION("return 0 if uninitialized") {
JsonArray unitialized;
REQUIRE(unitialized.memoryUsage() == 0);
}
SECTION("sizeofArray(0) if empty") {
REQUIRE(arr.memoryUsage() == sizeofArray(0));
}
SECTION("sizeofArray(1) after add") {
arr.add("hello");
REQUIRE(arr.memoryUsage() == sizeofArray(1));
}
SECTION("includes the size of the string") {
arr.add(std::string("hello"));
REQUIRE(arr.memoryUsage() == sizeofArray(1) + sizeofString(5));
}
SECTION("includes the size of the nested array") {
JsonArray nested = arr.createNestedArray();
nested.add(42);
REQUIRE(arr.memoryUsage() == 2 * sizeofArray(1));
}
SECTION("includes the size of the nested arrect") {
JsonObject nested = arr.createNestedObject();
nested["hello"] = "world";
REQUIRE(arr.memoryUsage() == sizeofObject(1) + sizeofArray(1));
}
}

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("JsonArray::operator[]") {
JsonDocument doc;
SpyingAllocator allocator;
JsonDocument doc(&allocator);
JsonArray array = doc.to<JsonArray>();
SECTION("Pad with null") {
@ -115,20 +118,22 @@ TEST_CASE("JsonArray::operator[]") {
SECTION("should not duplicate const char*") {
array[0] = "world";
const size_t expectedSize = sizeofArray(1);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool()));
}
SECTION("should duplicate char*") {
array[0] = const_cast<char*>("world");
const size_t expectedSize = sizeofArray(1) + sizeofString(5);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(5)));
}
SECTION("should duplicate std::string") {
array[0] = std::string("world");
const size_t expectedSize = sizeofArray(1) + sizeofString(5);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(5)));
}
SECTION("array[0].to<JsonObject>()") {