Tests: replace constants with sizeofString(n)

This commit is contained in:
Benoit Blanchon
2023-04-07 14:43:16 +02:00
parent 5e0e35615c
commit e9850152a7
18 changed files with 174 additions and 148 deletions

View File

@ -69,7 +69,7 @@ TEST_CASE("StringCopier::save() deduplicates strings") {
REQUIRE(s1 == s3);
REQUIRE(s2 != s3);
REQUIRE(pool.size() == 12);
REQUIRE(pool.size() == 2 * sizeofString(5));
}
SECTION("Requires terminator") {
@ -77,7 +77,7 @@ TEST_CASE("StringCopier::save() deduplicates strings") {
const char* s2 = addStringToPool(pool, "hello");
REQUIRE(s2 != s1);
REQUIRE(pool.size() == 12 + 6);
REQUIRE(pool.size() == sizeofString(11) + sizeofString(5));
}
SECTION("Don't overrun") {
@ -85,6 +85,6 @@ TEST_CASE("StringCopier::save() deduplicates strings") {
const char* s2 = addStringToPool(pool, "wor");
REQUIRE(s2 != s1);
REQUIRE(pool.size() == 12 + 4);
REQUIRE(pool.size() == sizeofString(11) + sizeofString(3));
}
}

View File

@ -21,8 +21,8 @@ TEST_CASE("MemoryPool::clear()") {
}
SECTION("Discards allocated strings") {
pool.saveString(adaptString(const_cast<char*>("123456789")));
REQUIRE(pool.size() == 10);
pool.saveString(adaptString("123456789"));
REQUIRE(pool.size() == sizeofString(9));
pool.clear();

View File

@ -11,7 +11,7 @@
using namespace ArduinoJson::detail;
static const char* saveString(MemoryPool& pool, const char* s) {
return pool.saveString(adaptString(const_cast<char*>(s)));
return pool.saveString(adaptString(s));
}
static const char* saveString(MemoryPool& pool, const char* s, size_t n) {
@ -25,21 +25,20 @@ TEST_CASE("MemoryPool::saveString()") {
const char* a = saveString(pool, "hello");
const char* b = saveString(pool, "world");
REQUIRE(a != b);
REQUIRE(pool.size() == 6 + 6);
REQUIRE(pool.size() == 2 * sizeofString(5));
}
SECTION("Deduplicates identical strings") {
const char* a = saveString(pool, "hello");
const char* b = saveString(pool, "hello");
REQUIRE(a == b);
REQUIRE(pool.size() == 6);
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);
REQUIRE(a == b);
REQUIRE(pool.size() == 12);
}
SECTION("Reuse part of a string if it ends with NUL") {
@ -47,13 +46,14 @@ TEST_CASE("MemoryPool::saveString()") {
const char* b = saveString(pool, "hello");
REQUIRE(a == b);
REQUIRE(pool.size() == 12);
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);
REQUIRE(a != b);
REQUIRE(pool.size() == 18);
REQUIRE(pool.size() == sizeofString(5) + sizeofString(11));
}
SECTION("Returns NULL when full") {