2013-01-31 22:58:27 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2013-01-31 22:58:27 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2014-01-06 22:42:02 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
|
|
|
|
2013-01-31 22:58:27 +01:00
|
|
|
#include <array>
|
2013-10-19 15:05:48 +02:00
|
|
|
#include <random>
|
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
|
|
|
|
|
|
|
operator ConstBuffer<T>() const {
|
|
|
|
return { begin(), size() };
|
|
|
|
}
|
|
|
|
|
|
|
|
operator ConstBuffer<void>() const {
|
|
|
|
return { begin(), size() * sizeof(T) };
|
|
|
|
}
|
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)
|
|
|
|
{
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(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];
|
|
|
|
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT(x >= y - int64_t(tolerance));
|
|
|
|
CPPUNIT_ASSERT(x <= y + int64_t(tolerance));
|
2013-02-01 15:52:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|