1 | /*
|
---|
2 | * ActOnAllUnitTest.cpp
|
---|
3 | *
|
---|
4 | * Created on: 04.10.2009
|
---|
5 | * Author: FrederikHeber
|
---|
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 "ActOnAlltest.hpp"
|
---|
15 | #include "ActOnAllUnitTest.hpp"
|
---|
16 |
|
---|
17 | /********************************************** Test classes **************************************/
|
---|
18 |
|
---|
19 | // Registers the fixture into the 'registry'
|
---|
20 | CPPUNIT_TEST_SUITE_REGISTRATION( ActOnAllTest );
|
---|
21 |
|
---|
22 |
|
---|
23 | void ActOnAllTest::setUp()
|
---|
24 | {
|
---|
25 | Vector *temp = NULL;
|
---|
26 | // fill list with vectors
|
---|
27 | temp = new Vector(1,0,0);
|
---|
28 | VL.AddVector(temp);
|
---|
29 | temp = new Vector(0,1,0);
|
---|
30 | VL.AddVector(temp);
|
---|
31 | temp = new Vector(0,0,1);
|
---|
32 | VL.AddVector(temp);
|
---|
33 | Ref = VL;
|
---|
34 | };
|
---|
35 |
|
---|
36 |
|
---|
37 | void ActOnAllTest::tearDown()
|
---|
38 | {
|
---|
39 | VL.EmptyList();
|
---|
40 | };
|
---|
41 |
|
---|
42 | /** UnitTest for VectorList::ActOnAll() and Vector::AddVector(), Vector::SubtractVector(),
|
---|
43 | */
|
---|
44 | void ActOnAllTest::AddSubtractTest()
|
---|
45 | {
|
---|
46 | const Vector test(1.,0.,0.);
|
---|
47 |
|
---|
48 | // adding, subtracting
|
---|
49 | VL.ActOnAll( &Vector::AddVector, &test );
|
---|
50 | CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
|
---|
51 | VL.ActOnAll( &Vector::SubtractVector, &test );
|
---|
52 | CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
|
---|
53 | };
|
---|
54 |
|
---|
55 | /** UnitTest for VectorList::ActOnAll()
|
---|
56 | */
|
---|
57 | void ActOnAllTest::ScaleTest()
|
---|
58 | {
|
---|
59 | double factor=2.;
|
---|
60 | double inverse=1./2.;
|
---|
61 |
|
---|
62 | // scaling by value
|
---|
63 | VL.ActOnAll( (void (Vector::*)(double)) &Vector::Scale, 2. );
|
---|
64 | CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
|
---|
65 |
|
---|
66 | VL.ActOnAll( (void (Vector::*)(double)) &Vector::Scale, 0.5 );
|
---|
67 | CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
|
---|
68 |
|
---|
69 | // scaling by ref
|
---|
70 | VL.ActOnAll( (void (Vector::*)(double *)) &Vector::Scale, &factor );
|
---|
71 | CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
|
---|
72 |
|
---|
73 | VL.ActOnAll( (void (Vector::*)(double *)) &Vector::Scale, &inverse );
|
---|
74 | CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
|
---|
75 |
|
---|
76 | // scaling by three values
|
---|
77 | double *factors = Malloc<double>(NDIM, "ActOnAllTest::ScaleTest - factors");
|
---|
78 | double *inverses = Malloc<double>(NDIM, "ActOnAllTest::ScaleTest - inverses");
|
---|
79 | for (int i=0;i<NDIM;++i) {
|
---|
80 | factors[i] = 2.;
|
---|
81 | inverses[i] = 1./factors[i];
|
---|
82 | }
|
---|
83 | VL.ActOnAll( (void (Vector::*)(double **)) &Vector::Scale, &factors );
|
---|
84 | CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
|
---|
85 |
|
---|
86 | VL.ActOnAll( (void (Vector::*)(double **)) &Vector::Scale, &inverses );
|
---|
87 | CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
|
---|
88 | };
|
---|
89 |
|
---|
90 | /** UnitTest for VectorList::ActOnAll() and Vector::MakeNormalVector()
|
---|
91 | */
|
---|
92 | void ActOnAllTest::NormalizeTest()
|
---|
93 | {
|
---|
94 | const Vector xaxis(1.,0.,0.);
|
---|
95 | const Vector yaxis(0.,1.,0.);
|
---|
96 |
|
---|
97 | // normalize with respect to x and y axis
|
---|
98 | bool (Vector::*f)(const Vector *, const Vector *) = &Vector::MakeNormalVector;
|
---|
99 | VL.ActOnAll( f, &xaxis, &yaxis );
|
---|
100 | CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
|
---|
101 |
|
---|
102 | // check that x and y components are zero
|
---|
103 | for (ListOfVectors::iterator Runner = VL.Vectors.begin(); Runner != VL.Vectors.end(); Runner++) {
|
---|
104 | CPPUNIT_ASSERT_EQUAL( (*Runner)->x[0] , 0. );
|
---|
105 | CPPUNIT_ASSERT_EQUAL( (*Runner)->x[1] , 0. );
|
---|
106 | }
|
---|
107 | };
|
---|
108 |
|
---|
109 |
|
---|
110 | /********************************************** Main routine **************************************/
|
---|
111 |
|
---|
112 | int main(int argc, char **argv)
|
---|
113 | {
|
---|
114 | // Get the top level suite from the registry
|
---|
115 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
116 |
|
---|
117 | // Adds the test to the list of test to run
|
---|
118 | CppUnit::TextUi::TestRunner runner;
|
---|
119 | runner.addTest( suite );
|
---|
120 |
|
---|
121 | // Change the default outputter to a compiler error format outputter
|
---|
122 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
123 | std::cerr ) );
|
---|
124 | // Run the tests.
|
---|
125 | bool wasSucessful = runner.run();
|
---|
126 |
|
---|
127 | // Return error code 1 if the one of test failed.
|
---|
128 | return wasSucessful ? 0 : 1;
|
---|
129 | };
|
---|