diff options
author | Max Kellermann <max@duempel.org> | 2013-10-19 15:05:48 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-10-19 15:05:48 +0200 |
commit | 7cb803ad5c2ba3678198ff954e4a8c8f565a4668 (patch) | |
tree | 6a7357bd8a24fc5fc217ce5e9bc3e02320fa2de6 /test/test_pcm_util.hxx | |
parent | 8e063829c426e447021050eb214a77182269602e (diff) | |
download | mpd-7cb803ad5c2ba3678198ff954e4a8c8f565a4668.tar.gz mpd-7cb803ad5c2ba3678198ff954e4a8c8f565a4668.tar.xz mpd-7cb803ad5c2ba3678198ff954e4a8c8f565a4668.zip |
test/test_pcm: use C++11 random instead of GLib
Diffstat (limited to 'test/test_pcm_util.hxx')
-rw-r--r-- | test/test_pcm_util.hxx | 34 |
1 files changed, 22 insertions, 12 deletions
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 <glib.h> - #include <array> +#include <random> #include <stddef.h> #include <stdint.h> template<typename T> -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> { - int32_t operator()() const { - auto t = GlibRandomInt::operator()(); +struct RandomInt24 : RandomInt<int32_t> { + int32_t operator()() { + auto t = RandomInt::operator()(); t &= 0xffffff; if (t & 0x800000) t |= 0xff000000; @@ -41,9 +46,14 @@ struct GlibRandomInt24 : GlibRandomInt<int32_t> { } }; -struct GlibRandomFloat { - float operator()() const { - return g_random_double_range(-1.0, 1.0); +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); } }; @@ -56,7 +66,7 @@ public: using std::array<T, N>::end; using std::array<T, N>::operator[]; - template<typename G=GlibRandomInt<T>> + template<typename G=RandomInt<T>> TestDataBuffer(G g=G()):std::array<T, N>() { for (auto &i : *this) i = g(); |