From 866b09992668f97af063dcd77dc5dd0e3a512b94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Nusser?= Date: Thu, 24 Mar 2016 00:11:28 +0100 Subject: New Random class for convenient random number generation. --- test/Makefile.am | 12 +++++- test/randomtest.cc | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 test/randomtest.cc (limited to 'test') diff --git a/test/Makefile.am b/test/Makefile.am index 6e56043..ccb21e6 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -3,7 +3,7 @@ include $(top_srcdir)/src/Makefile.am.drumgizmo TESTS = resource engine gui resampler lv2 configfile audiocache \ audiocachefile audiocacheidmanager audiocacheeventhandler \ - memchecker + memchecker random check_PROGRAMS = $(TESTS) @@ -128,5 +128,15 @@ memchecker_SOURCES = \ test.cc \ memcheckertest.cc +random_CXXFLAGS = -DOUTPUT=\"random\" $(CPPUNIT_CFLAGS) \ + -I$(top_srcdir)/src \ + -I$(top_srcdir)/hugin -DDISABLE_HUGIN +random_CFLAGS = -DDISABLE_HUGIN +random_LDFLAGS = $(CPPUNIT_LIBS) +random_SOURCES = \ + $(top_srcdir)/src/random.cc \ + test.cc \ + randomtest.cc + EXTRA_DIST = \ lv2_test_host.h diff --git a/test/randomtest.cc b/test/randomtest.cc new file mode 100644 index 0000000..16a16c4 --- /dev/null +++ b/test/randomtest.cc @@ -0,0 +1,119 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * randomtest.cc + * + * Sat Mar 26 08:48:53 CET 2016 + * Copyright 2016 André Nusser + * andre.nusser@googlemail.com + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DrumGizmo 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include + +#include + +#include +#include + +class RandomTest + : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(RandomTest); + CPPUNIT_TEST(rangeTest); + CPPUNIT_TEST(normalTest); + CPPUNIT_TEST(chooseTest); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp() + { + } + + void tearDown() + { + + } + + void rangeTest() + { + // check if random numbers are in the wanted range + Random rand; + + float float_lb = -42.23; + float float_ub = 666.666; + int int_lb = -42; + int int_ub = 23; + for (int i = 0; i<10000; i++) + { + float rand_float = rand.floatInRange(float_lb, float_ub); + float rand_int = rand.intInRange(int_lb, int_ub); + CPPUNIT_ASSERT(rand_float >= float_lb && rand_float <= float_ub); + CPPUNIT_ASSERT(rand_int >= int_lb && rand_int <= int_ub); + } + + // check if the series of random numbers is the one we expect + // for a certain seed. + rand = Random(666); + CPPUNIT_ASSERT_EQUAL(0, rand.intInRange(0,100)); + CPPUNIT_ASSERT_EQUAL(61, rand.intInRange(0,100)); + CPPUNIT_ASSERT_EQUAL(23, rand.intInRange(0,100)); + } + + void normalTest() + { + // This test might theoretically fail but it is extremely unlikely to happen. + Random rand; + + float real_mean = 42.0; + float real_stddev = 2.0; + int nr_of_samples = 50000; + + float sum = 0.; + float sum_of_squares = 0.; + for (int i=0; i= real_mean-epsilon && estimated_mean <= real_mean+epsilon); + CPPUNIT_ASSERT(estimated_stddev >= real_stddev-epsilon && estimated_stddev <= real_stddev+epsilon); + } + + void chooseTest() + { + Random rand; + + std::vector vec = { 42, 42, 42 }; + int nr_of_samples = 1000; + + for (int i=0; i