1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * ElementUnitTest.cpp
|
---|
10 | *
|
---|
11 | * Created on: Oct 29, 2009
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | using namespace std;
|
---|
21 |
|
---|
22 | #include <cppunit/CompilerOutputter.h>
|
---|
23 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
24 | #include <cppunit/ui/text/TestRunner.h>
|
---|
25 |
|
---|
26 | #include <sstream>
|
---|
27 |
|
---|
28 | #include "Element/element.hpp"
|
---|
29 |
|
---|
30 | // include headers that implement a archive in simple text format
|
---|
31 | #include <boost/archive/text_oarchive.hpp>
|
---|
32 | #include <boost/archive/text_iarchive.hpp>
|
---|
33 |
|
---|
34 | #include "ElementUnitTest.hpp"
|
---|
35 |
|
---|
36 | #ifdef HAVE_TESTRUNNER
|
---|
37 | #include "UnitTestMain.hpp"
|
---|
38 | #endif /*HAVE_TESTRUNNER*/
|
---|
39 |
|
---|
40 | /********************************************** Test classes **************************************/
|
---|
41 |
|
---|
42 | // Registers the fixture into the 'registry'
|
---|
43 | CPPUNIT_TEST_SUITE_REGISTRATION( ElementTest );
|
---|
44 |
|
---|
45 |
|
---|
46 | void ElementTest::setUp()
|
---|
47 | {
|
---|
48 | testelement = new element();
|
---|
49 | testelement->setSymbol("H");
|
---|
50 | };
|
---|
51 |
|
---|
52 |
|
---|
53 | void ElementTest::tearDown()
|
---|
54 | {
|
---|
55 | delete testelement;
|
---|
56 | };
|
---|
57 |
|
---|
58 | /** Test whether assignment works
|
---|
59 | *
|
---|
60 | */
|
---|
61 | void ElementTest::AssignmentTest()
|
---|
62 | {
|
---|
63 | element * otherelement = new element();
|
---|
64 | element * const backup = otherelement;
|
---|
65 | CPPUNIT_ASSERT( otherelement != testelement );
|
---|
66 | otherelement = testelement;
|
---|
67 | CPPUNIT_ASSERT( otherelement == testelement );
|
---|
68 | otherelement = backup;
|
---|
69 | *otherelement = *testelement;
|
---|
70 | CPPUNIT_ASSERT( *otherelement == *testelement );
|
---|
71 | delete backup;
|
---|
72 |
|
---|
73 | otherelement = new element(*testelement);
|
---|
74 | CPPUNIT_ASSERT( otherelement != testelement );
|
---|
75 | CPPUNIT_ASSERT( *otherelement == *testelement );
|
---|
76 | delete otherelement;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** Test whether the getters are working.
|
---|
80 | *
|
---|
81 | */
|
---|
82 | void ElementTest::GetterTest()
|
---|
83 | {
|
---|
84 | CPPUNIT_ASSERT_EQUAL( (atomId_t)0, testelement->getNumber() );
|
---|
85 | CPPUNIT_ASSERT_EQUAL( 0., testelement->getMass() );
|
---|
86 | const unsigned char * color = testelement->getColor();
|
---|
87 | for (size_t i = 0; i < 3; ++i)
|
---|
88 | CPPUNIT_ASSERT_EQUAL( (unsigned char)0, color[i] );
|
---|
89 | CPPUNIT_ASSERT_EQUAL( 0., testelement->getCovalentRadius() );
|
---|
90 | CPPUNIT_ASSERT_EQUAL( 0., testelement->getElectronegativity() );
|
---|
91 | CPPUNIT_ASSERT_EQUAL( 0., testelement->getVanDerWaalsRadius() );
|
---|
92 | CPPUNIT_ASSERT_EQUAL( 0, testelement->getAtomicNumber() );
|
---|
93 | CPPUNIT_ASSERT_EQUAL( 0., testelement->getValence() );
|
---|
94 | CPPUNIT_ASSERT_EQUAL( 0, testelement->getNoValenceOrbitals() );
|
---|
95 | CPPUNIT_ASSERT_EQUAL( 0, testelement->getNoValenceOrbitals() );
|
---|
96 | for (size_t i = 0; i < 3; ++i)
|
---|
97 | CPPUNIT_ASSERT_EQUAL( 0., testelement->getHBondDistance(i) );
|
---|
98 | for (size_t i = 0; i < 3; ++i)
|
---|
99 | CPPUNIT_ASSERT_EQUAL( 0., testelement->getHBondAngle(i) );
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** Test whether the setters are working.
|
---|
103 | *
|
---|
104 | */
|
---|
105 | void ElementTest::SetterTest()
|
---|
106 | {
|
---|
107 | // symbol
|
---|
108 | CPPUNIT_ASSERT( testelement->getSymbol() != std::string("He") );
|
---|
109 | testelement->setSymbol("He");
|
---|
110 | CPPUNIT_ASSERT_EQUAL( std::string("He"), testelement->getSymbol() );
|
---|
111 |
|
---|
112 | // name
|
---|
113 | CPPUNIT_ASSERT( testelement->getName() != std::string("Helium") );
|
---|
114 | testelement->setName("Helium");
|
---|
115 | CPPUNIT_ASSERT_EQUAL( std::string("Helium"), testelement->getName() );
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** Unit test for operator==()
|
---|
119 | */
|
---|
120 | void ElementTest::ComparisonTest()
|
---|
121 | {
|
---|
122 | testelement->setSymbol("He");
|
---|
123 | testelement->setName("Helium");
|
---|
124 | element * newelement = new element();
|
---|
125 | newelement->setSymbol("H");
|
---|
126 | newelement->setName("Hydrogen");
|
---|
127 |
|
---|
128 | CPPUNIT_ASSERT( *testelement != *newelement );
|
---|
129 |
|
---|
130 | newelement->setSymbol("He");
|
---|
131 | newelement->setName("Helium");
|
---|
132 |
|
---|
133 | CPPUNIT_ASSERT( *testelement == *newelement );
|
---|
134 |
|
---|
135 | delete newelement;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** Tests whether serialization of the class works
|
---|
139 | */
|
---|
140 | void ElementTest::SerializeTest()
|
---|
141 | {
|
---|
142 | const std::string he("He");
|
---|
143 | const std::string helium("Helium");
|
---|
144 | testelement->setSymbol(he);
|
---|
145 | testelement->setName(helium);
|
---|
146 |
|
---|
147 | CPPUNIT_ASSERT_EQUAL ( he, testelement->getSymbol() );
|
---|
148 | CPPUNIT_ASSERT_EQUAL( helium, testelement->getName() );
|
---|
149 |
|
---|
150 | // write element to stream
|
---|
151 | std::stringstream stream;
|
---|
152 | boost::archive::text_oarchive oa(stream);
|
---|
153 | oa << testelement;
|
---|
154 |
|
---|
155 | std::cout << "Contents of archive is " << stream.str() << std::endl;
|
---|
156 |
|
---|
157 | // create and open an archive for input
|
---|
158 | boost::archive::text_iarchive ia(stream);
|
---|
159 | // read class state from archive
|
---|
160 | element * newelement;
|
---|
161 |
|
---|
162 | ia >> newelement;
|
---|
163 |
|
---|
164 | CPPUNIT_ASSERT_EQUAL( he, newelement->getSymbol() );
|
---|
165 | CPPUNIT_ASSERT_EQUAL( helium, newelement->getName() );
|
---|
166 |
|
---|
167 | CPPUNIT_ASSERT (*testelement == *newelement);
|
---|
168 |
|
---|
169 | delete newelement;
|
---|
170 | };
|
---|