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

@ -10,7 +10,6 @@
#endif #endif
using ArduinoJson::detail::sizeofArray; using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofString;
TEST_CASE("string_view") { TEST_CASE("string_view") {
SpyingAllocator spy; SpyingAllocator spy;
@ -63,10 +62,11 @@ TEST_CASE("string_view") {
doc.add(std::string_view("example\0tree", 12)); doc.add(std::string_view("example\0tree", 12));
doc.add(std::string_view("example\0tree and a half", 12)); doc.add(std::string_view("example\0tree and a half", 12));
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(7)) Allocate(sizeofString("example")),
<< AllocatorLog::Allocate(sizeofString(12))); Allocate(sizeofString("example tree")),
});
} }
SECTION("as<std::string_view>()") { SECTION("as<std::string_view>()") {

View File

@ -5,6 +5,7 @@
#pragma once #pragma once
#include <ArduinoJson/Memory/Allocator.hpp> #include <ArduinoJson/Memory/Allocator.hpp>
#include <ArduinoJson/Memory/StringBuilder.hpp>
#include <ArduinoJson/Memory/VariantPool.hpp> #include <ArduinoJson/Memory/VariantPool.hpp>
#include <sstream> #include <sstream>
@ -30,63 +31,68 @@ struct FailingAllocator : ArduinoJson::Allocator {
} }
}; };
class AllocatorLogEntry {
public:
AllocatorLogEntry(std::string s, size_t n = 1) : str_(s), count_(n) {}
const std::string& str() const {
return str_;
}
size_t count() const {
return count_;
}
AllocatorLogEntry operator*(size_t n) const {
return AllocatorLogEntry(str_, n);
}
private:
std::string str_;
size_t count_;
};
inline AllocatorLogEntry Allocate(size_t s) {
char buffer[32];
sprintf(buffer, "allocate(%zu)", s);
return AllocatorLogEntry(buffer);
}
inline AllocatorLogEntry AllocateFail(size_t s) {
char buffer[32];
sprintf(buffer, "allocate(%zu) -> nullptr", s);
return AllocatorLogEntry(buffer);
}
inline AllocatorLogEntry Reallocate(size_t s1, size_t s2) {
char buffer[32];
sprintf(buffer, "reallocate(%zu, %zu)", s1, s2);
return AllocatorLogEntry(buffer);
}
inline AllocatorLogEntry ReallocateFail(size_t s1, size_t s2) {
char buffer[32];
sprintf(buffer, "reallocate(%zu, %zu) -> nullptr", s1, s2);
return AllocatorLogEntry(buffer);
}
inline AllocatorLogEntry Deallocate(size_t s) {
char buffer[32];
sprintf(buffer, "deallocate(%zu)", s);
return AllocatorLogEntry(buffer);
}
class AllocatorLog { class AllocatorLog {
public: public:
class Entry { AllocatorLog() = default;
public: AllocatorLog(std::initializer_list<AllocatorLogEntry> list) {
Entry(std::string s, size_t n = 1) : str_(s), count_(n) {} for (auto& entry : list)
append(entry);
const std::string& str() const {
return str_;
}
size_t count() const {
return count_;
}
Entry operator*(size_t n) const {
return Entry(str_, n);
}
private:
std::string str_;
size_t count_;
};
static Entry Allocate(size_t s) {
char buffer[32];
sprintf(buffer, "allocate(%zu)", s);
return Entry(buffer);
} }
static Entry AllocateFail(size_t s) { void append(const AllocatorLogEntry& entry) {
char buffer[32];
sprintf(buffer, "allocate(%zu) -> nullptr", s);
return Entry(buffer);
}
static Entry Reallocate(size_t s1, size_t s2) {
char buffer[32];
sprintf(buffer, "reallocate(%zu, %zu)", s1, s2);
return Entry(buffer);
};
static Entry ReallocateFail(size_t s1, size_t s2) {
char buffer[32];
sprintf(buffer, "reallocate(%zu, %zu) -> nullptr", s1, s2);
return Entry(buffer);
};
static Entry Deallocate(size_t s) {
char buffer[32];
sprintf(buffer, "deallocate(%zu)", s);
return Entry(buffer);
};
AllocatorLog& operator<<(const Entry& entry) {
for (size_t i = 0; i < entry.count(); i++) for (size_t i = 0; i < entry.count(); i++)
log_ << entry.str() << "\n"; log_ << entry.str() << "\n";
return *this;
} }
std::string str() const { std::string str() const {
@ -125,12 +131,12 @@ class SpyingAllocator : public ArduinoJson::Allocator {
auto block = reinterpret_cast<AllocatedBlock*>( auto block = reinterpret_cast<AllocatedBlock*>(
upstream_->allocate(sizeof(AllocatedBlock) + n - 1)); upstream_->allocate(sizeof(AllocatedBlock) + n - 1));
if (block) { if (block) {
log_ << AllocatorLog::Allocate(n); log_.append(Allocate(n));
allocatedBytes_ += n; allocatedBytes_ += n;
block->size = n; block->size = n;
return block->payload; return block->payload;
} else { } else {
log_ << AllocatorLog::AllocateFail(n); log_.append(AllocateFail(n));
return nullptr; return nullptr;
} }
} }
@ -138,7 +144,7 @@ class SpyingAllocator : public ArduinoJson::Allocator {
void deallocate(void* p) override { void deallocate(void* p) override {
auto block = AllocatedBlock::fromPayload(p); auto block = AllocatedBlock::fromPayload(p);
allocatedBytes_ -= block->size; allocatedBytes_ -= block->size;
log_ << AllocatorLog::Deallocate(block ? block->size : 0); log_.append(Deallocate(block ? block->size : 0));
upstream_->deallocate(block); upstream_->deallocate(block);
} }
@ -148,12 +154,12 @@ class SpyingAllocator : public ArduinoJson::Allocator {
block = reinterpret_cast<AllocatedBlock*>( block = reinterpret_cast<AllocatedBlock*>(
upstream_->reallocate(block, sizeof(AllocatedBlock) + n - 1)); upstream_->reallocate(block, sizeof(AllocatedBlock) + n - 1));
if (block) { if (block) {
log_ << AllocatorLog::Reallocate(oldSize, n); log_.append(Reallocate(oldSize, n));
block->size = n; block->size = n;
allocatedBytes_ += n - oldSize; allocatedBytes_ += n - oldSize;
return block->payload; return block->payload;
} else { } else {
log_ << AllocatorLog::ReallocateFail(oldSize, n); log_.append(ReallocateFail(oldSize, n));
return nullptr; return nullptr;
} }
} }
@ -259,3 +265,15 @@ inline size_t sizeofPool(
ArduinoJson::detail::SlotCount n = ARDUINOJSON_POOL_CAPACITY) { ArduinoJson::detail::SlotCount n = ARDUINOJSON_POOL_CAPACITY) {
return ArduinoJson::detail::VariantPool::slotsToBytes(n); return ArduinoJson::detail::VariantPool::slotsToBytes(n);
} }
inline size_t sizeofStringBuffer(size_t iteration = 1) {
// returns 31, 63, 127, 255, etc.
auto capacity = ArduinoJson::detail::StringBuilder::initialCapacity;
for (size_t i = 1; i < iteration; i++)
capacity = capacity * 2 + 1;
return ArduinoJson::detail::sizeofString(capacity);
}
inline size_t sizeofString(const char* s) {
return ArduinoJson::detail::sizeofString(strlen(s));
}

View File

@ -8,7 +8,6 @@
#include "Allocators.hpp" #include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray; using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonArray::add()") { TEST_CASE("JsonArray::add()") {
SpyingAllocator spy; SpyingAllocator spy;
@ -102,49 +101,56 @@ TEST_CASE("JsonArray::add()") {
SECTION("should not duplicate const char*") { SECTION("should not duplicate const char*") {
array.add("world"); array.add("world");
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool())); Allocate(sizeofPool()),
});
} }
SECTION("should duplicate char*") { SECTION("should duplicate char*") {
array.add(const_cast<char*>("world")); array.add(const_cast<char*>("world"));
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5))); Allocate(sizeofString("world")),
});
} }
SECTION("should duplicate std::string") { SECTION("should duplicate std::string") {
array.add(std::string("world")); array.add(std::string("world"));
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5))); Allocate(sizeofString("world")),
});
} }
SECTION("should duplicate serialized(const char*)") { SECTION("should duplicate serialized(const char*)") {
array.add(serialized("{}")); array.add(serialized("{}"));
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(2))); Allocate(sizeofString("{}")),
});
} }
SECTION("should duplicate serialized(char*)") { SECTION("should duplicate serialized(char*)") {
array.add(serialized(const_cast<char*>("{}"))); array.add(serialized(const_cast<char*>("{}")));
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(2))); Allocate(sizeofString("{}")),
});
} }
SECTION("should duplicate serialized(std::string)") { SECTION("should duplicate serialized(std::string)") {
array.add(serialized(std::string("{}"))); array.add(serialized(std::string("{}")));
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(2))); Allocate(sizeofString("{}")),
});
} }
SECTION("should duplicate serialized(std::string)") { SECTION("should duplicate serialized(std::string)") {
array.add(serialized(std::string("\0XX", 3))); array.add(serialized(std::string("\0XX", 3)));
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(3))); Allocate(sizeofString(" XX")),
});
} }
} }

