1 #ifndef SimTK_SimTKCOMMON_TESTING_H_
2 #define SimTK_SimTKCOMMON_TESTING_H_
162 Test(
const std::string& name) : testName(name)
164 std::clog <<
"Starting test " << testName <<
" ...\n";
165 startTime = std::clock();
168 std::clog <<
"Done. " << testName <<
" time: "
169 << 1000*(std::clock()-startTime)/CLOCKS_PER_SEC <<
"ms.\n";
176 template <
class T1,
class T2>
186 return std::abs(v1-v2) < scale*(float)tol;
190 return std::abs(v1-v2) < scale*(double)tol;
192 static bool numericallyEqual(
long double v1,
long double v2,
int n,
double tol=defTol<long double>()) {
194 return std::abs(v1-v2) < scale*(
long double)tol;
199 static bool numericallyEqual(
unsigned u1,
unsigned u2,
int n,
double tol=0) {
return u1==u2;}
210 static bool numericallyEqual(
double v1,
long double v2,
int n,
double tol=defTol<double>())
212 static bool numericallyEqual(
long double v1,
double v2,
int n,
double tol=defTol<double>())
232 static bool numericallyEqual(
int i1,
long double f2,
int n,
double tol=defTol<long double>())
234 static bool numericallyEqual(
long double f1,
int i2,
int n,
double tol=defTol<long double>())
236 static bool numericallyEqual(
unsigned i1,
long double f2,
int n,
double tol=defTol<long double>())
238 static bool numericallyEqual(
long double f1,
unsigned i2,
int n,
double tol=defTol<long double>())
242 static bool numericallyEqual(
const std::complex<P>& v1,
const std::complex<P>& v2,
int n,
double tol=defTol<P>()) {
289 template <
int M,
class E1,
int S1,
class E2,
int S2>
291 for (
int i=0; i<M; ++i)
if (!
numericallyEqual(v1[i],v2[i], n, tol))
return false;
294 template <
int N,
class E1,
int S1,
class E2,
int S2>
296 for (
int j=0; j<N; ++j)
if (!
numericallyEqual(v1[j],v2[j], n, tol))
return false;
299 template <
int M,
int N,
class E1,
int CS1,
int RS1,
class E2,
int CS2,
int RS2>
300 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>())) {
301 for (
int j=0; j<N; ++j)
if (!
numericallyEqual(v1(j),v2(j), n, tol))
return false;
304 template <
int N,
class E1,
int S1,
class E2,
int S2>
308 template <
class E1,
class E2>
310 if (v1.
size() != v2.
size())
return false;
311 for (
int i=0; i < v1.
size(); ++i)
315 template <
class E1,
class E2>
318 template <
class E1,
class E2>
321 template <
class E1,
class E2>
325 template <
class E1,
class E2>
327 if (v1.
size() != v2.
size())
return false;
328 for (
int i=0; i < v1.
size(); ++i)
332 template <
class E1,
class E2>
335 template <
class E1,
class E2>
338 template <
class E1,
class E2>
342 template <
class E1,
class E2>
345 for (
int j=0; j < v1.
ncol(); ++j)
349 template <
class E1,
class E2>
352 template <
class E1,
class E2>
355 template <
class E1,
class E2>
394 {
Mat<M,N> m;
for (
int j=0; j<N; ++j) m(j)=randVec<M>();
return m;}
399 {
Vector v(m);
for (
int i=0; i<m; ++i) v[i]=
randReal();
return v;}
421 std::clock_t startTime;
422 std::string testName;
428 Subtest(
const std::string& name) : subtestName(name)
431 sprintf(padded,
"%-20s", name.c_str());
432 paddedName = std::string(padded);
433 std::clog <<
" " << paddedName <<
" ... " << std::flush;
434 startTime = std::clock();
437 std::clog <<
"done. " << paddedName <<
" time: "
438 << 1000*(std::clock()-startTime)/CLOCKS_PER_SEC <<
"ms.\n";
441 std::clock_t startTime;
442 std::string subtestName;
443 std::string paddedName;
449 #define SimTK_START_TEST(testName) \
450 SimTK::Test simtk_test_(testName); \
454 #define SimTK_END_TEST() \
455 } catch(const std::exception& e) { \
456 std::cerr << "Test failed due to exception: " \
457 << e.what() << std::endl; \
460 std::cerr << "Test failed due to unrecognized exception.\n"; \
467 #define SimTK_SUBTEST(testFunction) \
468 do {SimTK::Test::Subtest sub(#testFunction); (testFunction)();} while(false)
469 #define SimTK_SUBTEST1(testFunction,arg1) \
472 do {SimTK::Test::Subtest sub(#testFunction); (testFunction)(arg1);} while(false)
473 #define SimTK_SUBTEST2(testFunction,arg1,arg2) \
476 do {SimTK::Test::Subtest sub(#testFunction); (testFunction)(arg1,arg2);} while(false)
477 #define SimTK_SUBTEST3(testFunction,arg1,arg2,arg3) \
480 do {SimTK::Test::Subtest sub(#testFunction); (testFunction)(arg1,arg2,arg3);} while(false)
481 #define SimTK_SUBTEST4(testFunction,arg1,arg2,arg3,arg4) \
484 do {SimTK::Test::Subtest sub(#testFunction); (testFunction)(arg1,arg2,arg3,arg4);} while(false)
487 #define SimTK_TEST(cond) {SimTK_ASSERT_ALWAYS((cond), "Test condition failed.");}
491 #define SimTK_TEST_FAILED(msg) {SimTK_ASSERT_ALWAYS(!"Test case failed.", msg);}
496 #define SimTK_TEST_FAILED1(fmt,a1) {SimTK_ASSERT1_ALWAYS(!"Test case failed.",fmt,a1);}
501 #define SimTK_TEST_FAILED2(fmt,a1,a2) {SimTK_ASSERT2_ALWAYS(!"Test case failed.",fmt,a1,a2);}
506 #define SimTK_TEST_EQ(v1,v2) \
507 {SimTK_ASSERT_ALWAYS(SimTK::Test::numericallyEqual((v1),(v2),1), \
508 "Test values should have been numerically equivalent at default tolerance.");}
512 #define SimTK_TEST_EQ_SIZE(v1,v2,n) \
513 {SimTK_ASSERT1_ALWAYS(SimTK::Test::numericallyEqual((v1),(v2),(n)), \
514 "Test values should have been numerically equivalent at size=%d times default tolerance.",(n));}
519 #define SimTK_TEST_EQ_TOL(v1,v2,tol) \
520 {SimTK_ASSERT1_ALWAYS(SimTK::Test::numericallyEqual((v1),(v2),1,(tol)), \
521 "Test values should have been numerically equivalent at tolerance=%g.",(tol));}
526 #define SimTK_TEST_NOTEQ(v1,v2) \
527 {SimTK_ASSERT_ALWAYS(!SimTK::Test::numericallyEqual((v1),(v2),1), \
528 "Test values should NOT have been numerically equivalent (at default tolerance).");}
533 #define SimTK_TEST_NOTEQ_SIZE(v1,v2,n) \
534 {SimTK_ASSERT1_ALWAYS(!SimTK::Test::numericallyEqual((v1),(v2),(n)), \
535 "Test values should NOT have been numerically equivalent at size=%d times default tolerance.",(n));}
540 #define SimTK_TEST_NOTEQ_TOL(v1,v2,tol) \
541 {SimTK_ASSERT1_ALWAYS(!SimTK::Test::numericallyEqual((v1),(v2),1,(tol)), \
542 "Test values should NOT have been numerically equivalent at tolerance=%g.",(tol));}
545 #define SimTK_TEST_MUST_THROW(stmt) \
546 do {int threw=0; try {stmt;} \
547 catch(const std::exception&){threw=1;} \
548 catch(...){threw=2;} \
549 if (threw==0) SimTK_TEST_FAILED1("Expected statement\n----\n%s\n----\n to throw an exception but it did not.",#stmt); \
550 if (threw==2) SimTK_TEST_FAILED1("Expected statement\n%s\n to throw an std::exception but it threw something else.",#stmt); \
554 #define SimTK_TEST_MUST_THROW_EXC(stmt,exc) \
555 do {int threw=0; try {stmt;} \
556 catch(const exc&){threw=1;} \
557 catch(...){threw=2;} \
558 if (threw==0) SimTK_TEST_FAILED1("Expected statement\n----\n%s\n----\n to throw an exception but it did not.",#stmt); \
559 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); \
563 #define SimTK_TEST_MAY_THROW(stmt) \
564 do {int threw=0; try {stmt;} \
565 catch(const std::exception&){threw=1;} \
566 catch(...){threw=2;} \
567 if (threw==2) SimTK_TEST_FAILED1("Expected statement\n%s\n to throw an std::exception but it threw something else.",#stmt); \
571 #define SimTK_TEST_MAY_THROW_EXC(stmt,exc) \
572 do {int threw=0; try {stmt;} \
573 catch(const exc&){threw=1;} \
574 catch(...){threw=2;} \
575 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); \
581 #define SimTK_TEST_MUST_THROW_DEBUG(stmt)
584 #define SimTK_TEST_MUST_THROW_EXC_DEBUG(stmt,exc)
588 #define SimTK_TEST_MUST_THROW_DEBUG(stmt) SimTK_TEST_MUST_THROW(stmt)
591 #define SimTK_TEST_MUST_THROW_EXC_DEBUG(stmt,exc) \
594 SimTK_TEST_MUST_THROW_EXC(stmt,exc)
603 #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:295
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:177
static bool numericallyEqual(double v1, long double v2, int n, double tol=defTol< double >())
Definition: Testing.h:210
static Mat< M, N > randMat()
Definition: Testing.h:393
Subtest(const std::string &name)
Definition: Testing.h:428
static bool numericallyEqual(const negator< conjugate< P > > &v1, const std::complex< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:278
static bool numericallyEqual(const UnitInertia_< P > &G1, const UnitInertia_< P > &G2, int n, double tol=defTol< P >())
Definition: Testing.h:371
RS is total spacing between rows in memory (default 1)
Definition: SymMat.h:71
Real getValue() const
Get the next value in the pseudo-random sequence.
This is the Vector class intended to appear in user code.
Definition: BigMatrix.h:186
static bool numericallyEqual(long double v1, long double v2, int n, double tol=defTol< long double >())
Definition: Testing.h:192
static bool numericallyEqual(const conjugate< P > &v1, const conjugate< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:247
static bool numericallyEqual(int i1, double f2, int n, double tol=defTol< double >())
Definition: Testing.h:224
static bool numericallyEqual(double v1, float v2, int n, double tol=defTol< float >())
Definition: Testing.h:204
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:305
static bool numericallyEqual(double f1, int i2, int n, double tol=defTol< double >())
Definition: Testing.h:226
static Row< N > randRow()
Definition: Testing.h:392
static bool numericallyEqual(const RowVector_< E1 > &v1, const RowVectorView_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:336
SimTK::conjugate<R> should be instantiated only for float, double, long double.
Definition: String.h:45
static bool numericallyEqual(long double v1, float v2, int n, double tol=defTol< float >())
Definition: Testing.h:208
static Vec3 randVec3()
Definition: Testing.h:403
static bool numericallyEqual(double v1, double v2, int n, double tol=defTol< double >())
Definition: Testing.h:188
The Rotation class is a Mat33 that guarantees that the matrix is a legitimate 3x3 array associated wi...
Definition: Quaternion.h:40
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, long double v2, int n, double tol=defTol< float >())
Definition: Testing.h:206
static bool numericallyEqual(const negator< P > &v1, const negator< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:262
TAsVec & updAsVec()
Definition: SymMat.h:816
static bool numericallyEqual(const std::complex< P > &v1, const negator< conjugate< P > > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:282
conjugate< Real > Conjugate
Definition: Scalar.h:57
int size() const
Definition: BigMatrix.h:1411
const SymMat33P & asSymMat33() const
Obtain a reference to the underlying symmetric matrix type.
Definition: MassProperties.h:401
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:300
static bool numericallyEqual(float v1, double v2, int n, double tol=defTol< float >())
Definition: Testing.h:202
static bool numericallyEqual(const P &v1, const negator< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:266
negator<N>, where N is a number type (real, complex, conjugate), is represented in memory identically...
Definition: String.h:44
static bool numericallyEqual(const VectorView_< E1 > &v1, const VectorView_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:309
static bool numericallyEqual(const Matrix_< E1 > &m1, const MatrixView_< E2 > &m2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:353
static Vec< M > randVec()
Definition: Testing.h:390
static bool numericallyEqual(unsigned u1, unsigned u2, int n, double tol=0)
Definition: Testing.h:199
static bool numericallyEqual(unsigned i1, float f2, int n, double tol=defTol< float >())
Definition: Testing.h:220
static bool numericallyEqual(int i1, long double f2, int n, double tol=defTol< long double >())
Definition: Testing.h:232
static Complex randComplex()
Definition: Testing.h:385
static double randDouble()
Definition: Testing.h:388
static Vector randVector(int m)
Definition: Testing.h:398
This class is identical to a RowVector_; it is used only to manage the C++ rules for when copy constr...
Definition: BigMatrix.h:189
static bool numericallyEqual(const RowVectorView_< E1 > &v1, const RowVector_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:339
static bool numericallyEqual(long double f1, unsigned i2, int n, double tol=defTol< long double >())
Definition: Testing.h:238
Rotation_< Real > Rotation
Definition: Rotation.h:47
static SymMat< N > randSymMat()
Definition: Testing.h:395
static bool numericallyEqual(const Matrix_< E1 > &m1, const Matrix_< E2 > &m2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:350
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:228
This is a fixed length column vector designed for no-overhead inline computation. ...
Definition: Vec.h:131
static bool numericallyEqual(const conjugate< P > &v1, const std::complex< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:257
static bool numericallyEqual(const negator< P > &v1, const P &v2, int n, double tol=defTol< P >())
Definition: Testing.h:270
int nrow() const
Return the number of rows m in the logical shape of this matrix.
Definition: BigMatrix.h:288
static bool numericallyEqual(const RowVector_< E1 > &v1, const RowVector_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:333
const TAsVec & getAsVec() const
Definition: SymMat.h:815
This class is identical to a Matrix_; it is used only to manage the C++ rules for when copy construct...
Definition: BigMatrix.h:179
static bool numericallyEqual(float f1, int i2, int n, double tol=defTol< float >())
Definition: Testing.h:218
static bool numericallyEqual(const RowVectorView_< E1 > &v1, const RowVectorView_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:326
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
static Real randReal()
Definition: Testing.h:381
static bool numericallyEqual(float v1, float v2, int n, double tol=defTol< float >())
Definition: Testing.h:184
static bool numericallyEqual(const Transform_< P > &T1, const Transform_< P > &T2, int n, double tol=defTol< P >())
Definition: Testing.h:365
static bool numericallyEqual(const MatrixView_< E1 > &m1, const Matrix_< E2 > &m2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:356
int size() const
Definition: BigMatrix.h:1690
static bool numericallyEqual(const negator< std::complex< P > > &v1, const conjugate< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:274
static SymMat33 randSymMat33()
Definition: Testing.h:405
This is the Matrix class intended to appear in user code.
Definition: BigMatrix.h:181
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
The physical meaning of an inertia is the distribution of a rigid body's mass about a particular poin...
Definition: MassProperties.h:82
Generic Row.
Definition: Row.h:118
A UnitInertia matrix is a unit-mass inertia matrix; you can convert it to an Inertia by multiplying i...
Definition: MassProperties.h:81
static Conjugate randConjugate()
Definition: Testing.h:386
static bool numericallyEqual(const Vector_< E1 > &v1, const VectorView_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:319
Internal utility class for generating test messages for subtests.
Definition: Testing.h:426
static bool numericallyEqual(const Rotation_< P > &R1, const Rotation_< P > &R2, int n, double tol=defTol< P >())
Definition: Testing.h:360
static bool numericallyEqual(long double f1, int i2, int n, double tol=defTol< long double >())
Definition: Testing.h:234
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:290
static Transform randTransform()
Definition: Testing.h:417
RowVectors are much less common than Vectors.
Definition: BigMatrix.h:191
static bool numericallyEqual(int i1, int i2, int n, double tol=0)
Definition: Testing.h:198
~Test()
Definition: Testing.h:167
static bool numericallyEqual(long double v1, double v2, int n, double tol=defTol< double >())
Definition: Testing.h:212
static bool numericallyEqual(const Inertia_< P > &I1, const Inertia_< P > &I2, int n, double tol=defTol< P >())
Definition: Testing.h:376
static bool numericallyEqual(int i1, float f2, int n, double tol=defTol< float >())
Definition: Testing.h:216
static bool numericallyEqual(const VectorView_< E1 > &v1, const Vector_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:322
static bool numericallyEqual(const std::complex< P > &v1, const std::complex< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:242
This class represents a small matrix whose size is known at compile time, containing elements of any ...
Definition: Mat.h:51
static Mat33 randMat33()
Definition: Testing.h:404
static SpatialVec randSpatialVec()
Definition: Testing.h:406
static bool numericallyEqual(unsigned i1, long double f2, int n, double tol=defTol< long double >())
Definition: Testing.h:236
This is the header which should be included in user programs that would like to make use of all the S...
int ncol() const
Return the number of columns n in the logical shape of this matrix.
Definition: BigMatrix.h:290
static Matrix randMatrix(int m, int n)
Definition: Testing.h:400
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:316
static double defTol()
Definition: Testing.h:173
static bool numericallyEqual(const conjugate< P > &v1, const negator< std::complex< P > > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:286
static bool numericallyEqual(const MatrixView_< E1 > &v1, const MatrixView_< E2 > &v2, int n, double tol=(defTol2< E1, E2 >()))
Definition: Testing.h:343
This class is identical to a Vector_; it is used only to manage the C++ rules for when copy construct...
Definition: BigMatrix.h:184
static bool numericallyEqual(const std::complex< P > &v1, const conjugate< P > &v2, int n, double tol=defTol< P >())
Definition: Testing.h:252
static float randFloat()
Definition: Testing.h:387
static Rotation randRotation()
Definition: Testing.h:413
~Subtest()
Definition: Testing.h:436
static bool numericallyEqual(double f1, unsigned i2, int n, double tol=defTol< double >())
Definition: Testing.h:230
static SpatialMat randSpatialMat()
Definition: Testing.h:409
Transform_< Real > Transform
Definition: Transform.h:44
static bool numericallyEqual(float f1, unsigned i2, int n, double tol=defTol< float >())
Definition: Testing.h:222