Files
ArduinoJson/extras/tests/JsonDocument/shrinkToFit.cpp

215 lines
6.1 KiB
C++
Raw Normal View History

// ArduinoJson - https://arduinojson.org
2023-02-16 11:45:01 +01:00
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <stdlib.h> // malloc, free
#include <string>
2023-04-02 16:47:37 +02:00
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
2023-04-02 16:47:37 +02:00
using ArduinoJson::detail::sizeofString;
2023-03-20 14:47:27 +01:00
class ArmoredAllocator : public Allocator {
public:
ArmoredAllocator() : _ptr(0), _size(0) {}
2023-03-20 14:47:27 +01:00
virtual ~ArmoredAllocator() {}
2023-03-20 14:47:27 +01:00
void* allocate(size_t size) override {
_ptr = malloc(size);
_size = size;
return _ptr;
}
2023-03-20 14:47:27 +01:00
void deallocate(void* ptr) override {
REQUIRE(ptr == _ptr);
free(ptr);
_ptr = 0;
_size = 0;
}
2023-03-20 14:47:27 +01:00
void* reallocate(void* ptr, size_t new_size) override {
REQUIRE(ptr == _ptr);
// don't call realloc, instead alloc a new buffer and erase the old one
// this way we make sure we support relocation
void* new_ptr = malloc(new_size);
memcpy(new_ptr, _ptr, std::min(new_size, _size));
memset(_ptr, '#', _size); // erase
free(_ptr);
_ptr = new_ptr;
return new_ptr;
}
private:
void* _ptr;
size_t _size;
};
TEST_CASE("JsonDocument::shrinkToFit()") {
2023-03-20 14:47:27 +01:00
ArmoredAllocator armoredAllocator;
2023-04-02 16:47:37 +02:00
SpyingAllocator spyingAllocator(&armoredAllocator);
JsonDocument doc(4096, &spyingAllocator);
SECTION("null") {
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "null");
REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(4096, 0));
}
SECTION("empty object") {
deserializeJson(doc, "{}");
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(4096, sizeofObject(0)));
}
SECTION("empty array") {
deserializeJson(doc, "[]");
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[]");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(4096, sizeofArray(0)));
}
SECTION("linked string") {
doc.set("hello");
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "hello");
REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(4096, 0));
}
SECTION("owned string") {
doc.set(std::string("abcdefg"));
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "abcdefg");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(4096, sizeofString(7)));
}
SECTION("linked raw") {
doc.set(serialized("[{},123]"));
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[{},123]");
REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(4096, 0));
}
SECTION("owned raw") {
doc.set(serialized(std::string("[{},12]")));
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[{},12]");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(4096, sizeofString(7)));
}
SECTION("linked key") {
doc["key"] = 42;
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"key\":42}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(4096, sizeofObject(1)));
}
SECTION("owned key") {
doc[std::string("abcdefg")] = 42;
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"abcdefg\":42}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(
4096, sizeofObject(1) + sizeofString(7)));
}
SECTION("linked string in array") {
doc.add("hello");
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(4096, sizeofArray(1)));
}
SECTION("owned string in array") {
doc.add(std::string("abcdefg"));
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[\"abcdefg\"]");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(
4096, sizeofArray(1) + sizeofString(7)));
}
SECTION("linked string in object") {
doc["key"] = "hello";
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"key\":\"hello\"}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(4096, sizeofObject(1)));
}
SECTION("owned string in object") {
doc["key"] = std::string("abcdefg");
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"key\":\"abcdefg\"}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(
4096, sizeofObject(1) + sizeofString(7)));
}
SECTION("unaligned") {
doc.add(std::string("?")); // two bytes in the string pool
REQUIRE(doc.memoryUsage() == sizeofObject(1) + sizeofString(1));
doc.shrinkToFit();
// the new capacity should be padded to align the pointers
REQUIRE(doc[0] == "?");
2023-04-02 16:47:37 +02:00
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
<< AllocatorLog::Reallocate(
4096, sizeofArray(1) + sizeof(void*)));
}
}