/* * bondgraphunittest.cpp * * Created on: Oct 29, 2009 * Author: heber */ using namespace std; #include #include #include #include #include #include #include "atom.hpp" #include "bond.hpp" #include "bondgraph.hpp" #include "element.hpp" #include "log.hpp" #include "molecule.hpp" #include "periodentafel.hpp" #include "bondgraphunittest.hpp" /********************************************** Test classes **************************************/ // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( BondGraphTest ); void BondGraphTest::setUp() { atom *Walker = NULL; // init private all pointers to zero TestMolecule = NULL; hydrogen = NULL; tafel = NULL; // construct element hydrogen = new element; hydrogen->Z = 1; hydrogen->CovalentRadius = 0.23; hydrogen->VanDerWaalsRadius = 1.09; strcpy(hydrogen->name, "hydrogen"); strcpy(hydrogen->symbol, "H"); carbon = new element; carbon->Z = 2; carbon->CovalentRadius = 0.68; carbon->VanDerWaalsRadius = 1.7; strcpy(carbon->name, "carbon"); strcpy(carbon->symbol, "C"); // construct periodentafel tafel = new periodentafel; tafel->AddElement(hydrogen); tafel->AddElement(carbon); // construct molecule (tetraeder of hydrogens) TestMolecule = new molecule(tafel); Walker = new atom(); Walker->type = carbon; Walker->node->Init(1., 0., 1. ); TestMolecule->AddAtom(Walker); Walker = new atom(); Walker->type = carbon; Walker->node->Init(0., 1., 1. ); TestMolecule->AddAtom(Walker); Walker = new atom(); Walker->type = carbon; Walker->node->Init(1., 1., 0. ); TestMolecule->AddAtom(Walker); Walker = new atom(); Walker->type = carbon; Walker->node->Init(0., 0., 0. ); TestMolecule->AddAtom(Walker); // check that TestMolecule was correctly constructed CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 4 ); // create a small file with table dummyname = new string("dummy.dat"); filename = new string("test.dat"); ofstream test(filename->c_str()); test << ".\tH\tC\n"; test << "H\t1.\t1.2\n"; test << "C\t1.2\t1.5\n"; test.close(); BG = new BondGraph(true); }; void BondGraphTest::tearDown() { // remove the file remove(filename->c_str()); delete(filename); delete(dummyname); delete(BG); // remove molecule delete(TestMolecule); // note that all the atoms are cleaned by TestMolecule delete(tafel); // note that element is cleaned by periodentafel }; /** UnitTest for BondGraphTest::LoadBondLengthTable(). */ void BondGraphTest::LoadTableTest() { CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) ); CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) ); CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) ); CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) ); }; /** UnitTest for BondGraphTest::ConstructBondGraph(). */ void BondGraphTest::ConstructGraphFromTableTest() { atom *Walker = TestMolecule->start->next; atom *Runner = TestMolecule->end->previous; CPPUNIT_ASSERT( TestMolecule->end != Walker ); CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) ); CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) ); CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) ); }; /** UnitTest for BondGraphTest::ConstructBondGraph(). */ void BondGraphTest::ConstructGraphFromCovalentRadiiTest() { atom *Walker = TestMolecule->start->next; atom *Runner = TestMolecule->end->previous; CPPUNIT_ASSERT( TestMolecule->end != Walker ); CPPUNIT_ASSERT_EQUAL( false , BG->LoadBondLengthTable(*dummyname) ); CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) ); CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) ); }; /********************************************** Main routine **************************************/ int main(int argc, char **argv) { // Get the top level suite from the registry CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); // Adds the test to the list of test to run CppUnit::TextUi::TestRunner runner; runner.addTest( suite ); // Change the default outputter to a compiler error format outputter runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), std::cerr ) ); // Run the tests. bool wasSucessful = runner.run(); // Return error code 1 if the one of test failed. return wasSucessful ? 0 : 1; };