1 #ifndef SimTK_SimTKCOMMON_TESTING_H_
2 #define SimTK_SimTKCOMMON_TESTING_H_
167 std::clog <<
"Starting test " << testName <<
" ...\n";
172 std::ostringstream fmt;
173 fmt << std::fixed << std::setprecision(1);
174 fmt <<
"\n" << testName <<
" done."
175 <<
" real/CPU ms: " << (finalRealTime-startRealTime)*1000
176 <<
" / " << (finalCpuTime-startCpuTime)*1000 <<std::endl;
177 std::clog << fmt.str();
184 template <
class T1,
class T2>
194 return std::abs(v1-v2) < scale*(float)tol;
198 return std::abs(v1-v2) < scale*(double)tol;
200 static bool numericallyEqual(
long double v1,
long double v2,
int n,
double tol=defTol<long double>()) {
202 return std::abs(v1-v2) < scale*(
long double)tol;
207 static bool numericallyEqual(
unsigned u1,
unsigned u2,
int n,
double tol=0) {
return u1==u2;}
218 static bool numericallyEqual(
double v1,
long double v2,
int n,
double tol=defTol<double>())
220 static bool numericallyEqual(
long double v1,
double v2,
int n,
double tol=defTol<double>())
240 static bool numericallyEqual(
int i1,
long double f2,
int n,
double tol=defTol<long double>())
242 static bool numericallyEqual(
long double f1,
int i2,
int n,
double tol=defTol<long double>())
244 static bool numericallyEqual(
unsigned i1,
long double f2,
int n,
double tol=defTol<long double>())
246 static bool numericallyEqual(
long double f1,
unsigned i2,
int n,
double tol=defTol<long double>())
250 static bool numericallyEqual(
const std::complex<P>& v1,
const std::complex<P>& v2,
int n,
double tol=defTol<P>()) {
297 template <
int M,
class E1,
int S1,
class E2,
int S2>
299 for (
int i=0; i<M; ++i)
if (!
numericallyEqual(v1[i],v2[i], n, tol))
return false;
302 template <
int N,
class E1,
int S1,
class E2,
int S2>
304 for (
int j=0; j<N; ++j)
if (!
numericallyEqual(v1[j],v2[j], n, tol))
return false;
307 template <
int M,
int N,
class E1,
int CS1,
int RS1,
class E2,
int CS2,
int RS2>
308 static bool numericallyEqual(
const Mat<M,N,E1,CS1,RS1>& v1,
const Mat<M,N,E2,CS2,RS2>& v2,
int n,
double tol=(defTol2<E1,E2>())) {
309 for (
int j=0; j<N; ++j)
if (!
numericallyEqual(v1(j),v2(j), n, tol))
return false;
312 template <
int N,
class E1,
int S1,
class E2,
int S2>
316 template <
class E1,
class E2>
318 if (v1.
size() != v2.
size())
return false;
319 for (
int i=0; i < v1.
size(); ++i)
323 template <
class E1,
class E2>
326 template <
class E1,
class E2>
329 template <
class E1,
class E2>
333 template <
class E1,
class E2>
335 if (v1.
size() != v2.
size())
return false;
336 for (
int i=0; i < v1.
size(); ++i)
340 template <
class E1,
class E2>
343 template <
class E1,
class E2>
346 template <
class E1,
class E2>
350 template <
class E1,
class E2>
353 for (
int j=0; j < v1.
ncol(); ++j)
357 template <
class E1,
class E2>
360 template <
class E1,
class E2>
363 template <
class E1,
class E2>
402 {
Mat<M,N> m;
for (
int j=0; j<N; ++j) m(j)=randVec<M>();
return m;}
407 {
Vector v(m);
for (
int i=0; i<m; ++i) v[i]=
randReal();
return v;}
429 const double startCpuTime;
430 const double startRealTime;
431 std::string testName;
442 std::clog <<
" " << subtestName <<
" ...\n" << std::flush;
447 std::ostringstream fmt;
448 fmt << std::fixed << std::setprecision(1);
449 fmt <<
" " << subtestName <<
" done."
450 <<
" real/CPU ms: " << (finalRealTime-startRealTime)*1000
451 <<
" / " << (finalCpuTime-startCpuTime)*1000 <<std::endl;
452 std::clog << fmt.str();
455 const double startCpuTime;
456 const double startRealTime;
457 std::string subtestName;
463 #define SimTK_START_TEST(testName) \
464 SimTK::Test simtk_test_(testName); \
468 #define SimTK_END_TEST() \
469 } catch(const std::exception& e) { \
470 std::cerr << "Test failed due to exception: " \
471 << e.what() << std::endl; \
474 std::cerr << "Test failed due to unrecognized exception.\n"; \
481 #define SimTK_SUBTEST(testFunction) \
482 do {SimTK::Test::Subtest sub(#testFunction); (testFunction)();} while(false)
483 #define SimTK_SUBTEST1(testFunction,arg1) \
486 do {SimTK::Test::Subtest sub(#testFunction); (testFunction)(arg1);} while(false)
487 #define SimTK_SUBTEST2(testFunction,arg1,arg2) \
490 do {SimTK::Test::Subtest sub(#testFunction); (testFunction)(arg1,arg2);} while(false)
491 #define SimTK_SUBTEST3(testFunction,arg1,arg2,arg3) \
494 do {SimTK::Test::Subtest sub(#testFunction); (testFunction)(arg1,arg2,arg3);} while(false)
495 #define SimTK_SUBTEST4(testFunction,arg1,arg2,arg3,arg4) \
498 do {SimTK::Test::Subtest sub(#testFunction); (testFunction)(arg1,arg2,arg3,arg4);} while(false)
501 #define SimTK_TEST(cond) {SimTK_ASSERT_ALWAYS((cond), "Test condition failed.");}
505 #define SimTK_TEST_FAILED(msg) {SimTK_ASSERT_ALWAYS(!"Test case failed.", msg);}
510 #define SimTK_TEST_FAILED1(fmt,a1) {SimTK_ASSERT1_ALWAYS(!"Test case failed.",fmt,a1);}
515 #define SimTK_TEST_FAILED2(fmt,a1,a2) {SimTK_ASSERT2_ALWAYS(!"Test case failed.",fmt,a1,a2);}
520 #define SimTK_TEST_EQ(v1,v2) \
521 {SimTK_ASSERT_ALWAYS(SimTK::Test::numericallyEqual((v1),(v2),1), \
522 "Test values should have been numerically equivalent at default tolerance.");}
526 #define SimTK_TEST_EQ_SIZE(v1,v2,n) \
527 {SimTK_ASSERT1_ALWAYS(SimTK::Test::numericallyEqual((v1),(v2),(n)), \
528 "Test values should have been numerically equivalent at size=%d times default tolerance.",(n));}
533 #define SimTK_TEST_EQ_TOL(v1,v2,tol) \
534 {SimTK_ASSERT1_ALWAYS(SimTK::Test::numericallyEqual((v1),(v2),1,(tol)), \
535 "Test values should have been numerically equivalent at tolerance=%g.",(tol));}
540 #define SimTK_TEST_NOTEQ(v1,v2) \
541 {SimTK_ASSERT_ALWAYS(!SimTK::Test::numericallyEqual((v1),(v2),1), \
542 "Test values should NOT have been numerically equivalent (at default tolerance).");}
547 #define SimTK_TEST_NOTEQ_SIZE(v1,v2,n) \
548 {SimTK_ASSERT1_ALWAYS(!SimTK::Test::numericallyEqual((v1),(v2),(n)), \
549 "Test values should NOT have been numerically equivalent at size=%d times default tolerance.",(n));}
554 #define SimTK_TEST_NOTEQ_TOL(v1,v2,tol) \
555 {SimTK_ASSERT1_ALWAYS(!SimTK::Test::numericallyEqual((v1),(v2),1,(tol)), \
556 "Test values should NOT have been numerically equivalent at tolerance=%g.",(tol));}
559 #define SimTK_TEST_MUST_THROW(stmt) \
560 do {int threw=0; try {stmt;} \
561 catch(const std::exception&){threw=1;} \
562 catch(...){threw=2;} \
563 if (threw==0) SimTK_TEST_FAILED1("Expected statement\n----\n%s\n----\n to throw an exception but it did not.",#stmt); \
564 if (threw==2) SimTK_TEST_FAILED1("Expected statement\n%s\n to throw an std::exception but it threw something else.",#stmt); \
568 #define SimTK_TEST_MUST_THROW_EXC(stmt,exc) \
569 do {int threw=0; try {stmt;} \
570 catch(const exc&){threw=1;} \
571 catch(...){threw=2;} \
572 if (threw==0) SimTK_TEST_FAILED1("Expected statement\n----\n%s\n----\n to throw an exception but it did not.",#stmt); \
573 if (threw==2) SimTK_TEST_FAILED2("Expected statement\n----\n%s\n----\n to throw exception type %s but it threw something else.",#stmt,#exc); \
577 #define SimTK_TEST_MAY_THROW(stmt) \
578 do {int threw=0; try {stmt;} \
579 catch(const std::exception&){threw=1;} \
580 catch(...){threw=2;} \
581 if (threw==2) SimTK_TEST_FAILED1("Expected statement\n%s\n to throw an std::exception but it threw something else.",#stmt); \
585 #define SimTK_TEST_MAY_THROW_EXC(stmt,exc) \
586 do {int threw=0; try {stmt;} \
587 catch(const exc&){threw=1;} \
588 catch(...){threw=2;} \
589 if (threw==2) SimTK_TEST_FAILED2("Expected statement\n----\n%s\n----\n to throw exception type %s but it threw something else.",#stmt,#exc); \
595 #define SimTK_TEST_MUST_THROW_DEBUG(stmt)
598 #define SimTK_TEST_MUST_THROW_EXC_DEBUG(stmt,exc)
602 #define SimTK_TEST_MUST_THROW_DEBUG(stmt) SimTK_TEST_MUST_THROW(stmt)
605 #define SimTK_TEST_MUST_THROW_EXC_DEBUG(stmt,exc) \
608 SimTK_TEST_MUST_THROW_EXC(stmt,exc)
617 #endif // SimTK_SimTKCOMMON_TESTING_H_
static bool numericallyEqual(const Row< N, E1, S1 > &v1, const Row< N, E2, S2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:303
Vec< 2, Vec3 > SpatialVec
Spatial vectors are used for (rotation,translation) quantities and consist of a pair of Vec3 objects...
Definition: MassProperties.h:50
static double defTol2()
Definition: Testing.h:185
static bool numericallyEqual(double v1, long double v2, int n, double tol=defTol< double >())
Definition: Testing.h:218
static Mat< M, N > randMat()
Definition: Testing.h:401
The Rotation class is a Mat33 that guarantees that the matrix can be interpreted as a legitimate 3x3 ...
Definition: Quaternion.h:40
Subtest(const std::string &name)
Definition: Testing.h:437
static bool numericallyEqual(const negator< conjugate< P > > &v1, const std::complex< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:286
static bool numericallyEqual(const UnitInertia_< P > &G1, const UnitInertia_< P > &G2, int n, double tol=defTol< P >())
Definition: Testing.h:379
Real getValue() const
Get the next value in the pseudo-random sequence.
static bool numericallyEqual(long double v1, long double v2, int n, double tol=defTol< long double >())
Definition: Testing.h:200
static bool numericallyEqual(const conjugate< P > &v1, const conjugate< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:255
static bool numericallyEqual(int i1, double f2, int n, double tol=defTol< double >())
Definition: Testing.h:232
static bool numericallyEqual(double v1, float v2, int n, double tol=defTol< float >())
Definition: Testing.h:212
static bool numericallyEqual(const SymMat< N, E1, S1 > &v1, const SymMat< N, E2, S2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:313
static bool numericallyEqual(double f1, int i2, int n, double tol=defTol< double >())
Definition: Testing.h:234
static Row< N > randRow()
Definition: Testing.h:400
static bool numericallyEqual(const RowVector_< E1 > &v1, const RowVectorView_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:344
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
static bool numericallyEqual(long double v1, float v2, int n, double tol=defTol< float >())
Definition: Testing.h:216
static Vec3 randVec3()
Definition: Testing.h:411
int size() const
Definition: VectorBase.h:396
static bool numericallyEqual(double v1, double v2, int n, double tol=defTol< double >())
Definition: Testing.h:196
std::complex< Real > Complex
This is the default complex type for SimTK, with precision for the real and imaginary parts set to th...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:580
static bool numericallyEqual(float v1, long double v2, int n, double tol=defTol< float >())
Definition: Testing.h:214
static bool numericallyEqual(const negator< P > &v1, const negator< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:270
static bool numericallyEqual(const std::complex< P > &v1, const negator< conjugate< P > > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:290
conjugate< Real > Conjugate
Definition: Scalar.h:57
(Advanced) This class is identical to Matrix_ except that it has shallow (reference) copy and assignm...
Definition: BigMatrix.h:167
static bool numericallyEqual(const Mat< M, N, E1, CS1, RS1 > &v1, const Mat< M, N, E2, CS2, RS2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:308
static bool numericallyEqual(float v1, double v2, int n, double tol=defTol< float >())
Definition: Testing.h:210
static bool numericallyEqual(const P &v1, const negator< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:274
This is a small, fixed-size symmetric or Hermitian matrix designed for no-overhead inline computation...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:592
static bool numericallyEqual(const VectorView_< E1 > &v1, const VectorView_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:317
static bool numericallyEqual(const Matrix_< E1 > &m1, const MatrixView_< E2 > &m2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:361
static Vec< M > randVec()
Definition: Testing.h:398
SimTK_Real Real
This is the default compiled-in floating point type for SimTK, either float or double.
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:577
static bool numericallyEqual(unsigned u1, unsigned u2, int n, double tol=0)
Definition: Testing.h:207
static bool numericallyEqual(unsigned i1, float f2, int n, double tol=defTol< float >())
Definition: Testing.h:228
double cpuTime()
Return the cumulative CPU time in seconds (both kernel and user time) that has been used so far by an...
Definition: Timing.h:197
static bool numericallyEqual(int i1, long double f2, int n, double tol=defTol< long double >())
Definition: Testing.h:240
static Complex randComplex()
Definition: Testing.h:393
static double randDouble()
Definition: Testing.h:396
static Vector randVector(int m)
Definition: Testing.h:406
static bool numericallyEqual(const RowVectorView_< E1 > &v1, const RowVector_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:347
static bool numericallyEqual(long double f1, unsigned i2, int n, double tol=defTol< long double >())
Definition: Testing.h:246
Rotation_< Real > Rotation
Definition: Rotation.h:47
static SymMat< N > randSymMat()
Definition: Testing.h:403
static bool numericallyEqual(const Matrix_< E1 > &m1, const Matrix_< E2 > &m2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:358
double realTime()
Return current time on the high-resolution interval timer in seconds.
Definition: Timing.h:259
Test(const std::string &name)
Definition: Testing.h:162
static bool numericallyEqual(unsigned i1, double f2, int n, double tol=defTol< double >())
Definition: Testing.h:236
A UnitInertia matrix is a unit-mass inertia matrix; you can convert it to an Inertia by multiplying i...
Definition: MassProperties.h:81
static bool numericallyEqual(const conjugate< P > &v1, const std::complex< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:265
static bool numericallyEqual(const negator< P > &v1, const P &v2, int n, double tol=defTol< P >())
Definition: Testing.h:278
const SymMat33P & asSymMat33() const
Obtain a reference to the underlying symmetric matrix type.
Definition: MassProperties.h:401
static bool numericallyEqual(const RowVector_< E1 > &v1, const RowVector_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:341
static bool numericallyEqual(float f1, int i2, int n, double tol=defTol< float >())
Definition: Testing.h:226
static bool numericallyEqual(const RowVectorView_< E1 > &v1, const RowVectorView_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:334
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
static Real randReal()
Definition: Testing.h:389
bool isSameRotationToWithinAngle(const Rotation_ &R, RealP okPointingAngleErrorRads) const
Return true if "this" Rotation is nearly identical to "R" within a specified pointing angle error...
static bool numericallyEqual(float v1, float v2, int n, double tol=defTol< float >())
Definition: Testing.h:192
static bool numericallyEqual(const Transform_< P > &T1, const Transform_< P > &T2, int n, double tol=defTol< P >())
Definition: Testing.h:373
static bool numericallyEqual(const MatrixView_< E1 > &m1, const Matrix_< E2 > &m2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:364
Represents a variable size row vector; much less common than the column vector type Vector_...
Definition: BigMatrix.h:174
int nrow() const
Return the number of rows m in the logical shape of this matrix.
Definition: MatrixBase.h:137
negator, where N is a number type (real, complex, conjugate), is represented in memory identically...
Definition: String.h:44
This is a fixed-length row vector designed for no-overhead inline computation.
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:590
static bool numericallyEqual(const negator< std::complex< P > > &v1, const conjugate< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:282
static SymMat33 randSymMat33()
Definition: Testing.h:413
(Advanced) This class is identical to RowVector_ except that it has shallow (reference) copy and assi...
Definition: BigMatrix.h:173
int ncol() const
Return the number of columns n in the logical shape of this matrix.
Definition: MatrixBase.h:139
RowVectorBase< typename CNT< ELEM >::TAbs > abs(const RowVectorBase< ELEM > &v)
Definition: VectorMath.h:120
Mat< 2, 2, Mat33 > SpatialMat
Spatial matrices are used to hold 6x6 matrices that are best viewed as 2x2 matrices of 3x3 matrices; ...
Definition: MassProperties.h:72
This is the vector class intended to appear in user code for large, variable size column vectors...
Definition: BigMatrix.h:171
This class represents a small matrix whose size is known at compile time, containing elements of any ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:591
static Conjugate randConjugate()
Definition: Testing.h:394
static bool numericallyEqual(const Vector_< E1 > &v1, const VectorView_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:327
Internal utility class for generating test messages for subtests.
Definition: Testing.h:435
const TAsVec & getAsVec() const
Definition: SymMat.h:831
static bool numericallyEqual(const Rotation_< P > &R1, const Rotation_< P > &R2, int n, double tol=defTol< P >())
Definition: Testing.h:368
static bool numericallyEqual(long double f1, int i2, int n, double tol=defTol< long double >())
Definition: Testing.h:242
static bool numericallyEqual(const Vec< M, E1, S1 > &v1, const Vec< M, E2, S2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:298
static Transform randTransform()
Definition: Testing.h:425
static bool numericallyEqual(int i1, int i2, int n, double tol=0)
Definition: Testing.h:206
This is a fixed-length column vector designed for no-overhead inline computation. ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:589
~Test()
Definition: Testing.h:169
This file ensures that we have access to the Posix time functions clock_getttime() and nanosleep()...
static bool numericallyEqual(long double v1, double v2, int n, double tol=defTol< double >())
Definition: Testing.h:220
static bool numericallyEqual(const Inertia_< P > &I1, const Inertia_< P > &I2, int n, double tol=defTol< P >())
Definition: Testing.h:384
static bool numericallyEqual(int i1, float f2, int n, double tol=defTol< float >())
Definition: Testing.h:224
(Advanced) This class is identical to Vector_ except that it has shallow (reference) copy and assignm...
Definition: BigMatrix.h:170
static bool numericallyEqual(const VectorView_< E1 > &v1, const Vector_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:330
SimTK::conjugate should be instantiated only for float, double, long double.
Definition: String.h:45
static bool numericallyEqual(const std::complex< P > &v1, const std::complex< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:250
static Mat33 randMat33()
Definition: Testing.h:412
static SpatialVec randSpatialVec()
Definition: Testing.h:414
static bool numericallyEqual(unsigned i1, long double f2, int n, double tol=defTol< long double >())
Definition: Testing.h:244
This is the header which should be included in user programs that would like to make use of all the S...
static Matrix randMatrix(int m, int n)
Definition: Testing.h:408
Includes internal headers providing declarations for the basic SimTK Core classes.
static bool numericallyEqual(const Vector_< E1 > &v1, const Vector_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:324
int size() const
Definition: RowVectorBase.h:237
static double defTol()
Definition: Testing.h:181
static bool numericallyEqual(const conjugate< P > &v1, const negator< std::complex< P > > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:294
static bool numericallyEqual(const MatrixView_< E1 > &v1, const MatrixView_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:351
static bool numericallyEqual(const std::complex< P > &v1, const conjugate< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:260
This is the matrix class intended to appear in user code for large, variable size matrices...
Definition: BigMatrix.h:168
static float randFloat()
Definition: Testing.h:395
static Rotation randRotation()
Definition: Testing.h:421
~Subtest()
Definition: Testing.h:444
static bool numericallyEqual(double f1, unsigned i2, int n, double tol=defTol< double >())
Definition: Testing.h:238
static SpatialMat randSpatialMat()
Definition: Testing.h:417
Transform_< Real > Transform
Definition: Transform.h:44
static bool numericallyEqual(float f1, unsigned i2, int n, double tol=defTol< float >())
Definition: Testing.h:230
TAsVec & updAsVec()
Definition: SymMat.h:832
The physical meaning of an inertia is the distribution of a rigid body's mass about a particular poin...
Definition: MassProperties.h:82