forked from bblanchon/ArduinoJson
Remove memoryUsage()
This commit is contained in:
@ -13,7 +13,6 @@ add_executable(JsonObjectTests
|
||||
invalid.cpp
|
||||
isNull.cpp
|
||||
iterator.cpp
|
||||
memoryUsage.cpp
|
||||
nesting.cpp
|
||||
remove.cpp
|
||||
size.cpp
|
||||
|
@ -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");
|
||||
|
@ -1,47 +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("JsonObject::memoryUsage()") {
|
||||
JsonDocument doc;
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("return 0 if uninitialized") {
|
||||
JsonObject unitialized;
|
||||
REQUIRE(unitialized.memoryUsage() == 0);
|
||||
}
|
||||
|
||||
SECTION("sizeofObject(0) for empty object") {
|
||||
REQUIRE(obj.memoryUsage() == sizeofObject(0));
|
||||
}
|
||||
|
||||
SECTION("sizeofObject(1) after add") {
|
||||
obj["hello"] = 42;
|
||||
REQUIRE(obj.memoryUsage() == sizeofObject(1));
|
||||
}
|
||||
|
||||
SECTION("includes the size of the key") {
|
||||
obj[std::string("hello")] = 42;
|
||||
REQUIRE(obj.memoryUsage() == sizeofObject(1) + sizeofString(5));
|
||||
}
|
||||
|
||||
SECTION("includes the size of the nested array") {
|
||||
JsonArray nested = obj.createNestedArray("nested");
|
||||
nested.add(42);
|
||||
REQUIRE(obj.memoryUsage() == sizeofObject(1) + sizeofArray(1));
|
||||
}
|
||||
|
||||
SECTION("includes the size of the nested object") {
|
||||
JsonObject nested = obj.createNestedObject("nested");
|
||||
nested["hello"] = "world";
|
||||
REQUIRE(obj.memoryUsage() == 2 * sizeofObject(1));
|
||||
}
|
||||
}
|
@ -83,28 +83,4 @@ TEST_CASE("std::string") {
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("memoryUsage() increases when adding a new key") {
|
||||
std::string key1("hello"), key2("world");
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
obj[key1] = 1;
|
||||
size_t sizeBefore = doc.memoryUsage();
|
||||
obj[key2] = 2;
|
||||
size_t sizeAfter = doc.memoryUsage();
|
||||
|
||||
REQUIRE(sizeAfter - sizeBefore >= key2.size());
|
||||
}
|
||||
|
||||
SECTION("memoryUsage() remains when adding the same key") {
|
||||
std::string key("hello");
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
obj[key] = 1;
|
||||
size_t sizeBefore = doc.memoryUsage();
|
||||
obj[key] = 2;
|
||||
size_t sizeAfter = doc.memoryUsage();
|
||||
|
||||
REQUIRE(sizeBefore == sizeAfter);
|
||||
}
|
||||
}
|
||||
|
@ -5,11 +5,14 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonObject::operator[]") {
|
||||
JsonDocument doc;
|
||||
SpyingAllocator allocator;
|
||||
JsonDocument doc(&allocator);
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("int") {
|
||||
@ -103,56 +106,65 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
obj["hello"] = "world";
|
||||
const size_t expectedSize = sizeofObject(1);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* value") {
|
||||
obj["hello"] = const_cast<char*>("world");
|
||||
const size_t expectedSize = sizeofObject(1) + sizeofString(5);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* key") {
|
||||
obj[const_cast<char*>("hello")] = "world";
|
||||
const size_t expectedSize = sizeofObject(1) + sizeofString(5);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* key&value") {
|
||||
obj[const_cast<char*>("hello")] = const_cast<char*>("world");
|
||||
const size_t expectedSize = sizeofObject(1) + 2 * sizeofString(5);
|
||||
REQUIRE(expectedSize <= doc.memoryUsage());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string value") {
|
||||
obj["hello"] = std::string("world");
|
||||
const size_t expectedSize = sizeofObject(1) + sizeofString(5);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool())
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string key") {
|
||||
obj[std::string("hello")] = "world";
|
||||
const size_t expectedSize = sizeofObject(1) + sizeofString(5);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string key&value") {
|
||||
obj[std::string("hello")] = std::string("world");
|
||||
const size_t expectedSize = sizeofObject(1) + 2 * sizeofString(5);
|
||||
REQUIRE(expectedSize <= doc.memoryUsage());
|
||||
REQUIRE(allocator.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";
|
||||
const size_t expectedSize = sizeofObject(1) + sizeofString(5);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofString(5))
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("should not duplicate a static JsonString key") {
|
||||
obj[JsonString("hello", JsonString::Linked)] = "world";
|
||||
const size_t expectedSize = sizeofObject(1);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
REQUIRE(allocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(sizeofPool()));
|
||||
}
|
||||
|
||||
SECTION("should ignore null key") {
|
||||
|
Reference in New Issue
Block a user