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

@ -8,7 +8,6 @@
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofString;
TEST_CASE("deserialize JSON array") {
SpyingAllocator spy;
@ -254,9 +253,10 @@ TEST_CASE("deserialize JSON array") {
JsonArray arr = doc.as<JsonArray>();
REQUIRE(arr.size() == 0);
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Deallocate(sizeofPool()));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Deallocate(sizeofPool()),
});
}
}
@ -307,10 +307,11 @@ TEST_CASE("deserialize JSON array under memory constraints") {
SECTION("don't store space characters") {
deserializeJson(doc, " [ \"1234567\" ] ");
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(7)));
REQUIRE(spy.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("1234567")),
});
}
}

View File

@ -13,7 +13,6 @@
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
TEST_CASE("Filtering") {
struct TestCase {
@ -49,7 +48,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"abcdefg\":\"hijklmn\"}",
sizeofObject(1) + 2*sizeofString(7)
sizeofObject(1) + sizeofString("abcdefg") + sizeofString("hijklmn")
},
{
"{\"hello\":\"world\"}",
@ -75,7 +74,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":null}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// Member is a number, but filter wants an array
@ -84,7 +83,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":null}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// Input is an array, but filter wants an object
@ -120,7 +119,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// skip a float
@ -129,7 +128,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// skip false
@ -138,7 +137,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// skip true
@ -147,7 +146,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// skip null
@ -156,7 +155,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// can skip a double-quoted string
@ -165,7 +164,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// can skip a single-quoted string
@ -174,7 +173,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// can skip an empty array
@ -183,7 +182,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// can skip an empty array with spaces in it
@ -192,7 +191,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// can skip an array
@ -201,7 +200,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// can skip an array with spaces in it
@ -210,7 +209,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// can skip an empty object
@ -219,7 +218,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// can skip an empty object with spaces in it
@ -228,7 +227,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// can skip an object
@ -237,7 +236,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// skip an object with spaces in it
@ -246,7 +245,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
"{\"an_integer\": 0,\"example\":{\"type\":\"int\",\"outcome\":42}}",
@ -254,7 +253,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":{\"outcome\":42}}",
2 * sizeofObject(1) + 2*sizeofString(7)
2 * sizeofObject(1) + 2*sizeofString("example")
},
{
// wildcard
@ -263,7 +262,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":{\"outcome\":42}}",
2 * sizeofObject(1) + 2*sizeofString(7)
2 * sizeofObject(1) + 2*sizeofString("example")
},
{
// exclusion filter (issue #1628)
@ -272,7 +271,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":1}",
sizeofObject(1) + sizeofString(7)
sizeofObject(1) + sizeofString("example")
},
{
// only the first element of array counts
@ -299,7 +298,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"[{\"example\":1},{\"example\":3}]",
sizeofArray(2) + 2 * sizeofObject(1) + sizeofString(7)
sizeofArray(2) + 2 * sizeofObject(1) + sizeofString("example")
},
{
"[',2,3]",

View File

@ -11,7 +11,6 @@
#include "CustomReader.hpp"
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
TEST_CASE("deserializeJson(char*)") {
SpyingAllocator spy;
@ -23,14 +22,14 @@ TEST_CASE("deserializeJson(char*)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(5))
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(5)));
REQUIRE(spy.log() ==
AllocatorLog{
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("world")),
});
}
TEST_CASE("deserializeJson(unsigned char*, unsigned int)") { // issue #1897

View File

@ -117,8 +117,9 @@ TEST_CASE("deserializeJson(JsonDocument&)") {
deserializeJson(doc, "{}");
REQUIRE(doc.is<JsonObject>());
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Deallocate(sizeofPool()));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Deallocate(sizeofPool()),
});
}
}

View File

