| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2013 Frederik Heber. 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 | * EigenvaluesUnitTest.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Sep 27, 2013 | 
|---|
| 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 headers that implement a archive in simple text format | 
|---|
| 42 | #include <boost/archive/text_oarchive.hpp> | 
|---|
| 43 | #include <boost/archive/text_iarchive.hpp> | 
|---|
| 44 |  | 
|---|
| 45 | #include "EigenvaluesUnitTest.hpp" | 
|---|
| 46 |  | 
|---|
| 47 | #include <algorithm> | 
|---|
| 48 | #include <limits> | 
|---|
| 49 |  | 
|---|
| 50 | #include <boost/assign.hpp> | 
|---|
| 51 | #include <boost/foreach.hpp> | 
|---|
| 52 |  | 
|---|
| 53 | #include <sstream> | 
|---|
| 54 |  | 
|---|
| 55 | #include "CodePatterns/Assert.hpp" | 
|---|
| 56 |  | 
|---|
| 57 | #ifdef HAVE_TESTRUNNER | 
|---|
| 58 | #include "UnitTestMain.hpp" | 
|---|
| 59 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 60 |  | 
|---|
| 61 | using namespace boost::assign; | 
|---|
| 62 |  | 
|---|
| 63 | /********************************************** Test classes **************************************/ | 
|---|
| 64 |  | 
|---|
| 65 | // Registers the fixture into the 'registry' | 
|---|
| 66 | CPPUNIT_TEST_SUITE_REGISTRATION( EigenvaluesTest ); | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | void EigenvaluesTest::setUp() | 
|---|
| 70 | { | 
|---|
| 71 | // failing asserts should be thrown | 
|---|
| 72 | ASSERT_DO(Assert::Throw); | 
|---|
| 73 |  | 
|---|
| 74 | samples += 1., 1.5, 2., 3., 4.; | 
|---|
| 75 | CPPUNIT_ASSERT_EQUAL( (size_t)5, samples.size() ); | 
|---|
| 76 |  | 
|---|
| 77 | ev = new Eigenvalues(samples); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | void EigenvaluesTest::tearDown() | 
|---|
| 81 | { | 
|---|
| 82 | delete ev; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | /** UnitTest for operator==() | 
|---|
| 86 | */ | 
|---|
| 87 | void EigenvaluesTest::equality_Test() | 
|---|
| 88 | { | 
|---|
| 89 | // create different ev | 
|---|
| 90 | Eigenvalues::samples_t othersamples; | 
|---|
| 91 | othersamples += 1., 2., 3.; | 
|---|
| 92 | Eigenvalues otherev(othersamples); | 
|---|
| 93 |  | 
|---|
| 94 | CPPUNIT_ASSERT( !(*ev == otherev) ); | 
|---|
| 95 | CPPUNIT_ASSERT( *ev != otherev ); | 
|---|
| 96 |  | 
|---|
| 97 | // test against empty ev | 
|---|
| 98 | Eigenvalues emptyev; | 
|---|
| 99 | CPPUNIT_ASSERT( !(*ev == emptyev) ); | 
|---|
| 100 | CPPUNIT_ASSERT( *ev != emptyev ); | 
|---|
| 101 |  | 
|---|
| 102 | // tests against themselves | 
|---|
| 103 | CPPUNIT_ASSERT( *ev == *ev ); | 
|---|
| 104 | CPPUNIT_ASSERT( otherev == otherev ); | 
|---|
| 105 | CPPUNIT_ASSERT( emptyev == emptyev ); | 
|---|
| 106 |  | 
|---|
| 107 | // check against ZeroInstance | 
|---|
| 108 | CPPUNIT_ASSERT( *ev != ZeroInstance<Eigenvalues>() ); | 
|---|
| 109 | CPPUNIT_ASSERT( otherev != ZeroInstance<Eigenvalues>() ); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | /** UnitTest for operator+=() | 
|---|
| 113 | */ | 
|---|
| 114 | void EigenvaluesTest::operatorPlusEqual_NonOverlapping_Test() | 
|---|
| 115 | { | 
|---|
| 116 | { | 
|---|
| 117 | // create non-overlapping set | 
|---|
| 118 | Eigenvalues::samples_t othersamples; | 
|---|
| 119 | othersamples += 5., 6., 7.; | 
|---|
| 120 | Eigenvalues otherev(othersamples); | 
|---|
| 121 | const size_t otheraddedsize = otherev.addedsamples.size(); | 
|---|
| 122 | const size_t othersubtractedsize = otherev.subtractedsamples.size(); | 
|---|
| 123 | const size_t addedsize = ev->addedsamples.size(); | 
|---|
| 124 | const size_t subtractedsize = ev->subtractedsamples.size(); | 
|---|
| 125 | *ev += otherev; | 
|---|
| 126 | // otherev did not change | 
|---|
| 127 | CPPUNIT_ASSERT_EQUAL( otheraddedsize, otherev.addedsamples.size() ); | 
|---|
| 128 | CPPUNIT_ASSERT_EQUAL( othersubtractedsize, otherev.subtractedsamples.size() ); | 
|---|
| 129 | // ev grew | 
|---|
| 130 | CPPUNIT_ASSERT_EQUAL( addedsize+otheraddedsize, ev->addedsamples.size() ); | 
|---|
| 131 | CPPUNIT_ASSERT_EQUAL( subtractedsize+othersubtractedsize, ev->subtractedsamples.size() ); | 
|---|
| 132 | } | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | /** UnitTest for operator+=() | 
|---|
| 136 | */ | 
|---|
| 137 | void EigenvaluesTest::operatorPlusEqual_Test() | 
|---|
| 138 | { | 
|---|
| 139 | { | 
|---|
| 140 | // create non-overlapping set | 
|---|
| 141 | Eigenvalues::samples_t othersamples; | 
|---|
| 142 | othersamples += 1., 3., 5.; | 
|---|
| 143 | Eigenvalues otherev(othersamples); | 
|---|
| 144 | const size_t otheraddedsize = otherev.addedsamples.size(); | 
|---|
| 145 | const size_t othersubtractedsize = otherev.subtractedsamples.size(); | 
|---|
| 146 | const size_t addedsize = ev->addedsamples.size(); | 
|---|
| 147 | const size_t subtractedsize = ev->subtractedsamples.size(); | 
|---|
| 148 | *ev += otherev; | 
|---|
| 149 | // otherev did not change | 
|---|
| 150 | CPPUNIT_ASSERT_EQUAL( otheraddedsize, otherev.addedsamples.size() ); | 
|---|
| 151 | CPPUNIT_ASSERT_EQUAL( othersubtractedsize, otherev.subtractedsamples.size() ); | 
|---|
| 152 | // ev grew | 
|---|
| 153 | CPPUNIT_ASSERT( addedsize+otheraddedsize != ev->addedsamples.size() ); | 
|---|
| 154 | CPPUNIT_ASSERT( subtractedsize+othersubtractedsize == ev->subtractedsamples.size() ); | 
|---|
| 155 | CPPUNIT_ASSERT_EQUAL( (size_t)6, ev->addedsamples.size() ); | 
|---|
| 156 | CPPUNIT_ASSERT_EQUAL( (size_t)0, ev->subtractedsamples.size() ); | 
|---|
| 157 | } | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | /** UnitTest for operator-=() | 
|---|
| 161 | */ | 
|---|
| 162 | void EigenvaluesTest::operatorMinusEqual_NonOverlapping_Test() | 
|---|
| 163 | { | 
|---|
| 164 | { | 
|---|
| 165 | // create non-overlapping set | 
|---|
| 166 | Eigenvalues::samples_t othersamples; | 
|---|
| 167 | othersamples += 5., 6., 7.; | 
|---|
| 168 | Eigenvalues otherev(othersamples); | 
|---|
| 169 | const size_t otheraddedsize = otherev.addedsamples.size(); | 
|---|
| 170 | const size_t othersubtractedsize = otherev.subtractedsamples.size(); | 
|---|
| 171 | const size_t addedsize = ev->addedsamples.size(); | 
|---|
| 172 | //    const size_t subtractedsize = ev->subtractedsamples.size(); | 
|---|
| 173 | *ev -= otherev; | 
|---|
| 174 | // otherev did not change | 
|---|
| 175 | CPPUNIT_ASSERT_EQUAL( otheraddedsize, otherev.addedsamples.size() ); | 
|---|
| 176 | CPPUNIT_ASSERT_EQUAL( othersubtractedsize, otherev.subtractedsamples.size() ); | 
|---|
| 177 | // ev grew only in subtracted | 
|---|
| 178 | CPPUNIT_ASSERT_EQUAL( addedsize, ev->addedsamples.size() ); | 
|---|
| 179 | CPPUNIT_ASSERT_EQUAL( otheraddedsize, ev->subtractedsamples.size() ); | 
|---|
| 180 | } | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | /** UnitTest for operator-=() | 
|---|
| 184 | */ | 
|---|
| 185 | void EigenvaluesTest::operatorMinusEqual_Test() | 
|---|
| 186 | { | 
|---|
| 187 | { | 
|---|
| 188 | // create non-overlapping set | 
|---|
| 189 | Eigenvalues::samples_t othersamples; | 
|---|
| 190 | othersamples += 1., 3., 5.; | 
|---|
| 191 | Eigenvalues otherev(othersamples); | 
|---|
| 192 | const size_t otheraddedsize = otherev.addedsamples.size(); | 
|---|
| 193 | const size_t othersubtractedsize = otherev.subtractedsamples.size(); | 
|---|
| 194 | const size_t addedsize = ev->addedsamples.size(); | 
|---|
| 195 | //    const size_t subtractedsize = ev->subtractedsamples.size(); | 
|---|
| 196 | *ev -= otherev; | 
|---|
| 197 | // otherev did not change | 
|---|
| 198 | CPPUNIT_ASSERT_EQUAL( otheraddedsize, otherev.addedsamples.size() ); | 
|---|
| 199 | CPPUNIT_ASSERT_EQUAL( othersubtractedsize, otherev.subtractedsamples.size() ); | 
|---|
| 200 | // ev grew only in subtracted | 
|---|
| 201 | CPPUNIT_ASSERT( addedsize == ev->addedsamples.size() ); | 
|---|
| 202 | CPPUNIT_ASSERT( otheraddedsize == ev->subtractedsamples.size() ); | 
|---|
| 203 | } | 
|---|
| 204 | } | 
|---|