View File

@ -39,7 +39,8 @@ TEST_CASE("JsonArray::clear()") {
for (int i = 0; i < ARDUINOJSON_POOL_CAPACITY; i++) for (int i = 0; i < ARDUINOJSON_POOL_CAPACITY; i++)
array.add(i); array.add(i);
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool())); Allocate(sizeofPool()),
});
} }
} }

View File

@ -105,7 +105,7 @@ TEST_CASE("Removed elements are recycled") {
// add one element; it should use the free slot // add one element; it should use the free slot
array.add(42); array.add(42);
REQUIRE(spy.log() == AllocatorLog() << AllocatorLog::Allocate( REQUIRE(spy.log() == AllocatorLog{
sizeofPool()) // only one pool Allocate(sizeofPool()), // only one pool
); });
} }

View File

@ -9,7 +9,6 @@
#include "Allocators.hpp" #include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray; using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonArray::operator[]") { TEST_CASE("JsonArray::operator[]") {
SpyingAllocator spy; SpyingAllocator spy;
@ -118,22 +117,25 @@ TEST_CASE("JsonArray::operator[]") {
SECTION("should not duplicate const char*") { SECTION("should not duplicate const char*") {
array[0] = "world"; array[0] = "world";
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool())); Allocate(sizeofPool()),
});
} }
SECTION("should duplicate char*") { SECTION("should duplicate char*") {
array[0] = const_cast<char*>("world"); array[0] = const_cast<char*>("world");
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5))); Allocate(sizeofString("world")),
});
} }
SECTION("should duplicate std::string") { SECTION("should duplicate std::string") {
array[0] = std::string("world"); array[0] = std::string("world");
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5))); Allocate(sizeofString("world")),
});
} }
SECTION("array[0].to<JsonObject>()") { SECTION("array[0].to<JsonObject>()") {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,7 +6,6 @@
#include <catch.hpp> #include <catch.hpp>
typedef ArduinoJson::detail::ElementProxy<JsonDocument&> ElementProxy; typedef ArduinoJson::detail::ElementProxy<JsonDocument&> ElementProxy;
using ArduinoJson::detail::sizeofString;
TEST_CASE("ElementProxy::add()") { TEST_CASE("ElementProxy::add()") {
JsonDocument doc; JsonDocument doc;

View File

@ -12,7 +12,6 @@
using ArduinoJson::detail::sizeofArray; using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject; using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
typedef ArduinoJson::detail::MemberProxy<JsonDocument&, const char*> typedef ArduinoJson::detail::MemberProxy<JsonDocument&, const char*>
MemberProxy; MemberProxy;
@ -326,9 +325,10 @@ TEST_CASE("Deduplicate keys") {
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str(); const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
CHECK(key1 == key2); CHECK(key1 == key2);
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(7))); Allocate(sizeofString("example")),
});
} }
SECTION("char*") { SECTION("char*") {
@ -340,9 +340,10 @@ TEST_CASE("Deduplicate keys") {
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str(); const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
CHECK(key1 == key2); CHECK(key1 == key2);
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(7))); Allocate(sizeofString("example")),
});
} }
SECTION("Arduino String") { SECTION("Arduino String") {
@ -353,9 +354,10 @@ TEST_CASE("Deduplicate keys") {
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str(); const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
CHECK(key1 == key2); CHECK(key1 == key2);
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(7))); Allocate(sizeofString("example")),
});
} }
SECTION("Flash string") { SECTION("Flash string") {
@ -366,9 +368,10 @@ TEST_CASE("Deduplicate keys") {
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str(); const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
CHECK(key1 == key2); CHECK(key1 == key2);
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(7))); Allocate(sizeofString("example")),
});
} }
} }
@ -385,7 +388,8 @@ TEST_CASE("MemberProxy under memory constraints") {
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
REQUIRE(doc.size() == 0); REQUIRE(doc.size() == 0);
REQUIRE(doc.overflowed() == true); REQUIRE(doc.overflowed() == true);
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::AllocateFail(sizeofString(5))); AllocateFail(sizeofString("hello")),
});
} }
} }

