Tests: make allocator assertions more readable

This commit is contained in:
Benoit Blanchon
2023-07-26 06:06:38 +02:00
parent 30ec507989
commit 9a11d98117
31 changed files with 1127 additions and 1287 deletions

View File

@ -20,12 +20,13 @@ TEST_CASE("StringBuilder") {
str.startString();
str.save();
REQUIRE(resources.size() == sizeofString(0));
REQUIRE(resources.size() == sizeofString(""));
REQUIRE(resources.overflowed() == false);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(0)));
AllocatorLog{
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("")),
});
}
SECTION("Short string fits in first allocation") {
@ -37,29 +38,29 @@ TEST_CASE("StringBuilder") {
REQUIRE(str.isValid() == true);
REQUIRE(str.str() == "hello");
REQUIRE(resources.overflowed() == false);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31)));
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofStringBuffer()),
});
}
SECTION("Long string needs reallocation") {
StringBuilder str(&resources);
const char* lorem =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
"eiusmod tempor incididunt ut labore et dolore magna aliqua.";
str.startString();
str.append(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
"eiusmod tempor incididunt ut labore et dolore magna aliqua.");
str.append(lorem);
REQUIRE(str.isValid() == true);
REQUIRE(str.str() ==
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
"eiusmod tempor incididunt ut labore et dolore magna aliqua.");
REQUIRE(str.str() == lorem);
REQUIRE(resources.overflowed() == false);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(63))
<< AllocatorLog::Reallocate(sizeofString(63),
sizeofString(127)));
AllocatorLog{
Allocate(sizeofStringBuffer(1)),
Reallocate(sizeofStringBuffer(1), sizeofStringBuffer(2)),
Reallocate(sizeofStringBuffer(2), sizeofStringBuffer(3)),
});
}
SECTION("Realloc fails") {
@ -72,10 +73,11 @@ TEST_CASE("StringBuilder") {
"eiusmod tempor incididunt ut labore et dolore magna aliqua.");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::ReallocateFail(sizeofString(31),
sizeofString(63))
<< AllocatorLog::Deallocate(sizeofString(31)));
AllocatorLog{
Allocate(sizeofStringBuffer()),
ReallocateFail(sizeofStringBuffer(), sizeofStringBuffer(2)),
Deallocate(sizeofStringBuffer()),
});
REQUIRE(str.isValid() == false);
REQUIRE(resources.overflowed() == true);
}
@ -88,8 +90,9 @@ TEST_CASE("StringBuilder") {
REQUIRE(str.isValid() == false);
REQUIRE(resources.overflowed() == true);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocateFail(sizeofStringBuffer()),
});
}
}
@ -113,7 +116,7 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
REQUIRE(s1->references == 2);
REQUIRE(s2->references == 1);
REQUIRE(s3->references == 2);
REQUIRE(resources.size() == 2 * sizeofString(5));
REQUIRE(resources.size() == sizeofString("hello") + sizeofString("world"));
}
SECTION("Requires terminator") {
@ -123,7 +126,8 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
REQUIRE(s2 != s1);
REQUIRE(s1->references == 1);
REQUIRE(s2->references == 1);
REQUIRE(resources.size() == sizeofString(11) + sizeofString(5));
REQUIRE(resources.size() ==
sizeofString("hello world") + sizeofString("hello"));
}
SECTION("Don't overrun") {
@ -133,6 +137,7 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
REQUIRE(s2 != s1);
REQUIRE(s1->references == 1);
REQUIRE(s2->references == 1);
REQUIRE(resources.size() == sizeofString(11) + sizeofString(3));
REQUIRE(resources.size() ==
sizeofString("hello world") + sizeofString("wor"));
}
}

View File

