Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

base/BaseTest.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)2002 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: BaseTest.cpp 1029 2004-02-11 20:45:54Z jungd $
00019   $Revision: 1.3 $
00020   $Date: 2004-02-11 15:45:54 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <base/BaseTest>
00026 
00027 #include <base/array>
00028 #include <base/Math>
00029 #include <base/Transform>
00030 
00031 
00032 using base::BaseTest;
00033 
00034 using base::array;
00035 using base::Math;
00036 using base::Transform;
00037 using base::inverse;
00038 
00039 
00040 
00041 BaseTest::BaseTest()
00042 {
00043 }
00044 
00045 
00046 void BaseTest::setUp() 
00047 { 
00048 
00049 }
00050   
00051 
00052 void BaseTest::tearDown()
00053 { 
00054 }
00055 
00056 
00057 
00058 void BaseTest::testarray()
00059 {
00060   array<int> a0;          // size 0
00061   CPPUNIT_ASSERT(a0.size() == 0);
00062 
00063   array<int> a1(1);       // size 1
00064   a1[0]=0;
00065   CPPUNIT_ASSERT(a1.size() == 1);
00066   CPPUNIT_ASSERT(a1[0] == 0);
00067 
00068   array<int> a2(2,6);     // size 2, capacity 6
00069   CPPUNIT_ASSERT(a2.size() == 2);
00070   CPPUNIT_ASSERT(a2.capacity() == 6);
00071 
00072   // array assign
00073   a2[0]=a2[1]=-1;
00074   a1 = a2;
00075   CPPUNIT_ASSERT(a1.size() == 2);
00076   CPPUNIT_ASSERT(a1[0] == -1);
00077   CPPUNIT_ASSERT(a1[1] == -1);
00078   
00079   // auto resize
00080   a1.at(3) = -4;
00081   CPPUNIT_ASSERT(a1.size() == 4);
00082   CPPUNIT_ASSERT(a1[3] == -4);
00083 
00084   // swap
00085   a1.swap(a2);
00086   CPPUNIT_ASSERT(a1.size() == 2);
00087   CPPUNIT_ASSERT(a1[0] == -1);
00088   a1[0] = 1;
00089 
00090   // resize
00091   a1.resize(10);
00092   CPPUNIT_ASSERT(a1.size() == 10);
00093   a1[9] = 9;
00094   CPPUNIT_ASSERT(a1[0] == 1);
00095   CPPUNIT_ASSERT(a1[9] == 9);
00096 
00097   // clear
00098   a1.clear();
00099   CPPUNIT_ASSERT(a1.size() == 0);
00100   CPPUNIT_ASSERT(a1.empty());
00101 
00102   // operator==
00103   a1 = a2;
00104   CPPUNIT_ASSERT( a1 == a2 );
00105   a1.at(a1.size()) = -2;
00106   CPPUNIT_ASSERT( a1 != a2 );
00107 
00108   // sucessive resizing
00109   a1 = array<int>(0,2);
00110   for(int i=0; i<32; i++)
00111     a1.push_back(i);
00112 
00113   CPPUNIT_ASSERT( a1.size() == 32 );
00114   for(int i=0; i<32; i++)
00115     CPPUNIT_ASSERT( a1[i] == i );
00116 
00117   // iterators
00118   a2.resize(1);
00119   a2[0] = 5;
00120   CPPUNIT_ASSERT( a2.size() == 1 );
00121   CPPUNIT_ASSERT( (*a2.begin()) == 5 );
00122 
00123   array<int>::const_iterator i = a1.begin();
00124   array<int>::const_iterator e = a1.end();
00125   int c=0;
00126   while (i != e) {
00127     CPPUNIT_ASSERT( (*i) == c );
00128     ++i; ++c;
00129   }
00130 
00131 }
00132 
00133 
00134 
00135 
00136 void BaseTest::testTransform()
00137 {
00138   // check identity
00139   Transform ti;
00140   CPPUNIT_ASSERT( ti.identity() );
00141   CPPUNIT_ASSERT( ti.isTransRotationOnly() );
00142   CPPUNIT_ASSERT( !ti.containsRotation() );
00143   CPPUNIT_ASSERT( !ti.containsTranslation() );
00144   
00145   Quat4 q1( Vector3(1,2,3), consts::Pi/3.0 ); // rot about axis by angle
00146   Transform r1(q1);
00147   
00148   // check construction from Quat4
00149   CPPUNIT_ASSERT( !r1.containsTranslation() );
00150   CPPUNIT_ASSERT( r1.containsRotation() );
00151   CPPUNIT_ASSERT( !r1.identity() );
00152   CPPUNIT_ASSERT( r1.isTransRotationOnly() );
00153   
00154   Quat4 q2(Vector3(1,2,1), consts::Pi/4.0);
00155   Transform r2(q2);
00156   
00157   // multiply and check against raw matrix multiply
00158   Transform r3( r1*r2 );
00159 
00160   Matrix3 m1 = Orient(q1);
00161   Matrix3 m2 = Orient(q2);
00162   
00163   Matrix3 m3( m1*m2 );
00164   
00165   CPPUNIT_ASSERT( Matrix4(m3,Point3()).equals( r3.getTransform() ) );
00166   CPPUNIT_ASSERT( Orient(m3).equals( r3.getRotation() ) );
00167   
00168   Matrix3 m3i( inverse(m3) );
00169   Transform r3i( inverse(r3) );
00170   
00171   CPPUNIT_ASSERT( Matrix4(m3i,Point3()).equals( r3i.getTransform() ) );
00172   CPPUNIT_ASSERT( Orient(m3i).equals( r3i.getRotation() ) );
00173   
00174   Transform ti2( r3*r3i );
00175   CPPUNIT_ASSERT( ti2.equals(ti) );
00176   
00177   
00178   // now do some checks that involve translation as well as rotation
00179   Transform t1(Vector3(3,2,4), q1);
00180   CPPUNIT_ASSERT( t1.containsTranslation() );
00181   CPPUNIT_ASSERT( t1.containsRotation() );
00182   CPPUNIT_ASSERT( !t1.identity() );
00183   CPPUNIT_ASSERT( t1.isTransRotationOnly() );
00184 
00185   Transform t1i( inverse(t1) );  
00186   Transform ti3( t1*t1i );
00187   CPPUNIT_ASSERT( ti3.equals(ti) );
00188 
00189   // check that converting the rotation component from mat->orient and back is invariant  
00190   Transform t2(Vector3(2,2,3), q2);
00191   Transform t2i( inverse(t2) ); // inverse always forces conversion to mat somewhere internally
00192   Transform t2ib( t2i );
00193   t2ib.setRotationComponent( t2ib.getRotation() );
00194   CPPUNIT_ASSERT( t2i.equals(t2ib) );
00195   
00196 }
00197 
00198 
00199 
00200 #ifdef DEBUG
00201 CPPUNIT_TEST_SUITE_REGISTRATION( BaseTest );
00202 #endif
00203 

Generated on Thu Jul 29 15:56:04 2004 for OpenSim by doxygen 1.3.6