Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Jul 3, 2023
1 parent f017e9a commit b84077c
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tests/ecos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ add_test_executable(test_variable_identifier)
add_test_executable(test_connection)
add_test_executable(test_property)
add_test_executable(test_runner)
add_test_executable(test_ssp_parser)
add_test_executable(test_unzipper)
target_link_libraries(test_unzipper PRIVATE libzip::zip)

if (MSVC AND ECOS_BUILD_CLIB)
add_test_executable(test_clib)
target_link_libraries(test_clib PUBLIC libecosc)
endif()
endif()
116 changes: 116 additions & 0 deletions tests/ecos/test_ssp_parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>

#include "ecos/ssp/ssp.hpp"

using namespace ecos;

namespace
{

void checkSystemStructure(const ssp::SystemStructureDescription& ssd)
{
CHECK(ssd.name == "QuarterTruck");

const auto system = ssd.system;
CHECK(system.name == "QuarterTruckSystem");

CHECK(system.connections.size() == 4);

const auto components = system.elements.components;
CHECK(components.size() == 3);

REQUIRE(components.count("chassis"));
REQUIRE(components.count("wheel"));
REQUIRE(components.count("ground"));

const ssp::Component& chassis = components.at("chassis");
CHECK(chassis.source == "resources/chassis.fmu");
REQUIRE(chassis.connectors.size() == 2);
CHECK(chassis.connectors.at("p.e").name == "p.e");
CHECK(chassis.connectors.at("p.e").kind == "output");
CHECK(!chassis.connectors.at("p.e").type.unit.has_value());
CHECK(chassis.connectors.at("p.e").type.typeName() == "Real");
CHECK(chassis.connectors.at("p.f").name == "p.f");
CHECK(chassis.connectors.at("p.f").kind == "input");
CHECK(!chassis.connectors.at("p.f").type.unit.has_value());
CHECK(chassis.connectors.at("p.f").type.typeName() == "Real");

const ssp::Component& wheel = components.at("wheel");
CHECK(wheel.source == "resources/wheel.fmu");
REQUIRE(wheel.connectors.size() == 4);
CHECK(wheel.connectors.at("p.f").name == "p.f");
CHECK(wheel.connectors.at("p1.e").name == "p1.e");
CHECK(wheel.connectors.at("p.e").name == "p.e");
CHECK(wheel.connectors.at("p1.f").name == "p1.f");

CHECK(wheel.connectors.at("p.e").type == wheel.connectors.at("p1.f").type);

const auto& wheelParameters = wheel.parameterSets;
REQUIRE(wheelParameters.at("initialValues").parameters.size() == 3);

const ssp::Component& ground = components.at("ground");
CHECK(ground.source == "resources/ground.fmu");
CHECK(ground.connectors.size() == 2);

const auto& groundParameters = ground.parameterSets;
REQUIRE(groundParameters.empty());

REQUIRE(system.elements.parameterSets.size() == 1);
REQUIRE(system.elements.parameterSets.count("initialValues"));
const auto& initialValues = system.elements.parameterSets.at("initialValues");
REQUIRE(initialValues.size() == 2);
REQUIRE(initialValues.count(chassis));
REQUIRE(initialValues.at(chassis).size() == 3);
REQUIRE(initialValues.count(wheel));
REQUIRE(initialValues.at(wheel).size() == 3);
}

} // namespace

TEST_CASE("test_ssp_parser_archive")
{
std::filesystem::path temporal;

{
std::shared_ptr<temp_dir> tmp;

{
const auto quarterTruckArchive = std::string(DATA_FOLDER) + "/ssp/quarter_truck/quarter-truck.ssp";
ssp::SystemStructureDescription desc(quarterTruckArchive);
checkSystemStructure(desc);
tmp = desc.get_temp_dir();
temporal = tmp->path();

const auto& groundComponent = desc.system.elements.components.at("ground");
const auto groundFmu = desc.file(groundComponent.source);
REQUIRE(std::filesystem::exists(groundFmu));
REQUIRE(groundFmu.extension().string() == ".fmu");
}

REQUIRE(std::filesystem::exists(temporal));
}

REQUIRE(!std::filesystem::exists(temporal));
}

TEST_CASE("test_ssp_parser_folder")
{
const auto quarterTruckFolder = std::string(DATA_FOLDER) + "/ssp/quarter_truck";
ssp::SystemStructureDescription desc(quarterTruckFolder);
checkSystemStructure(desc);

const auto& ex = desc.defaultExperiment;
REQUIRE(ex);
CHECK_THAT(*ex->start, Catch::Matchers::WithinRel(10.));

const auto& annotations = ex->annotations;
REQUIRE(annotations.size() == 1);
REQUIRE(annotations.front().type == "com.opensimulationplatform");
const auto& annotationNode = annotations.front().node;
const auto algorithmNode = annotationNode.child("osp:Algorithm");
REQUIRE(algorithmNode);
const auto fixedStepNode = algorithmNode.child("osp:FixedStepAlgorithm");
REQUIRE(fixedStepNode);
CHECK_THAT(fixedStepNode.attribute("baseStepSize").as_double(), Catch::Matchers::WithinRel(1e-4));
}

0 comments on commit b84077c

Please sign in to comment.