Pass StringNode* to VariantData

This commit is contained in:
Benoit Blanchon
2023-05-02 18:29:19 +02:00
parent 5c0338970c
commit 167ea08c53
13 changed files with 90 additions and 60 deletions

View File

@ -10,11 +10,11 @@
using namespace ArduinoJson::detail;
static const char* saveString(MemoryPool& pool, const char* s) {
static StringNode* saveString(MemoryPool& pool, const char* s) {
return pool.saveString(adaptString(s));
}
static const char* saveString(MemoryPool& pool, const char* s, size_t n) {
static StringNode* saveString(MemoryPool& pool, const char* s, size_t n) {
return pool.saveString(adaptString(s, n));
}
@ -22,35 +22,47 @@ TEST_CASE("MemoryPool::saveString()") {
MemoryPool pool(32);
SECTION("Duplicates different strings") {
const char* a = saveString(pool, "hello");
const char* b = saveString(pool, "world");
REQUIRE(a != b);
auto a = saveString(pool, "hello");
auto b = saveString(pool, "world");
REQUIRE(a->data != b->data);
REQUIRE(a->length == 5);
REQUIRE(b->length == 5);
REQUIRE(a->references == 1);
REQUIRE(b->references == 1);
REQUIRE(pool.size() == 2 * sizeofString(5));
}
SECTION("Deduplicates identical strings") {
const char* a = saveString(pool, "hello");
const char* b = saveString(pool, "hello");
auto a = saveString(pool, "hello");
auto b = saveString(pool, "hello");
REQUIRE(a == b);
REQUIRE(a->length == 5);
REQUIRE(a->references == 2);
REQUIRE(pool.size() == sizeofString(5));
}
SECTION("Deduplicates identical strings that contain NUL") {
const char* a = saveString(pool, "hello\0world", 11);
const char* b = saveString(pool, "hello\0world", 11);
auto a = saveString(pool, "hello\0world", 11);
auto b = saveString(pool, "hello\0world", 11);
REQUIRE(a == b);
REQUIRE(a->length == 11);
REQUIRE(a->references == 2);
REQUIRE(pool.size() == sizeofString(11));
}
SECTION("Don't stop on first NUL") {
const char* a = saveString(pool, "hello");
const char* b = saveString(pool, "hello\0world", 11);
auto a = saveString(pool, "hello");
auto b = saveString(pool, "hello\0world", 11);
REQUIRE(a != b);
REQUIRE(a->length == 5);
REQUIRE(b->length == 11);
REQUIRE(a->references == 1);
REQUIRE(b->references == 1);
REQUIRE(pool.size() == sizeofString(5) + sizeofString(11));
}
SECTION("Returns NULL when allocation fails") {
MemoryPool pool2(32, FailingAllocator::instance());
REQUIRE(0 == saveString(pool2, "a"));
REQUIRE(saveString(pool2, "a") == nullptr);
}
}