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