source: src/RandomNumbers/unittests/RandomNumberEngineFactoryUnitTest.cpp@ 94605f

Last change on this file since 94605f was 94605f, checked in by Frederik Heber <heber@…>, 10 years ago

Replaced .. by getConstInstance() wherever possible.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * RandomNumberEngineFactoryUnitTest.cpp
25 *
26 * Created on: Jan 03, 2011
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include <cppunit/CompilerOutputter.h>
36#include <cppunit/extensions/TestFactoryRegistry.h>
37#include <cppunit/ui/text/TestRunner.h>
38
39#include "CodePatterns/Assert.hpp"
40
41#include "RandomNumberEngineFactoryUnitTest.hpp"
42
43#include "RandomNumbers/RandomNumberEngine.hpp"
44#include "RandomNumbers/RandomNumberEngine_Encapsulation.hpp"
45#include "RandomNumbers/RandomNumberEngineFactory.hpp"
46
47#include <boost/nondet_random.hpp>
48#include <boost/random.hpp>
49#include <boost/random/additive_combine.hpp>
50#include <boost/random/discard_block.hpp>
51#include <boost/random/inversive_congruential.hpp>
52#include <boost/random/lagged_fibonacci.hpp>
53#include <boost/random/linear_congruential.hpp>
54#include <boost/random/linear_feedback_shift.hpp>
55#include <boost/random/mersenne_twister.hpp>
56#include <boost/random/random_number_generator.hpp>
57#include <boost/random/ranlux.hpp>
58#include <boost/random/shuffle_output.hpp>
59#include <boost/random/subtract_with_carry.hpp>
60#include <boost/random/xor_combine.hpp>
61#include <boost/random/bernoulli_distribution.hpp>
62#include <boost/random/binomial_distribution.hpp>
63#include <boost/random/cauchy_distribution.hpp>
64#include <boost/random/exponential_distribution.hpp>
65#include <boost/random/gamma_distribution.hpp>
66#include <boost/random/geometric_distribution.hpp>
67#include <boost/random/linear_congruential.hpp>
68#include <boost/random/lognormal_distribution.hpp>
69#include <boost/random/normal_distribution.hpp>
70#include <boost/random/poisson_distribution.hpp>
71#include <boost/random/triangle_distribution.hpp>
72#include <boost/random/uniform_01.hpp>
73#include <boost/random/uniform_int.hpp>
74#include <boost/random/uniform_on_sphere.hpp>
75#include <boost/random/uniform_real.hpp>
76#include <boost/random/uniform_smallint.hpp>
77
78#include <typeinfo>
79
80#ifdef HAVE_TESTRUNNER
81#include "UnitTestMain.hpp"
82#endif /*HAVE_TESTRUNNER*/
83
84/********************************************** Test classes **************************************/
85
86// Registers the fixture into the 'registry'
87CPPUNIT_TEST_SUITE_REGISTRATION( RandomNumberEngineFactoryTest );
88
89void RandomNumberEngineFactoryTest::setUp()
90{
91 rndA = NULL;
92 rndA_1 = NULL;
93 rndA_2 = NULL;
94 rndA_3 = NULL;
95 RandomNumberEngineFactory::getInstance();
96}
97
98void RandomNumberEngineFactoryTest::tearDown()
99{
100 delete rndA;
101 delete rndA_1;
102 delete rndA_2;
103 delete rndA_3;
104 RandomNumberEngineFactory::purgeInstance();
105}
106
107void RandomNumberEngineFactoryTest::EngineTest()
108{
109 // check the injectiveness of enum and string map
110 const RandomNumberEngineFactory &factory = RandomNumberEngineFactory::getConstInstance();
111 for (RandomNumberEngineFactory::NameMap::const_iterator
112 iter = RandomNumberEngineFactory::names.begin();
113 iter != RandomNumberEngineFactory::names.end();
114 ++iter) {
115 CPPUNIT_ASSERT_EQUAL(
116 iter->second,
117 factory.getName( factory.getEnum(iter->second) )
118 );
119 }
120
121 // check one of the engines in the table
122 rndA = RandomNumberEngineFactory::getInstance().
123 ManipulablePrototypeTable[RandomNumberEngineFactory::minstd_rand0]->clone();
124 CPPUNIT_ASSERT_EQUAL(
125 std::string(typeid(boost::minstd_rand0).name()),
126 rndA->name()
127 );
128
129 // range of minstd_rand0 with c=0 is [1,m)
130 CPPUNIT_ASSERT_EQUAL( 1., rndA->min() );
131}
132
133void RandomNumberEngineFactoryTest::PrototypeManipulationTest()
134{
135 // make unmodified clone
136 rndA_1 = RandomNumberEngineFactory::getInstance().
137 getProduct(RandomNumberEngineFactory::minstd_rand0);
138
139 // obtain manipulator
140 RandomNumberEngine &prototype = RandomNumberEngineFactory::getInstance().
141 getPrototype(RandomNumberEngineFactory::minstd_rand0);
142
143 // change the prototype directly
144 CPPUNIT_ASSERT ( 2 != prototype.getseed() );
145 prototype.seed(2); // note for this prototype seed of 0 is not allowd by boost::random
146
147 // check that prototype has indeed been manipulated
148 rndA_2 = RandomNumberEngineFactory::getInstance().
149 getProduct(RandomNumberEngineFactory::minstd_rand0);
150 CPPUNIT_ASSERT_EQUAL( (unsigned int)2, rndA_2->getseed() );
151 CPPUNIT_ASSERT( rndA_2->getseed() != rndA_1->getseed());
152
153 // manipulate prototype
154 RandomNumberEngine_Parameters *params = rndA_1->getParameterSet();
155 CPPUNIT_ASSERT ( rndA_1->getseed() != (unsigned int)3 );
156 params->seed = 3;
157 RandomNumberEngineFactory::getInstance().
158 manipulatePrototype(RandomNumberEngineFactory::minstd_rand0, *params);
159
160 //
161 rndA_3 = RandomNumberEngineFactory::getInstance().
162 getProduct(RandomNumberEngineFactory::minstd_rand0);
163 CPPUNIT_ASSERT_EQUAL( (unsigned int)3, rndA_3->getseed() );
164 CPPUNIT_ASSERT( rndA_3->getseed() != rndA_1->getseed());
165
166 delete params;
167}
168
Note: See TracBrowser for help on using the repository browser.