Skip to content

Commit

Permalink
Test: add AllocatorLog
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Apr 1, 2023
1 parent 912137c commit 0e75737
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 13 deletions.
60 changes: 54 additions & 6 deletions extras/tests/Helpers/Allocators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,77 @@ struct FailingAllocator : ArduinoJson::Allocator {
}
};

class AllocatorLog {
public:
struct Allocate {
Allocate(size_t s) : size(s) {}
size_t size;
};

struct Reallocate {
Reallocate(size_t s) : size(s) {}
size_t size;
};

struct Deallocate {};

AllocatorLog& operator<<(const Allocate& a) {
_log << "allocate(" << a.size << ")\n";
return *this;
}

AllocatorLog& operator<<(const Deallocate&) {
_log << "deallocate()\n";
return *this;
}

AllocatorLog& operator<<(const Reallocate& a) {
_log << "reallocate(" << a.size << ")\n";
return *this;
}

std::string str() const {
auto s = _log.str();
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:
std::ostringstream _log;
};

class SpyingAllocator : public ArduinoJson::Allocator {
public:
virtual ~SpyingAllocator() {}

void* allocate(size_t n) override {
_log << "A" << n;
_log << AllocatorLog::Allocate(n);
return malloc(n);
}

void deallocate(void* p) override {
_log << "F";
_log << AllocatorLog::Deallocate();
free(p);
}

void* reallocate(void* ptr, size_t n) override {
_log << "R" << n;
_log << AllocatorLog::Reallocate(n);
return realloc(ptr, n);
}

std::string log() const {
return _log.str();
const AllocatorLog& log() const {
return _log;
}

private:
std::ostringstream _log;
AllocatorLog _log;
};
42 changes: 35 additions & 7 deletions extras/tests/JsonDocument/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ TEST_CASE("JsonDocument's allocator") {

SECTION("Construct/Destruct") {
{ JsonDocument doc(4096, &spyingAllocator); }
REQUIRE(spyingAllocator.log() == "A4096F");
REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096)
<< AllocatorLog::Deallocate());
}

SECTION("Copy construct") {
Expand All @@ -56,7 +58,11 @@ TEST_CASE("JsonDocument's allocator") {
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
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") {
Expand All @@ -71,7 +77,9 @@ TEST_CASE("JsonDocument's allocator") {
REQUIRE(doc1.capacity() == 0);
REQUIRE(doc2.capacity() == 4096);
}
REQUIRE(spyingAllocator.log() == "A4096F");
REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096)
<< AllocatorLog::Deallocate());
}

SECTION("Copy assign larger") {
Expand All @@ -86,7 +94,13 @@ TEST_CASE("JsonDocument's allocator") {
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
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") {
Expand All @@ -101,7 +115,13 @@ TEST_CASE("JsonDocument's allocator") {
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
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") {
Expand All @@ -116,7 +136,11 @@ TEST_CASE("JsonDocument's allocator") {
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
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") {
Expand All @@ -132,7 +156,11 @@ TEST_CASE("JsonDocument's allocator") {
REQUIRE(doc1.capacity() == 0);
REQUIRE(doc2.capacity() == 4096);
}
REQUIRE(spyingAllocator.log() == "A4096A8FF");
REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096)
<< AllocatorLog::Allocate(8)
<< AllocatorLog::Deallocate()
<< AllocatorLog::Deallocate());
}

SECTION("garbageCollect()") {
Expand Down

0 comments on commit 0e75737

Please sign in to comment.