| [d7d022] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [d7d022] | 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 |  | 
|---|
| [3bdb6d] | 28 | #include "Element/element.hpp" | 
|---|
| [d7d022] | 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 | { | 
|---|
| [ed26ae] | 84 | CPPUNIT_ASSERT_EQUAL( (atomId_t)0, testelement->getAtomicNumber() ); | 
|---|
| [d7d022] | 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->getValence() ); | 
|---|
|  | 93 | CPPUNIT_ASSERT_EQUAL( 0, testelement->getNoValenceOrbitals() ); | 
|---|
|  | 94 | CPPUNIT_ASSERT_EQUAL( 0, testelement->getNoValenceOrbitals() ); | 
|---|
|  | 95 | for (size_t i = 0; i < 3; ++i) | 
|---|
|  | 96 | CPPUNIT_ASSERT_EQUAL( 0., testelement->getHBondDistance(i) ); | 
|---|
|  | 97 | for (size_t i = 0; i < 3; ++i) | 
|---|
|  | 98 | CPPUNIT_ASSERT_EQUAL( 0., testelement->getHBondAngle(i) ); | 
|---|
|  | 99 | } | 
|---|
|  | 100 |  | 
|---|
|  | 101 | /** Test whether the setters are working. | 
|---|
|  | 102 | * | 
|---|
|  | 103 | */ | 
|---|
|  | 104 | void ElementTest::SetterTest() | 
|---|
|  | 105 | { | 
|---|
|  | 106 | // symbol | 
|---|
|  | 107 | CPPUNIT_ASSERT( testelement->getSymbol() != std::string("He") ); | 
|---|
|  | 108 | testelement->setSymbol("He"); | 
|---|
|  | 109 | CPPUNIT_ASSERT_EQUAL( std::string("He"), testelement->getSymbol() ); | 
|---|
|  | 110 |  | 
|---|
|  | 111 | // name | 
|---|
|  | 112 | CPPUNIT_ASSERT( testelement->getName() != std::string("Helium") ); | 
|---|
|  | 113 | testelement->setName("Helium"); | 
|---|
|  | 114 | CPPUNIT_ASSERT_EQUAL( std::string("Helium"), testelement->getName() ); | 
|---|
|  | 115 | } | 
|---|
|  | 116 |  | 
|---|
|  | 117 | /** Unit test for operator==() | 
|---|
|  | 118 | */ | 
|---|
|  | 119 | void ElementTest::ComparisonTest() | 
|---|
|  | 120 | { | 
|---|
|  | 121 | testelement->setSymbol("He"); | 
|---|
|  | 122 | testelement->setName("Helium"); | 
|---|
|  | 123 | element * newelement = new element(); | 
|---|
|  | 124 | newelement->setSymbol("H"); | 
|---|
|  | 125 | newelement->setName("Hydrogen"); | 
|---|
|  | 126 |  | 
|---|
|  | 127 | CPPUNIT_ASSERT( *testelement != *newelement ); | 
|---|
|  | 128 |  | 
|---|
|  | 129 | newelement->setSymbol("He"); | 
|---|
|  | 130 | newelement->setName("Helium"); | 
|---|
|  | 131 |  | 
|---|
|  | 132 | CPPUNIT_ASSERT( *testelement == *newelement ); | 
|---|
|  | 133 |  | 
|---|
|  | 134 | delete newelement; | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
|  | 137 | /** Tests whether serialization of the class works | 
|---|
|  | 138 | */ | 
|---|
|  | 139 | void ElementTest::SerializeTest() | 
|---|
|  | 140 | { | 
|---|
|  | 141 | const std::string he("He"); | 
|---|
|  | 142 | const std::string helium("Helium"); | 
|---|
|  | 143 | testelement->setSymbol(he); | 
|---|
|  | 144 | testelement->setName(helium); | 
|---|
|  | 145 |  | 
|---|
|  | 146 | CPPUNIT_ASSERT_EQUAL ( he, testelement->getSymbol() ); | 
|---|
|  | 147 | CPPUNIT_ASSERT_EQUAL( helium, testelement->getName() ); | 
|---|
|  | 148 |  | 
|---|
|  | 149 | // write element to stream | 
|---|
|  | 150 | std::stringstream stream; | 
|---|
|  | 151 | boost::archive::text_oarchive oa(stream); | 
|---|
|  | 152 | oa << testelement; | 
|---|
|  | 153 |  | 
|---|
|  | 154 | std::cout << "Contents of archive is " << stream.str() << std::endl; | 
|---|
|  | 155 |  | 
|---|
|  | 156 | // create and open an archive for input | 
|---|
|  | 157 | boost::archive::text_iarchive ia(stream); | 
|---|
|  | 158 | // read class state from archive | 
|---|
|  | 159 | element * newelement; | 
|---|
|  | 160 |  | 
|---|
|  | 161 | ia >> newelement; | 
|---|
|  | 162 |  | 
|---|
|  | 163 | CPPUNIT_ASSERT_EQUAL( he, newelement->getSymbol() ); | 
|---|
|  | 164 | CPPUNIT_ASSERT_EQUAL( helium, newelement->getName() ); | 
|---|
|  | 165 |  | 
|---|
|  | 166 | CPPUNIT_ASSERT (*testelement == *newelement); | 
|---|
|  | 167 |  | 
|---|
|  | 168 | delete newelement; | 
|---|
|  | 169 | }; | 
|---|