Skip to content

Commit

Permalink
add array test
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Dec 3, 2024
1 parent 4edacd7 commit 50c0612
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions export/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function(make_test modelIdentifier sources)

endfunction()

make_test(array_test array_test.cpp)
make_test(basic_test basic_test.cpp)
make_test(identity_test identity_test.cpp)
make_test(test_resource test_resource.cpp)
99 changes: 99 additions & 0 deletions export/tests/array_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

#include "catch2/matchers/catch_matchers_vector.hpp"
#include "fmi2/fmi2Functions.h"


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

#include <fmu4cpp/fmu_base.hpp>

class Model : public fmu4cpp::fmu_base {

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

for (int i = 0; i < reals_.size(); i++) {
register_variable(
real("reals" + std::to_string(i), [this, i] { return reals_[i]; }, [this, i](double val) { reals_[i] = val; }).setCausality(fmu4cpp::causality_t::PARAMETER).setVariability(fmu4cpp::variability_t::TUNABLE));
}

Model::reset();
}

bool do_step(double currentTime, double dt) override {
return true;
}

void reset() override {
reals_.assign({1, 2, 3, 4});
}

private:
std::vector<double> reals_;
};

fmu4cpp::model_info fmu4cpp::get_model_info() {
model_info info;
info.modelName = "Array";
info.description = "A simple model with arrays";
info.modelIdentifier = FMU4CPP_MODEL_IDENTIFIER;
return info;
}

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

void fmilogger(fmi2Component, fmi2String instanceName, fmi2Status status, fmi2String category, fmi2String message, ...) {
va_list args;
va_start(args, message);
char msgstr[1024];
sprintf(msgstr, "%i: [%s] %s\n", status, instanceName, message);
printf(msgstr, args);
va_end(args);
}

TEST_CASE("test_array") {

Model model("", "");
const auto guid = model.guid();

fmi2CallbackFunctions callbackFunctions;
callbackFunctions.logger = &fmilogger;

auto c = fmi2Instantiate("array", fmi2CoSimulation, guid.c_str(), "", &callbackFunctions, false, true);
REQUIRE(c);

REQUIRE(fmi2EnterInitializationMode(c) == fmi2OK);
REQUIRE(fmi2ExitInitializationMode(c) == fmi2OK);
REQUIRE(fmi2SetupExperiment(c, false, 0, 0, false, 0) == fmi2OK);

std::vector<double> values(4);
for (int i = 0; i < 4; i++) {
fmi2ValueReference ref = i;
fmi2GetReal(c, &ref, 1, &values[i]);
}
REQUIRE(values == std::vector<double>{1, 2, 3, 4});

for (int i = 0; i < 4; i++) {
fmi2ValueReference ref = i;
fmi2Real val = 9;
fmi2SetReal(c, &ref, 1, &val);
}

for (int i = 0; i < 4; i++) {
fmi2ValueReference ref = i;
fmi2GetReal(c, &ref, 1, &values[i]);
}
REQUIRE(values == std::vector<double>{9, 9, 9, 9});


REQUIRE(fmi2Terminate(c) == fmi2OK);

fmi2FreeInstance(c);
}

0 comments on commit 50c0612

Please # to comment.