sfizz/tests/BufferT.cpp

197 lines
6.1 KiB
C++
Raw Normal View History

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"
#include <algorithm>
using namespace Catch::literals;
TEST_CASE("[Buffer] Empty (float)")
{
sfz::Buffer<float> emptyBuffer;
REQUIRE(emptyBuffer.empty());
REQUIRE(emptyBuffer.size() == 0);
}
TEST_CASE("[Buffer] Empty (int)")
{
sfz::Buffer<int> emptyBuffer;
REQUIRE(emptyBuffer.empty());
REQUIRE(emptyBuffer.size() == 0);
}
TEST_CASE("[Buffer] Empty (double)")
{
sfz::Buffer<double> emptyBuffer;
REQUIRE(emptyBuffer.empty());
REQUIRE(emptyBuffer.size() == 0);
}
TEST_CASE("[Buffer] Empty (uint8_t)")
{
sfz::Buffer<uint8_t> emptyBuffer;
REQUIRE(emptyBuffer.empty());
REQUIRE(emptyBuffer.size() == 0);
}
2019-08-24 11:32:19 +02:00
template <class Type>
void checkBoundaries(sfz::Buffer<Type>& buffer, int expectedSize)
{
2019-08-07 23:39:35 +02:00
REQUIRE((int)buffer.size() == expectedSize);
REQUIRE(((size_t)buffer.data() & (sfz::SIMDConfig::defaultAlignment - 1)) == 0);
REQUIRE(((size_t)buffer.alignedEnd() & (sfz::SIMDConfig::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 };
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)
element = 0.0f;
2019-08-24 11:32:19 +02:00
for (auto& element : buffer)
REQUIRE(element == 0.0f);
}
TEST_CASE("[Buffer] Resize 10 floats ")
{
const int baseSize { 10 };
const int smallSize { baseSize / 2 };
const int bigSize { baseSize * 2 };
sfz::Buffer<float> buffer(baseSize);
REQUIRE(!buffer.empty());
2019-08-03 18:14:58 +02:00
checkBoundaries(buffer, baseSize);
2019-08-24 11:32:19 +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);
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 };
const int smallSize { baseSize / 2 };
const int bigSize { baseSize * 2 };
sfz::Buffer<float> buffer(baseSize);
REQUIRE(!buffer.empty());
2019-08-03 18:14:58 +02:00
checkBoundaries(buffer, baseSize);
2019-08-24 11:32:19 +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);
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 };
sfz::Buffer<float> buffer(baseSize);
REQUIRE(!buffer.empty());
2019-08-03 18:14:58 +02:00
checkBoundaries(buffer, baseSize);
2019-08-24 11:32:19 +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);
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 };
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
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; }));
}
2020-04-19 18:48:11 +02:00
TEST_CASE("[Buffer] Buffer counter")
{
sfz::BufferCounter& counter = sfz::Buffer<float>::counter();
REQUIRE(counter.getNumBuffers() == 0);
REQUIRE(counter.getTotalBytes() == 0);
// create an empty buffer
sfz::Buffer<float> 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<float> 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));
}