source: src/Patterns/unittests/ManipulablePrototypeFactoryUnitTest.cpp@ fe056c

Last change on this file since fe056c was 8dd38e, checked in by Frederik Heber <heber@…>, 15 years ago

New patterns ManipulableClone and ManipulablePrototypeFactory, changed PrototypeFactory.

  • ManipulableClone is a Clone that can spawn a manipulated copy varied by given parameters.
  • prototypes in PrototypeFactory cannot be manipulated anymore.
  • PrototypeFactory::getPrototypeManipulator() -> getPrototype() and returned reference is const (and a ref, no pointer. Preventing its accidental deletion).
  • ManipulablePrototypeFactory then has non-const references returned by getProduct().
  • ManipulablePrototypeFactory::manipulatePrototype() allows direct manipulation of the prototype by a given parameter set.
  • Added unit tests for the new patterns.
  • Changed unit tests for PrototypeFactory.
  • Library version is now 4:0:0, API version is 1.0.7.
  • Property mode set to 100644
File size: 7.1 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 * ManipulablePrototypeFactoryUnitTest.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 "ManipulablePrototypeFactoryUnitTest.hpp"
23
24#include "stubs/CommonStub.hpp"
25#include "stubs/ManipulableCloneStub.hpp"
26#include "stubs/ManipulablePrototypeFactoryStub.hpp"
27
28#include <boost/nondet_random.hpp>
29#include <boost/random.hpp>
30#include <boost/random/additive_combine.hpp>
31#include <boost/random/discard_block.hpp>
32#include <boost/random/inversive_congruential.hpp>
33#include <boost/random/lagged_fibonacci.hpp>
34#include <boost/random/linear_congruential.hpp>
35#include <boost/random/linear_feedback_shift.hpp>
36#include <boost/random/mersenne_twister.hpp>
37#include <boost/random/random_number_generator.hpp>
38#include <boost/random/ranlux.hpp>
39#include <boost/random/shuffle_output.hpp>
40#include <boost/random/subtract_with_carry.hpp>
41#include <boost/random/xor_combine.hpp>
42#include <boost/random/bernoulli_distribution.hpp>
43#include <boost/random/binomial_distribution.hpp>
44#include <boost/random/cauchy_distribution.hpp>
45#include <boost/random/exponential_distribution.hpp>
46#include <boost/random/gamma_distribution.hpp>
47#include <boost/random/geometric_distribution.hpp>
48#include <boost/random/linear_congruential.hpp>
49#include <boost/random/lognormal_distribution.hpp>
50#include <boost/random/normal_distribution.hpp>
51#include <boost/random/poisson_distribution.hpp>
52#include <boost/random/triangle_distribution.hpp>
53#include <boost/random/uniform_01.hpp>
54#include <boost/random/uniform_int.hpp>
55#include <boost/random/uniform_on_sphere.hpp>
56#include <boost/random/uniform_real.hpp>
57#include <boost/random/uniform_smallint.hpp>
58
59#include <typeinfo>
60
61#ifdef HAVE_TESTRUNNER
62#include "UnitTestMain.hpp"
63#endif /*HAVE_TESTRUNNER*/
64
65/********************************************** Test classes **************************************/
66
67// Registers the fixture into the 'registry'
68CPPUNIT_TEST_SUITE_REGISTRATION( ManipulablePrototypeFactoryTest );
69
70void ManipulablePrototypeFactoryTest::setUp()
71{
72 rndA =
73 ManipulablePrototypeFactoryStub::getInstance().
74 ManipulablePrototypeTable[ManipulablePrototypeFactoryStub::Aclass]->clone();
75 CPPUNIT_ASSERT_EQUAL( 0, rndA->getcount() );
76 rndB =
77 ManipulablePrototypeFactoryStub::getInstance().
78 ManipulablePrototypeTable[ManipulablePrototypeFactoryStub::Bclass]->clone();
79 CPPUNIT_ASSERT_EQUAL( 0, rndB->getcount() );
80
81 rndA_1 = NULL;
82 rndA_2 = NULL;
83 rndA_3 = NULL;
84}
85
86void ManipulablePrototypeFactoryTest::tearDown()
87{
88 delete rndA;
89 delete rndA_1;
90 delete rndA_2;
91 delete rndA_3;
92 delete rndB;
93 ManipulablePrototypeFactoryStub::purgeInstance();
94}
95
96void ManipulablePrototypeFactoryTest::DistributionTest()
97{
98 // check the injectiveness of enum and string map
99 for (ManipulablePrototypeFactoryStub::NameMap::const_iterator
100 iter = ManipulablePrototypeFactoryStub::getInstance().names.begin();
101 iter != ManipulablePrototypeFactoryStub::getInstance().names.end();
102 ++iter) {
103 CPPUNIT_ASSERT_EQUAL(
104 iter->second,
105 ManipulablePrototypeFactoryStub::getInstance().getName(
106 ManipulablePrototypeFactoryStub::getInstance().getEnum(
107 iter->second
108 )
109 )
110 );
111 }
112
113 // check distributions in the table
114 CPPUNIT_ASSERT_EQUAL(
115 std::string(typeid(ManipulablePrototype<teststubs::Aclass>).name()),
116 std::string(typeid(*rndA).name())
117 );
118 CPPUNIT_ASSERT_EQUAL(
119 std::string(typeid(ManipulablePrototype<teststubs::Bclass>).name()),
120 std::string(typeid(*rndB).name())
121 );
122}
123
124void ManipulablePrototypeFactoryTest::getProductEnumTest()
125{
126 rndA_1 =
127 ManipulablePrototypeFactoryStub::getInstance().getProduct(ManipulablePrototypeFactoryStub::Aclass);
128 CPPUNIT_ASSERT( typeid(*rndA) == typeid(*rndA_1) );
129}
130
131void ManipulablePrototypeFactoryTest::getProductNameTest()
132{
133 rndA_1 =
134 ManipulablePrototypeFactoryStub::getInstance().getProduct(std::string("Aclass"));
135 CPPUNIT_ASSERT( typeid(*rndA) == typeid(*rndA_1) );
136}
137
138void ManipulablePrototypeFactoryTest::getProductTypeTest()
139{
140 rndA_1 =
141 ManipulablePrototypeFactoryStub::getInstance().getProduct( typeid(teststubs::Aclass) );
142 CPPUNIT_ASSERT( typeid(*rndA) == typeid(*rndA_1) );
143}
144
145void ManipulablePrototypeFactoryTest::Individualitytest()
146{
147 // increasing this does not change the prototype
148 rndA->count();
149
150 rndA_1 = ManipulablePrototypeFactoryStub::getInstance().getProduct(ManipulablePrototypeFactoryStub::Aclass);
151 CPPUNIT_ASSERT_EQUAL( 0, rndA_1->getcount() );
152 CPPUNIT_ASSERT( rndA->getcount() != rndA_1->getcount() );
153 rndA_1->count();
154 CPPUNIT_ASSERT( rndA->getcount() == rndA_1->getcount() );
155
156 rndA_2 = ManipulablePrototypeFactoryStub::getInstance().getProduct(ManipulablePrototypeFactoryStub::Aclass);
157 CPPUNIT_ASSERT_EQUAL( 0, rndA_2->getcount() );
158 CPPUNIT_ASSERT( rndA->getcount() != rndA_2->getcount() );
159 rndA_2->count();
160 CPPUNIT_ASSERT( rndA->getcount() == rndA_2->getcount() );
161 CPPUNIT_ASSERT( rndA_1->getcount() == rndA_2->getcount() );
162}
163
164void ManipulablePrototypeFactoryTest::getPrototypetest()
165{
166 // this method is protected and only friends may access it.
167 IManipulablePrototype &rndA_1c = ManipulablePrototypeFactoryStub::getInstance().getPrototype(std::string("Aclass"));
168 CPPUNIT_ASSERT_EQUAL( 0, rndA_1c.getcount() );
169
170 // clone the type and check whether new default values holds
171 rndA_2 = ManipulablePrototypeFactoryStub::getInstance().getProduct(ManipulablePrototypeFactoryStub::Aclass);
172 CPPUNIT_ASSERT_EQUAL( rndA_1c.getcount(), rndA_2->getcount() );
173 rndA_2->count();
174 CPPUNIT_ASSERT( rndA_1c.getcount() != rndA_2->getcount() );
175
176 // the following is possible (with PrototypeFactory not!)
177 rndA_1c.count();
178 CPPUNIT_ASSERT_EQUAL( 1, rndA_1c.getcount() );
179
180 // rndA has been cloned before we have manipulated the prototype
181 CPPUNIT_ASSERT( rndA->getcount() != rndA_2->getcount() );
182
183 // prototype is at 1, has been manipulated!
184 rndA_3 = ManipulablePrototypeFactoryStub::getInstance().getProduct(ManipulablePrototypeFactoryStub::Aclass);
185 CPPUNIT_ASSERT_EQUAL( 1, rndA_3->getcount() );
186
187 // rndA is older, before manipulation, hence still at 0
188 CPPUNIT_ASSERT( rndA->getcount() != rndA_3->getcount() );
189}
190
191void ManipulablePrototypeFactoryTest::manipulatePrototypetest()
192{
193 teststubs::classParameters params;
194 params.counter = 100;
195 ManipulablePrototypeFactoryStub::getInstance().manipulatePrototype(std::string("Aclass"), params);
196
197 rndA_1 = ManipulablePrototypeFactoryStub::getInstance().getProduct(std::string("Aclass"));
198 CPPUNIT_ASSERT( 100 == rndA_1->getcount() );
199
200 rndA_1->count();
201 CPPUNIT_ASSERT( 101 == rndA_1->getcount() );
202
203 rndA_2 = ManipulablePrototypeFactoryStub::getInstance().getProduct(std::string("Aclass"));
204 CPPUNIT_ASSERT( 100 == rndA_2->getcount() );
205 CPPUNIT_ASSERT( rndA_1->getcount() != rndA_2->getcount() );
206}
Note: See TracBrowser for help on using the repository browser.