mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-25 16:27:33 +02:00
Test: add AllocatorLog
This commit is contained in:
@ -29,29 +29,79 @@ struct FailingAllocator : ArduinoJson::Allocator {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class SpyingAllocator : public ArduinoJson::Allocator {
|
class AllocatorLog {
|
||||||
public:
|
public:
|
||||||
virtual ~SpyingAllocator() {}
|
struct Allocate {
|
||||||
|
Allocate(size_t s) : size(s) {}
|
||||||
|
size_t size;
|
||||||
|
};
|
||||||
|
|
||||||
void* allocate(size_t n) override {
|
struct Reallocate {
|
||||||
_log << "A" << n;
|
Reallocate(size_t s) : size(s) {}
|
||||||
return malloc(n);
|
size_t size;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Deallocate {};
|
||||||
|
|
||||||
|
AllocatorLog& operator<<(const Allocate& a) {
|
||||||
|
_log << "allocate(" << a.size << ")\n";
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void deallocate(void* p) override {
|
AllocatorLog& operator<<(const Deallocate&) {
|
||||||
_log << "F";
|
_log << "deallocate()\n";
|
||||||
free(p);
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* reallocate(void* ptr, size_t n) override {
|
AllocatorLog& operator<<(const Reallocate& a) {
|
||||||
_log << "R" << n;
|
_log << "reallocate(" << a.size << ")\n";
|
||||||
return realloc(ptr, n);
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string log() const {
|
std::string str() const {
|
||||||
return _log.str();
|
auto s = _log.str();
|
||||||
|
if (s.empty())
|
||||||
|
return "(empty)";
|
||||||
|
s.pop_back(); // remove the trailing '\n'
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const AllocatorLog& other) const {
|
||||||
|
return str() == other.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
friend std::ostream& operator<<(std::ostream& os, const AllocatorLog& log) {
|
||||||
|
os << log.str();
|
||||||
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::ostringstream _log;
|
std::ostringstream _log;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SpyingAllocator : public ArduinoJson::Allocator {
|
||||||
|
public:
|
||||||
|
virtual ~SpyingAllocator() {}
|
||||||
|
|
||||||
|
void* allocate(size_t n) override {
|
||||||
|
_log << AllocatorLog::Allocate(n);
|
||||||
|
return malloc(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocate(void* p) override {
|
||||||
|
_log << AllocatorLog::Deallocate();
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* reallocate(void* ptr, size_t n) override {
|
||||||
|
_log << AllocatorLog::Reallocate(n);
|
||||||
|
return realloc(ptr, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
const AllocatorLog& log() const {
|
||||||
|
return _log;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
AllocatorLog _log;
|
||||||
|
};
|
||||||
|
@ -42,7 +42,9 @@ TEST_CASE("JsonDocument's allocator") {
|
|||||||
|
|
||||||
SECTION("Construct/Destruct") {
|
SECTION("Construct/Destruct") {
|
||||||
{ JsonDocument doc(4096, &spyingAllocator); }
|
{ JsonDocument doc(4096, &spyingAllocator); }
|
||||||
REQUIRE(spyingAllocator.log() == "A4096F");
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Deallocate());
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Copy construct") {
|
SECTION("Copy construct") {
|
||||||
@ -56,7 +58,11 @@ TEST_CASE("JsonDocument's allocator") {
|
|||||||
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(doc2.capacity() == 4096);
|
REQUIRE(doc2.capacity() == 4096);
|
||||||
}
|
}
|
||||||
REQUIRE(spyingAllocator.log() == "A4096A4096FF");
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Deallocate()
|
||||||
|
<< AllocatorLog::Deallocate());
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Move construct") {
|
SECTION("Move construct") {
|
||||||
@ -71,7 +77,9 @@ TEST_CASE("JsonDocument's allocator") {
|
|||||||
REQUIRE(doc1.capacity() == 0);
|
REQUIRE(doc1.capacity() == 0);
|
||||||
REQUIRE(doc2.capacity() == 4096);
|
REQUIRE(doc2.capacity() == 4096);
|
||||||
}
|
}
|
||||||
REQUIRE(spyingAllocator.log() == "A4096F");
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Deallocate());
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Copy assign larger") {
|
SECTION("Copy assign larger") {
|
||||||
@ -86,7 +94,13 @@ TEST_CASE("JsonDocument's allocator") {
|
|||||||
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(doc2.capacity() == 4096);
|
REQUIRE(doc2.capacity() == 4096);
|
||||||
}
|
}
|
||||||
REQUIRE(spyingAllocator.log() == "A4096A8FA4096FF");
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Allocate(8)
|
||||||
|
<< AllocatorLog::Deallocate()
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Deallocate()
|
||||||
|
<< AllocatorLog::Deallocate());
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Copy assign smaller") {
|
SECTION("Copy assign smaller") {
|
||||||
@ -101,7 +115,13 @@ TEST_CASE("JsonDocument's allocator") {
|
|||||||
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(doc2.capacity() == 1024);
|
REQUIRE(doc2.capacity() == 1024);
|
||||||
}
|
}
|
||||||
REQUIRE(spyingAllocator.log() == "A1024A4096FA1024FF");
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(1024)
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Deallocate()
|
||||||
|
<< AllocatorLog::Allocate(1024)
|
||||||
|
<< AllocatorLog::Deallocate()
|
||||||
|
<< AllocatorLog::Deallocate());
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Copy assign same size") {
|
SECTION("Copy assign same size") {
|
||||||
@ -116,7 +136,11 @@ TEST_CASE("JsonDocument's allocator") {
|
|||||||
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(doc2.capacity() == 1024);
|
REQUIRE(doc2.capacity() == 1024);
|
||||||
}
|
}
|
||||||
REQUIRE(spyingAllocator.log() == "A1024A1024FF");
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(1024)
|
||||||
|
<< AllocatorLog::Allocate(1024)
|
||||||
|
<< AllocatorLog::Deallocate()
|
||||||
|
<< AllocatorLog::Deallocate());
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Move assign") {
|
SECTION("Move assign") {
|
||||||
@ -132,7 +156,11 @@ TEST_CASE("JsonDocument's allocator") {
|
|||||||
REQUIRE(doc1.capacity() == 0);
|
REQUIRE(doc1.capacity() == 0);
|
||||||
REQUIRE(doc2.capacity() == 4096);
|
REQUIRE(doc2.capacity() == 4096);
|
||||||
}
|
}
|
||||||
REQUIRE(spyingAllocator.log() == "A4096A8FF");
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Allocate(8)
|
||||||
|
<< AllocatorLog::Deallocate()
|
||||||
|
<< AllocatorLog::Deallocate());
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("garbageCollect()") {
|
SECTION("garbageCollect()") {
|
||||||
|
Reference in New Issue
Block a user