source: molecuilder/src/unittests/analysisbondsunittest.cpp@ 36c5cf

Last change on this file since 36c5cf was 44becc, checked in by Frederik Heber <heber@…>, 16 years ago

Tests now work with Eclipse ECUT's TestRunner.

  • new switch in configure.ac: --enable-ecut
  • all tests are compiled as single test as before
  • and a new TestRunner test suite that combines all test into a single executable which can be run as CppUnit program in Eclipse (and then gives JUnit like output).
  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[341850]1/*
2 * analysisbondsunittest.cpp
3 *
4 * Created on: Nov 7, 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>
[e6fe8a]16#include <cstring>
[341850]17
[e462b3]18#include "analysis_bonds.hpp"
19#include "analysisbondsunittest.hpp"
[341850]20#include "atom.hpp"
21#include "bond.hpp"
22#include "bondgraph.hpp"
23#include "element.hpp"
24#include "molecule.hpp"
25#include "periodentafel.hpp"
26
[44becc]27#ifdef HAVE_TESTRUNNER
28#include "UnitTestMain.hpp"
29#endif /*HAVE_TESTRUNNER*/
30
[341850]31/********************************************** Test classes **************************************/
32
33// Registers the fixture into the 'registry'
34CPPUNIT_TEST_SUITE_REGISTRATION( AnalysisBondsTest );
35
36
37void AnalysisBondsTest::setUp()
38{
39 atom *Walker = NULL;
40
41 // init private all pointers to zero
42 TestMolecule = NULL;
43 hydrogen = NULL;
44 tafel = NULL;
45
46 // construct element
47 hydrogen = new element;
48 hydrogen->Z = 1;
[e462b3]49 hydrogen->Valence = 1;
50 hydrogen->NoValenceOrbitals = 1;
[341850]51 strcpy(hydrogen->name, "hydrogen");
52 strcpy(hydrogen->symbol, "H");
53 carbon = new element;
54 carbon->Z = 1;
[e462b3]55 carbon->Valence = 4;
56 carbon->NoValenceOrbitals = 4;
[341850]57 strcpy(carbon->name, "carbon");
58 strcpy(carbon->symbol, "C");
59
60
61 // construct periodentafel
62 tafel = new periodentafel;
63 tafel->AddElement(hydrogen);
64 tafel->AddElement(carbon);
65
66 // construct molecule (tetraeder of hydrogens)
67 TestMolecule = new molecule(tafel);
68 Walker = new atom();
69 Walker->type = hydrogen;
[e462b3]70 Walker->node->Init(1.5, 0., 1.5 );
[341850]71 TestMolecule->AddAtom(Walker);
72 Walker = new atom();
73 Walker->type = hydrogen;
[e462b3]74 Walker->node->Init(0., 1.5, 1.5 );
[341850]75 TestMolecule->AddAtom(Walker);
76 Walker = new atom();
77 Walker->type = hydrogen;
[e462b3]78 Walker->node->Init(1.5, 1.5, 0. );
[341850]79 TestMolecule->AddAtom(Walker);
80 Walker = new atom();
81 Walker->type = hydrogen;
82 Walker->node->Init(0., 0., 0. );
83 TestMolecule->AddAtom(Walker);
[e462b3]84 Walker = new atom();
85 Walker->type = carbon;
86 Walker->node->Init(0.5, 0.5, 0.5 );
87 TestMolecule->AddAtom(Walker);
[341850]88
89 // check that TestMolecule was correctly constructed
[e462b3]90 CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 5 );
[341850]91
92 // create a small file with table
93 filename = new string("test.dat");
94 ofstream test(filename->c_str());
95 test << ".\tH\tC\n";
96 test << "H\t1.\t1.2\n";
97 test << "C\t1.2\t1.5\n";
[e462b3]98 test.close();
[341850]99 BG = new BondGraph(true);
[e462b3]100
101 CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
102 CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
103 CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) );
104 CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) );
105
106 BG->ConstructBondGraph(TestMolecule);
[341850]107};
108
109
110void AnalysisBondsTest::tearDown()
111{
112 // remove the file
113 remove(filename->c_str());
114 delete(filename);
115 delete(BG);
116
117 // remove molecule
118 delete(TestMolecule);
119 // note that all the atoms are cleaned by TestMolecule
120 delete(tafel);
121 // note that element is cleaned by periodentafel
122};
123
[e462b3]124/** UnitTest for GetMaxMinMeanBondCount().
[341850]125 */
[e462b3]126void AnalysisBondsTest::GetMaxMinMeanBondCountTest()
[341850]127{
[e462b3]128 double Min = 20.; // check that initialization resets these arbitrary values
129 double Mean = 200.;
130 double Max = 1e-6;
131 GetMaxMinMeanBondCount(TestMolecule, Min, Mean, Max);
132 CPPUNIT_ASSERT_EQUAL( 1., Min );
133 CPPUNIT_ASSERT_EQUAL( 1.6, Mean );
134 CPPUNIT_ASSERT_EQUAL( 4., Max );
[341850]135
[e462b3]136};
[341850]137
[e462b3]138/** UnitTest for MinMaxBondDistanceBetweenElements().
139 */
140void AnalysisBondsTest::MinMeanMaxBondDistanceBetweenElementsTest()
141{
142 double Min = 20.; // check that initialization resets these arbitrary values
143 double Mean = 2e+6;
144 double Max = 1e-6;
145 double Min2 = 20.;
146 double Mean2 = 2e+6;
147 double Max2 = 1e-6;
148 const double maxbondlength = sqrt(1.*1. + 1.*1. + .5*.5);
149 const double minbondlength = sqrt(.5*.5 + .5*.5 + .5*.5);
150 const double meanbondlength = (minbondlength+3.*maxbondlength)/4.;
151 // check bond lengths C-H
152 MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, carbon, Min, Mean, Max);
153 CPPUNIT_ASSERT_EQUAL( minbondlength , Min );
154 CPPUNIT_ASSERT_EQUAL( meanbondlength , Mean );
155 CPPUNIT_ASSERT_EQUAL( maxbondlength , Max );
156
157 // check that elements are symmetric, i.e. C-H == H-C
158 MinMeanMaxBondDistanceBetweenElements(TestMolecule, carbon, hydrogen, Min2, Mean2, Max2);
159 CPPUNIT_ASSERT_EQUAL( Min , Min2 );
160 CPPUNIT_ASSERT_EQUAL( Mean , Mean2 );
161 CPPUNIT_ASSERT_EQUAL( Max , Max2 );
162
163 // check no bond case (no bonds H-H in system!)
164 MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, hydrogen, Min, Mean, Max);
165 CPPUNIT_ASSERT_EQUAL( 0. , Min );
166 CPPUNIT_ASSERT_EQUAL( 0. , Mean );
167 CPPUNIT_ASSERT_EQUAL( 0. , Max );
[341850]168};
Note: See TracBrowser for help on using the repository browser.