mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-24 07:47:32 +02:00
Pass StringNode*
to VariantData
This commit is contained in:
@ -93,39 +93,46 @@ TEST_CASE("StringCopier") {
|
||||
}
|
||||
}
|
||||
|
||||
static const char* addStringToPool(MemoryPool& pool, const char* s) {
|
||||
static StringNode* addStringToPool(MemoryPool& pool, const char* s) {
|
||||
StringCopier str(&pool);
|
||||
str.startString();
|
||||
str.append(s);
|
||||
return str.save().c_str();
|
||||
return str.save();
|
||||
}
|
||||
|
||||
TEST_CASE("StringCopier::save() deduplicates strings") {
|
||||
MemoryPool pool(4096);
|
||||
|
||||
SECTION("Basic") {
|
||||
const char* s1 = addStringToPool(pool, "hello");
|
||||
const char* s2 = addStringToPool(pool, "world");
|
||||
const char* s3 = addStringToPool(pool, "hello");
|
||||
auto s1 = addStringToPool(pool, "hello");
|
||||
auto s2 = addStringToPool(pool, "world");
|
||||
auto s3 = addStringToPool(pool, "hello");
|
||||
|
||||
REQUIRE(s1 == s3);
|
||||
REQUIRE(s2 != s3);
|
||||
REQUIRE(s1->references == 2);
|
||||
REQUIRE(s2->references == 1);
|
||||
REQUIRE(s3->references == 2);
|
||||
REQUIRE(pool.size() == 2 * sizeofString(5));
|
||||
}
|
||||
|
||||
SECTION("Requires terminator") {
|
||||
const char* s1 = addStringToPool(pool, "hello world");
|
||||
const char* s2 = addStringToPool(pool, "hello");
|
||||
auto s1 = addStringToPool(pool, "hello world");
|
||||
auto s2 = addStringToPool(pool, "hello");
|
||||
|
||||
REQUIRE(s2 != s1);
|
||||
REQUIRE(s1->references == 1);
|
||||
REQUIRE(s2->references == 1);
|
||||
REQUIRE(pool.size() == sizeofString(11) + sizeofString(5));
|
||||
}
|
||||
|
||||
SECTION("Don't overrun") {
|
||||
const char* s1 = addStringToPool(pool, "hello world");
|
||||
const char* s2 = addStringToPool(pool, "wor");
|
||||
auto s1 = addStringToPool(pool, "hello world");
|
||||
auto s2 = addStringToPool(pool, "wor");
|
||||
|
||||
REQUIRE(s2 != s1);
|
||||
REQUIRE(s1->references == 1);
|
||||
REQUIRE(s2->references == 1);
|
||||
REQUIRE(pool.size() == sizeofString(11) + sizeofString(3));
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user