source: molecuilder/src/unittests/bondgraphunittest.cpp@ 8d9d38

Last change on this file since 8d9d38 was 8d9d38, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Made the world solely responsible for creating and erasing molecules.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * bondgraphunittest.cpp
3 *
4 * Created on: Oct 29, 2009
5 * Author: heber
6 */
7
8using namespace std;
9
10#include <cppunit/CompilerOutputter.h>
11#include <cppunit/extensions/TestFactoryRegistry.h>
12#include <cppunit/ui/text/TestRunner.h>
13
14#include <iostream>
15#include <stdio.h>
16#include <cstring>
17
18#include "World.hpp"
19#include "atom.hpp"
20#include "bond.hpp"
21#include "bondgraph.hpp"
22#include "element.hpp"
23#include "molecule.hpp"
24#include "periodentafel.hpp"
25#include "bondgraphunittest.hpp"
26
27/********************************************** Test classes **************************************/
28
29// Registers the fixture into the 'registry'
30CPPUNIT_TEST_SUITE_REGISTRATION( BondGraphTest );
31
32
33void BondGraphTest::setUp()
34{
35 atom *Walker = NULL;
36
37 // init private all pointers to zero
38 TestMolecule = NULL;
39 hydrogen = NULL;
40 tafel = NULL;
41
42 // construct element
43 hydrogen = new element;
44 hydrogen->Z = 1;
45 strcpy(hydrogen->name, "hydrogen");
46 strcpy(hydrogen->symbol, "H");
47 carbon = new element;
48 carbon->Z = 1;
49 strcpy(carbon->name, "carbon");
50 strcpy(carbon->symbol, "C");
51
52
53 // construct periodentafel
54 tafel = World::get()->getPeriode();
55 tafel->AddElement(hydrogen);
56 tafel->AddElement(carbon);
57
58 // construct molecule (tetraeder of hydrogens)
59 TestMolecule = World::get()->createMolecule();
60 Walker = World::get()->createAtom();
61 Walker->type = hydrogen;
62 Walker->node->Init(1., 0., 1. );
63 TestMolecule->AddAtom(Walker);
64 Walker = World::get()->createAtom();
65 Walker->type = hydrogen;
66 Walker->node->Init(0., 1., 1. );
67 TestMolecule->AddAtom(Walker);
68 Walker = World::get()->createAtom();
69 Walker->type = hydrogen;
70 Walker->node->Init(1., 1., 0. );
71 TestMolecule->AddAtom(Walker);
72 Walker = World::get()->createAtom();
73 Walker->type = hydrogen;
74 Walker->node->Init(0., 0., 0. );
75 TestMolecule->AddAtom(Walker);
76
77 // check that TestMolecule was correctly constructed
78 CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 4 );
79
80 // create a small file with table
81 filename = new string("test.dat");
82 ofstream test(filename->c_str());
83 test << ".\tH\tC\n";
84 test << "H\t1.\t1.2\n";
85 test << "C\t1.2\t1.5\n";
86 test.close();
87 BG = new BondGraph(true);
88};
89
90
91void BondGraphTest::tearDown()
92{
93 // remove the file
94 remove(filename->c_str());
95 delete(filename);
96 delete(BG);
97
98 // remove molecule
99 World::get()->destroyMolecule(TestMolecule);
100 // note that all the atoms are cleaned by TestMolecule
101 World::destroy();
102};
103
104/** UnitTest for BondGraphTest::LoadBondLengthTable().
105 */
106void BondGraphTest::LoadTableTest()
107{
108 CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
109 CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
110 CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) );
111 CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) );
112};
113
114/** UnitTest for BondGraphTest::ConstructBondGraph().
115 */
116void BondGraphTest::ConstructGraphTest()
117{
118 atom *Walker = TestMolecule->start->next;
119 atom *Runner = TestMolecule->end->previous;
120 CPPUNIT_ASSERT( TestMolecule->end != Walker );
121 CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
122 CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) );
123 CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) );
124};
125
126
127/********************************************** Main routine **************************************/
128
129int main(int argc, char **argv)
130{
131 // Get the top level suite from the registry
132 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
133
134 // Adds the test to the list of test to run
135 CppUnit::TextUi::TestRunner runner;
136 runner.addTest( suite );
137
138 // Change the default outputter to a compiler error format outputter
139 runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
140 std::cerr ) );
141 // Run the tests.
142 bool wasSucessful = runner.run();
143
144 // Return error code 1 if the one of test failed.
145 return wasSucessful ? 0 : 1;
146};
Note: See TracBrowser for help on using the repository browser.