2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2013-01-31 22:58:27 +01:00
|
|
|
|
2018-10-16 19:01:13 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2013-01-31 22:58:27 +01:00
|
|
|
#include <array>
|
2013-10-19 15:05:48 +02:00
|
|
|
#include <random>
|
2022-07-04 15:27:03 +02:00
|
|
|
#include <span>
|
2013-01-31 22:58:27 +01:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
template<typename T>
|
2013-10-19 15:05:48 +02:00
|
|
|
struct RandomInt {
|
|
|
|
typedef std::minstd_rand Engine;
|
|
|
|
Engine engine;
|
|
|
|
|
|
|
|
static_assert(sizeof(T) <= sizeof(Engine::result_type),
|
|
|
|
"RNG result type too small");
|
|
|
|
|
|
|
|
T operator()() {
|
2015-06-22 22:02:12 +02:00
|
|
|
return T(engine());
|
2013-01-31 22:58:27 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-19 15:05:48 +02:00
|
|
|
struct RandomInt24 : RandomInt<int32_t> {
|
|
|
|
int32_t operator()() {
|
|
|
|
auto t = RandomInt::operator()();
|
2013-01-31 22:58:27 +01:00
|
|
|
t &= 0xffffff;
|
|
|
|
if (t & 0x800000)
|
|
|
|
t |= 0xff000000;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-19 15:05:48 +02:00
|
|
|
struct RandomFloat {
|
|
|
|
std::mt19937 gen;
|
|
|
|
std::uniform_real_distribution<float> dis;
|
|
|
|
|
|
|
|
RandomFloat():gen(std::random_device()()), dis(-1.0, 1.0) {}
|
|
|
|
|
|
|
|
float operator()() {
|
|
|
|
return dis(gen);
|
2013-01-31 22:58:27 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, size_t N>
|
|
|
|
class TestDataBuffer : std::array<T, N> {
|
|
|
|
public:
|
|
|
|
using typename std::array<T, N>::const_pointer;
|
2013-02-01 15:52:03 +01:00
|
|
|
using std::array<T, N>::size;
|
2013-01-31 22:58:27 +01:00
|
|
|
using std::array<T, N>::begin;
|
|
|
|
using std::array<T, N>::end;
|
|
|
|
using std::array<T, N>::operator[];
|
|
|
|
|
2013-10-19 15:05:48 +02:00
|
|
|
template<typename G=RandomInt<T>>
|
2013-01-31 22:58:27 +01:00
|
|
|
TestDataBuffer(G g=G()):std::array<T, N>() {
|
|
|
|
for (auto &i : *this)
|
|
|
|
i = g();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
operator typename std::array<T, N>::const_pointer() const {
|
|
|
|
return begin();
|
|
|
|
}
|
2014-01-06 22:42:02 +01:00
|
|
|
|
2022-07-04 15:27:03 +02:00
|
|
|
operator std::span<const T>() const {
|
2014-01-06 22:42:02 +01:00
|
|
|
return { begin(), size() };
|
|
|
|
}
|
|
|
|
|
2022-07-04 15:27:03 +02:00
|
|
|
operator std::span<const std::byte>() const {
|
|
|
|
return { (const std::byte *)begin(), size() * sizeof(T) };
|
2014-01-06 22:42:02 +01:00
|
|
|
}
|
2013-01-31 22:58:27 +01:00
|
|
|
};
|
2013-02-01 15:52:03 +01:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool
|
|
|
|
AssertEqualWithTolerance(const T &a, const T &b, unsigned tolerance)
|
|
|
|
{
|
2018-10-16 19:01:13 +02:00
|
|
|
EXPECT_EQ(a.size(), b.size());
|
2013-02-01 15:52:03 +01:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < a.size(); ++i) {
|
|
|
|
int64_t x = a[i], y = b[i];
|
|
|
|
|
2018-10-16 19:01:13 +02:00
|
|
|
EXPECT_GE(x, y - int64_t(tolerance));
|
|
|
|
EXPECT_LE(x, y + int64_t(tolerance));
|
2013-02-01 15:52:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|