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

185 lines
4.5 KiB
C++
Raw Normal View History

// ArduinoJson - https://arduinojson.org
2024-01-03 08:47:06 +01:00
// Copyright © 2014-2024, 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"
#include "Literals.hpp"
2023-04-02 16:47:37 +02:00
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
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(&spyingAllocator);
SECTION("null") {
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "null");
REQUIRE(spyingAllocator.log() == AllocatorLog{});
}
SECTION("empty object") {
deserializeJson(doc, "{}");
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{}");
REQUIRE(spyingAllocator.log() == AllocatorLog{});
}
SECTION("empty array") {
deserializeJson(doc, "[]");
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[]");
REQUIRE(spyingAllocator.log() == AllocatorLog{});
}
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{});
}
SECTION("owned string") {
doc.set("abcdefg"_s);
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{
Allocate(sizeofString("abcdefg")),
});
}
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{
Allocate(sizeofString("[{},12]")),
});
}
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{
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofObject(1)),
});
}
SECTION("owned key") {
doc["abcdefg"_s] = 42;
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"abcdefg\":42}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog{
Allocate(sizeofString("abcdefg")),
Allocate(sizeofPool()),
Reallocate(sizeofPool(), 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{
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofArray(1)),
});
}
SECTION("owned string in array") {
doc.add("abcdefg"_s);
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[\"abcdefg\"]");
2023-07-21 10:38:35 +02:00
REQUIRE(spyingAllocator.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("abcdefg")),
Reallocate(sizeofPool(), 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{
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofObject(1)),
});
}
SECTION("owned string in object") {
doc["key"] = "abcdefg"_s;
2023-04-02 16:47:37 +02:00
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"key\":\"abcdefg\"}");
2023-07-21 10:38:35 +02:00
REQUIRE(spyingAllocator.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("abcdefg")),
Reallocate(sizeofPool(), sizeofPool(1)),
});
}
}