Changeset 4bc937 for molecuilder


Ignore:
Timestamp:
Oct 5, 2009, 8:51:24 PM (16 years ago)
Author:
Frederik Heber <heber@…>
Children:
d40b96
Parents:
424d1ce
Message:

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.
Location:
molecuilder/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/ActOnAllUnitTest.cpp

    r424d1ce r4bc937  
    3131  temp = new Vector(0,0,1);
    3232  VL.AddVector(temp);
     33  Ref = VL;
    3334};
    3435
     
    3940};
    4041
     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
    4155/** UnitTest for VectorList::ActOnAllVectors()
    4256 */
    43 void ActOnAllTest::ListTest()
     57void ActOnAllTest::ScaleTest()
    4458{
    45   CPPUNIT_ASSERT_EQUAL( true, true );
     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 );
    4688};
     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
    47109
    48110/********************************************** Main routine **************************************/
  • molecuilder/src/ActOnAllUnitTest.hpp

    r424d1ce r4bc937  
    1818{
    1919    CPPUNIT_TEST_SUITE( ActOnAllTest) ;
    20     CPPUNIT_TEST ( ListTest );
     20    CPPUNIT_TEST ( AddSubtractTest );
     21    CPPUNIT_TEST ( ScaleTest );
     22    CPPUNIT_TEST ( NormalizeTest );
    2123    CPPUNIT_TEST_SUITE_END();
    2224
     
    2426      void setUp();
    2527      void tearDown();
    26       void ListTest();
    27 
     28      void AddSubtractTest();
     29      void ScaleTest();
     30      void NormalizeTest();
    2831
    2932private:
    3033      class VectorList VL;
     34      class VectorList Ref;
    3135};
    3236
  • molecuilder/src/ActOnAlltest.hpp

    r424d1ce r4bc937  
    2020public:
    2121  VectorList();
     22  VectorList(VectorList &ref);
    2223  ~VectorList();
    2324
     
    2526  void EmptyList();
    2627
    27   template <T> void ActOnAllVectors( void (*f)(T) )
     28  template <typename res, typename T> void ActOnAllVectors( res (Vector::*f)(T), T t );
     29  template <typename res, typename T, typename U> void ActOnAllVectors( res (Vector::*f)(T, U), T t, U u );
     30  template <typename res, typename T, typename U, typename V> void ActOnAllVectors( res (Vector::*f)(T, U, V), T t, U u, V v);
    2831
    29 private:
     32  bool operator == (VectorList &rhs);
     33  VectorList& operator = (VectorList &ref);
     34
     35//private:
    3036  ListOfVectors Vectors;
    3137};
    3238
    33 template <T> void VectorList::ActOnAllVectors( void (*f)(T) )
     39
     40VectorList::VectorList() { };
     41VectorList::VectorList(VectorList &ref)
    3442{
    35   for (ListOfVectors::iterator Runner = Vectors.begin(); Runner != Vectors.end(); Runner++)
    36     f(T);
     43  ref.EmptyList();
     44  for (ListOfVectors::iterator Runner = Vectors.begin(); Runner != Vectors.end(); Runner++) {
     45    ref.Vectors.push_back(new Vector (**Runner));
     46  }
     47};
     48
     49VectorList & VectorList::operator = (VectorList &ref)
     50{
     51  EmptyList();
     52  for (ListOfVectors::iterator Runner = ref.Vectors.begin(); Runner != ref.Vectors.end(); Runner++) {
     53    Vectors.push_back(new Vector (**Runner));
     54  }
     55  return *this;
     56};
     57
     58bool VectorList::operator == (VectorList &rhs)
     59{
     60  bool status = true;
     61  ListOfVectors::iterator lRunner = Vectors.begin();
     62  ListOfVectors::iterator rRunner = rhs.Vectors.begin();
     63  for (;(lRunner != Vectors.end()) && (rRunner != rhs.Vectors.end()); lRunner++, rRunner++) {
     64    //cout << **lRunner << " == " << **rRunner << endl;
     65    status = status && (**lRunner == **rRunner);
     66  }
     67  return status;
     68};
     69
     70VectorList::~VectorList() { };
     71
     72template <typename res, typename T> void VectorList::ActOnAllVectors( res (Vector::*f)(T), T t )
     73{
     74  Vector *test = NULL;
     75  for (ListOfVectors::iterator Runner = Vectors.begin(); Runner != Vectors.end(); Runner++) {
     76    test = *Runner;
     77    (test->*f)(t);
     78  }
     79};
     80
     81template <typename res, typename T, typename U> void VectorList::ActOnAllVectors( res (Vector::*f)(T, U), T t, U u )
     82{
     83  Vector *test = NULL;
     84  for (ListOfVectors::iterator Runner = Vectors.begin(); Runner != Vectors.end(); Runner++) {
     85    test = *Runner;
     86    (test->*f)(t, u);
     87  }
     88};
     89
     90template <typename res, typename T, typename U, typename V> void VectorList::ActOnAllVectors( res (Vector::*f)(T, U, V), T t, U u, V v)
     91{
     92  Vector *test = NULL;
     93  for (ListOfVectors::iterator Runner = Vectors.begin(); Runner != Vectors.end(); Runner++) {
     94    test = *Runner;
     95    (test->*f)(t, u, v);
     96  }
    3797};
    3898
  • molecuilder/src/Makefile.am

    r424d1ce r4bc937  
    1616TESTS = VectorUnitTest MemoryAllocatorUnitTest MemoryUsageObserverUnitTest TesselationUnitTest
    1717check_PROGRAMS = $(TESTS)
     18
    1819VectorUnitTest_SOURCES = defs.hpp helpers.cpp helpers.hpp leastsquaremin.cpp leastsquaremin.hpp memoryallocator.hpp memoryusageobserver.cpp memoryusageobserver.hpp vectorunittest.cpp vectorunittest.hpp vector.cpp vector.hpp verbose.cpp verbose.hpp
    1920VectorUnitTest_CXXFLAGS = $(CPPUNIT_CFLAGS)
    2021VectorUnitTest_LDFLAGS = $(CPPUNIT_LIBS) -ldl
     22
    2123TesselationUnitTest_SOURCES = defs.hpp helpers.cpp helpers.hpp leastsquaremin.cpp leastsquaremin.hpp linkedcell.cpp linkedcell.hpp memoryallocator.hpp memoryusageobserver.cpp memoryusageobserver.hpp tesselation.cpp tesselation.hpp tesselationhelpers.cpp tesselationhelpers.hpp tesselationunittest.cpp tesselationunittest.hpp vector.cpp vector.hpp verbose.cpp verbose.hpp
    2224TesselationUnitTest_CXXFLAGS = $(CPPUNIT_CFLAGS)
    2325TesselationUnitTest_LDFLAGS = $(CPPUNIT_LIBS) -ldl
    24 ActOnAllTest_SOURCES = ActOnAllTest.hp ActOnAllUnitTest.cpp ActOnAllUnitTest.hpp vector.cpp vector.hpp
     26
     27ActOnAllTest_SOURCES = ActOnAllTest.hp ActOnAllUnitTest.cpp ActOnAllUnitTest.hpp defs.hpp helpers.cpp helpers.hpp memoryallocator.hpp memoryusageobserver.cpp memoryusageobserver.hpp vector.cpp vector.hpp verbose.cpp verbose.hpp leastsquaremin.cpp leastsquaremin.hpp
    2528ActOnAllTest_CXXFLAGS = $(CPPUNIT_CFLAGS)
    2629ActOnAllTest_LDFLAGS = $(CPPUNIT_LIBS) -ldl
  • molecuilder/src/molecules.hpp

    r424d1ce r4bc937  
    132132  bool IsEnd();
    133133
     134  // templates for allowing global manipulation of all vectors
     135  template <typename res, typename T> void ActOnAllVectors( res (Vector::*f)(T), T t );
     136  template <typename res, typename T, typename U> void ActOnAllVectors( res (Vector::*f)(T, U), T t, U u );
     137  template <typename res, typename T, typename U, typename V> void ActOnAllVectors( res (Vector::*f)(T, U, V), T t, U u, V v);
     138
    134139  /// remove atoms from molecule.
    135140  bool AddAtom(atom *pointer);
     
    249254};
    250255
     256
     257template <typename res, typename T> void molecule::ActOnAllVectors( res (Vector::*f)(T), T t )
     258{
     259  atom *Walker = start;
     260  while (Walker->next != end) {
     261    Walker = Walker->next;
     262    ((*Walker->node)->*f)(t);
     263  }
     264};
     265
     266template <typename res, typename T, typename U> void molecule::ActOnAllVectors( res (Vector::*f)(T, U), T t, U u )
     267{
     268  atom *Walker = start;
     269  while (Walker->next != end) {
     270    Walker = Walker->next;
     271    ((*Walker->node)->*f)(t, u);
     272  }
     273};
     274
     275template <typename res, typename T, typename U, typename V> void molecule::ActOnAllVectors( res (Vector::*f)(T, U, V), T t, U u, V v)
     276{
     277  atom *Walker = start;
     278  while (Walker->next != end) {
     279    Walker = Walker->next;
     280    ((*Walker->node)->*f)(t, u, v);
     281  }
     282};
     283
    251284/** A list of \a molecule classes.
    252285 */
Note: See TracChangeset for help on using the changeset viewer.