From 99bce82ba4950d61467957f7ec8a2cc2e19fded8 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Sun, 19 Apr 2020 19:31:52 +0200 Subject: [PATCH] Fix the test --- tests/BufferT.cpp | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/tests/BufferT.cpp b/tests/BufferT.cpp index dfad1483..8d43291d 100644 --- a/tests/BufferT.cpp +++ b/tests/BufferT.cpp @@ -150,47 +150,54 @@ TEST_CASE("[Buffer] Buffer counter") { sfz::BufferCounter& counter = sfz::Buffer::counter(); - REQUIRE(counter.getNumBuffers() == 0); - REQUIRE(counter.getTotalBytes() == 0); + // handle the eventuality that the buffer counter does not start at zero + const int initialNumBuffers = counter.getNumBuffers(); + const int initialTotalBytes = counter.getTotalBytes(); + auto haveNumBuffers = [&](int n) -> bool { + return n == counter.getNumBuffers() - initialNumBuffers; + }; + auto haveTotalAllocation = [&](int n) -> bool { + return n * static_cast(sizeof(float)) == counter.getTotalBytes() - initialTotalBytes; + }; // create an empty buffer sfz::Buffer b1; - REQUIRE(counter.getNumBuffers() == 0); - REQUIRE(counter.getTotalBytes() == 0); + REQUIRE(haveNumBuffers(0)); + REQUIRE(haveTotalAllocation(0)); // clear an empty buffer b1.clear(); - REQUIRE(counter.getNumBuffers() == 0); - REQUIRE(counter.getTotalBytes() == 0); + REQUIRE(haveNumBuffers(0)); + REQUIRE(haveTotalAllocation(0)); // create a sized buffer sfz::Buffer b2(5); - REQUIRE(counter.getNumBuffers() == 1); - REQUIRE(counter.getTotalBytes() == (b2.allocationSize()) * sizeof(float)); + REQUIRE(haveNumBuffers(1)); + REQUIRE(haveTotalAllocation(b2.allocationSize())); // resize an empty buffer b1.resize(3); - REQUIRE(counter.getNumBuffers() == 2); - REQUIRE(counter.getTotalBytes() == (b1.allocationSize() + b2.allocationSize()) * sizeof(float)); + REQUIRE(haveNumBuffers(2)); + REQUIRE(haveTotalAllocation(b1.allocationSize() + b2.allocationSize())); // resize a non-empty buffer b1.resize(7); - REQUIRE(counter.getNumBuffers() == 2); - REQUIRE(counter.getTotalBytes() == (b1.allocationSize() + b2.allocationSize()) * sizeof(float)); + REQUIRE(haveNumBuffers(2)); + REQUIRE(haveTotalAllocation(b1.allocationSize() + b2.allocationSize())); // clear a non-empty buffer b2.clear(); - REQUIRE(counter.getNumBuffers() == 1); - REQUIRE(counter.getTotalBytes() == (b1.allocationSize()) * sizeof(float)); + REQUIRE(haveNumBuffers(1)); + REQUIRE(haveTotalAllocation(b1.allocationSize())); // move an empty buffer into a non-empty one b1 = std::move(b2); - REQUIRE(counter.getNumBuffers() == 0); - REQUIRE(counter.getTotalBytes() == 0); + REQUIRE(haveNumBuffers(0)); + REQUIRE(haveTotalAllocation(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)); + REQUIRE(haveNumBuffers(1)); + REQUIRE(haveTotalAllocation(b2.allocationSize())); }