2020-01-25 10:04:31 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
2020-01-25 13:13:07 +01:00
|
|
|
// This code is part of the sfizz library and is licensed under a BSD 2-clause
|
|
|
|
|
// license. You should have receive a LICENSE.md file along with the code.
|
|
|
|
|
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
|
2019-08-30 00:49:58 +02:00
|
|
|
|
2020-01-18 17:35:27 +01:00
|
|
|
#include "sfizz/Buffer.h"
|
2019-08-24 11:32:19 +02:00
|
|
|
#include "catch2/catch.hpp"
|
2019-07-30 00:29:23 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
using namespace Catch::literals;
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[Buffer] Empty (float)")
|
|
|
|
|
{
|
2019-09-25 23:48:52 +02:00
|
|
|
sfz::Buffer<float> emptyBuffer;
|
2019-07-30 00:29:23 +02:00
|
|
|
REQUIRE(emptyBuffer.empty());
|
|
|
|
|
REQUIRE(emptyBuffer.size() == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[Buffer] Empty (int)")
|
|
|
|
|
{
|
2019-09-25 23:48:52 +02:00
|
|
|
sfz::Buffer<int> emptyBuffer;
|
2019-07-30 00:29:23 +02:00
|
|
|
REQUIRE(emptyBuffer.empty());
|
|
|
|
|
REQUIRE(emptyBuffer.size() == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[Buffer] Empty (double)")
|
|
|
|
|
{
|
2019-09-25 23:48:52 +02:00
|
|
|
sfz::Buffer<double> emptyBuffer;
|
2019-07-30 00:29:23 +02:00
|
|
|
REQUIRE(emptyBuffer.empty());
|
|
|
|
|
REQUIRE(emptyBuffer.size() == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[Buffer] Empty (uint8_t)")
|
|
|
|
|
{
|
2019-09-25 23:48:52 +02:00
|
|
|
sfz::Buffer<uint8_t> emptyBuffer;
|
2019-07-30 00:29:23 +02:00
|
|
|
REQUIRE(emptyBuffer.empty());
|
|
|
|
|
REQUIRE(emptyBuffer.size() == 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-24 11:32:19 +02:00
|
|
|
template <class Type>
|
2019-09-25 23:48:52 +02:00
|
|
|
void checkBoundaries(sfz::Buffer<Type>& buffer, int expectedSize)
|
2019-07-30 00:29:23 +02:00
|
|
|
{
|
2019-08-07 23:39:35 +02:00
|
|
|
REQUIRE((int)buffer.size() == expectedSize);
|
2020-05-30 10:41:50 +02:00
|
|
|
REQUIRE(((size_t)buffer.data() & (sfz::config::defaultAlignment - 1)) == 0);
|
|
|
|
|
REQUIRE(((size_t)buffer.alignedEnd() & (sfz::config::defaultAlignment - 1)) == 0);
|
2019-08-03 18:14:58 +02:00
|
|
|
REQUIRE(std::distance(buffer.begin(), buffer.end()) == expectedSize);
|
|
|
|
|
REQUIRE(std::distance(buffer.begin(), buffer.alignedEnd()) >= expectedSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[Buffer] 10 floats ")
|
|
|
|
|
{
|
|
|
|
|
const int baseSize { 10 };
|
2019-09-25 23:48:52 +02:00
|
|
|
sfz::Buffer<float> buffer(baseSize);
|
2019-08-03 18:14:58 +02:00
|
|
|
checkBoundaries(buffer, baseSize);
|
2019-08-24 11:32:19 +02:00
|
|
|
|
|
|
|
|
for (auto& element : buffer)
|
2019-07-30 00:29:23 +02:00
|
|
|
element = 0.0f;
|
2019-08-24 11:32:19 +02:00
|
|
|
for (auto& element : buffer)
|
2019-07-30 00:29:23 +02:00
|
|
|
REQUIRE(element == 0.0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[Buffer] Resize 10 floats ")
|
|
|
|
|
{
|
|
|
|
|
const int baseSize { 10 };
|
|
|
|
|
const int smallSize { baseSize / 2 };
|
|
|
|
|
const int bigSize { baseSize * 2 };
|
2019-09-25 23:48:52 +02:00
|
|
|
sfz::Buffer<float> buffer(baseSize);
|
2019-07-30 00:29:23 +02:00
|
|
|
REQUIRE(!buffer.empty());
|
2019-08-03 18:14:58 +02:00
|
|
|
checkBoundaries(buffer, baseSize);
|
2019-08-24 11:32:19 +02:00
|
|
|
|
2019-07-30 00:29:23 +02:00
|
|
|
std::fill(buffer.begin(), buffer.end(), 1.0f);
|
2019-08-24 11:32:19 +02:00
|
|
|
|
2020-04-19 18:05:43 +02:00
|
|
|
REQUIRE(buffer.resize(smallSize, std::nothrow));
|
2019-08-03 18:14:58 +02:00
|
|
|
checkBoundaries(buffer, smallSize);
|
|
|
|
|
|
2020-03-10 18:02:00 +01:00
|
|
|
REQUIRE(std::all_of(buffer.begin(), buffer.end(), [](float value) { return value == 1.0f; }));
|
2019-08-24 11:32:19 +02:00
|
|
|
|
2020-04-19 18:05:43 +02:00
|
|
|
REQUIRE(buffer.resize(bigSize, std::nothrow));
|
2019-08-03 18:14:58 +02:00
|
|
|
checkBoundaries(buffer, bigSize);
|
2019-07-30 00:29:23 +02:00
|
|
|
for (auto i = 0; i < smallSize; ++i)
|
|
|
|
|
REQUIRE(buffer[i] == 1.0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[Buffer] Resize 4096 floats ")
|
|
|
|
|
{
|
2019-08-03 18:14:58 +02:00
|
|
|
const int baseSize { 4096 };
|
2019-07-30 00:29:23 +02:00
|
|
|
const int smallSize { baseSize / 2 };
|
|
|
|
|
const int bigSize { baseSize * 2 };
|
2019-09-25 23:48:52 +02:00
|
|
|
sfz::Buffer<float> buffer(baseSize);
|
2019-07-30 00:29:23 +02:00
|
|
|
REQUIRE(!buffer.empty());
|
2019-08-03 18:14:58 +02:00
|
|
|
checkBoundaries(buffer, baseSize);
|
2019-08-24 11:32:19 +02:00
|
|
|
|
2019-07-30 00:29:23 +02:00
|
|
|
std::fill(buffer.begin(), buffer.end(), 1.0f);
|
2019-08-24 11:32:19 +02:00
|
|
|
|
2020-04-19 18:05:43 +02:00
|
|
|
REQUIRE(buffer.resize(smallSize, std::nothrow));
|
2019-08-03 18:14:58 +02:00
|
|
|
checkBoundaries(buffer, smallSize);
|
2019-08-24 11:32:19 +02:00
|
|
|
|
2020-03-10 18:02:00 +01:00
|
|
|
REQUIRE(std::all_of(buffer.begin(), buffer.end(), [](float value) { return value == 1.0f; }));
|
2019-08-24 11:32:19 +02:00
|
|
|
|
2020-04-19 18:05:43 +02:00
|
|
|
REQUIRE(buffer.resize(bigSize, std::nothrow));
|
2019-08-03 18:14:58 +02:00
|
|
|
checkBoundaries(buffer, bigSize);
|
2019-07-30 00:29:23 +02:00
|
|
|
for (auto i = 0; i < smallSize; ++i)
|
|
|
|
|
REQUIRE(buffer[i] == 1.0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[Buffer] Resize 65536 floats ")
|
|
|
|
|
{
|
|
|
|
|
const int baseSize { 10 };
|
|
|
|
|
const int smallSize { baseSize / 2 };
|
|
|
|
|
const int bigSize { baseSize * 2 };
|
2019-09-25 23:48:52 +02:00
|
|
|
sfz::Buffer<float> buffer(baseSize);
|
2019-07-30 00:29:23 +02:00
|
|
|
REQUIRE(!buffer.empty());
|
2019-08-03 18:14:58 +02:00
|
|
|
checkBoundaries(buffer, baseSize);
|
2019-08-24 11:32:19 +02:00
|
|
|
|
2019-07-30 00:29:23 +02:00
|
|
|
std::fill(buffer.begin(), buffer.end(), 1.0f);
|
2019-08-24 11:32:19 +02:00
|
|
|
|
2020-04-19 18:05:43 +02:00
|
|
|
REQUIRE(buffer.resize(smallSize, std::nothrow));
|
2019-08-03 18:14:58 +02:00
|
|
|
checkBoundaries(buffer, smallSize);
|
2019-08-24 11:32:19 +02:00
|
|
|
|
2020-03-10 18:02:00 +01:00
|
|
|
REQUIRE(std::all_of(buffer.begin(), buffer.end(), [](float value) { return value == 1.0f; }));
|
2019-08-24 11:32:19 +02:00
|
|
|
|
2020-04-19 18:05:43 +02:00
|
|
|
REQUIRE(buffer.resize(bigSize, std::nothrow));
|
2019-08-03 18:14:58 +02:00
|
|
|
checkBoundaries(buffer, bigSize);
|
2019-07-30 00:29:23 +02:00
|
|
|
for (auto i = 0; i < smallSize; ++i)
|
|
|
|
|
REQUIRE(buffer[i] == 1.0f);
|
2019-08-24 11:32:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[Buffer] Copy and move")
|
|
|
|
|
{
|
|
|
|
|
const int baseSize { 128 };
|
2019-09-25 23:48:52 +02:00
|
|
|
sfz::Buffer<float> buffer(baseSize);
|
|
|
|
|
sfz::Buffer<float> copied { baseSize - 4 };
|
2019-08-24 11:32:19 +02:00
|
|
|
std::fill(buffer.begin(), buffer.end(), 1.0f);
|
|
|
|
|
std::fill(copied.begin(), copied.end(), 2.0f);
|
|
|
|
|
copied = buffer;
|
|
|
|
|
checkBoundaries(copied, baseSize);
|
2020-03-10 18:02:00 +01:00
|
|
|
REQUIRE(std::all_of(copied.begin(), copied.end(), [](float value) { return value == 1.0f; }));
|
2019-08-24 11:32:19 +02:00
|
|
|
|
2019-09-25 23:48:52 +02:00
|
|
|
sfz::Buffer<float> copyConstructed { buffer };
|
2019-08-24 11:32:19 +02:00
|
|
|
checkBoundaries(copyConstructed, baseSize);
|
2020-03-10 18:02:00 +01:00
|
|
|
REQUIRE(std::all_of(copyConstructed.begin(), copyConstructed.end(), [](float value) { return value == 1.0f; }));
|
2019-08-24 11:32:19 +02:00
|
|
|
|
2020-04-19 18:05:43 +02:00
|
|
|
sfz::Buffer<float> moveConstructed { std::move(buffer) };
|
|
|
|
|
REQUIRE(buffer.empty());
|
|
|
|
|
checkBoundaries(moveConstructed, baseSize);
|
|
|
|
|
REQUIRE(std::all_of(moveConstructed.begin(), moveConstructed.end(), [](float value) { return value == 1.0f; }));
|
2019-12-02 12:22:02 +01:00
|
|
|
}
|
2020-04-19 18:48:11 +02:00
|
|
|
|
|
|
|
|
TEST_CASE("[Buffer] Buffer counter")
|
|
|
|
|
{
|
|
|
|
|
sfz::BufferCounter& counter = sfz::Buffer<float>::counter();
|
|
|
|
|
|
2020-04-19 19:31:52 +02:00
|
|
|
// handle the eventuality that the buffer counter does not start at zero
|
2021-02-24 02:28:25 +01:00
|
|
|
const size_t initialNumBuffers = counter.getNumBuffers();
|
|
|
|
|
const size_t initialTotalBytes = counter.getTotalBytes();
|
|
|
|
|
auto haveNumBuffers = [&](size_t n) -> bool {
|
2020-04-19 19:31:52 +02:00
|
|
|
return n == counter.getNumBuffers() - initialNumBuffers;
|
|
|
|
|
};
|
2021-02-24 02:28:25 +01:00
|
|
|
auto haveTotalAllocation = [&](size_t n) -> bool {
|
|
|
|
|
return n * sizeof(float) == counter.getTotalBytes() - initialTotalBytes;
|
2020-04-19 19:31:52 +02:00
|
|
|
};
|
2020-04-19 18:48:11 +02:00
|
|
|
|
|
|
|
|
// create an empty buffer
|
|
|
|
|
sfz::Buffer<float> b1;
|
2020-04-19 19:31:52 +02:00
|
|
|
REQUIRE(haveNumBuffers(0));
|
|
|
|
|
REQUIRE(haveTotalAllocation(0));
|
2020-04-19 18:48:11 +02:00
|
|
|
|
|
|
|
|
// clear an empty buffer
|
|
|
|
|
b1.clear();
|
2020-04-19 19:31:52 +02:00
|
|
|
REQUIRE(haveNumBuffers(0));
|
|
|
|
|
REQUIRE(haveTotalAllocation(0));
|
2020-04-19 18:48:11 +02:00
|
|
|
|
|
|
|
|
// create a sized buffer
|
|
|
|
|
sfz::Buffer<float> b2(5);
|
2020-04-19 19:31:52 +02:00
|
|
|
REQUIRE(haveNumBuffers(1));
|
|
|
|
|
REQUIRE(haveTotalAllocation(b2.allocationSize()));
|
2020-04-19 18:48:11 +02:00
|
|
|
|
|
|
|
|
// resize an empty buffer
|
|
|
|
|
b1.resize(3);
|
2020-04-19 19:31:52 +02:00
|
|
|
REQUIRE(haveNumBuffers(2));
|
|
|
|
|
REQUIRE(haveTotalAllocation(b1.allocationSize() + b2.allocationSize()));
|
2020-04-19 18:48:11 +02:00
|
|
|
|
|
|
|
|
// resize a non-empty buffer
|
|
|
|
|
b1.resize(7);
|
2020-04-19 19:31:52 +02:00
|
|
|
REQUIRE(haveNumBuffers(2));
|
|
|
|
|
REQUIRE(haveTotalAllocation(b1.allocationSize() + b2.allocationSize()));
|
2020-04-19 18:48:11 +02:00
|
|
|
|
|
|
|
|
// clear a non-empty buffer
|
|
|
|
|
b2.clear();
|
2020-04-19 19:31:52 +02:00
|
|
|
REQUIRE(haveNumBuffers(1));
|
|
|
|
|
REQUIRE(haveTotalAllocation(b1.allocationSize()));
|
2020-04-19 18:48:11 +02:00
|
|
|
|
|
|
|
|
// move an empty buffer into a non-empty one
|
|
|
|
|
b1 = std::move(b2);
|
2020-04-19 19:31:52 +02:00
|
|
|
REQUIRE(haveNumBuffers(0));
|
|
|
|
|
REQUIRE(haveTotalAllocation(0));
|
2020-04-19 18:48:11 +02:00
|
|
|
|
|
|
|
|
// move a non-empty buffer into an empty one
|
|
|
|
|
b1.resize(3);
|
|
|
|
|
b2 = std::move(b1);
|
2020-04-19 19:31:52 +02:00
|
|
|
REQUIRE(haveNumBuffers(1));
|
|
|
|
|
REQUIRE(haveTotalAllocation(b2.allocationSize()));
|
2020-04-19 18:48:11 +02:00
|
|
|
}
|