| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| 6 | * | 
|---|
| 7 | * | 
|---|
| 8 | *   This file is part of MoleCuilder. | 
|---|
| 9 | * | 
|---|
| 10 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 11 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 12 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 13 | *    (at your option) any later version. | 
|---|
| 14 | * | 
|---|
| 15 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 16 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 17 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 18 | *    GNU General Public License for more details. | 
|---|
| 19 | * | 
|---|
| 20 | *    You should have received a copy of the GNU General Public License | 
|---|
| 21 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | /* | 
|---|
| 25 | * IndexedVectorsUnitTest.cpp | 
|---|
| 26 | * | 
|---|
| 27 | *  Created on: Jul 29, 2012 | 
|---|
| 28 | *      Author: heber | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 | // include config.h | 
|---|
| 32 | #ifdef HAVE_CONFIG_H | 
|---|
| 33 | #include <config.h> | 
|---|
| 34 | #endif | 
|---|
| 35 |  | 
|---|
| 36 | using namespace std; | 
|---|
| 37 |  | 
|---|
| 38 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 39 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 40 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 41 |  | 
|---|
| 42 | #include "IndexedVectorsUnitTest.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | #include <cmath> | 
|---|
| 45 | #include <limits> | 
|---|
| 46 |  | 
|---|
| 47 | #include <boost/assign.hpp> | 
|---|
| 48 |  | 
|---|
| 49 | #include "CodePatterns/Assert.hpp" | 
|---|
| 50 |  | 
|---|
| 51 | #ifdef HAVE_TESTRUNNER | 
|---|
| 52 | #include "UnitTestMain.hpp" | 
|---|
| 53 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 54 |  | 
|---|
| 55 | using namespace boost::assign; | 
|---|
| 56 |  | 
|---|
| 57 | /********************************************** Test classes **************************************/ | 
|---|
| 58 |  | 
|---|
| 59 | // Registers the fixture into the 'registry' | 
|---|
| 60 | CPPUNIT_TEST_SUITE_REGISTRATION( IndexedVectorsTest ); | 
|---|
| 61 |  | 
|---|
| 62 |  | 
|---|
| 63 | void IndexedVectorsTest::setUp() | 
|---|
| 64 | { | 
|---|
| 65 | // failing asserts should be thrown | 
|---|
| 66 | ASSERT_DO(Assert::Throw); | 
|---|
| 67 |  | 
|---|
| 68 | // create two vector_t | 
|---|
| 69 | ones(1.,1.,1.); | 
|---|
| 70 | twos(2.,2.,2.); | 
|---|
| 71 | threes(3.,3.,3.); | 
|---|
| 72 | CPPUNIT_ASSERT_EQUAL( IndexedVectors::value_t::FixedSize, ones.size() ); | 
|---|
| 73 | CPPUNIT_ASSERT_EQUAL( IndexedVectors::value_t::FixedSize, twos.size() ); | 
|---|
| 74 | CPPUNIT_ASSERT_EQUAL( IndexedVectors::value_t::FixedSize, threes.size() ); | 
|---|
| 75 |  | 
|---|
| 76 | // create values | 
|---|
| 77 |  | 
|---|
| 78 | IndexedVectors::values_t values; | 
|---|
| 79 | values.push_back(ones); | 
|---|
| 80 | values.push_back(twos); | 
|---|
| 81 | IndexedVectors::values_t othervalues; | 
|---|
| 82 | othervalues.push_back(threes); | 
|---|
| 83 | othervalues.push_back(threes); | 
|---|
| 84 |  | 
|---|
| 85 | // create two indices | 
|---|
| 86 | IndexedVectors::indices_t indices; | 
|---|
| 87 | indices += 1,2; | 
|---|
| 88 | IndexedVectors::indices_t otherindices; | 
|---|
| 89 | otherindices += 1,3; | 
|---|
| 90 |  | 
|---|
| 91 | // create indexed values | 
|---|
| 92 | ivectors = new IndexedVectors(indices, values); | 
|---|
| 93 | otherivectors = new IndexedVectors(otherindices, othervalues); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 | void IndexedVectorsTest::tearDown() | 
|---|
| 98 | { | 
|---|
| 99 | delete ivectors; | 
|---|
| 100 | delete otherivectors; | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | static void checkValueInIndexedVectors( | 
|---|
| 104 | const IndexedVectors &_vectors, | 
|---|
| 105 | const IndexedVectors::index_t &_index, | 
|---|
| 106 | const IndexedVectors::value_t &_compareto | 
|---|
| 107 | ) | 
|---|
| 108 | { | 
|---|
| 109 | const IndexedVectors::indexedvalues_t &indexedvalues = _vectors.getValues(); | 
|---|
| 110 | IndexedVectors::indexedvalues_t::const_iterator iter = indexedvalues.find(_index); | 
|---|
| 111 | CPPUNIT_ASSERT( iter != indexedvalues.end() ); | 
|---|
| 112 | CPPUNIT_ASSERT( _compareto == iter->second ); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | /** UnitTest for cstor's | 
|---|
| 116 | */ | 
|---|
| 117 | void IndexedVectorsTest::Constructor_Test() | 
|---|
| 118 | { | 
|---|
| 119 | // check whether -1 is dropped | 
|---|
| 120 | IndexedVectors::indices_t indices; | 
|---|
| 121 | indices += 1,-1,3; | 
|---|
| 122 | IndexedVectors::values_t values; | 
|---|
| 123 | values.push_back(ones); | 
|---|
| 124 | values.push_back(twos); | 
|---|
| 125 | values.push_back(threes); | 
|---|
| 126 | IndexedVectors testivectors(indices, values); | 
|---|
| 127 |  | 
|---|
| 128 | CPPUNIT_ASSERT_EQUAL( (size_t)2, testivectors.getValues().size() ); | 
|---|
| 129 | checkValueInIndexedVectors(testivectors, 1, ones); | 
|---|
| 130 | checkValueInIndexedVectors(testivectors, 3, threes); | 
|---|
| 131 | CPPUNIT_ASSERT( testivectors.getValues().find(-1) == testivectors.getValues().end() ); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | /** UnitTest for operator+=() | 
|---|
| 135 | */ | 
|---|
| 136 | void IndexedVectorsTest::operatorPlusEqual_Test() | 
|---|
| 137 | { | 
|---|
| 138 | // safeguard initial sizes | 
|---|
| 139 | CPPUNIT_ASSERT_EQUAL( (size_t)2, ivectors->getValues().size() ); | 
|---|
| 140 | CPPUNIT_ASSERT_EQUAL( (size_t)2, otherivectors->getValues().size() ); | 
|---|
| 141 |  | 
|---|
| 142 | // perform operation | 
|---|
| 143 | *ivectors += *otherivectors; | 
|---|
| 144 |  | 
|---|
| 145 | // check new and ole sizes | 
|---|
| 146 | CPPUNIT_ASSERT_EQUAL( (size_t)3, ivectors->getValues().size() ); | 
|---|
| 147 | CPPUNIT_ASSERT_EQUAL( (size_t)2, otherivectors->getValues().size() ); | 
|---|
| 148 |  | 
|---|
| 149 | // then check result | 
|---|
| 150 | IndexedVectors::value_t result; | 
|---|
| 151 | CPPUNIT_ASSERT_EQUAL( IndexedVectors::value_t::FixedSize, result.size() ); | 
|---|
| 152 | for (size_t i=0; i<IndexedVectors::value_t::FixedSize; ++i) | 
|---|
| 153 | result[i] = ones[i] + threes[i]; | 
|---|
| 154 | for (IndexedVectors::indexedvalues_t::const_iterator iter = ivectors->getValues().begin(); | 
|---|
| 155 | iter != ivectors->getValues().end(); ++iter) { | 
|---|
| 156 | CPPUNIT_ASSERT_EQUAL( IndexedVectors::value_t::FixedSize, iter->second.size() ); | 
|---|
| 157 | } | 
|---|
| 158 | checkValueInIndexedVectors(*ivectors, 1, result); | 
|---|
| 159 | checkValueInIndexedVectors(*ivectors, 2, twos); | 
|---|
| 160 | checkValueInIndexedVectors(*ivectors, 3, threes); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | /** UnitTest for operator-=() | 
|---|
| 164 | */ | 
|---|
| 165 | void IndexedVectorsTest::operatorMinusEqual_Test() | 
|---|
| 166 | { | 
|---|
| 167 | // safeguard initial sizes | 
|---|
| 168 | CPPUNIT_ASSERT_EQUAL( (size_t)2, ivectors->getValues().size() ); | 
|---|
| 169 | CPPUNIT_ASSERT_EQUAL( (size_t)2, otherivectors->getValues().size() ); | 
|---|
| 170 |  | 
|---|
| 171 | // perform operation | 
|---|
| 172 | *ivectors -= *otherivectors; | 
|---|
| 173 |  | 
|---|
| 174 | // check new and ole sizes | 
|---|
| 175 | CPPUNIT_ASSERT_EQUAL( (size_t)3, ivectors->getValues().size() ); | 
|---|
| 176 | CPPUNIT_ASSERT_EQUAL( (size_t)2, otherivectors->getValues().size() ); | 
|---|
| 177 |  | 
|---|
| 178 | // then check result | 
|---|
| 179 | IndexedVectors::value_t result; | 
|---|
| 180 | IndexedVectors::value_t thirdresult; | 
|---|
| 181 | CPPUNIT_ASSERT_EQUAL( IndexedVectors::value_t::FixedSize, result.size() ); | 
|---|
| 182 | for (size_t i=0; i<IndexedVectors::value_t::FixedSize; ++i) { | 
|---|
| 183 | result[i] = ones[i] - threes[i]; | 
|---|
| 184 | thirdresult[i] = -threes[i]; | 
|---|
| 185 | } | 
|---|
| 186 | for (IndexedVectors::indexedvalues_t::const_iterator iter = ivectors->getValues().begin(); | 
|---|
| 187 | iter != ivectors->getValues().end(); ++iter) { | 
|---|
| 188 | CPPUNIT_ASSERT_EQUAL( IndexedVectors::value_t::FixedSize, iter->second.size() ); | 
|---|
| 189 | } | 
|---|
| 190 | checkValueInIndexedVectors(*ivectors, 1, result); | 
|---|
| 191 | checkValueInIndexedVectors(*ivectors, 2, twos); | 
|---|
| 192 | checkValueInIndexedVectors(*ivectors, 3, thirdresult); | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 |  | 
|---|
| 196 | /** UnitTest for operator==() | 
|---|
| 197 | */ | 
|---|
| 198 | void IndexedVectorsTest::equality_Test() | 
|---|
| 199 | { | 
|---|
| 200 | CPPUNIT_ASSERT( !(*ivectors == *otherivectors) ); | 
|---|
| 201 | CPPUNIT_ASSERT( *ivectors != *otherivectors ); | 
|---|
| 202 |  | 
|---|
| 203 | // test against empty ivectors | 
|---|
| 204 | IndexedVectors emptyivectors; | 
|---|
| 205 | CPPUNIT_ASSERT( !(*ivectors == emptyivectors) ); | 
|---|
| 206 | CPPUNIT_ASSERT( *ivectors != emptyivectors ); | 
|---|
| 207 |  | 
|---|
| 208 | // tests against themselves | 
|---|
| 209 | CPPUNIT_ASSERT( *ivectors == *ivectors ); | 
|---|
| 210 | CPPUNIT_ASSERT( *otherivectors == *otherivectors ); | 
|---|
| 211 | CPPUNIT_ASSERT( emptyivectors == emptyivectors ); | 
|---|
| 212 |  | 
|---|
| 213 | // check against ZeroInstance | 
|---|
| 214 | CPPUNIT_ASSERT( *ivectors != ZeroInstance<IndexedVectors>() ); | 
|---|
| 215 | CPPUNIT_ASSERT( *otherivectors != ZeroInstance<IndexedVectors>() ); | 
|---|
| 216 | } | 
|---|