| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * | 
|---|
| 6 | * | 
|---|
| 7 | *   This file is part of MoleCuilder. | 
|---|
| 8 | * | 
|---|
| 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 12 | *    (at your option) any later version. | 
|---|
| 13 | * | 
|---|
| 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 17 | *    GNU General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | *    You should have received a copy of the GNU General Public License | 
|---|
| 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* | 
|---|
| 24 | * AnalysisCorrelationToPointUnitTest.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Oct 13, 2009 | 
|---|
| 27 | *      Author: heber | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | using namespace std; | 
|---|
| 36 |  | 
|---|
| 37 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 38 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 39 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 40 |  | 
|---|
| 41 | #include <cstring> | 
|---|
| 42 |  | 
|---|
| 43 | #include "Analysis/analysis_correlation.hpp" | 
|---|
| 44 | #include "Atom/atom.hpp" | 
|---|
| 45 | #include "Descriptors/MoleculeDescriptor.hpp" | 
|---|
| 46 | #include "Element/element.hpp" | 
|---|
| 47 | #include "Element/periodentafel.hpp" | 
|---|
| 48 | #include "molecule.hpp" | 
|---|
| 49 | #include "World.hpp" | 
|---|
| 50 |  | 
|---|
| 51 | #include "AnalysisCorrelationToPointUnitTest.hpp" | 
|---|
| 52 |  | 
|---|
| 53 | #ifdef HAVE_TESTRUNNER | 
|---|
| 54 | #include "UnitTestMain.hpp" | 
|---|
| 55 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 56 |  | 
|---|
| 57 | /********************************************** Test classes **************************************/ | 
|---|
| 58 |  | 
|---|
| 59 | // Registers the fixture into the 'registry' | 
|---|
| 60 | CPPUNIT_TEST_SUITE_REGISTRATION( AnalysisCorrelationToPointUnitTest ); | 
|---|
| 61 |  | 
|---|
| 62 | void AnalysisCorrelationToPointUnitTest::setUp() | 
|---|
| 63 | { | 
|---|
| 64 | atom *Walker = NULL; | 
|---|
| 65 |  | 
|---|
| 66 | // init private all pointers to zero | 
|---|
| 67 | TestMolecule = NULL; | 
|---|
| 68 | pointmap = NULL; | 
|---|
| 69 | binmap = NULL; | 
|---|
| 70 | point = NULL; | 
|---|
| 71 |  | 
|---|
| 72 | // construct element list | 
|---|
| 73 | std::vector<const element *> elements; | 
|---|
| 74 | hydrogen = World::getInstance().getPeriode()->FindElement(1); | 
|---|
| 75 | CPPUNIT_ASSERT(hydrogen != NULL && "hydrogen element not found"); | 
|---|
| 76 | elements.push_back(hydrogen); | 
|---|
| 77 | // construct molecule (tetraeder of hydrogens) | 
|---|
| 78 | TestMolecule = World::getInstance().createMolecule(); | 
|---|
| 79 | Walker = World::getInstance().createAtom(); | 
|---|
| 80 | Walker->setType(hydrogen); | 
|---|
| 81 | Walker->setPosition(Vector(1., 0., 1. )); | 
|---|
| 82 | TestMolecule->AddAtom(Walker); | 
|---|
| 83 | Walker = World::getInstance().createAtom(); | 
|---|
| 84 | Walker->setType(hydrogen); | 
|---|
| 85 | Walker->setPosition(Vector(0., 1., 1. )); | 
|---|
| 86 | TestMolecule->AddAtom(Walker); | 
|---|
| 87 | Walker = World::getInstance().createAtom(); | 
|---|
| 88 | Walker->setType(hydrogen); | 
|---|
| 89 | Walker->setPosition(Vector(1., 1., 0. )); | 
|---|
| 90 | TestMolecule->AddAtom(Walker); | 
|---|
| 91 | Walker = World::getInstance().createAtom(); | 
|---|
| 92 | Walker->setType(hydrogen); | 
|---|
| 93 | Walker->setPosition(Vector(0., 0., 0. )); | 
|---|
| 94 | TestMolecule->AddAtom(Walker); | 
|---|
| 95 |  | 
|---|
| 96 | // check that TestMolecule was correctly constructed | 
|---|
| 97 | CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 ); | 
|---|
| 98 |  | 
|---|
| 99 | TestMolecule->ActiveFlag = true; | 
|---|
| 100 |  | 
|---|
| 101 | // init point | 
|---|
| 102 | point = new Vector(1.,1.,1.); | 
|---|
| 103 |  | 
|---|
| 104 | // init maps | 
|---|
| 105 | World::getInstance().selectAllMolecules(AllMolecules()); | 
|---|
| 106 | allMolecules = const_cast<const World &>(World::getInstance()).getSelectedMolecules(); | 
|---|
| 107 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, allMolecules.size()); | 
|---|
| 108 | pointmap = CorrelationToPoint( allMolecules, elements, (const Vector *)point ); | 
|---|
| 109 | binmap = NULL; | 
|---|
| 110 |  | 
|---|
| 111 | }; | 
|---|
| 112 |  | 
|---|
| 113 |  | 
|---|
| 114 | void AnalysisCorrelationToPointUnitTest::tearDown() | 
|---|
| 115 | { | 
|---|
| 116 | if (pointmap != NULL) | 
|---|
| 117 | delete(pointmap); | 
|---|
| 118 | if (binmap != NULL) | 
|---|
| 119 | delete(binmap); | 
|---|
| 120 |  | 
|---|
| 121 | delete(point); | 
|---|
| 122 | World::purgeInstance(); | 
|---|
| 123 | logger::purgeInstance(); | 
|---|
| 124 | }; | 
|---|
| 125 |  | 
|---|
| 126 | void AnalysisCorrelationToPointUnitTest::CorrelationToPointTest() | 
|---|
| 127 | { | 
|---|
| 128 | // do the pair correlation | 
|---|
| 129 | CPPUNIT_ASSERT( pointmap != NULL ); | 
|---|
| 130 | CPPUNIT_ASSERT_EQUAL( (size_t)4, pointmap->size() ); | 
|---|
| 131 |  | 
|---|
| 132 | }; | 
|---|
| 133 |  | 
|---|
| 134 | void AnalysisCorrelationToPointUnitTest::CorrelationToPointBinNoRangeTest() | 
|---|
| 135 | { | 
|---|
| 136 | BinPairMap::iterator tester; | 
|---|
| 137 | // put pair correlation into bins and check with no range | 
|---|
| 138 | binmap = BinData( pointmap, 0.5, 0., 0. ); | 
|---|
| 139 | OutputCorrelationMap<BinPairMap> ( (ofstream *)&cout, binmap, OutputCorrelation_Header, OutputCorrelation_Value ); | 
|---|
| 140 | CPPUNIT_ASSERT_EQUAL( (size_t)3, binmap->size() ); | 
|---|
| 141 | tester = binmap->begin(); | 
|---|
| 142 | CPPUNIT_ASSERT_EQUAL( 1., tester->first ); | 
|---|
| 143 | CPPUNIT_ASSERT_EQUAL( 3, tester->second ); | 
|---|
| 144 |  | 
|---|
| 145 | }; | 
|---|
| 146 |  | 
|---|
| 147 | void AnalysisCorrelationToPointUnitTest::CorrelationToPointBinRangeTest() | 
|---|
| 148 | { | 
|---|
| 149 | BinPairMap::iterator tester; | 
|---|
| 150 | // ... and check with [0., 2.] range | 
|---|
| 151 | binmap = BinData( pointmap, 0.5, 0., 2. ); | 
|---|
| 152 | OutputCorrelationMap<BinPairMap> ( (ofstream *)&cout, binmap, OutputCorrelation_Header, OutputCorrelation_Value ); | 
|---|
| 153 | CPPUNIT_ASSERT_EQUAL( (size_t)5, binmap->size() ); | 
|---|
| 154 | tester = binmap->begin(); | 
|---|
| 155 | CPPUNIT_ASSERT_EQUAL( 0., tester->first ); | 
|---|
| 156 | tester = binmap->find(1.); | 
|---|
| 157 | CPPUNIT_ASSERT_EQUAL( 1., tester->first ); | 
|---|
| 158 | CPPUNIT_ASSERT_EQUAL( 3, tester->second ); | 
|---|
| 159 |  | 
|---|
| 160 | }; | 
|---|