source: src/Patterns/unittests/PrototypeFactoryUnitTest.cpp@ 9f39db

Last change on this file since 9f39db was 567640, checked in by Frederik Heber <heber@…>, 15 years ago

PrototypeFactory now allows for replacing prototypes.

  • if a prototype can only be manipulated via its constructor, it is essential for the factory to allow for changing prototypes in a controlled manner. All the functions on prototypes are protected, hence only friends may get access.
  • Library version is now 3:1:1, API version is 1.0.6.
  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7/*
8 * PrototypeFactoryUnitTest.cpp
9 *
10 * Created on: Jan 03, 2011
11 * Author: heber
12 */
13// include config.h
14#ifdef HAVE_CONFIG_H
15#include <config.h>
16#endif
17#include <cppunit/CompilerOutputter.h>
18#include <cppunit/extensions/TestFactoryRegistry.h>
19#include <cppunit/ui/text/TestRunner.h>
20#include "Assert.hpp"
21
22#include "PrototypeFactoryUnitTest.hpp"
23
24#include "stubs/CommonStub.hpp"
25#include "stubs/PrototypeFactoryStub.hpp"
26
27#include <boost/nondet_random.hpp>
28#include <boost/random.hpp>
29#include <boost/random/additive_combine.hpp>
30#include <boost/random/discard_block.hpp>
31#include <boost/random/inversive_congruential.hpp>
32#include <boost/random/lagged_fibonacci.hpp>
33#include <boost/random/linear_congruential.hpp>
34#include <boost/random/linear_feedback_shift.hpp>
35#include <boost/random/mersenne_twister.hpp>
36#include <boost/random/random_number_generator.hpp>
37#include <boost/random/ranlux.hpp>
38#include <boost/random/shuffle_output.hpp>
39#include <boost/random/subtract_with_carry.hpp>
40#include <boost/random/xor_combine.hpp>
41#include <boost/random/bernoulli_distribution.hpp>
42#include <boost/random/binomial_distribution.hpp>
43#include <boost/random/cauchy_distribution.hpp>
44#include <boost/random/exponential_distribution.hpp>
45#include <boost/random/gamma_distribution.hpp>
46#include <boost/random/geometric_distribution.hpp>
47#include <boost/random/linear_congruential.hpp>
48#include <boost/random/lognormal_distribution.hpp>
49#include <boost/random/normal_distribution.hpp>
50#include <boost/random/poisson_distribution.hpp>
51#include <boost/random/triangle_distribution.hpp>
52#include <boost/random/uniform_01.hpp>
53#include <boost/random/uniform_int.hpp>
54#include <boost/random/uniform_on_sphere.hpp>
55#include <boost/random/uniform_real.hpp>
56#include <boost/random/uniform_smallint.hpp>
57
58#include <typeinfo>
59
60#ifdef HAVE_TESTRUNNER
61#include "UnitTestMain.hpp"
62#endif /*HAVE_TESTRUNNER*/
63
64/********************************************** Test classes **************************************/
65
66// Registers the fixture into the 'registry'
67CPPUNIT_TEST_SUITE_REGISTRATION( PrototypeFactoryTest );
68
69void PrototypeFactoryTest::setUp()
70{
71 rndA =
72 PrototypeFactoryStub::getInstance().
73 PrototypeTable[PrototypeFactoryStub::Aclass]->clone();
74 CPPUNIT_ASSERT_EQUAL( 0, rndA->getcount() );
75 rndB =
76 PrototypeFactoryStub::getInstance().
77 PrototypeTable[PrototypeFactoryStub::Bclass]->clone();
78 CPPUNIT_ASSERT_EQUAL( 0, rndB->getcount() );
79
80 rndA_1 = NULL;
81 rndA_2 = NULL;
82 rndA_3 = NULL;
83}
84
85void PrototypeFactoryTest::tearDown()
86{
87 delete rndA;
88 delete rndA_1;
89 delete rndA_2;
90 delete rndA_3;
91 delete rndB;
92 PrototypeFactoryStub::purgeInstance();
93}
94
95void PrototypeFactoryTest::DistributionTest()
96{
97 // check the injectiveness of enum and string map
98 for (PrototypeFactoryStub::NameMap::const_iterator
99 iter = PrototypeFactoryStub::getInstance().names.begin();
100 iter != PrototypeFactoryStub::getInstance().names.end();
101 ++iter) {
102 CPPUNIT_ASSERT_EQUAL(
103 iter->second,
104 PrototypeFactoryStub::getInstance().getName(
105 PrototypeFactoryStub::getInstance().getEnum(
106 iter->second
107 )
108 )
109 );
110 }
111
112 // check distributions in the table
113 CPPUNIT_ASSERT_EQUAL(
114 std::string(typeid(Prototype<teststubs::Aclass>).name()),
115 std::string(typeid(*rndA).name())
116 );
117 CPPUNIT_ASSERT_EQUAL(
118 std::string(typeid(Prototype<teststubs::Bclass>).name()),
119 std::string(typeid(*rndB).name())
120 );
121}
122
123void PrototypeFactoryTest::getProductEnumTest()
124{
125 rndA_1 =
126 PrototypeFactoryStub::getInstance().getProduct(PrototypeFactoryStub::Aclass);
127 CPPUNIT_ASSERT( typeid(*rndA) == typeid(*rndA_1) );
128}
129
130void PrototypeFactoryTest::getProductNameTest()
131{
132 rndA_1 =
133 PrototypeFactoryStub::getInstance().getProduct(std::string("Aclass"));
134 CPPUNIT_ASSERT( typeid(*rndA) == typeid(*rndA_1) );
135}
136
137void PrototypeFactoryTest::getProductTypeTest()
138{
139 rndA_1 =
140 PrototypeFactoryStub::getInstance().getProduct( typeid(teststubs::Aclass) );
141 CPPUNIT_ASSERT( typeid(*rndA) == typeid(*rndA_1) );
142}
143
144void PrototypeFactoryTest::Individualitytest()
145{
146 // increasing this does not change the prototype
147 rndA->count();
148
149 rndA_1 = PrototypeFactoryStub::getInstance().getProduct(PrototypeFactoryStub::Aclass);
150 CPPUNIT_ASSERT_EQUAL( 0, rndA_1->getcount() );
151 CPPUNIT_ASSERT( rndA->getcount() != rndA_1->getcount() );
152 rndA_1->count();
153 CPPUNIT_ASSERT( rndA->getcount() == rndA_1->getcount() );
154
155 rndA_2 = PrototypeFactoryStub::getInstance().getProduct(PrototypeFactoryStub::Aclass);
156 CPPUNIT_ASSERT_EQUAL( 0, rndA_2->getcount() );
157 CPPUNIT_ASSERT( rndA->getcount() != rndA_2->getcount() );
158 rndA_2->count();
159 CPPUNIT_ASSERT( rndA->getcount() == rndA_2->getcount() );
160 CPPUNIT_ASSERT( rndA_1->getcount() == rndA_2->getcount() );
161}
162
163void PrototypeFactoryTest::PrototypeManipulatortest()
164{
165 // this method is protected and only friends may access it.
166 IPrototype *rndA_1 = PrototypeFactoryStub::getInstance().getPrototypeManipulator(std::string("Aclass"));
167
168 // do something with the prototype.
169 rndA_1->setcount(256);
170
171 // clone the type and check whether new default values holds
172 rndA_2 = PrototypeFactoryStub::getInstance().getProduct(PrototypeFactoryStub::Aclass);
173 CPPUNIT_ASSERT_EQUAL( 256, rndA_2->getcount() );
174
175 // rndA has been cloned before we have manipulated the prototype
176 CPPUNIT_ASSERT( rndA->getcount() != rndA_2->getcount() );
177
178 // do something with the prototype.
179 rndA_1->setcount(0);
180
181 rndA_3 = PrototypeFactoryStub::getInstance().getProduct(PrototypeFactoryStub::Aclass);
182 CPPUNIT_ASSERT_EQUAL( 0, rndA_3->getcount() );
183 CPPUNIT_ASSERT( rndA->getcount() == rndA_3->getcount() );
184}
185
186void PrototypeFactoryTest::installPrototypetest()
187{
188 Prototype< teststubs::Aclass> *newprototype = new Prototype< teststubs::Aclass> ();
189 newprototype->setcount(1);
190 PrototypeFactoryStub::getInstance().installPrototype(newprototype, std::string("Aclass"));
191 newprototype = NULL;
192
193 IPrototype *rndA_1 = PrototypeFactoryStub::getInstance().getProduct(std::string("Aclass"));
194 CPPUNIT_ASSERT( 1 == rndA_1->getcount() );
195
196 rndA_1->count();
197 CPPUNIT_ASSERT( 2 == rndA_1->getcount() );
198
199 IPrototype *rndA_2 = PrototypeFactoryStub::getInstance().getProduct(std::string("Aclass"));
200 CPPUNIT_ASSERT( 1 == rndA_2->getcount() );
201 CPPUNIT_ASSERT( rndA_1->getcount() != rndA_2->getcount() );
202}
Note: See TracBrowser for help on using the repository browser.