| 1 | /* | 
|---|
| 2 | * linearsystemofequationsunittest.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Jan 8, 2010 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | using namespace std; | 
|---|
| 9 |  | 
|---|
| 10 | #include <iomanip> | 
|---|
| 11 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 12 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 13 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 14 |  | 
|---|
| 15 | #include "linearsystemofequationsunittest.hpp" | 
|---|
| 16 | #include "vector.hpp" | 
|---|
| 17 |  | 
|---|
| 18 | #ifdef HAVE_TESTRUNNER | 
|---|
| 19 | #include "UnitTestMain.hpp" | 
|---|
| 20 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 21 |  | 
|---|
| 22 | /********************************************** Test classes **************************************/ | 
|---|
| 23 |  | 
|---|
| 24 | // Registers the fixture into the 'registry' | 
|---|
| 25 | CPPUNIT_TEST_SUITE_REGISTRATION( LinearSystemOfEquationsTest ); | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 | void LinearSystemOfEquationsTest::setUp() | 
|---|
| 29 | { | 
|---|
| 30 | s = new LinearSystemOfEquations(4,4); | 
|---|
| 31 | }; | 
|---|
| 32 |  | 
|---|
| 33 | void LinearSystemOfEquationsTest::tearDown() | 
|---|
| 34 | { | 
|---|
| 35 | delete(s); | 
|---|
| 36 | }; | 
|---|
| 37 |  | 
|---|
| 38 | /** Unit test for initialization. | 
|---|
| 39 | * | 
|---|
| 40 | */ | 
|---|
| 41 | void LinearSystemOfEquationsTest::InitializationTest() | 
|---|
| 42 | { | 
|---|
| 43 | // Setting A | 
|---|
| 44 | double array[] = { 1., 0., 0., 0., | 
|---|
| 45 | 0., 2., 0., 0., | 
|---|
| 46 | 0., 0., 3., 0., | 
|---|
| 47 | 0., 0., 0., 4. }; | 
|---|
| 48 | s->SetA(array); | 
|---|
| 49 |  | 
|---|
| 50 | // Setting b | 
|---|
| 51 | }; | 
|---|
| 52 |  | 
|---|
| 53 | /** Unit test for symmetry property. | 
|---|
| 54 | * | 
|---|
| 55 | */ | 
|---|
| 56 | void LinearSystemOfEquationsTest::SymmetricTest() | 
|---|
| 57 | { | 
|---|
| 58 | CPPUNIT_ASSERT_EQUAL( true, s->SetSymmetric(true) ); | 
|---|
| 59 | //LinearSystemOfEquations *t = new LinearSystemOfEquations(4,3); | 
|---|
| 60 | //CPPUNIT_ASSERT_EQUAL( true, t->SetSymmetric(true) ); | 
|---|
| 61 | //delete(t); | 
|---|
| 62 | }; | 
|---|
| 63 |  | 
|---|
| 64 | /** Unit test for simple Solve. | 
|---|
| 65 | * | 
|---|
| 66 | */ | 
|---|
| 67 | void LinearSystemOfEquationsTest::SolveSimpleTest() | 
|---|
| 68 | { | 
|---|
| 69 | // set up A and b | 
|---|
| 70 | double m_array[] = { 1., 0., 0., 0., | 
|---|
| 71 | 0., 2., 0., 0., | 
|---|
| 72 | 0., 0., 3., 0., | 
|---|
| 73 | 0., 0., 0., 4. }; | 
|---|
| 74 | double v_array[] = { 1., 2., 3., 4. }; | 
|---|
| 75 | s->SetA(m_array); | 
|---|
| 76 | s->Setb(v_array); | 
|---|
| 77 |  | 
|---|
| 78 | // solve | 
|---|
| 79 | double *array = new double[4]; | 
|---|
| 80 | s->GetSolutionAsArray(array); | 
|---|
| 81 | for (int i=0;i<4;i++) | 
|---|
| 82 | CPPUNIT_ASSERT_EQUAL( 1., array[i]); | 
|---|
| 83 | delete[](array); | 
|---|
| 84 | }; | 
|---|
| 85 |  | 
|---|
| 86 | /** Unit test for advanced Solve. | 
|---|
| 87 | * | 
|---|
| 88 | */ | 
|---|
| 89 | void LinearSystemOfEquationsTest::SolveAdvancedTest() | 
|---|
| 90 | { | 
|---|
| 91 | // set up A and b | 
|---|
| 92 | double m_array[] = { 0.18, 0.60, 0.57, 0.96, | 
|---|
| 93 | 0.41, 0.24, 0.99, 0.58, | 
|---|
| 94 | 0.14, 0.30, 0.97, 0.66, | 
|---|
| 95 | 0.51, 0.13, 0.19, 0.85 }; | 
|---|
| 96 | double v_array[] = { 1.0, 2.0, 3.0, 4.0 }; | 
|---|
| 97 | double x_array[] = { -4.05205022957398, -12.60561139590692, 1.660911626708844, 8.693766928795233 }; | 
|---|
| 98 | s->SetA(m_array); | 
|---|
| 99 | s->Setb(v_array); | 
|---|
| 100 |  | 
|---|
| 101 | // solve | 
|---|
| 102 | double *array = new double[4]; | 
|---|
| 103 | s->GetSolutionAsArray(array); | 
|---|
| 104 | for (int i=0;i<4;i++) { | 
|---|
| 105 | CPPUNIT_ASSERT( fabs(x_array[i] - array[i]) < MYEPSILON ); | 
|---|
| 106 | } | 
|---|
| 107 | delete[](array); | 
|---|
| 108 | }; | 
|---|