From 7cb803ad5c2ba3678198ff954e4a8c8f565a4668 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 19 Oct 2013 15:05:48 +0200 Subject: [PATCH] test/test_pcm: use C++11 random instead of GLib --- test/test_pcm_dither.cxx | 2 +- test/test_pcm_mix.cxx | 4 ++-- test/test_pcm_pack.cxx | 4 +--- test/test_pcm_util.hxx | 34 ++++++++++++++++++++++------------ test/test_pcm_volume.cxx | 4 ++-- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/test/test_pcm_dither.cxx b/test/test_pcm_dither.cxx index 5df64c2d1..710deffcc 100644 --- a/test/test_pcm_dither.cxx +++ b/test/test_pcm_dither.cxx @@ -25,7 +25,7 @@ void PcmDitherTest::TestDither24() { constexpr unsigned N = 256; - const auto src = TestDataBuffer(GlibRandomInt24()); + const auto src = TestDataBuffer(RandomInt24()); int16_t dest[N]; PcmDither dither; diff --git a/test/test_pcm_mix.cxx b/test/test_pcm_mix.cxx index 9c78399dc..2a8a11388 100644 --- a/test/test_pcm_mix.cxx +++ b/test/test_pcm_mix.cxx @@ -22,7 +22,7 @@ #include "test_pcm_util.hxx" #include "pcm/PcmMix.hxx" -template> +template> static void TestPcmMix(G g=G()) { @@ -72,7 +72,7 @@ PcmMixTest::TestMix16() void PcmMixTest::TestMix24() { - TestPcmMix(GlibRandomInt24()); + TestPcmMix(RandomInt24()); } void diff --git a/test/test_pcm_pack.cxx b/test/test_pcm_pack.cxx index 0033ceb07..cab78c499 100644 --- a/test/test_pcm_pack.cxx +++ b/test/test_pcm_pack.cxx @@ -22,13 +22,11 @@ #include "pcm/PcmPack.hxx" #include "system/ByteOrder.hxx" -#include - void PcmPackTest::TestPack24() { constexpr unsigned N = 256; - const auto src = TestDataBuffer(GlibRandomInt24()); + const auto src = TestDataBuffer(RandomInt24()); uint8_t dest[N * 3]; pcm_pack_24(dest, src.begin(), src.end()); diff --git a/test/test_pcm_util.hxx b/test/test_pcm_util.hxx index df12aa067..b378c75a7 100644 --- a/test/test_pcm_util.hxx +++ b/test/test_pcm_util.hxx @@ -17,23 +17,28 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include - #include +#include #include #include template -struct GlibRandomInt { - T operator()() const { - return T(g_random_int()); +struct RandomInt { + typedef std::minstd_rand Engine; + Engine engine; + + static_assert(sizeof(T) <= sizeof(Engine::result_type), + "RNG result type too small"); + + T operator()() { + return T(random()); } }; -struct GlibRandomInt24 : GlibRandomInt { - int32_t operator()() const { - auto t = GlibRandomInt::operator()(); +struct RandomInt24 : RandomInt { + int32_t operator()() { + auto t = RandomInt::operator()(); t &= 0xffffff; if (t & 0x800000) t |= 0xff000000; @@ -41,9 +46,14 @@ struct GlibRandomInt24 : GlibRandomInt { } }; -struct GlibRandomFloat { - float operator()() const { - return g_random_double_range(-1.0, 1.0); +struct RandomFloat { + std::mt19937 gen; + std::uniform_real_distribution dis; + + RandomFloat():gen(std::random_device()()), dis(-1.0, 1.0) {} + + float operator()() { + return dis(gen); } }; @@ -56,7 +66,7 @@ public: using std::array::end; using std::array::operator[]; - template> + template> TestDataBuffer(G g=G()):std::array() { for (auto &i : *this) i = g(); diff --git a/test/test_pcm_volume.cxx b/test/test_pcm_volume.cxx index 1c6730c36..764d8b127 100644 --- a/test/test_pcm_volume.cxx +++ b/test/test_pcm_volume.cxx @@ -94,7 +94,7 @@ PcmVolumeTest::TestVolume24() { constexpr unsigned N = 256; static int32_t zero[N]; - const auto src = TestDataBuffer(GlibRandomInt24()); + const auto src = TestDataBuffer(RandomInt24()); int32_t dest[N]; @@ -158,7 +158,7 @@ PcmVolumeTest::TestVolumeFloat() { constexpr unsigned N = 256; static float zero[N]; - const auto src = TestDataBuffer(GlibRandomFloat()); + const auto src = TestDataBuffer(RandomFloat()); float dest[N];