source: molecuilder/src/ActOnAllUnitTest.cpp@ 4bc937

Last change on this file since 4bc937 was 4bc937, checked in by Frederik Heber <heber@…>, 16 years ago

Member functions of Vector class may be called while going over an iteration of a list containing Vectors, thanks to member function pointers.

  • Unit test ActOnAllTest is the basic class for showing, how we can use any member function of Vector:: and make it act globally over a list of vectors.
  • this is incorporated into the definition of class molecule for the vectors contained in the atoms chained list.
  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 * ActOnAllUnitTest.cpp
3 *
4 * Created on: 04.10.2009
5 * Author: FrederikHeber
6 */
7
8using 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'
20CPPUNIT_TEST_SUITE_REGISTRATION( ActOnAllTest );
21
22
23void 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
37void ActOnAllTest::tearDown()
38{
39 VL.EmptyList();
40};
41
42/** UnitTest for VectorList::ActOnAllVectors() and Vector::AddVector(), Vector::SubtractVector(),
43 */
44void ActOnAllTest::AddSubtractTest()
45{
46 const Vector test(1.,0.,0.);
47
48 // adding, subtracting
49 VL.ActOnAllVectors( &Vector::AddVector, &test );
50 CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
51 VL.ActOnAllVectors( &Vector::SubtractVector, &test );
52 CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
53};
54
55/** UnitTest for VectorList::ActOnAllVectors()
56 */
57void ActOnAllTest::ScaleTest()
58{
59 double factor=2.;
60 double inverse=1./2.;
61
62 // scaling by value
63 VL.ActOnAllVectors( (void (Vector::*)(double)) &Vector::Scale, 2. );
64 CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
65
66 VL.ActOnAllVectors( (void (Vector::*)(double)) &Vector::Scale, 0.5 );
67 CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
68
69 // scaling by ref
70 VL.ActOnAllVectors( (void (Vector::*)(double *)) &Vector::Scale, &factor );
71 CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
72
73 VL.ActOnAllVectors( (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.ActOnAllVectors( (void (Vector::*)(double **)) &Vector::Scale, &factors );
84 CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
85
86 VL.ActOnAllVectors( (void (Vector::*)(double **)) &Vector::Scale, &inverses );
87 CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
88};
89
90/** UnitTest for VectorList::ActOnAllVectors() and Vector::MakeNormalVector()
91 */
92void 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.ActOnAllVectors( 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
112int 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};
Note: See TracBrowser for help on using the repository browser.