Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Simulation tests to catch2 #3897

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
543 changes: 231 additions & 312 deletions OpenSim/Simulation/Test/testInverseKinematicsSolver.cpp

Large diffs are not rendered by default.

73 changes: 7 additions & 66 deletions OpenSim/Simulation/Test/testManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,71 +47,12 @@ Manager Tests:
#include <OpenSim/Common/LoadOpenSimLibrary.h>
#include <OpenSim/Simulation/Control/PrescribedController.h>
#include <OpenSim/Common/Constant.h>
#include <catch2/catch_all.hpp>

using namespace OpenSim;
using namespace std;
void testStationCalcWithManager();
void testStateChangesBetweenIntegration();
void testExcitationUpdatesWithManager();
void testConstructors();
void testIntegratorInterface();
void testExceptions();

int main()
{
SimTK::Array_<std::string> failures;

try { testStationCalcWithManager(); }
catch (const std::exception& e) {
cout << e.what() << endl;
failures.push_back("testStationCalcWithManager");
}

try { testStateChangesBetweenIntegration(); }
catch (const std::exception& e) {
cout << e.what() << endl;
failures.push_back("testStateChangesBetweenIntegration");
}

try { testExcitationUpdatesWithManager(); }
catch (const std::exception& e) {
cout << e.what() << endl;
failures.push_back("testExcitationUpdatesWithManager");
}

try { testConstructors(); }
catch (const std::exception& e) {
cout << e.what() << endl;
failures.push_back("testConstructors");
}

try { testIntegratorInterface(); }
catch (const std::exception& e) {
cout << e.what() << endl;
failures.push_back("testIntegratorInterface");
}

try { testExceptions(); }
catch (const std::exception& e) {
cout << e.what() << endl;
failures.push_back("testExceptions");
}

if (!failures.empty()) {
cout << "Done, with failure(s): " << failures << endl;
return 1;
}

cout << "Done. All cases passed." << endl;

return 0;
}

//==============================================================================
// Test Cases
//==============================================================================

void testStationCalcWithManager()
TEST_CASE("testStationCalcWithManager")
{
using SimTK::Vec3;

Expand Down Expand Up @@ -185,7 +126,7 @@ void testStationCalcWithManager()
}
}

void testStateChangesBetweenIntegration()
TEST_CASE("testStateChangesBetweenIntegration")
{
cout << "Running testStateChangesBetweenIntegration" << endl;

Expand Down Expand Up @@ -275,7 +216,7 @@ void testStateChangesBetweenIntegration()

}

void testExcitationUpdatesWithManager()
TEST_CASE("testExcitationUpdatesWithManager")
{
cout << "Running testExcitationUpdatesWithManager" << endl;
LoadOpenSimLibrary("osimActuators");
Expand Down Expand Up @@ -324,7 +265,7 @@ void testExcitationUpdatesWithManager()
}
}

void testConstructors()
TEST_CASE("testConstructors")
{
cout << "Running testConstructors" << endl;

Expand Down Expand Up @@ -382,7 +323,7 @@ void testConstructors()
SimTK_TEST_EQ(sliderCoord.getSpeedValue(outState4), finalSpeed);
}

void testIntegratorInterface()
TEST_CASE("testIntegratorInterface")
{
cout << "Running testIntegratorInterface" << endl;

Expand Down Expand Up @@ -450,7 +391,7 @@ void testIntegratorInterface()
manager.setIntegratorInternalStepLimit(nSteps);
}