View File

@ -11,7 +11,6 @@
#include "Allocators.hpp" #include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray; using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonDocument::add()") { TEST_CASE("JsonDocument::add()") {
SpyingAllocator spy; SpyingAllocator spy;
@ -21,16 +20,18 @@ TEST_CASE("JsonDocument::add()") {
doc.add(42); doc.add(42);
REQUIRE(doc.as<std::string>() == "[42]"); REQUIRE(doc.as<std::string>() == "[42]");
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool())); Allocate(sizeofPool()),
});
} }
SECTION("const char*") { SECTION("const char*") {
doc.add("hello"); doc.add("hello");
REQUIRE(doc.as<std::string>() == "[\"hello\"]"); REQUIRE(doc.as<std::string>() == "[\"hello\"]");
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool())); Allocate(sizeofPool()),
});
} }
SECTION("std::string") { SECTION("std::string") {
@ -38,9 +39,10 @@ TEST_CASE("JsonDocument::add()") {
doc.add(std::string("example")); doc.add(std::string("example"));
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>()); CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(7))); Allocate(sizeofString("example")),
});
} }
SECTION("char*") { SECTION("char*") {
@ -49,9 +51,10 @@ TEST_CASE("JsonDocument::add()") {
doc.add(value); doc.add(value);
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>()); CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(7))); Allocate(sizeofString("example")),
});
} }
SECTION("Arduino String") { SECTION("Arduino String") {
@ -59,9 +62,10 @@ TEST_CASE("JsonDocument::add()") {
doc.add(String("example")); doc.add(String("example"));
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>()); CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(7))); Allocate(sizeofString("example")),
});
} }
SECTION("Flash string") { SECTION("Flash string") {
@ -69,8 +73,9 @@ TEST_CASE("JsonDocument::add()") {
doc.add(F("example")); doc.add(F("example"));
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>()); CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(7))); Allocate(sizeofString("example")),
});
} }
} }

View File

@ -7,8 +7,6 @@
#include "Allocators.hpp" #include "Allocators.hpp"
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonDocument assignment") { TEST_CASE("JsonDocument assignment") {
SpyingAllocator spyingAllocator; SpyingAllocator spyingAllocator;
@ -22,11 +20,11 @@ TEST_CASE("JsonDocument assignment") {
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}"); REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofString(5)) // hello Allocate(sizeofString("hello")),
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5)) // world Allocate(sizeofString("world")),
); });
} }
SECTION("Copy assignment reallocates when capacity is smaller") { SECTION("Copy assignment reallocates when capacity is smaller") {
@ -38,11 +36,11 @@ TEST_CASE("JsonDocument assignment") {
doc2 = doc1; doc2 = doc1;
REQUIRE(doc2.as<std::string>() == "[{\"hello\":\"world\"}]"); REQUIRE(doc2.as<std::string>() == "[{\"hello\":\"world\"}]");
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5)) // hello Allocate(sizeofString("hello")),
<< AllocatorLog::Allocate(sizeofString(5)) // world Allocate(sizeofString("world")),
); });
} }
SECTION("Copy assignment reallocates when capacity is larger") { SECTION("Copy assignment reallocates when capacity is larger") {
@ -54,11 +52,11 @@ TEST_CASE("JsonDocument assignment") {
doc2 = doc1; doc2 = doc1;
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}"); REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofString(5)) // hello Allocate(sizeofString("hello")),
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5)) // world Allocate(sizeofString("world")),
); });
} }
SECTION("Move assign") { SECTION("Move assign") {
@ -72,14 +70,14 @@ TEST_CASE("JsonDocument assignment") {
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}"); REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(doc1.as<std::string>() == "null"); REQUIRE(doc1.as<std::string>() == "null");
} }
REQUIRE( REQUIRE(spyingAllocator.log() == AllocatorLog{
spyingAllocator.log() == Allocate(sizeofString("hello")),
AllocatorLog() << AllocatorLog::Allocate(sizeofString(5)) // hello Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofString("world")),
<< AllocatorLog::Allocate(sizeofString(5)) // world Deallocate(sizeofString("hello")),
<< AllocatorLog::Deallocate(sizeofString(5)) // hello Deallocate(sizeofString("world")),
<< AllocatorLog::Deallocate(sizeofString(5)) // world Deallocate(sizeofPool()),
<< AllocatorLog::Deallocate(sizeofPool())); });
} }
SECTION("Assign from JsonObject") { SECTION("Assign from JsonObject") {

View File

@ -8,14 +8,13 @@
#include "Allocators.hpp" #include "Allocators.hpp"
using ArduinoJson::detail::addPadding; using ArduinoJson::detail::addPadding;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonDocument constructor") { TEST_CASE("JsonDocument constructor") {
SpyingAllocator spyingAllocator; SpyingAllocator spyingAllocator;
SECTION("JsonDocument(size_t)") { SECTION("JsonDocument(size_t)") {
{ JsonDocument doc(&spyingAllocator); } { JsonDocument doc(&spyingAllocator); }
REQUIRE(spyingAllocator.log() == AllocatorLog()); REQUIRE(spyingAllocator.log() == AllocatorLog{});
} }
SECTION("JsonDocument(const JsonDocument&)") { SECTION("JsonDocument(const JsonDocument&)") {
@ -28,11 +27,12 @@ TEST_CASE("JsonDocument constructor") {
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!"); REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!"); REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
} }
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31)) Allocate(sizeofStringBuffer()),
<< AllocatorLog::Allocate(sizeofString(31)) Allocate(sizeofStringBuffer()),
<< AllocatorLog::Deallocate(sizeofString(31)) Deallocate(sizeofStringBuffer()),
<< AllocatorLog::Deallocate(sizeofString(31))); Deallocate(sizeofStringBuffer()),
});
} }
SECTION("JsonDocument(JsonDocument&&)") { SECTION("JsonDocument(JsonDocument&&)") {
@ -45,9 +45,10 @@ TEST_CASE("JsonDocument constructor") {
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!"); REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
REQUIRE(doc1.as<std::string>() == "null"); REQUIRE(doc1.as<std::string>() == "null");
} }
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31)) Allocate(sizeofStringBuffer()),
<< AllocatorLog::Deallocate(sizeofString(31))); Deallocate(sizeofStringBuffer()),
});
} }
SECTION("JsonDocument(JsonObject)") { SECTION("JsonDocument(JsonObject)") {
@ -58,8 +59,9 @@ TEST_CASE("JsonDocument constructor") {
JsonDocument doc2(obj, &spyingAllocator); JsonDocument doc2(obj, &spyingAllocator);
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}"); REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofPool())); Allocate(sizeofPool()),
});
} }
SECTION("Construct from JsonArray") { SECTION("Construct from JsonArray") {
@ -70,8 +72,9 @@ TEST_CASE("JsonDocument constructor") {
JsonDocument doc2(arr, &spyingAllocator); JsonDocument doc2(arr, &spyingAllocator);
REQUIRE(doc2.as<std::string>() == "[\"hello\"]"); REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofPool())); Allocate(sizeofPool()),
});
} }
SECTION("Construct from JsonVariant") { SECTION("Construct from JsonVariant") {
@ -81,7 +84,8 @@ TEST_CASE("JsonDocument constructor") {
JsonDocument doc2(doc1.as<JsonVariant>(), &spyingAllocator); JsonDocument doc2(doc1.as<JsonVariant>(), &spyingAllocator);
REQUIRE(doc2.as<std::string>() == "hello"); REQUIRE(doc2.as<std::string>() == "hello");
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofString(5))); Allocate(sizeofString("hello")),
});
} }
} }

View File

@ -12,7 +12,6 @@
using ArduinoJson::detail::sizeofArray; using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject; using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
class ArmoredAllocator : public Allocator { class ArmoredAllocator : public Allocator {
public: public:
@ -48,7 +47,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
doc.shrinkToFit(); doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "null"); REQUIRE(doc.as<std::string>() == "null");
REQUIRE(spyingAllocator.log() == AllocatorLog()); REQUIRE(spyingAllocator.log() == AllocatorLog{});
} }
SECTION("empty object") { SECTION("empty object") {
@ -57,7 +56,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
doc.shrinkToFit(); doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{}"); REQUIRE(doc.as<std::string>() == "{}");
REQUIRE(spyingAllocator.log() == AllocatorLog()); REQUIRE(spyingAllocator.log() == AllocatorLog{});
} }
SECTION("empty array") { SECTION("empty array") {
@ -66,7 +65,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
doc.shrinkToFit(); doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[]"); REQUIRE(doc.as<std::string>() == "[]");
REQUIRE(spyingAllocator.log() == AllocatorLog()); REQUIRE(spyingAllocator.log() == AllocatorLog{});
} }
SECTION("linked string") { SECTION("linked string") {
@ -75,7 +74,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
doc.shrinkToFit(); doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "hello"); REQUIRE(doc.as<std::string>() == "hello");
REQUIRE(spyingAllocator.log() == AllocatorLog()); REQUIRE(spyingAllocator.log() == AllocatorLog{});
} }
SECTION("owned string") { SECTION("owned string") {
@ -85,8 +84,9 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
doc.shrinkToFit(); doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "abcdefg"); REQUIRE(doc.as<std::string>() == "abcdefg");
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofString(7))); Allocate(sizeofString("abcdefg")),
});
} }
SECTION("raw string") { SECTION("raw string") {
@ -95,8 +95,9 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
doc.shrinkToFit(); doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[{},12]"); REQUIRE(doc.as<std::string>() == "[{},12]");
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofString(7))); Allocate(sizeofString("[{},12]")),
});
} }
SECTION("linked key") { SECTION("linked key") {
@ -105,10 +106,11 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
doc.shrinkToFit(); doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"key\":42}"); REQUIRE(doc.as<std::string>() == "{\"key\":42}");
REQUIRE(spyingAllocator.log() == AllocatorLog() REQUIRE(spyingAllocator.log() ==
<< AllocatorLog::Allocate(sizeofPool()) AllocatorLog{
<< AllocatorLog::Reallocate( Allocate(sizeofPool()),
sizeofPool(), sizeofObject(1))); Reallocate(sizeofPool(), sizeofObject(1)),
});
} }
SECTION("owned key") { SECTION("owned key") {
@ -118,10 +120,11 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
REQUIRE(doc.as<std::string>() == "{\"abcdefg\":42}"); REQUIRE(doc.as<std::string>() == "{\"abcdefg\":42}");
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(7)) AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofString("abcdefg")),
<< AllocatorLog::Reallocate(sizeofPool(), Allocate(sizeofPool()),
sizeofObject(1))); Reallocate(sizeofPool(), sizeofObject(1)),
});
} }
SECTION("linked string in array") { SECTION("linked string in array") {
@ -130,10 +133,11 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
doc.shrinkToFit(); doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[\"hello\"]"); REQUIRE(doc.as<std::string>() == "[\"hello\"]");
REQUIRE(spyingAllocator.log() == AllocatorLog() REQUIRE(spyingAllocator.log() ==
<< AllocatorLog::Allocate(sizeofPool()) AllocatorLog{
<< AllocatorLog::Reallocate( Allocate(sizeofPool()),
sizeofPool(), sizeofArray(1))); Reallocate(sizeofPool(), sizeofArray(1)),
});
} }
SECTION("owned string in array") { SECTION("owned string in array") {
@ -143,10 +147,11 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
REQUIRE(doc.as<std::string>() == "[\"abcdefg\"]"); REQUIRE(doc.as<std::string>() == "[\"abcdefg\"]");
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) AllocatorLog{
<< AllocatorLog::Allocate(sizeofString(7)) Allocate(sizeofPool()),
<< AllocatorLog::Reallocate(sizeofPool(), Allocate(sizeofString("abcdefg")),
sizeofArray(1))); Reallocate(sizeofPool(), sizeofArray(1)),
});
} }
SECTION("linked string in object") { SECTION("linked string in object") {
@ -155,10 +160,11 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
doc.shrinkToFit(); doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"key\":\"hello\"}"); REQUIRE(doc.as<std::string>() == "{\"key\":\"hello\"}");
REQUIRE(spyingAllocator.log() == AllocatorLog() REQUIRE(spyingAllocator.log() ==
<< AllocatorLog::Allocate(sizeofPool()) AllocatorLog{
<< AllocatorLog::Reallocate( Allocate(sizeofPool()),
sizeofPool(), sizeofObject(1))); Reallocate(sizeofPool(), sizeofObject(1)),
});
} }
SECTION("owned string in object") { SECTION("owned string in object") {
@ -168,9 +174,10 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
REQUIRE(doc.as<std::string>() == "{\"key\":\"abcdefg\"}"); REQUIRE(doc.as<std::string>() == "{\"key\":\"abcdefg\"}");
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) AllocatorLog{
<< AllocatorLog::Allocate(sizeofString(7)) Allocate(sizeofPool()),
<< AllocatorLog::Reallocate(sizeofPool(), Allocate(sizeofString("abcdefg")),
sizeofPool(1))); Reallocate(sizeofPool(), sizeofPool(1)),
});
} }
} }

View File

@ -7,8 +7,6 @@
#include "Allocators.hpp" #include "Allocators.hpp"
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonObject::set()") { TEST_CASE("JsonObject::set()") {
SpyingAllocator spy; SpyingAllocator spy;
JsonDocument doc1(&spy); JsonDocument doc1(&spy);
@ -25,8 +23,9 @@ TEST_CASE("JsonObject::set()") {
REQUIRE(success == true); REQUIRE(success == true);
REQUIRE(obj2["hello"] == std::string("world")); REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool())); Allocate(sizeofPool()),
});
} }
SECTION("copy local string value") { SECTION("copy local string value") {
@ -37,9 +36,10 @@ TEST_CASE("JsonObject::set()") {
REQUIRE(success == true); REQUIRE(success == true);
REQUIRE(obj2["hello"] == std::string("world")); REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5))); Allocate(sizeofString("world")),
});
} }
SECTION("copy local key") { SECTION("copy local key") {
@ -50,9 +50,10 @@ TEST_CASE("JsonObject::set()") {
REQUIRE(success == true); REQUIRE(success == true);
REQUIRE(obj2["hello"] == std::string("world")); REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofString(5)) Allocate(sizeofString("hello")),
<< AllocatorLog::Allocate(sizeofPool())); Allocate(sizeofPool()),
});
} }
SECTION("copy string from deserializeJson()") { SECTION("copy string from deserializeJson()") {
@ -63,10 +64,11 @@ TEST_CASE("JsonObject::set()") {
REQUIRE(success == true); REQUIRE(success == true);
REQUIRE(obj2["hello"] == std::string("world")); REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofString(5)) Allocate(sizeofString("hello")),
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5))); Allocate(sizeofString("world")),
});
} }
SECTION("copy string from deserializeMsgPack()") { SECTION("copy string from deserializeMsgPack()") {
@ -77,10 +79,11 @@ TEST_CASE("JsonObject::set()") {
REQUIRE(success == true); REQUIRE(success == true);
REQUIRE(obj2["hello"] == std::string("world")); REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofString(5)) Allocate(sizeofString("hello")),
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5))); Allocate(sizeofString("world")),
});
} }
SECTION("should work with JsonObjectConst") { SECTION("should work with JsonObjectConst") {

View File

@ -8,7 +8,6 @@
#include "Allocators.hpp" #include "Allocators.hpp"
using ArduinoJson::detail::sizeofObject; using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonObject::operator[]") { TEST_CASE("JsonObject::operator[]") {
SpyingAllocator spy; SpyingAllocator spy;
@ -106,65 +105,72 @@ TEST_CASE("JsonObject::operator[]") {
SECTION("should not duplicate const char*") { SECTION("should not duplicate const char*") {
obj["hello"] = "world"; obj["hello"] = "world";
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{Allocate(sizeofPool())});
<< AllocatorLog::Allocate(sizeofPool()));
} }
SECTION("should duplicate char* value") { SECTION("should duplicate char* value") {
obj["hello"] = const_cast<char*>("world"); obj["hello"] = const_cast<char*>("world");
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5))); Allocate(sizeofString("world")),
});
} }
SECTION("should duplicate char* key") { SECTION("should duplicate char* key") {
obj[const_cast<char*>("hello")] = "world"; obj[const_cast<char*>("hello")] = "world";
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofString(5)) Allocate(sizeofString("hello")),
<< AllocatorLog::Allocate(sizeofPool())); Allocate(sizeofPool()),
});
} }
SECTION("should duplicate char* key&value") { SECTION("should duplicate char* key&value") {
obj[const_cast<char*>("hello")] = const_cast<char*>("world"); obj[const_cast<char*>("hello")] = const_cast<char*>("world");
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofString(5)) Allocate(sizeofString("hello")),
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5))); Allocate(sizeofString("world")),
});
} }
SECTION("should duplicate std::string value") { SECTION("should duplicate std::string value") {
obj["hello"] = std::string("world"); obj["hello"] = std::string("world");
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5))); Allocate(sizeofString("world")),
});
} }
SECTION("should duplicate std::string key") { SECTION("should duplicate std::string key") {
obj[std::string("hello")] = "world"; obj[std::string("hello")] = "world";
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofString(5)) Allocate(sizeofString("hello")),
<< AllocatorLog::Allocate(sizeofPool())); Allocate(sizeofPool()),
});
} }
SECTION("should duplicate std::string key&value") { SECTION("should duplicate std::string key&value") {
obj[std::string("hello")] = std::string("world"); obj[std::string("hello")] = std::string("world");
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofString(5)) Allocate(sizeofString("hello")),
<< AllocatorLog::Allocate(sizeofPool()) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofString(5))); Allocate(sizeofString("world")),
});
} }
SECTION("should duplicate a non-static JsonString key") { SECTION("should duplicate a non-static JsonString key") {
obj[JsonString("hello", JsonString::Copied)] = "world"; obj[JsonString("hello", JsonString::Copied)] = "world";
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofString(5)) Allocate(sizeofString("hello")),
<< AllocatorLog::Allocate(sizeofPool())); Allocate(sizeofPool()),
});
} }
SECTION("should not duplicate a static JsonString key") { SECTION("should not duplicate a static JsonString key") {
obj[JsonString("hello", JsonString::Linked)] = "world"; obj[JsonString("hello", JsonString::Linked)] = "world";
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool())); Allocate(sizeofPool()),
});
} }
SECTION("should ignore null key") { SECTION("should ignore null key") {

View File

@ -5,8 +5,6 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <catch.hpp> #include <catch.hpp>
using ArduinoJson::detail::sizeofString;
TEST_CASE("serialize JsonArray to std::string") { TEST_CASE("serialize JsonArray to std::string") {
JsonDocument doc; JsonDocument doc;
JsonArray array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();

View File

@ -8,8 +8,6 @@
#include "Allocators.hpp" #include "Allocators.hpp"
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonVariant::clear()") { TEST_CASE("JsonVariant::clear()") {
SpyingAllocator spy; SpyingAllocator spy;
JsonDocument doc(&spy); JsonDocument doc(&spy);
@ -33,8 +31,9 @@ TEST_CASE("JsonVariant::clear()") {
var.set(std::string("hello")); var.set(std::string("hello"));
var.clear(); var.clear();
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofString(5)) Allocate(sizeofString("hello")),
<< AllocatorLog::Deallocate(sizeofString(5))); Deallocate(sizeofString("hello")),
});
} }
} }

View File

@ -6,8 +6,6 @@
#include <catch.hpp> #include <catch.hpp>
#include "Allocators.hpp" #include "Allocators.hpp"
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonVariant::set(JsonVariant)") { TEST_CASE("JsonVariant::set(JsonVariant)") {
KillswitchAllocator killswitch; KillswitchAllocator killswitch;
SpyingAllocator spyingAllocator(&killswitch); SpyingAllocator spyingAllocator(&killswitch);
@ -44,7 +42,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1); var2.set(var1);
REQUIRE(spyingAllocator.log() == AllocatorLog()); REQUIRE(spyingAllocator.log() == AllocatorLog{});
} }
SECTION("stores char* by copy") { SECTION("stores char* by copy") {
@ -54,8 +52,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1); var2.set(var1);
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7)))); Allocate(sizeofString("hello!!")),
});
} }
SECTION("fails gracefully if string allocation fails") { SECTION("fails gracefully if string allocation fails") {
@ -67,8 +66,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1); var2.set(var1);
REQUIRE(doc2.overflowed() == true); REQUIRE(doc2.overflowed() == true);
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString((7)))); AllocateFail(sizeofString("hello!!")),
});
} }
SECTION("stores std::string by copy") { SECTION("stores std::string by copy") {
@ -77,8 +77,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1); var2.set(var1);
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7)))); Allocate(sizeofString("hello!!")),
});
} }
SECTION("stores Serialized<const char*> by copy") { SECTION("stores Serialized<const char*> by copy") {
@ -87,8 +88,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1); var2.set(var1);
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7)))); Allocate(sizeofString("hello!!")),
});
} }
SECTION("stores Serialized<char*> by copy") { SECTION("stores Serialized<char*> by copy") {
@ -98,8 +100,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1); var2.set(var1);
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7)))); Allocate(sizeofString("hello!!")),
});
} }
SECTION("stores Serialized<std::string> by copy") { SECTION("stores Serialized<std::string> by copy") {
@ -108,8 +111,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1); var2.set(var1);
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7)))); Allocate(sizeofString("hello!!")),
});
} }
SECTION("fails gracefully if raw string allocation fails") { SECTION("fails gracefully if raw string allocation fails") {
@ -120,8 +124,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1); var2.set(var1);
REQUIRE(doc2.overflowed() == true); REQUIRE(doc2.overflowed() == true);
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString((7)))); AllocateFail(sizeofString("hello!!")),
});
} }
SECTION("destination is unbound") { SECTION("destination is unbound") {

View File

@ -9,7 +9,6 @@
#include "Allocators.hpp" #include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray; using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonVariant::remove(int)") { TEST_CASE("JsonVariant::remove(int)") {
SpyingAllocator spy; SpyingAllocator spy;
@ -26,19 +25,21 @@ TEST_CASE("JsonVariant::remove(int)") {
spy.clearLog(); spy.clearLog();
var.remove(1); var.remove(1);
REQUIRE(var.as<std::string>() == "[\"hello\",\"world\"]"); REQUIRE(var.as<std::string>() == "[\"hello\",\"world\"]");
REQUIRE(spy.log() == AllocatorLog()); REQUIRE(spy.log() == AllocatorLog{});
spy.clearLog(); spy.clearLog();
var.remove(1); var.remove(1);
REQUIRE(var.as<std::string>() == "[\"hello\"]"); REQUIRE(var.as<std::string>() == "[\"hello\"]");
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Deallocate(sizeofString(5))); Deallocate(sizeofString("world")),
});
spy.clearLog(); spy.clearLog();
var.remove(0); var.remove(0);
REQUIRE(var.as<std::string>() == "[]"); REQUIRE(var.as<std::string>() == "[]");
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Deallocate(sizeofString(5))); Deallocate(sizeofString("hello")),
});
} }
SECTION("release strings in nested array") { SECTION("release strings in nested array") {
@ -51,8 +52,9 @@ TEST_CASE("JsonVariant::remove(int)") {
var.remove(0); var.remove(0);
REQUIRE(var.as<std::string>() == "[]"); REQUIRE(var.as<std::string>() == "[]");
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Deallocate(sizeofString(5))); Deallocate(sizeofString("hello")),
});
} }
} }

View File

@ -8,7 +8,6 @@
#include "Allocators.hpp" #include "Allocators.hpp"
using ArduinoJson::detail::sizeofObject; using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 }; enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 };
@ -186,32 +185,37 @@ TEST_CASE("JsonVariant::set() releases the previous value") {
SECTION("int") { SECTION("int") {
v.set(42); v.set(42);
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Deallocate(sizeofString(5))); Deallocate(sizeofString("world")),
});
} }
SECTION("bool") { SECTION("bool") {
v.set(false); v.set(false);
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Deallocate(sizeofString(5))); Deallocate(sizeofString("world")),
});
} }
SECTION("const char*") { SECTION("const char*") {
v.set("hello"); v.set("hello");
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Deallocate(sizeofString(5))); Deallocate(sizeofString("world")),
});
} }
SECTION("float") { SECTION("float") {
v.set(1.2); v.set(1.2);
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Deallocate(sizeofString(5))); Deallocate(sizeofString("world")),
});
} }
SECTION("Serialized<const char*>") { SECTION("Serialized<const char*>") {
v.set(serialized("[]")); v.set(serialized("[]"));
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Deallocate(sizeofString(5)) Deallocate(sizeofString("world")),
<< AllocatorLog::Allocate(sizeofString(2))); Allocate(sizeofString("[]")),
});
} }
} }

View File

@ -11,7 +11,6 @@
#include "Allocators.hpp" #include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray; using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofString;
struct PrintOneCharacterAtATime { struct PrintOneCharacterAtATime {
static size_t printStringTo(const std::string& s, Print& p) { static size_t printStringTo(const std::string& s, Print& p) {
@ -65,10 +64,11 @@ TEST_CASE("Printable") {
CHECK(doc.as<std::string>() == value); CHECK(doc.as<std::string>() == value);
CHECK(printable.totalBytesWritten() == 7); CHECK(printable.totalBytesWritten() == 7);
CHECK(doc.overflowed() == false); CHECK(doc.overflowed() == false);
CHECK(spy.log() == AllocatorLog() CHECK(spy.log() ==
<< AllocatorLog::Allocate(sizeofString(31)) AllocatorLog{
<< AllocatorLog::Reallocate(sizeofString(31), Allocate(sizeofStringBuffer()),
sizeofString(7))); Reallocate(sizeofStringBuffer(), sizeofString("example")),
});
} }
SECTION("Via Print::write(const char* size_t)") { SECTION("Via Print::write(const char* size_t)") {
@ -77,10 +77,11 @@ TEST_CASE("Printable") {
CHECK(doc.as<std::string>() == value); CHECK(doc.as<std::string>() == value);
CHECK(printable.totalBytesWritten() == 7); CHECK(printable.totalBytesWritten() == 7);
CHECK(doc.overflowed() == false); CHECK(doc.overflowed() == false);
CHECK(spy.log() == AllocatorLog() CHECK(spy.log() ==
<< AllocatorLog::Allocate(sizeofString(31)) AllocatorLog{
<< AllocatorLog::Reallocate(sizeofString(31), Allocate(sizeofStringBuffer()),
sizeofString(7))); Reallocate(sizeofStringBuffer(), sizeofString("example")),
});
} }
} }
@ -100,8 +101,9 @@ TEST_CASE("Printable") {
CHECK(doc.isNull()); CHECK(doc.isNull());
CHECK(printable.totalBytesWritten() == 0); CHECK(printable.totalBytesWritten() == 0);
CHECK(doc.overflowed() == true); CHECK(doc.overflowed() == true);
CHECK(spy.log() == AllocatorLog() CHECK(spy.log() == AllocatorLog{
<< AllocatorLog::AllocateFail(sizeofString(31))); AllocateFail(sizeofStringBuffer()),
});
} }
SECTION("Via Print::write(const char*, size_t)") { SECTION("Via Print::write(const char*, size_t)") {
@ -113,8 +115,9 @@ TEST_CASE("Printable") {
CHECK(doc.isNull()); CHECK(doc.isNull());
CHECK(printable.totalBytesWritten() == 0); CHECK(printable.totalBytesWritten() == 0);
CHECK(doc.overflowed() == true); CHECK(doc.overflowed() == true);
CHECK(spy.log() == AllocatorLog() CHECK(spy.log() == AllocatorLog{
<< AllocatorLog::AllocateFail(sizeofString(31))); AllocateFail(sizeofStringBuffer()),
});
} }
} }
@ -135,11 +138,12 @@ TEST_CASE("Printable") {
CHECK(doc.isNull()); CHECK(doc.isNull());
CHECK(printable.totalBytesWritten() == 31); CHECK(printable.totalBytesWritten() == 31);
CHECK(doc.overflowed() == true); CHECK(doc.overflowed() == true);
CHECK(spy.log() == AllocatorLog() CHECK(spy.log() ==
<< AllocatorLog::Allocate(sizeofString(31)) AllocatorLog{
<< AllocatorLog::ReallocateFail(sizeofString(31), Allocate(sizeofStringBuffer()),
sizeofString(63)) ReallocateFail(sizeofStringBuffer(), sizeofStringBuffer(2)),
<< AllocatorLog::Deallocate(sizeofString(31))); Deallocate(sizeofStringBuffer()),
});
} }
SECTION("Via Print::write(const char*, size_t)") { SECTION("Via Print::write(const char*, size_t)") {
@ -151,11 +155,12 @@ TEST_CASE("Printable") {
CHECK(doc.isNull()); CHECK(doc.isNull());
CHECK(printable.totalBytesWritten() == 31); CHECK(printable.totalBytesWritten() == 31);
CHECK(doc.overflowed() == true); CHECK(doc.overflowed() == true);
CHECK(spy.log() == AllocatorLog() CHECK(spy.log() ==
<< AllocatorLog::Allocate(sizeofString(31)) AllocatorLog{
<< AllocatorLog::ReallocateFail(sizeofString(31), Allocate(sizeofStringBuffer()),
sizeofString(63)) ReallocateFail(sizeofStringBuffer(), sizeofStringBuffer(2)),
<< AllocatorLog::Deallocate(sizeofString(31))); Deallocate(sizeofStringBuffer()),
});
} }
} }
@ -175,12 +180,13 @@ TEST_CASE("Printable") {
REQUIRE(doc.size() == 2); REQUIRE(doc.size() == 2);
CHECK(doc[0] == "Hello World!"); CHECK(doc[0] == "Hello World!");
CHECK(doc[1] == "Hello World!"); CHECK(doc[1] == "Hello World!");
CHECK(spy.log() == AllocatorLog() CHECK(spy.log() ==
<< AllocatorLog::Allocate(sizeofPool()) AllocatorLog{
<< AllocatorLog::Allocate(sizeofString(31)) Allocate(sizeofPool()),
<< AllocatorLog::Reallocate(sizeofString(31), Allocate(sizeofStringBuffer()),
sizeofString(12)) Reallocate(sizeofStringBuffer(), sizeofString("Hello World!")),
<< AllocatorLog::Allocate(sizeofString(31)) Allocate(sizeofStringBuffer()),
<< AllocatorLog::Deallocate(sizeofString(31))); Deallocate(sizeofStringBuffer()),
});
} }
} }

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -17,7 +17,7 @@ TEST_CASE("ResourceManager::shrinkToFit()") {
SECTION("empty") { SECTION("empty") {
resources.shrinkToFit(); resources.shrinkToFit();
REQUIRE(spyingAllocator.log() == AllocatorLog()); REQUIRE(spyingAllocator.log() == AllocatorLog{});
} }
SECTION("only one pool") { SECTION("only one pool") {
@ -26,9 +26,10 @@ TEST_CASE("ResourceManager::shrinkToFit()") {
resources.shrinkToFit(); resources.shrinkToFit();
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) AllocatorLog{
<< AllocatorLog::Reallocate(sizeofPool(), Allocate(sizeofPool()),
sizeof(VariantSlot))); Reallocate(sizeofPool(), sizeof(VariantSlot)),
});
} }
SECTION("more pools than initial count") { SECTION("more pools than initial count") {
@ -37,20 +38,20 @@ TEST_CASE("ResourceManager::shrinkToFit()") {
i++) i++)
resources.allocSlot(); resources.allocSlot();
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) * AllocatorLog{
ARDUINOJSON_INITIAL_POOL_COUNT Allocate(sizeofPool()) * ARDUINOJSON_INITIAL_POOL_COUNT,
<< AllocatorLog::Allocate(sizeofPoolList( Allocate(sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2)),
ARDUINOJSON_INITIAL_POOL_COUNT * 2)) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofPool())); });
spyingAllocator.clearLog(); spyingAllocator.clearLog();
resources.shrinkToFit(); resources.shrinkToFit();
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() ==
AllocatorLog() AllocatorLog{
<< AllocatorLog::Reallocate(sizeofPool(), sizeof(VariantSlot)) Reallocate(sizeofPool(), sizeof(VariantSlot)),
<< AllocatorLog::Reallocate( Reallocate(sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2),
sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2), sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT + 1)),
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(a1->data() == b.getSlot(a1.id())->data());
REQUIRE(b1->data() == a.getSlot(b1.id())->data()); REQUIRE(b1->data() == a.getSlot(b1.id())->data());
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() == AllocatorLog{
<< AllocatorLog::Allocate(sizeofPool()) * 2); Allocate(sizeofPool()) * 2,
});
} }
SECTION("Only left using preallocated pool list") { SECTION("Only left using preallocated pool list") {
@ -48,12 +49,12 @@ TEST_CASE("ResourceManager::swap()") {
REQUIRE(a1->data() == b.getSlot(a1.id())->data()); REQUIRE(a1->data() == b.getSlot(a1.id())->data());
REQUIRE(b1->data() == a.getSlot(b1.id())->data()); REQUIRE(b1->data() == a.getSlot(b1.id())->data());
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() ==
<< AllocatorLog::Allocate(sizeofPool()) * AllocatorLog{
(ARDUINOJSON_INITIAL_POOL_COUNT + 1) Allocate(sizeofPool()) * (ARDUINOJSON_INITIAL_POOL_COUNT + 1),
<< AllocatorLog::Allocate(sizeofPoolList( Allocate(sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2)),
ARDUINOJSON_INITIAL_POOL_COUNT * 2)) Allocate(sizeofPool()),
<< AllocatorLog::Allocate(sizeofPool())); });
} }
SECTION("Only right using preallocated pool list") { SECTION("Only right using preallocated pool list") {
@ -69,12 +70,12 @@ TEST_CASE("ResourceManager::swap()") {
REQUIRE(a1->data() == b.getSlot(a1.id())->data()); REQUIRE(a1->data() == b.getSlot(a1.id())->data());
REQUIRE(b1->data() == a.getSlot(b1.id())->data()); REQUIRE(b1->data() == a.getSlot(b1.id())->data());
REQUIRE(spy.log() == AllocatorLog() REQUIRE(spy.log() ==
<< AllocatorLog::Allocate(sizeofPool()) * AllocatorLog{
ARDUINOJSON_INITIAL_POOL_COUNT Allocate(sizeofPool()) * ARDUINOJSON_INITIAL_POOL_COUNT,
<< AllocatorLog::Allocate(sizeofPoolList( Allocate(sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2)),
ARDUINOJSON_INITIAL_POOL_COUNT * 2)) Allocate(sizeofPool()) * 2,
<< AllocatorLog::Allocate(sizeofPool()) * 2); });
} }
SECTION("None is using preallocated pool list") { SECTION("None is using preallocated pool list") {