Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Nov 15, 2024
1 parent be8802d commit 70c4289
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 4 deletions.
1 change: 1 addition & 0 deletions export/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_subdirectory(src)
add_subdirectory(examples)

# internal executable invoked by CMake to generate the modelDescription.xml from a shared library
add_executable(descriptionGenerator "descriptionGenerator.cpp")
if (UNIX)
target_link_libraries(descriptionGenerator PRIVATE dl)
Expand Down
8 changes: 6 additions & 2 deletions export/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ function(make_test modelIdentifier sources)
add_executable(${modelIdentifier} ${sources} "$<TARGET_OBJECTS:fmu4cpp>")
add_test(NAME ${modelIdentifier} COMMAND ${modelIdentifier})
target_link_libraries(${modelIdentifier} PUBLIC Catch2::Catch2WithMain)
target_compile_definitions(${modelIdentifier} PRIVATE FMU4CPP_MODEL_IDENTIFIER="${modelIdentifier}")
target_compile_definitions(${modelIdentifier}
PRIVATE
FMU4CPP_MODEL_IDENTIFIER="${modelIdentifier}"
TEST_CASE_RESOURCE_LOCATION="${CMAKE_CURRENT_SOURCE_DIR}/resources"
)
target_include_directories(${modelIdentifier}
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/../include"
"${CMAKE_CURRENT_SOURCE_DIR}/../src"
)


endfunction()

make_test(basic_test basic_test.cpp)
make_test(test_resource test_resource.cpp)
46 changes: 44 additions & 2 deletions export/tests/basic_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ std::unique_ptr<fmu4cpp::fmu_base> fmu4cpp::createInstance(const std::string &in
return std::make_unique<Model>(instanceName, fmuResourceLocation);
}

TEST_CASE("basic") {
TEST_CASE("basic_test") {

const auto instance = fmu4cpp::createInstance("", "");

Expand Down Expand Up @@ -94,4 +94,46 @@ TEST_CASE("basic") {
REQUIRE(str->get() == "0");

instance->terminate();
}
}

TEST_CASE("wrong call order") {

const auto instance = fmu4cpp::createInstance("", "");

double t = 0;
double dt = 0.1;

auto real = instance->get_real_variable("myReal");
REQUIRE(real);
auto integer = instance->get_int_variable("myInteger");
REQUIRE(integer);
auto boolean = instance->get_bool_variable("myBoolean");
REQUIRE(boolean);
auto str = instance->get_string_variable("myString");
REQUIRE(str);

instance->setup_experiment(t, {}, {});
instance->enter_initialisation_mode();
instance->exit_initialisation_mode();

int i = 0;
while (t < 10) {
instance->do_step(t, dt);

REQUIRE(real->get() == Catch::Approx(t));
REQUIRE(boolean->get() == (i % 2 == 0));
REQUIRE(integer->get() == ++i);
REQUIRE(str->get() == std::to_string(i));

t += dt;
}

instance->reset();

REQUIRE(real->get() == 0);
REQUIRE(boolean->get() == false);
REQUIRE(integer->get() == 0);
REQUIRE(str->get() == "0");

instance->terminate();
}
1 change: 1 addition & 0 deletions export/tests/resources/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello resource!
79 changes: 79 additions & 0 deletions export/tests/test_resource.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_approx.hpp>

#include <fmu4cpp/fmu_base.hpp>
#include <fstream>

class Model : public fmu4cpp::fmu_base {

public:
Model(const std::string &instanceName, const std::filesystem::path &resources)
: fmu_base(instanceName, resources) {

std::ifstream file(resources.string() + "/data.txt");

std::string data;
std::getline(file, data);

register_variable(string("data", [data] { return data; })
.setCausality(fmu4cpp::causality_t::OUTPUT));

Model::reset();
}

bool do_step(double currentTime, double dt) override {

return true;
}

void reset() override {
// do nothing
}

private:
bool boolean_;
int integer_;
double real_;
std::string str_;
};

fmu4cpp::model_info fmu4cpp::get_model_info() {
model_info m;
m.modelIdentifier = FMU4CPP_MODEL_IDENTIFIER;
return m;
}

std::unique_ptr<fmu4cpp::fmu_base> fmu4cpp::createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation) {
return std::make_unique<Model>(instanceName, fmuResourceLocation);
}

TEST_CASE("model with resource") {

std::string expected{"Hello resource!"};

const auto instance = fmu4cpp::createInstance("myInstance", TEST_CASE_RESOURCE_LOCATION);

auto strVar = instance->get_string_variable("data");
REQUIRE(strVar);

double t = 0;
double dt = 0.1;
instance->setup_experiment(t, {}, {});
instance->enter_initialisation_mode();
instance->exit_initialisation_mode();

while (t < 10) {
instance->do_step(t, dt);

REQUIRE(strVar->get() == expected);

t += dt;
}

instance->reset();

REQUIRE(strVar->get() == expected);

instance->terminate();
}

0 comments on commit 70c4289

Please sign in to comment.