void testExceptions()
TEST_CASE("testExceptions")
{
cout << "Running testExceptions" << endl;
LoadOpenSimLibrary("osimActuators");
Expand Down
167 changes: 78 additions & 89 deletions OpenSim/Simulation/Test/testModelInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,126 +28,114 @@
#include <OpenSim/Common/LoadOpenSimLibrary.h>

#include <memory>
#include <catch2/catch_all.hpp>

using namespace OpenSim;
using namespace std;

void testModelFinalizePropertiesAndConnections();
void testModelTopologyErrors();
void testDoesNotSegfaultWithUnusualConnections();

int main() {
TEST_CASE("testModelFinalizePropertiesAndConnections")
{
LoadOpenSimLibrary("osimActuators");
Model model("arm26.osim");

SimTK_START_TEST("testModelInterface");
SimTK_SUBTEST(testModelFinalizePropertiesAndConnections);
SimTK_SUBTEST(testModelTopologyErrors);
SimTK_SUBTEST(testDoesNotSegfaultWithUnusualConnections);
SimTK_END_TEST();
}


void testModelFinalizePropertiesAndConnections()
{
Model model("arm26.osim");
model.printSubcomponentInfo();

model.printSubcomponentInfo();
// all subcomponents are accounted for since Model constructor invokes
// finalizeFromProperties().
ASSERT(model.countNumComponents() > 0);

// all subcomponents are accounted for since Model constructor invokes
// finalizeFromProperties().
ASSERT(model.countNumComponents() > 0);
// model must be up-to-date with its properties
ASSERT(model.isObjectUpToDateWithProperties());

// model must be up-to-date with its properties
ASSERT(model.isObjectUpToDateWithProperties());
// get writable access to Components contained in the model's Set
auto& muscles = model.updMuscles();
// to make edits, for example, muscles[0].upd_min_control() = 0.02;
ASSERT(!model.isObjectUpToDateWithProperties());

// get writable access to Components contained in the model's Set
auto& muscles = model.updMuscles();
// to make edits, for example, muscles[0].upd_min_control() = 0.02;
ASSERT(!model.isObjectUpToDateWithProperties());
model.finalizeFromProperties();
ASSERT(model.isObjectUpToDateWithProperties());

model.finalizeFromProperties();
ASSERT(model.isObjectUpToDateWithProperties());
// get writable access to another Set for the purpose of editing
auto& bodies = model.updBodySet();
// for example, bodies[1].upd_mass() = 0.05;
ASSERT(!model.isObjectUpToDateWithProperties());

// get writable access to another Set for the purpose of editing
auto& bodies = model.updBodySet();
// for example, bodies[1].upd_mass() = 0.05;
ASSERT(!model.isObjectUpToDateWithProperties());
model.finalizeFromProperties();
ASSERT(model.isObjectUpToDateWithProperties());

model.finalizeFromProperties();
ASSERT(model.isObjectUpToDateWithProperties());
// make an edit through model's ComponentList access
for (auto& body : model.updComponentList<Body>()) {
body.upd_mass_center() = SimTK::Vec3(0);
break;
}
ASSERT(!model.isObjectUpToDateWithProperties());

// make an edit through model's ComponentList access
for (auto& body : model.updComponentList<Body>()) {
body.upd_mass_center() = SimTK::Vec3(0);
break;
}
ASSERT(!model.isObjectUpToDateWithProperties());
SimTK::State dummy_state;

SimTK::State dummy_state;
ASSERT_THROW(ComponentHasNoSystem, model.getSystem());
ASSERT_THROW(ComponentHasNoSystem, model.realizeDynamics(dummy_state));
ASSERT_THROW(ComponentHasNoSystem,
model.computeStateVariableDerivatives(dummy_state));
// should not be able to create a Manager either
ASSERT_THROW(ComponentHasNoSystem,
Manager manager(model));

ASSERT_THROW(ComponentHasNoSystem, model.getSystem());
ASSERT_THROW(ComponentHasNoSystem, model.realizeDynamics(dummy_state));
ASSERT_THROW(ComponentHasNoSystem,
model.computeStateVariableDerivatives(dummy_state));
// should not be able to create a Manager either
ASSERT_THROW(ComponentHasNoSystem,
Manager manager(model));
// get a valid System and corresponding state
SimTK::State state = model.initSystem();
Manager manager(model);
state.setTime(0.0);

// get a valid System and corresponding state
SimTK::State state = model.initSystem();
Manager manager(model);
state.setTime(0.0);
// this should invalidate the System
model.finalizeFromProperties();

// this should invalidate the System
model.finalizeFromProperties();
// verify that finalizeFromProperties() wipes out the underlying System
ASSERT_THROW(ComponentHasNoSystem, model.getSystem());

// verify that finalizeFromProperties() wipes out the underlying System
ASSERT_THROW(ComponentHasNoSystem, model.getSystem());
// should not be able to "trick" the manager into integrating a model
// given a stale but compatible state
ASSERT_THROW(ComponentHasNoSystem, manager.initialize(state));

// should not be able to "trick" the manager into integrating a model
// given a stale but compatible state
ASSERT_THROW(ComponentHasNoSystem, manager.initialize(state));
// once again, get a valid System and corresponding state
state = model.initSystem();

// once again, get a valid System and corresponding state
state = model.initSystem();

// Test for the effects of calling finalizeConnections() on the model
// after initSystem() has been called.
// In this case, there are no changes to the connections to be finalized.
model.finalizeConnections();
// Test for the effects of calling finalizeConnections() on the model
// after initSystem() has been called.
// In this case, there are no changes to the connections to be finalized.
model.finalizeConnections();

// verify that finalizeConnections() does not wipe out the underlying
// System when there are no changes to the connections
auto& sys = model.getSystem();
// verify that finalizeConnections() does not wipe out the underlying
// System when there are no changes to the connections
auto& sys = model.getSystem();

auto elbowInHumerus = new PhysicalOffsetFrame("elbow_in_humerus",
model.getComponent<Body>("./bodyset/r_humerus"),
SimTK::Transform(SimTK::Vec3(0, -0.33, 0)) );
auto elbowInHumerus = new PhysicalOffsetFrame("elbow_in_humerus",
model.getComponent<Body>("./bodyset/r_humerus"),
SimTK::Transform(SimTK::Vec3(0, -0.33, 0)) );

model.addComponent(elbowInHumerus);
model.addComponent(elbowInHumerus);

// establish the new offset frame as part of the model but not
// used by any joints, constraints or forces
state = model.initSystem();
// establish the new offset frame as part of the model but not
// used by any joints, constraints or forces
state = model.initSystem();

// update the elbow Joint and connect its socket to the new frame
Joint& elbow = model.updComponent<Joint>("./jointset/r_elbow");
elbow.connectSocket_parent_frame(*elbowInHumerus);
// update the elbow Joint and connect its socket to the new frame
Joint& elbow = model.updComponent<Joint>("./jointset/r_elbow");
elbow.connectSocket_parent_frame(*elbowInHumerus);

// satisfy the new connections in the model
model.finalizeConnections();
// satisfy the new connections in the model
model.finalizeConnections();

// now finalizing the connections will invalidate the System because
// a Component (the elbow Joint and its connection) was updated
ASSERT_THROW(ComponentHasNoSystem, model.getSystem());
// now finalizing the connections will invalidate the System because
// a Component (the elbow Joint and its connection) was updated
ASSERT_THROW(ComponentHasNoSystem, model.getSystem());

// verify the new connection was made
ASSERT(model.getComponent<Joint>("./jointset/r_elbow")
.getParentFrame().getName() == "elbow_in_humerus");
// verify the new connection was made
ASSERT(model.getComponent<Joint>("./jointset/r_elbow")
.getParentFrame().getName() == "elbow_in_humerus");
}

void testModelTopologyErrors()
TEST_CASE("testModelTopologyErrors")
{
LoadOpenSimLibrary("osimActuators");
Model model("arm26.osim");
model.initSystem();

Expand Down Expand Up @@ -232,7 +220,7 @@ void testModelTopologyErrors()
ASSERT_THROW(JointFramesHaveSameBaseFrame, degenerate.initSystem());
}

void testDoesNotSegfaultWithUnusualConnections()
TEST_CASE("testDoesNotSegfaultWithUnusualConnections")
{
// automated reproduction for bug reported in #3299
//
Expand All @@ -242,6 +230,7 @@ void testDoesNotSegfaultWithUnusualConnections()

try
{
LoadOpenSimLibrary("osimActuators");
OpenSim::Model m;
OpenSim::PhysicalFrame const& groundFrame = m.getGround();

Expand Down
Loading
Loading