diff --git a/OpenSim/Common/Test/testGCVSpline.cpp b/OpenSim/Common/Test/testGCVSpline.cpp index 49b22f4c90..c572fcffbc 100644 --- a/OpenSim/Common/Test/testGCVSpline.cpp +++ b/OpenSim/Common/Test/testGCVSpline.cpp @@ -24,69 +24,65 @@ #include #include +#define CATCH_CONFIG_MAIN +#include + using namespace OpenSim; using namespace std; -int main() { - try { - const int size = 101; - const double T = 1.0; - const double omega = 2 * SimTK::Pi; - const double dt = T / (size - 1); - double x[size], y[size]; - for (int i = 0; i < size; ++i) { - x[i] = dt*i; - y[i] = sin(omega*x[i]); - } - GCVSpline spline(5, size, x, y); - SimTK::Vector t(1, 0.0); - // Should obtain the input samples exactly - for (int i = 0; i < size; ++i) { - t[0] = x[i]; - //cout << t[0] << " error = " << y[i] - spline.calcValue(t) << endl; - ASSERT_EQUAL(y[i], spline.calcValue(t), - SimTK::SignificantReal, __FILE__, __LINE__, - "GCVSpline failed to reproduce input data points."); - } - cout << "GCVSpline successfully reproduced input data points." << endl; - - for (int i = 0; i < (2*size-1); ++i) { - t[0] = dt / 2 * i; - //cout << t[0] << " error = " << sin(omega*t[0]) - spline.calcValue(t) << endl; - ASSERT_EQUAL(sin(omega*t[0]), spline.calcValue(t), - dt*dt, __FILE__, __LINE__, - "GCVSpline failed to interpolate within accuracy requirement."); - } - cout << "GCVSpline successfully interpolated within accuracy." << endl; +TEST_CASE("GCVSpline Behaves as Expected") +{ + const int size = 101; + const double T = 1.0; + const double omega = 2 * SimTK::Pi; + const double dt = T / (size - 1); + double x[size], y[size]; + for (int i = 0; i < size; ++i) { + x[i] = dt*i; + y[i] = sin(omega*x[i]); + } + GCVSpline spline(5, size, x, y); + SimTK::Vector t(1, 0.0); + // Should obtain the input samples exactly + for (int i = 0; i < size; ++i) { + t[0] = x[i]; + //cout << t[0] << " error = " << y[i] - spline.calcValue(t) << endl; + ASSERT_EQUAL(y[i], spline.calcValue(t), + SimTK::SignificantReal, __FILE__, __LINE__, + "GCVSpline failed to reproduce input data points."); + } + cout << "GCVSpline successfully reproduced input data points." << endl; - std::vector derivComponents(1, 0); //take first derivative - for (int i = 5; i < size-5; ++i) { - t[0] = x[i]; - double dy = omega*cos(omega*t[0]); - double dS = spline.calcDerivative(derivComponents, t); + for (int i = 0; i < (2*size-1); ++i) { + t[0] = dt / 2 * i; + //cout << t[0] << " error = " << sin(omega*t[0]) - spline.calcValue(t) << endl; + ASSERT_EQUAL(sin(omega*t[0]), spline.calcValue(t), + dt*dt, __FILE__, __LINE__, + "GCVSpline failed to interpolate within accuracy requirement."); + } + cout << "GCVSpline successfully interpolated within accuracy." << endl; - //cout << t[0] << " error = " << dy - dS<< endl; - ASSERT_EQUAL(dy, dS, - omega*dt*dt, __FILE__, __LINE__, - "GCVSpline failed to reproduce accurate first derivative."); - } - cout << "GCVSpline successfully produced first derivatives." << endl; + std::vector derivComponents(1, 0); //take first derivative + for (int i = 5; i < size-5; ++i) { + t[0] = x[i]; + double dy = omega*cos(omega*t[0]); + double dS = spline.calcDerivative(derivComponents, t); - GCVSpline spline2(5, size, x, y); - for (int i = 0; i < size; ++i) { - t[0] = x[i]; - double dS = spline.calcDerivative(derivComponents, t); - double dS2 = spline2.calcDerivative(derivComponents, t); - //cout << t[0] << " error = " << dS - dS2 << endl; - ASSERT_EQUAL(dS, dS, - SimTK::Eps, __FILE__, __LINE__, - "Duplicate GCVSpline failed to reproduce identical first derivative."); - } + //cout << t[0] << " error = " << dy - dS<< endl; + ASSERT_EQUAL(dy, dS, + omega*dt*dt, __FILE__, __LINE__, + "GCVSpline failed to reproduce accurate first derivative."); } - catch(const Exception& e) { - e.print(cerr); - return 1; + cout << "GCVSpline successfully produced first derivatives." << endl; + + GCVSpline spline2(5, size, x, y); + for (int i = 0; i < size; ++i) { + t[0] = x[i]; + double dS = spline.calcDerivative(derivComponents, t); + double dS2 = spline2.calcDerivative(derivComponents, t); + //cout << t[0] << " error = " << dS - dS2 << endl; + ASSERT_EQUAL(dS, dS, + SimTK::Eps, __FILE__, __LINE__, + "Duplicate GCVSpline failed to reproduce identical first derivative."); } - cout << "Done" << endl; - return 0; }