mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-22 06:52:24 +02:00
Rename MemoryPool
to ResourceManager
This commit is contained in:
69
extras/tests/ResourceManager/saveString.cpp
Normal file
69
extras/tests/ResourceManager/saveString.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
static StringNode* saveString(ResourceManager& resources, const char* s) {
|
||||
return resources.saveString(adaptString(s));
|
||||
}
|
||||
|
||||
static StringNode* saveString(ResourceManager& resources, const char* s,
|
||||
size_t n) {
|
||||
return resources.saveString(adaptString(s, n));
|
||||
}
|
||||
|
||||
TEST_CASE("ResourceManager::saveString()") {
|
||||
ResourceManager resources(32);
|
||||
|
||||
SECTION("Duplicates different strings") {
|
||||
auto a = saveString(resources, "hello");
|
||||
auto b = saveString(resources, "world");
|
||||
REQUIRE(a->data != b->data);
|
||||
REQUIRE(a->length == 5);
|
||||
REQUIRE(b->length == 5);
|
||||
REQUIRE(a->references == 1);
|
||||
REQUIRE(b->references == 1);
|
||||
REQUIRE(resources.size() == 2 * sizeofString(5));
|
||||
}
|
||||
|
||||
SECTION("Deduplicates identical strings") {
|
||||
auto a = saveString(resources, "hello");
|
||||
auto b = saveString(resources, "hello");
|
||||
REQUIRE(a == b);
|
||||
REQUIRE(a->length == 5);
|
||||
REQUIRE(a->references == 2);
|
||||
REQUIRE(resources.size() == sizeofString(5));
|
||||
}
|
||||
|
||||
SECTION("Deduplicates identical strings that contain NUL") {
|
||||
auto a = saveString(resources, "hello\0world", 11);
|
||||
auto b = saveString(resources, "hello\0world", 11);
|
||||
REQUIRE(a == b);
|
||||
REQUIRE(a->length == 11);
|
||||
REQUIRE(a->references == 2);
|
||||
REQUIRE(resources.size() == sizeofString(11));
|
||||
}
|
||||
|
||||
SECTION("Don't stop on first NUL") {
|
||||
auto a = saveString(resources, "hello");
|
||||
auto b = saveString(resources, "hello\0world", 11);
|
||||
REQUIRE(a != b);
|
||||
REQUIRE(a->length == 5);
|
||||
REQUIRE(b->length == 11);
|
||||
REQUIRE(a->references == 1);
|
||||
REQUIRE(b->references == 1);
|
||||
REQUIRE(resources.size() == sizeofString(5) + sizeofString(11));
|
||||
}
|
||||
|
||||
SECTION("Returns NULL when allocation fails") {
|
||||
ResourceManager pool2(32, FailingAllocator::instance());
|
||||
REQUIRE(saveString(pool2, "a") == nullptr);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user