@ -30,7 +30,7 @@ TEST_CASE("ResourceManager::saveString()") {
REQUIRE(b->length == 5);
REQUIRE(a->references == 1);
REQUIRE(b->references == 1);
REQUIRE(resources.size() == 2 * sizeofString(5));
REQUIRE(resources.size() == sizeofString("hello") + sizeofString("world"));
}
SECTION("Deduplicates identical strings") {
@ -39,7 +39,7 @@ TEST_CASE("ResourceManager::saveString()") {
REQUIRE(a == b);
REQUIRE(a->length == 5);
REQUIRE(a->references == 2);
REQUIRE(resources.size() == sizeofString(5));
REQUIRE(resources.size() == sizeofString("hello"));
}
SECTION("Deduplicates identical strings that contain NUL") {
@ -48,7 +48,7 @@ TEST_CASE("ResourceManager::saveString()") {
REQUIRE(a == b);
REQUIRE(a->length == 11);
REQUIRE(a->references == 2);
REQUIRE(resources.size() == sizeofString(11));
REQUIRE(resources.size() == sizeofString("hello world"));
}
SECTION("Don't stop on first NUL") {
@ -59,7 +59,8 @@ TEST_CASE("ResourceManager::saveString()") {
REQUIRE(b->length == 11);
REQUIRE(a->references == 1);
REQUIRE(b->references == 1);
REQUIRE(resources.size() == sizeofString(5) + sizeofString(11));
REQUIRE(resources.size() ==
sizeofString("hello") + sizeofString("hello world"));
}
SECTION("Returns NULL when allocation fails") {

View File

@ -17,7 +17,7 @@ TEST_CASE("ResourceManager::shrinkToFit()") {
SECTION("empty") {
resources.shrinkToFit();
REQUIRE(spyingAllocator.log() == AllocatorLog());
REQUIRE(spyingAllocator.log() == AllocatorLog{});
}
SECTION("only one pool") {
@ -26,9 +26,10 @@ TEST_CASE("ResourceManager::shrinkToFit()") {
resources.shrinkToFit();
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Reallocate(sizeofPool(),
sizeof(VariantSlot)));
AllocatorLog{
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeof(VariantSlot)),
});
}
SECTION("more pools than initial count") {
@ -37,20 +38,20 @@ TEST_CASE("ResourceManager::shrinkToFit()") {
i++)
resources.allocSlot();
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) *
ARDUINOJSON_INITIAL_POOL_COUNT
<< AllocatorLog::Allocate(sizeofPoolList(
ARDUINOJSON_INITIAL_POOL_COUNT * 2))
<< AllocatorLog::Allocate(sizeofPool()));
AllocatorLog{
Allocate(sizeofPool()) * ARDUINOJSON_INITIAL_POOL_COUNT,
Allocate(sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2)),
Allocate(sizeofPool()),
});
spyingAllocator.clearLog();
resources.shrinkToFit();
REQUIRE(spyingAllocator.log() ==
AllocatorLog()
<< AllocatorLog::Reallocate(sizeofPool(), sizeof(VariantSlot))
<< AllocatorLog::Reallocate(
sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2),
sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT + 1)));
AllocatorLog{
Reallocate(sizeofPool(), sizeof(VariantSlot)),
Reallocate(sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2),
sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT + 1)),
});
}
}

View File

@ -31,8 +31,9 @@ TEST_CASE("ResourceManager::swap()") {
REQUIRE(a1->data() == b.getSlot(a1.id())->data());
REQUIRE(b1->data() == a.getSlot(b1.id())->data());
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool()) * 2);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()) * 2,
});
}
SECTION("Only left using preallocated pool list") {
@ -48,12 +49,12 @@ TEST_CASE("ResourceManager::swap()") {
REQUIRE(a1->data() == b.getSlot(a1.id())->data());
REQUIRE(b1->data() == a.getSlot(b1.id())->data());
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool()) *
(ARDUINOJSON_INITIAL_POOL_COUNT + 1)
<< AllocatorLog::Allocate(sizeofPoolList(
ARDUINOJSON_INITIAL_POOL_COUNT * 2))
<< AllocatorLog::Allocate(sizeofPool()));
REQUIRE(spy.log() ==
AllocatorLog{
Allocate(sizeofPool()) * (ARDUINOJSON_INITIAL_POOL_COUNT + 1),
Allocate(sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2)),
Allocate(sizeofPool()),
});
}
SECTION("Only right using preallocated pool list") {
@ -69,12 +70,12 @@ TEST_CASE("ResourceManager::swap()") {
REQUIRE(a1->data() == b.getSlot(a1.id())->data());
REQUIRE(b1->data() == a.getSlot(b1.id())->data());
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool()) *
ARDUINOJSON_INITIAL_POOL_COUNT
<< AllocatorLog::Allocate(sizeofPoolList(
ARDUINOJSON_INITIAL_POOL_COUNT * 2))
<< AllocatorLog::Allocate(sizeofPool()) * 2);
REQUIRE(spy.log() ==
AllocatorLog{
Allocate(sizeofPool()) * ARDUINOJSON_INITIAL_POOL_COUNT,
Allocate(sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2)),
Allocate(sizeofPool()) * 2,
});
}
SECTION("None is using preallocated pool list") {