mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-16 20:12:16 +02:00
Added string deduplication (closes #1303)
This commit is contained in:
@ -38,8 +38,47 @@ TEST_CASE("StringCopier") {
|
||||
|
||||
str.startString(&pool);
|
||||
str.append('h');
|
||||
str.commit(&pool);
|
||||
str.save(&pool);
|
||||
|
||||
REQUIRE(1 == pool.size());
|
||||
}
|
||||
}
|
||||
|
||||
static const char* addStringToPool(MemoryPool* pool, const char* s) {
|
||||
StringCopier str;
|
||||
str.startString(pool);
|
||||
str.append(s);
|
||||
str.append('\0');
|
||||
return str.save(pool);
|
||||
}
|
||||
|
||||
TEST_CASE("StringCopier::save() deduplicates strings") {
|
||||
char buffer[4096];
|
||||
MemoryPool pool(buffer, 4096);
|
||||
|
||||
SECTION("Basic") {
|
||||
const char* s1 = addStringToPool(&pool, "hello");
|
||||
const char* s2 = addStringToPool(&pool, "world");
|
||||
const char* s3 = addStringToPool(&pool, "hello");
|
||||
|
||||
REQUIRE(s1 == s3);
|
||||
REQUIRE(s2 != s3);
|
||||
REQUIRE(pool.size() == 12);
|
||||
}
|
||||
|
||||
SECTION("Requires terminator") {
|
||||
const char* s1 = addStringToPool(&pool, "hello world");
|
||||
const char* s2 = addStringToPool(&pool, "hello");
|
||||
|
||||
REQUIRE(s2 != s1);
|
||||
REQUIRE(pool.size() == 12 + 6);
|
||||
}
|
||||
|
||||
SECTION("Don't overrun") {
|
||||
const char* s1 = addStringToPool(&pool, "hello world");
|
||||
const char* s2 = addStringToPool(&pool, "wor");
|
||||
|
||||
REQUIRE(s2 != s1);
|
||||
REQUIRE(pool.size() == 12 + 4);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user