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

183 lines
5.2 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:
2023-03-20 14:47:27 +01:00
virtual ~ArmoredAllocator() {}
2023-03-20 14:47:27 +01:00
void* allocate(size_t size) override {
return malloc(size);
}
2023-03-20 14:47:27 +01:00
void deallocate(void* ptr) override {
free(ptr);
}
2023-03-20 14:47:27 +01:00
void* reallocate(void* ptr, size_t new_size) override {
// 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);
memset(new_ptr, '#', new_size); // erase
if (ptr) {
memcpy(new_ptr, ptr, std::min(new_size, new_size));
free(ptr);
}
return new_ptr;
}
};
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-11 10:03:47 +02:00
REQUIRE(doc.as<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)
2023-04-11 10:03:47 +02:00
<< AllocatorLog::Allocate(sizeofString(7))
<< AllocatorLog::Reallocate(4096, 0));
}
SECTION("raw string") {
doc.set(serialized("[{},12]"));
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[{},12]");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
2023-04-11 10:03:47 +02:00
<< AllocatorLog::Allocate(sizeofString(7))
<< AllocatorLog::Reallocate(4096, 0));
}
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)
2023-04-11 10:03:47 +02:00
<< AllocatorLog::Allocate(sizeofString(7))
<< AllocatorLog::Reallocate(4096, sizeofObject(1)));
}
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)
2023-04-11 10:03:47 +02:00
<< AllocatorLog::Allocate(sizeofString(7))
<< AllocatorLog::Reallocate(4096, sizeofArray(1)));
}
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)
2023-04-11 10:03:47 +02:00
<< AllocatorLog::Allocate(sizeofString(7))
<< AllocatorLog::Reallocate(4096, sizeofObject(1)));
}
}