@ -8,7 +8,6 @@
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
TEST_CASE("deserialize JSON object") {
SpyingAllocator spy;
@ -285,26 +284,19 @@ TEST_CASE("deserialize JSON object") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"a\":2}");
REQUIRE(spy.log() ==
AllocatorLog()
// a
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(1))
// pool
<< AllocatorLog::Allocate(sizeofPool())
// b
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(1))
// c
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(1))
// string builder
<< AllocatorLog::Allocate(sizeofString(31))
// remove b & c
<< AllocatorLog::Deallocate(sizeofString(1)) * 2
// string builder
<< AllocatorLog::Deallocate(sizeofString(31))
);
AllocatorLog{
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("a")),
Allocate(sizeofPool()),
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("b")),
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("c")),
Allocate(sizeofStringBuffer()),
Deallocate(sizeofString("b")),
Deallocate(sizeofString("c")),
Deallocate(sizeofStringBuffer()),
});
}
SECTION("Repeated key with zero copy mode") { // issue #1697
@ -331,22 +323,17 @@ TEST_CASE("deserialize JSON object") {
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 0);
REQUIRE(spy.log() == AllocatorLog()
// string "hello"
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(5))
// pool
<< AllocatorLog::Allocate(sizeofPool())
// string "world"
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(5))
// free pool
<< AllocatorLog::Deallocate(sizeofPool())
// free "hello" and "world"
<< AllocatorLog::Deallocate(sizeofString(5))
<< AllocatorLog::Deallocate(sizeofString(5)));
REQUIRE(spy.log() ==
AllocatorLog{
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("world")),
Deallocate(sizeofPool()),
Deallocate(sizeofString("hello")),
Deallocate(sizeofString("world")),
});
}
SECTION("Issue #1335") {

View File

@ -10,7 +10,6 @@
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
TEST_CASE("Valid JSON strings value") {
struct TestCase {
@ -106,39 +105,43 @@ TEST_CASE("Allocation of the key fails") {
SECTION("Quoted string, first member") {
REQUIRE(deserializeJson(doc, "{\"example\":1}") ==
DeserializationError::NoMemory);
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::AllocateFail(sizeofString(31)));
REQUIRE(spy.log() == AllocatorLog{
AllocateFail(sizeofStringBuffer()),
});
}
SECTION("Quoted string, second member") {
timebomb.setCountdown(3);
REQUIRE(deserializeJson(doc, "{\"hello\":1,\"world\"}") ==
DeserializationError::NoMemory);
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(5))
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::AllocateFail(sizeofString(31)));
REQUIRE(spy.log() ==
AllocatorLog{
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
Allocate(sizeofPool()),
AllocateFail(sizeofStringBuffer()),
});
}
SECTION("Non-Quoted string, first member") {
REQUIRE(deserializeJson(doc, "{example:1}") ==
DeserializationError::NoMemory);
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::AllocateFail(sizeofString(31)));
REQUIRE(spy.log() == AllocatorLog{
AllocateFail(sizeofStringBuffer()),
});
}
SECTION("Non-Quoted string, second member") {
timebomb.setCountdown(3);
REQUIRE(deserializeJson(doc, "{hello:1,world}") ==
DeserializationError::NoMemory);
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(5))
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::AllocateFail(sizeofString(31)));
REQUIRE(spy.log() ==
AllocatorLog{
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
Allocate(sizeofPool()),
AllocateFail(sizeofStringBuffer()),
});
}
}
@ -149,8 +152,9 @@ TEST_CASE("String allocation fails") {
SECTION("Input is const char*") {
REQUIRE(deserializeJson(doc, "\"hello\"") ==
DeserializationError::NoMemory);
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::AllocateFail(sizeofString(31)));
REQUIRE(spy.log() == AllocatorLog{
AllocateFail(sizeofStringBuffer()),
});
}
}
@ -160,17 +164,14 @@ TEST_CASE("Deduplicate values") {
deserializeJson(doc, "[\"example\",\"example\"]");
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
REQUIRE(spy.log() == AllocatorLog()
// pool
<< AllocatorLog::Allocate(sizeofPool())
// string builder
<< AllocatorLog::Allocate(sizeofString(31))
// string "example"
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(7))
// string builder
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Deallocate(sizeofString(31)));
REQUIRE(spy.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("example")),
Allocate(sizeofStringBuffer()),
Deallocate(sizeofStringBuffer()),
});
}
TEST_CASE("Deduplicate keys") {
@ -182,15 +183,12 @@ TEST_CASE("Deduplicate keys") {
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
CHECK(key1 == key2);
REQUIRE(spy.log() == AllocatorLog()
// pool
<< AllocatorLog::Allocate(sizeofPool())
// string builder
<< AllocatorLog::Allocate(sizeofString(31))
// string "example"
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(7))
// string builder
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Deallocate(sizeofString(31)));
REQUIRE(spy.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("example")),
Allocate(sizeofStringBuffer()),
Deallocate(sizeofStringBuffer()),
});
}