MOAB: Mesh Oriented datABase  (version 5.4.1)
UnitUtil.hpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2006 Lawrence Livermore National Laboratory.  Under
00005     the terms of Contract B545069 with the University of Wisconsin --
00006     Madison, Lawrence Livermore National Laboratory retains certain
00007     rights in this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     (2006) [email protected]
00024 
00025   ***************************************************************** */
00026 
00027 /** \file UnitUtil.hpp
00028  *  \brief Utility functions for use in unit tests
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #ifndef MSQ_UNIT_UTIL_HPP
00033 #define MSQ_UNIT_UTIL_HPP
00034 
00035 #include "Mesquite.hpp"
00036 #include "Vector3D.hpp"
00037 #include "Matrix3D.hpp"
00038 #include "MsqMatrix.hpp"
00039 #include "MsqError.hpp"
00040 
00041 #include <string>
00042 #include <cstdio>
00043 
00044 #include "cppunit/extensions/HelperMacros.h"
00045 
00046 #define ASSERT_MESSAGE( MSG, COND ) CPPUNIT_NS::Asserter::failIf( !( COND ), ( MSG ), CPPUNIT_SOURCELINE() )
00047 
00048 /** Assert that MBMesquite API has not flagged an error */
00049 #define ASSERT_NO_ERROR( A ) ASSERT_MESSAGE( ( A ).error_message(), !MSQ_CHKERR( ( A ) ) )
00050 
00051 /**\brief compare two vectors (Vector3D)
00052  *
00053  * Ensure that the test result Vector3D \a v2 is within \a eps of the
00054  * expected vector \a v1 .
00055  */
00056 #define CPPUNIT_ASSERT_VECTORS_EQUAL( v1, v2, eps ) \
00057     ASSERT_MESSAGE( utest_vect_message( ( v1 ), ( v2 ) ), utest_vect_equal( ( v1 ), ( v2 ), ( eps ) ) )
00058 
00059 /**\brief compare two matrices (Matrix3D)
00060  *
00061  * Ensure that the test result Matrix3D \a m2 is within \a eps of the
00062  * expected matrix \a m1
00063  */
00064 #define CPPUNIT_ASSERT_MATRICES_EQUAL( m1, m2, eps ) \
00065     ASSERT_MESSAGE( utest_mat_message( ( m1 ), ( m2 ) ), utest_mat_equal( ( m1 ), ( m2 ), ( eps ) ) )
00066 
00067 /** compare matrix (MsqMatrix) with the identity matrix  */
00068 #define ASSERT_IDENTITY_MATRIX( M ) ASSERT_MESSAGE( ident_check_msg( M ), ident_check( M ) )
00069 
00070 /** compare two matrices (MsqMatrix) */
00071 #define ASSERT_MATRICES_EQUAL( A, B, E ) ASSERT_MESSAGE( mat_equal_check_msg( A, B ), mat_equal_check( A, B, E ) )
00072 
00073 /** compare two matrices (MsqMatrix) */
00074 #define ASSERT_MATRICES_DIFFERENT( A, B, E ) \
00075     ASSERT_MESSAGE( mat_not_equal_check_msg( A, B ), !mat_equal_check( A, B, E ) )
00076 
00077 /** compare two arrays of values */
00078 #define ASSERT_ARRAYS_EQUAL( A, B, LEN )                                      \
00079     CPPUNIT_NS::Asserter::failIf( !( arrays_equal( ( A ), ( B ), ( LEN ) ) ), \
00080                                   arrays_not_equal_msg( ( A ), ( LEN ), ( B ), ( LEN ) ), CPPUNIT_SOURCELINE() )
00081 
00082 #define ASSERT_STD_VECTORS_EQUAL( A, B )                                                                    \
00083     CPPUNIT_NS::Asserter::failIf( ( ( A ) != ( B ) ),                                                       \
00084                                   arrays_not_equal_msg( &( A )[0], ( A ).size(), &( B )[0], ( B ).size() ), \
00085                                   CPPUNIT_SOURCELINE() )
00086 
00087 /** make string representation of cartesian vector */
00088 inline std::string utest_vect_str( const MBMesquite::Vector3D& v )
00089 {
00090     char buffer[128];
00091     sprintf( buffer, "[%f, %f, %f]", v[0], v[1], v[2] );
00092     return buffer;
00093 }
00094 
00095 /** make string representation of 3x3 matrix */
00096 inline std::string utest_mat_str( const MBMesquite::Matrix3D& m )
00097 {
00098     char buffer[256];
00099     sprintf( buffer, "[%f, %f, %f] [%f, %f, %f] [%f, %f, %f]", m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2],
00100              m[2][0], m[2][1], m[2][2] );
00101     return buffer;
00102 }
00103 
00104 /** make string representation of 3x3 symetric matrix */
00105 inline std::string utest_mat_str( const MBMesquite::SymMatrix3D& m )
00106 {
00107     char buffer[256];
00108     sprintf( buffer, "[%f, %f, %f] [%f, %f, %f] [%f, %f, %f]", m( 0, 0 ), m( 0, 1 ), m( 0, 2 ), m( 1, 0 ), m( 1, 1 ),
00109              m( 1, 2 ), m( 2, 0 ), m( 2, 1 ), m( 2, 2 ) );
00110     return buffer;
00111 }
00112 
00113 /** make error message for failed vector copmarison */
00114 inline CppUnit::Message utest_vect_message( const MBMesquite::Vector3D& v1, const MBMesquite::Vector3D& v2 )
00115 {
00116     CppUnit::Message m( "equality assertion failed" );
00117     m.addDetail( std::string( "Expected: " ) + utest_vect_str( v1 ) );
00118     m.addDetail( std::string( "Actual  : " ) + utest_vect_str( v2 ) );
00119     return m;
00120 }
00121 
00122 /** make error message for failed matrix copmarison */
00123 inline CppUnit::Message utest_mat_message( const MBMesquite::Matrix3D& m1, const MBMesquite::Matrix3D& m2 )
00124 {
00125     CppUnit::Message m( "equality assertion failed" );
00126     m.addDetail( std::string( "Expected: " ) + utest_mat_str( m1 ) );
00127     m.addDetail( std::string( "Actual  : " ) + utest_mat_str( m2 ) );
00128     return m;
00129 }
00130 
00131 /** make error message for failed symmetric matrix copmarison */
00132 inline CppUnit::Message utest_mat_message( const MBMesquite::SymMatrix3D& m1, const MBMesquite::SymMatrix3D& m2 )
00133 {
00134     CppUnit::Message m( "equality assertion failed" );
00135     m.addDetail( std::string( "Expected: " ) + utest_mat_str( m1 ) );
00136     m.addDetail( std::string( "Actual  : " ) + utest_mat_str( m2 ) );
00137     return m;
00138 }
00139 
00140 /** compare vectors */
00141 inline bool utest_vect_equal( const MBMesquite::Vector3D& v1, const MBMesquite::Vector3D& v2, double eps )
00142 {
00143     return ( fabs( v1[0] - v2[0] ) < eps ) && ( fabs( v1[1] - v2[1] ) < eps ) && ( fabs( v1[2] - v2[2] ) < eps );
00144 }
00145 
00146 /** compare matrices */
00147 inline bool utest_mat_equal( const MBMesquite::Matrix3D& m1, const MBMesquite::Matrix3D& m2, double eps )
00148 {
00149     return ( fabs( m1[0][0] - m2[0][0] ) < eps ) && ( fabs( m1[0][1] - m2[0][1] ) < eps ) &&
00150            ( fabs( m1[0][2] - m2[0][2] ) < eps ) && ( fabs( m1[1][0] - m2[1][0] ) < eps ) &&
00151            ( fabs( m1[1][1] - m2[1][1] ) < eps ) && ( fabs( m1[1][2] - m2[1][2] ) < eps ) &&
00152            ( fabs( m1[2][0] - m2[2][0] ) < eps ) && ( fabs( m1[2][1] - m2[2][1] ) < eps ) &&
00153            ( fabs( m1[2][2] - m2[2][2] ) < eps );
00154 }
00155 
00156 /** compare matrices */
00157 inline bool utest_mat_equal( const MBMesquite::SymMatrix3D& m1, const MBMesquite::SymMatrix3D& m2, double eps )
00158 {
00159     return ( fabs( m1( 0, 0 ) - m2( 0, 0 ) ) < eps ) && ( fabs( m1( 0, 1 ) - m2( 0, 1 ) ) < eps ) &&
00160            ( fabs( m1( 0, 2 ) - m2( 0, 2 ) ) < eps ) && ( fabs( m1( 1, 1 ) - m2( 1, 1 ) ) < eps ) &&
00161            ( fabs( m1( 1, 2 ) - m2( 1, 2 ) ) < eps ) && ( fabs( m1( 2, 2 ) - m2( 2, 2 ) ) < eps );
00162 }
00163 
00164 template < unsigned R, unsigned C >
00165 inline std::string msq_mat_str( const MBMesquite::MsqMatrix< R, C >& m )
00166 {
00167     std::ostringstream os;
00168     for( unsigned i = 0; i < R; ++i )
00169     {
00170         os << "[" << m( i, 0 );
00171         for( unsigned j = 1; j < C; ++j )
00172             os << ", " << m( i, j );
00173         os << "]";
00174     }
00175     return os.str();
00176 }
00177 
00178 template < unsigned R, unsigned C >
00179 inline CppUnit::Message ident_check_msg( const MBMesquite::MsqMatrix< R, C >& m )
00180 {
00181     CppUnit::Message mes( "Identity Assertion Failed" );
00182     mes.addDetail( std::string( "Actual: " ) + msq_mat_str( m ) );
00183     return mes;
00184 }
00185 
00186 template < unsigned R, unsigned C >
00187 inline bool ident_check( const MBMesquite::MsqMatrix< R, C >& m )
00188 {
00189     for( unsigned i = 0; i < R; ++i )
00190         for( unsigned j = 0; j < C; ++j )
00191             if( i == j && fabs( m( i, j ) - 1.0 ) > 1e-6 )
00192                 return false;
00193             else if( i != j && fabs( m( i, j ) ) > 1e-6 )
00194                 return false;
00195     return true;
00196 }
00197 
00198 template < unsigned R, unsigned C >
00199 inline CppUnit::Message mat_equal_check_msg( const MBMesquite::MsqMatrix< R, C >& A,
00200                                              const MBMesquite::MsqMatrix< R, C >& B )
00201 {
00202     CppUnit::Message mes( "Matrix Equality Assertion Failed" );
00203     mes.addDetail( std::string( "Expected: " ) + msq_mat_str( A ) );
00204     mes.addDetail( std::string( "Actual:   " ) + msq_mat_str( B ) );
00205     return mes;
00206 }
00207 
00208 template < unsigned R, unsigned C >
00209 inline CppUnit::Message mat_not_equal_check_msg( const MBMesquite::MsqMatrix< R, C >& A,
00210                                                  const MBMesquite::MsqMatrix< R, C >& B )
00211 {
00212     CppUnit::Message mes( "Matrix Inequality Assertion Failed" );
00213     mes.addDetail( std::string( "Expected: " ) + msq_mat_str( A ) );
00214     mes.addDetail( std::string( "Actual:   " ) + msq_mat_str( B ) );
00215     return mes;
00216 }
00217 
00218 template < unsigned R, unsigned C >
00219 inline bool mat_equal_check( const MBMesquite::MsqMatrix< R, C >& A,
00220                              const MBMesquite::MsqMatrix< R, C >& B,
00221                              double eps )
00222 {
00223     for( unsigned i = 0; i < R; ++i )
00224         for( unsigned j = 0; j < C; ++j )
00225             if( fabs( A( i, j ) - B( i, j ) ) > eps ) return false;
00226     return true;
00227 }
00228 
00229 template < typename T1, typename T2 >
00230 inline bool arrays_equal( const T1* A, const T2* B, size_t len )
00231 {
00232     for( size_t i = 0; i < len; ++i )
00233         if( A[i] != B[i] ) return false;
00234     return true;
00235 }
00236 
00237 template < typename T1, typename T2 >
00238 inline CppUnit::Message arrays_not_equal_msg( const T1* A, size_t A_len, const T2* B, size_t B_len )
00239 {
00240     CppUnit::Message mes( "Equality Assertion Failed for Arrays" );
00241 
00242     std::ostringstream strA;
00243     if( A_len == 0 )
00244         strA << "(empty)";
00245     else
00246     {
00247         strA << '[' << A[0];
00248         for( size_t i = 1; i < A_len; ++i )
00249             strA << ',' << A[i];
00250         strA << ']';
00251     }
00252     mes.addDetail( std::string( "Expected: " ) + strA.str() );
00253 
00254     std::ostringstream strB;
00255     if( B_len == 0 )
00256         strB << "(empty)";
00257     else
00258     {
00259         strB << '[' << B[0];
00260         for( size_t i = 1; i < B_len; ++i )
00261             strB << ',' << B[i];
00262         strB << ']';
00263     }
00264     mes.addDetail( std::string( "Actual: " ) + strB.str() );
00265 
00266     return mes;
00267 }
00268 
00269 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines