| [c4d4df] | 1 | /* | 
|---|
|  | 2 | * AnalysisCorrelationToSurfaceUnitTest.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Oct 13, 2009 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | using namespace std; | 
|---|
|  | 9 |  | 
|---|
|  | 10 | #include <cppunit/CompilerOutputter.h> | 
|---|
|  | 11 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
|  | 12 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
|  | 13 |  | 
|---|
|  | 14 | #include "analysis_correlation.hpp" | 
|---|
|  | 15 | #include "AnalysisCorrelationToSurfaceUnitTest.hpp" | 
|---|
|  | 16 |  | 
|---|
|  | 17 | #include "atom.hpp" | 
|---|
|  | 18 | #include "boundary.hpp" | 
|---|
|  | 19 | #include "element.hpp" | 
|---|
|  | 20 | #include "molecule.hpp" | 
|---|
|  | 21 | #include "linkedcell.hpp" | 
|---|
|  | 22 | #include "periodentafel.hpp" | 
|---|
|  | 23 | #include "tesselation.hpp" | 
|---|
|  | 24 |  | 
|---|
|  | 25 | /********************************************** Test classes **************************************/ | 
|---|
|  | 26 |  | 
|---|
|  | 27 | // Registers the fixture into the 'registry' | 
|---|
|  | 28 | CPPUNIT_TEST_SUITE_REGISTRATION( AnalysisCorrelationToSurfaceUnitTest ); | 
|---|
|  | 29 |  | 
|---|
|  | 30 | void AnalysisCorrelationToSurfaceUnitTest::setUp() | 
|---|
|  | 31 | { | 
|---|
|  | 32 | atom *Walker = NULL; | 
|---|
|  | 33 |  | 
|---|
|  | 34 | // init private all pointers to zero | 
|---|
| [a5551b] | 35 | TestList = NULL; | 
|---|
| [c4d4df] | 36 | TestMolecule = NULL; | 
|---|
|  | 37 | hydrogen = NULL; | 
|---|
|  | 38 | tafel = NULL; | 
|---|
|  | 39 | surfacemap = NULL; | 
|---|
|  | 40 | binmap = NULL; | 
|---|
|  | 41 | Surface = NULL; | 
|---|
|  | 42 | LC = NULL; | 
|---|
|  | 43 |  | 
|---|
|  | 44 | // construct element | 
|---|
|  | 45 | hydrogen = new element; | 
|---|
|  | 46 | hydrogen->Z = 1; | 
|---|
|  | 47 | strcpy(hydrogen->name, "hydrogen"); | 
|---|
| [bbc338] | 48 | strcpy(hydrogen->symbol, "H"); | 
|---|
|  | 49 | carbon = new element; | 
|---|
|  | 50 | carbon->Z = 6; | 
|---|
|  | 51 | strcpy(carbon->name, "carbon"); | 
|---|
|  | 52 | strcpy(carbon->symbol, "C"); | 
|---|
| [c4d4df] | 53 |  | 
|---|
|  | 54 | // construct periodentafel | 
|---|
|  | 55 | tafel = new periodentafel; | 
|---|
|  | 56 | tafel->AddElement(hydrogen); | 
|---|
| [bbc338] | 57 | tafel->AddElement(carbon); | 
|---|
| [c4d4df] | 58 |  | 
|---|
| [bbc338] | 59 | // construct molecule (tetraeder of hydrogens) base | 
|---|
| [c4d4df] | 60 | TestMolecule = new molecule(tafel); | 
|---|
|  | 61 | Walker = new atom(); | 
|---|
|  | 62 | Walker->type = hydrogen; | 
|---|
|  | 63 | Walker->node->Init(1., 0., 1. ); | 
|---|
|  | 64 | TestMolecule->AddAtom(Walker); | 
|---|
|  | 65 | Walker = new atom(); | 
|---|
|  | 66 | Walker->type = hydrogen; | 
|---|
|  | 67 | Walker->node->Init(0., 1., 1. ); | 
|---|
|  | 68 | TestMolecule->AddAtom(Walker); | 
|---|
|  | 69 | Walker = new atom(); | 
|---|
|  | 70 | Walker->type = hydrogen; | 
|---|
|  | 71 | Walker->node->Init(1., 1., 0. ); | 
|---|
|  | 72 | TestMolecule->AddAtom(Walker); | 
|---|
|  | 73 | Walker = new atom(); | 
|---|
|  | 74 | Walker->type = hydrogen; | 
|---|
|  | 75 | Walker->node->Init(0., 0., 0. ); | 
|---|
|  | 76 | TestMolecule->AddAtom(Walker); | 
|---|
|  | 77 |  | 
|---|
|  | 78 | // check that TestMolecule was correctly constructed | 
|---|
|  | 79 | CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 4 ); | 
|---|
|  | 80 |  | 
|---|
| [a5551b] | 81 | TestList = new MoleculeListClass; | 
|---|
|  | 82 | TestMolecule->ActiveFlag = true; | 
|---|
|  | 83 | TestList->insert(TestMolecule); | 
|---|
|  | 84 |  | 
|---|
| [c4d4df] | 85 | // init tesselation and linked cell | 
|---|
|  | 86 | Surface = new Tesselation; | 
|---|
| [e138de] | 87 | FindNonConvexBorder(TestMolecule, Surface, (const LinkedCell *&)LC, 2.5, NULL); | 
|---|
| [c4d4df] | 88 | LC = new LinkedCell(TestMolecule, 5.); | 
|---|
|  | 89 | CPPUNIT_ASSERT_EQUAL( (size_t)4, Surface->PointsOnBoundary.size() ); | 
|---|
|  | 90 | CPPUNIT_ASSERT_EQUAL( (size_t)6, Surface->LinesOnBoundary.size() ); | 
|---|
|  | 91 | CPPUNIT_ASSERT_EQUAL( (size_t)4, Surface->TrianglesOnBoundary.size() ); | 
|---|
|  | 92 |  | 
|---|
| [bbc338] | 93 | // add outer atoms | 
|---|
|  | 94 | Walker = new atom(); | 
|---|
|  | 95 | Walker->type = carbon; | 
|---|
|  | 96 | Walker->node->Init(4., 0., 4. ); | 
|---|
|  | 97 | TestMolecule->AddAtom(Walker); | 
|---|
|  | 98 | Walker = new atom(); | 
|---|
|  | 99 | Walker->type = carbon; | 
|---|
|  | 100 | Walker->node->Init(0., 4., 4. ); | 
|---|
|  | 101 | TestMolecule->AddAtom(Walker); | 
|---|
|  | 102 | Walker = new atom(); | 
|---|
|  | 103 | Walker->type = carbon; | 
|---|
|  | 104 | Walker->node->Init(4., 4., 0. ); | 
|---|
|  | 105 | TestMolecule->AddAtom(Walker); | 
|---|
|  | 106 | // add inner atoms | 
|---|
|  | 107 | Walker = new atom(); | 
|---|
|  | 108 | Walker->type = carbon; | 
|---|
|  | 109 | Walker->node->Init(0.5, 0.5, 0.5 ); | 
|---|
|  | 110 | TestMolecule->AddAtom(Walker); | 
|---|
|  | 111 |  | 
|---|
| [c4d4df] | 112 | // init maps | 
|---|
| [bbc338] | 113 | surfacemap = NULL; | 
|---|
| [c4d4df] | 114 | binmap = NULL; | 
|---|
|  | 115 |  | 
|---|
|  | 116 | }; | 
|---|
|  | 117 |  | 
|---|
|  | 118 |  | 
|---|
|  | 119 | void AnalysisCorrelationToSurfaceUnitTest::tearDown() | 
|---|
|  | 120 | { | 
|---|
|  | 121 | if (surfacemap != NULL) | 
|---|
|  | 122 | delete(surfacemap); | 
|---|
|  | 123 | if (binmap != NULL) | 
|---|
|  | 124 | delete(binmap); | 
|---|
|  | 125 |  | 
|---|
|  | 126 | // remove | 
|---|
| [a5551b] | 127 | delete(TestList); | 
|---|
| [776b64] | 128 | delete(Surface); | 
|---|
|  | 129 | // note that all the atoms are cleaned by TestMolecule | 
|---|
| [c4d4df] | 130 | delete(LC); | 
|---|
|  | 131 | delete(tafel); | 
|---|
|  | 132 | // note that element is cleaned by periodentafel | 
|---|
|  | 133 | }; | 
|---|
|  | 134 |  | 
|---|
|  | 135 |  | 
|---|
|  | 136 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceTest() | 
|---|
|  | 137 | { | 
|---|
|  | 138 | // do the pair correlation | 
|---|
| [e138de] | 139 | surfacemap = CorrelationToSurface( TestList, hydrogen, Surface, LC ); | 
|---|
| [c4d4df] | 140 | CPPUNIT_ASSERT( surfacemap != NULL ); | 
|---|
|  | 141 | CPPUNIT_ASSERT_EQUAL( (size_t)4, surfacemap->size() ); | 
|---|
|  | 142 | }; | 
|---|
|  | 143 |  | 
|---|
| [bbc338] | 144 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceHydrogenBinNoRangeTest() | 
|---|
| [c4d4df] | 145 | { | 
|---|
|  | 146 | BinPairMap::iterator tester; | 
|---|
| [e138de] | 147 | surfacemap = CorrelationToSurface( TestList, hydrogen, Surface, LC ); | 
|---|
| [c4d4df] | 148 | // put pair correlation into bins and check with no range | 
|---|
| [e138de] | 149 | binmap = BinData( surfacemap, 0.5, 0., 0. ); | 
|---|
| [c4d4df] | 150 | CPPUNIT_ASSERT_EQUAL( (size_t)1, binmap->size() ); | 
|---|
| [e138de] | 151 | //OutputCorrelation ( binmap ); | 
|---|
| [c4d4df] | 152 | tester = binmap->begin(); | 
|---|
|  | 153 | CPPUNIT_ASSERT_EQUAL( 0., tester->first ); | 
|---|
|  | 154 | CPPUNIT_ASSERT_EQUAL( 4, tester->second ); | 
|---|
|  | 155 |  | 
|---|
|  | 156 | }; | 
|---|
|  | 157 |  | 
|---|
| [bbc338] | 158 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceHydrogenBinRangeTest() | 
|---|
| [c4d4df] | 159 | { | 
|---|
|  | 160 | BinPairMap::iterator tester; | 
|---|
| [e138de] | 161 | surfacemap = CorrelationToSurface( TestList, hydrogen, Surface, LC ); | 
|---|
| [c4d4df] | 162 | // ... and check with [0., 2.] range | 
|---|
| [e138de] | 163 | binmap = BinData( surfacemap, 0.5, 0., 2. ); | 
|---|
| [c4d4df] | 164 | CPPUNIT_ASSERT_EQUAL( (size_t)5, binmap->size() ); | 
|---|
| [e138de] | 165 | //OutputCorrelation ( binmap ); | 
|---|
| [c4d4df] | 166 | tester = binmap->begin(); | 
|---|
|  | 167 | CPPUNIT_ASSERT_EQUAL( 0., tester->first ); | 
|---|
|  | 168 | CPPUNIT_ASSERT_EQUAL( 4, tester->second ); | 
|---|
|  | 169 | tester = binmap->find(1.); | 
|---|
|  | 170 | CPPUNIT_ASSERT_EQUAL( 1., tester->first ); | 
|---|
|  | 171 | CPPUNIT_ASSERT_EQUAL( 0, tester->second ); | 
|---|
|  | 172 |  | 
|---|
|  | 173 | }; | 
|---|
|  | 174 |  | 
|---|
| [bbc338] | 175 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceCarbonBinNoRangeTest() | 
|---|
|  | 176 | { | 
|---|
|  | 177 | BinPairMap::iterator tester; | 
|---|
| [e138de] | 178 | surfacemap = CorrelationToSurface( TestList, carbon, Surface, LC ); | 
|---|
| [bbc338] | 179 | // put pair correlation into bins and check with no range | 
|---|
| [e138de] | 180 | binmap = BinData( surfacemap, 0.5, 0., 0. ); | 
|---|
| [bbc338] | 181 | CPPUNIT_ASSERT_EQUAL( (size_t)2, binmap->size() ); | 
|---|
|  | 182 | OutputCorrelation ( (ofstream *)&cout, binmap ); | 
|---|
|  | 183 | // inside point is first and must have negative value | 
|---|
|  | 184 | tester = binmap->lower_bound(2.95); // start depends on the min value and | 
|---|
|  | 185 | CPPUNIT_ASSERT( tester != binmap->end() ); | 
|---|
|  | 186 | CPPUNIT_ASSERT_EQUAL( 3, tester->second ); | 
|---|
|  | 187 | // inner point | 
|---|
| [7ea9e6] | 188 | tester = binmap->lower_bound(-0.5); | 
|---|
| [bbc338] | 189 | CPPUNIT_ASSERT( tester != binmap->end() ); | 
|---|
|  | 190 | CPPUNIT_ASSERT_EQUAL( 1, tester->second ); | 
|---|
|  | 191 | }; | 
|---|
|  | 192 |  | 
|---|
|  | 193 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceCarbonBinRangeTest() | 
|---|
|  | 194 | { | 
|---|
|  | 195 | BinPairMap::iterator tester; | 
|---|
| [e138de] | 196 | surfacemap = CorrelationToSurface( TestList, carbon, Surface, LC ); | 
|---|
| [bbc338] | 197 | // ... and check with [0., 2.] range | 
|---|
| [e138de] | 198 | binmap = BinData( surfacemap, 0.5, -2., 4. ); | 
|---|
| [bbc338] | 199 | CPPUNIT_ASSERT_EQUAL( (size_t)13, binmap->size() ); | 
|---|
|  | 200 | OutputCorrelation ( (ofstream *)&cout, binmap ); | 
|---|
|  | 201 | // three outside points | 
|---|
| [7ea9e6] | 202 | tester = binmap->lower_bound(3.); | 
|---|
| [bbc338] | 203 | CPPUNIT_ASSERT( tester != binmap->end() ); | 
|---|
|  | 204 | CPPUNIT_ASSERT_EQUAL( 3, tester->second ); | 
|---|
|  | 205 | // inner point | 
|---|
| [7ea9e6] | 206 | tester = binmap->lower_bound(-0.5); | 
|---|
| [bbc338] | 207 | CPPUNIT_ASSERT( tester != binmap->end() ); | 
|---|
|  | 208 | CPPUNIT_ASSERT_EQUAL( 1, tester->second ); | 
|---|
|  | 209 |  | 
|---|
|  | 210 | }; | 
|---|
|  | 211 |  | 
|---|
| [c4d4df] | 212 | /********************************************** Main routine **************************************/ | 
|---|
|  | 213 |  | 
|---|
|  | 214 | int main(int argc, char **argv) | 
|---|
|  | 215 | { | 
|---|
|  | 216 | // Get the top level suite from the registry | 
|---|
|  | 217 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); | 
|---|
|  | 218 |  | 
|---|
|  | 219 | // Adds the test to the list of test to run | 
|---|
|  | 220 | CppUnit::TextUi::TestRunner runner; | 
|---|
|  | 221 | runner.addTest( suite ); | 
|---|
|  | 222 |  | 
|---|
|  | 223 | // Change the default outputter to a compiler error format outputter | 
|---|
|  | 224 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), | 
|---|
|  | 225 | std::cerr ) ); | 
|---|
|  | 226 | // Run the tests. | 
|---|
|  | 227 | bool wasSucessful = runner.run(); | 
|---|
|  | 228 |  | 
|---|
|  | 229 | // Return error code 1 if the one of test failed. | 
|---|
|  | 230 | return wasSucessful ? 0 : 1; | 
|---|
|  | 231 | }; | 
|---|