1 | /*
|
---|
2 | * gslvectorunittest.cpp
|
---|
3 | *
|
---|
4 | * Created on: Jan 8, 2010
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | #include <cppunit/CompilerOutputter.h>
|
---|
11 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
12 | #include <cppunit/ui/text/TestRunner.h>
|
---|
13 |
|
---|
14 | #include "gslvectorunittest.hpp"
|
---|
15 |
|
---|
16 | /********************************************** Test classes **************************************/
|
---|
17 |
|
---|
18 | // Registers the fixture into the 'registry'
|
---|
19 | CPPUNIT_TEST_SUITE_REGISTRATION( GSLVectorTest );
|
---|
20 |
|
---|
21 |
|
---|
22 | void GSLVectorTest::setUp()
|
---|
23 | {
|
---|
24 | v = new GSLVector(3);
|
---|
25 | };
|
---|
26 |
|
---|
27 | void GSLVectorTest::tearDown()
|
---|
28 | {
|
---|
29 | delete(v);
|
---|
30 | };
|
---|
31 |
|
---|
32 | /** Unit test for accessing elements in the vector.
|
---|
33 | *
|
---|
34 | */
|
---|
35 | void GSLVectorTest::AccessTest()
|
---|
36 | {
|
---|
37 | // set
|
---|
38 | for (int i=0;i<3;i++)
|
---|
39 | v->Set(i,i);
|
---|
40 |
|
---|
41 | // and check
|
---|
42 | double *ptr = NULL;
|
---|
43 | for (int i=0;i<3;i++) {
|
---|
44 | CPPUNIT_ASSERT_EQUAL( (double)i, v->Get(i) );
|
---|
45 | ptr = v->Pointer(i);
|
---|
46 | CPPUNIT_ASSERT_EQUAL( (double)i, *ptr );
|
---|
47 | }
|
---|
48 |
|
---|
49 | // out of bounds
|
---|
50 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(3) );
|
---|
51 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1) );
|
---|
52 | };
|
---|
53 |
|
---|
54 |
|
---|
55 | /** Unit test for initializing the vector.
|
---|
56 | *
|
---|
57 | */
|
---|
58 | void GSLVectorTest::InitializaionTest()
|
---|
59 | {
|
---|
60 | // set zero
|
---|
61 | v->SetZero();
|
---|
62 | for (int i=0;i<3;i++)
|
---|
63 | CPPUNIT_ASSERT_EQUAL( 0., v->Get(i) );
|
---|
64 |
|
---|
65 | // set basis
|
---|
66 | for (int i=0;i<3;i++) {
|
---|
67 | v->SetBasis(i);
|
---|
68 | for (int j=0;j<3;j++)
|
---|
69 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , v->Get(j) );
|
---|
70 | }
|
---|
71 |
|
---|
72 | // set all
|
---|
73 | v->SetAll(1.5);
|
---|
74 | for (int i=0;i<3;i++)
|
---|
75 | CPPUNIT_ASSERT_EQUAL( 1.5, v->Get(i) );
|
---|
76 | };
|
---|
77 |
|
---|
78 | /** Unit test for copying vectors.
|
---|
79 | *
|
---|
80 | */
|
---|
81 | void GSLVectorTest::CopyTest()
|
---|
82 | {
|
---|
83 | // set basis
|
---|
84 | GSLVector *dest = NULL;
|
---|
85 | for (int i=0;i<3;i++) {
|
---|
86 | v->SetBasis(i);
|
---|
87 | dest = new GSLVector(v);
|
---|
88 | for (int j=0;j<3;j++)
|
---|
89 | CPPUNIT_ASSERT_EQUAL( v->Get(j) , dest->Get(j) );
|
---|
90 |
|
---|
91 | delete(dest);
|
---|
92 | }
|
---|
93 | };
|
---|
94 |
|
---|
95 | /** Unit test for exchanging elements of a vector.
|
---|
96 | *
|
---|
97 | */
|
---|
98 | void GSLVectorTest::ExchangeTest()
|
---|
99 | {
|
---|
100 | // set basis
|
---|
101 | for (int i=0;i<3;i++) {
|
---|
102 | v->SetBasis(i);
|
---|
103 | v->SwapElements((i)%3,(i+1)%3);
|
---|
104 | for (int j=0;j<3;j++)
|
---|
105 | CPPUNIT_ASSERT_EQUAL( (i+1)%3 == j ? 1. : 0. , v->Get(j) );
|
---|
106 | }
|
---|
107 |
|
---|
108 | // set to 1,2,3 and reverse
|
---|
109 | for (int i=0;i<3;i++)
|
---|
110 | v->Set(i,i+1);
|
---|
111 | v->Reverse();
|
---|
112 | for (int j=0;j<3;j++)
|
---|
113 | CPPUNIT_ASSERT_EQUAL( (double)(3-j), v->Get(j) );
|
---|
114 | };
|
---|
115 |
|
---|
116 |
|
---|
117 | /********************************************** Main routine **************************************/
|
---|
118 |
|
---|
119 | int main(int argc, char **argv)
|
---|
120 | {
|
---|
121 | // Get the top level suite from the registry
|
---|
122 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
123 |
|
---|
124 | // Adds the test to the list of test to run
|
---|
125 | CppUnit::TextUi::TestRunner runner;
|
---|
126 | runner.addTest( suite );
|
---|
127 |
|
---|
128 | // Change the default outputter to a compiler error format outputter
|
---|
129 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
130 | std::cerr ) );
|
---|
131 | // Run the tests.
|
---|
132 | bool wasSucessful = runner.run();
|
---|
133 |
|
---|
134 | // Return error code 1 if the one of test failed.
|
---|
135 | return wasSucessful ? 0 : 1;
|
---|
136 | };
|
---|