| 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 |  * LinearSystemOfEquationsunittest.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Jan 8, 2010
 | 
|---|
| 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 <limits>
 | 
|---|
| 38 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 39 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 40 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 41 | #include <cmath>
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "defs.hpp"
 | 
|---|
| 44 | #include "Vector.hpp"
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include "LinearSystemOfEquationsUnitTest.hpp"
 | 
|---|
| 47 | 
 | 
|---|
| 48 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 49 | #include "UnitTestMain.hpp"
 | 
|---|
| 50 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 51 | 
 | 
|---|
| 52 | /********************************************** Test classes **************************************/
 | 
|---|
| 53 | 
 | 
|---|
| 54 | // Registers the fixture into the 'registry'
 | 
|---|
| 55 | CPPUNIT_TEST_SUITE_REGISTRATION( LinearSystemOfEquationsTest );
 | 
|---|
| 56 | 
 | 
|---|
| 57 | 
 | 
|---|
| 58 | void LinearSystemOfEquationsTest::setUp()
 | 
|---|
| 59 | {
 | 
|---|
| 60 |   s = new LinearSystemOfEquations(4,4);
 | 
|---|
| 61 | };
 | 
|---|
| 62 | 
 | 
|---|
| 63 | void LinearSystemOfEquationsTest::tearDown()
 | 
|---|
| 64 | {
 | 
|---|
| 65 |   delete(s);
 | 
|---|
| 66 | };
 | 
|---|
| 67 | 
 | 
|---|
| 68 | /** Unit test for initialization.
 | 
|---|
| 69 |  *
 | 
|---|
| 70 |  */
 | 
|---|
| 71 | void LinearSystemOfEquationsTest::InitializationTest()
 | 
|---|
| 72 | {
 | 
|---|
| 73 |   // Setting A
 | 
|---|
| 74 |   double array[] = { 1., 0., 0., 0.,
 | 
|---|
| 75 |                      0., 2., 0., 0.,
 | 
|---|
| 76 |                      0., 0., 3., 0.,
 | 
|---|
| 77 |                      0., 0., 0., 4. };
 | 
|---|
| 78 |   s->SetA(array);
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   // Setting b
 | 
|---|
| 81 | };
 | 
|---|
| 82 | 
 | 
|---|
| 83 | /** Unit test for symmetry property.
 | 
|---|
| 84 |  *
 | 
|---|
| 85 |  */
 | 
|---|
| 86 | void LinearSystemOfEquationsTest::SymmetricTest()
 | 
|---|
| 87 | {
 | 
|---|
| 88 |   CPPUNIT_ASSERT_EQUAL( true, s->SetSymmetric(true) );
 | 
|---|
| 89 |   //LinearSystemOfEquations *t = new LinearSystemOfEquations(4,3);
 | 
|---|
| 90 |   //CPPUNIT_ASSERT_EQUAL( true, t->SetSymmetric(true) );
 | 
|---|
| 91 |   //delete(t);
 | 
|---|
| 92 | };
 | 
|---|
| 93 | 
 | 
|---|
| 94 | /** Unit test for simple Solve.
 | 
|---|
| 95 |  *
 | 
|---|
| 96 |  */
 | 
|---|
| 97 | void LinearSystemOfEquationsTest::SolveSimpleTest()
 | 
|---|
| 98 | {
 | 
|---|
| 99 |   // set up A and b
 | 
|---|
| 100 |   double m_array[] = { 1., 0., 0., 0.,
 | 
|---|
| 101 |                        0., 2., 0., 0.,
 | 
|---|
| 102 |                        0., 0., 3., 0.,
 | 
|---|
| 103 |                        0., 0., 0., 4. };
 | 
|---|
| 104 |   double v_array[] = { 1., 2., 3., 4. };
 | 
|---|
| 105 |   s->SetA(m_array);
 | 
|---|
| 106 |   s->Setb(v_array);
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   // solve
 | 
|---|
| 109 |   double *array = new double[4];
 | 
|---|
| 110 |   s->GetSolutionAsArray(array);
 | 
|---|
| 111 |   for (int i=0;i<4;i++)
 | 
|---|
| 112 |     CPPUNIT_ASSERT_EQUAL( 1., array[i]);
 | 
|---|
| 113 |   delete[](array);
 | 
|---|
| 114 | };
 | 
|---|
| 115 | 
 | 
|---|
| 116 | /** Unit test for advanced Solve.
 | 
|---|
| 117 |  *
 | 
|---|
| 118 |  */
 | 
|---|
| 119 | void LinearSystemOfEquationsTest::SolveAdvancedTest()
 | 
|---|
| 120 | {
 | 
|---|
| 121 |   // set up A and b
 | 
|---|
| 122 |   double m_array[] = { 0.18, 0.60, 0.57, 0.96,
 | 
|---|
| 123 |                        0.41, 0.24, 0.99, 0.58,
 | 
|---|
| 124 |                        0.14, 0.30, 0.97, 0.66,
 | 
|---|
| 125 |                        0.51, 0.13, 0.19, 0.85 };
 | 
|---|
| 126 |   double v_array[] = { 1.0, 2.0, 3.0, 4.0 };
 | 
|---|
| 127 |   double x_array[] = { -4.05205022957398, -12.60561139590692, 1.660911626708844, 8.693766928795233 };
 | 
|---|
| 128 |   s->SetA(m_array);
 | 
|---|
| 129 |   s->Setb(v_array);
 | 
|---|
| 130 | 
 | 
|---|
| 131 |   // solve
 | 
|---|
| 132 |   double *array = new double[4];
 | 
|---|
| 133 |   s->GetSolutionAsArray(array);
 | 
|---|
| 134 |   for (int i=0;i<4;i++) {
 | 
|---|
| 135 |     CPPUNIT_ASSERT( fabs(x_array[i] - array[i]) <= LINALG_MYEPSILON() );
 | 
|---|
| 136 |   }
 | 
|---|
| 137 |   delete[](array);
 | 
|---|
| 138 | };
 | 
|---|