From 4562201455e33e69bb7f90c290f4f9e623e80471 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Sun, 19 Apr 2020 18:48:11 +0200 Subject: [PATCH] Add some tests for the buffer counter --- src/sfizz/Buffer.h | 8 ++++++++ tests/BufferT.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/sfizz/Buffer.h b/src/sfizz/Buffer.h index ea47bb2b..5f75a888 100644 --- a/src/sfizz/Buffer.h +++ b/src/sfizz/Buffer.h @@ -211,6 +211,14 @@ public: _counter.bufferDeleted(largerSize * sizeof(value_type)); } + /** + * @brief Get the size of the larger block which is actually allocated + */ + size_t allocationSize() const noexcept + { + return largerSize; + } + /** * @brief Construct a new Buffer object from an existing one * diff --git a/tests/BufferT.cpp b/tests/BufferT.cpp index f886f4d9..dfad1483 100644 --- a/tests/BufferT.cpp +++ b/tests/BufferT.cpp @@ -145,3 +145,52 @@ TEST_CASE("[Buffer] Copy and move") checkBoundaries(moveConstructed, baseSize); REQUIRE(std::all_of(moveConstructed.begin(), moveConstructed.end(), [](float value) { return value == 1.0f; })); } + +TEST_CASE("[Buffer] Buffer counter") +{ + sfz::BufferCounter& counter = sfz::Buffer::counter(); + + REQUIRE(counter.getNumBuffers() == 0); + REQUIRE(counter.getTotalBytes() == 0); + + // create an empty buffer + sfz::Buffer b1; + REQUIRE(counter.getNumBuffers() == 0); + REQUIRE(counter.getTotalBytes() == 0); + + // clear an empty buffer + b1.clear(); + REQUIRE(counter.getNumBuffers() == 0); + REQUIRE(counter.getTotalBytes() == 0); + + // create a sized buffer + sfz::Buffer b2(5); + REQUIRE(counter.getNumBuffers() == 1); + REQUIRE(counter.getTotalBytes() == (b2.allocationSize()) * sizeof(float)); + + // resize an empty buffer + b1.resize(3); + REQUIRE(counter.getNumBuffers() == 2); + REQUIRE(counter.getTotalBytes() == (b1.allocationSize() + b2.allocationSize()) * sizeof(float)); + + // resize a non-empty buffer + b1.resize(7); + REQUIRE(counter.getNumBuffers() == 2); + REQUIRE(counter.getTotalBytes() == (b1.allocationSize() + b2.allocationSize()) * sizeof(float)); + + // clear a non-empty buffer + b2.clear(); + REQUIRE(counter.getNumBuffers() == 1); + REQUIRE(counter.getTotalBytes() == (b1.allocationSize()) * sizeof(float)); + + // move an empty buffer into a non-empty one + b1 = std::move(b2); + REQUIRE(counter.getNumBuffers() == 0); + REQUIRE(counter.getTotalBytes() == 0); + + // move a non-empty buffer into an empty one + b1.resize(3); + b2 = std::move(b1); + REQUIRE(counter.getNumBuffers() == 1); + REQUIRE(counter.getTotalBytes() == (b2.allocationSize()) * sizeof(float)); +}