Skip to content

Commit e948130

Browse files
committed
add get_value_refs method and corresponding tests for uniqueness
1 parent 26a683a commit e948130

6 files changed

Lines changed: 53 additions & 9 deletions

File tree

export/include/fmu4cpp/fmu_base.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ namespace fmu4cpp {
226226
return false;
227227
}
228228

229+
[[nodiscard]] std::vector<unsigned int> get_value_refs() const;
230+
229231
virtual ~fmu_base() = default;
230232

231233
protected:

export/src/fmu4cpp/fmu_base.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,31 @@ namespace fmu4cpp {
6666
return indentedString;
6767
}
6868

69-
IntVariable fmu_base::integer(const std::string &name, int *ptr, const std::function<void(int)>& onChange) {
69+
IntVariable fmu_base::integer(const std::string &name, int *ptr, const std::function<void(int)> &onChange) {
7070
return {name, static_cast<unsigned int>(numVariables_), ++numVariables_, ptr, onChange};
7171
}
7272

7373
IntVariable fmu_base::integer(const std::string &name, const std::function<int()> &getter, const std::optional<std::function<void(int)>> &setter) {
7474
return {name, static_cast<unsigned int>(numVariables_), ++numVariables_, getter, setter};
7575
}
7676

77-
RealVariable fmu_base::real(const std::string &name, double *ptr, const std::function<void(double)>& onChange) {
77+
RealVariable fmu_base::real(const std::string &name, double *ptr, const std::function<void(double)> &onChange) {
7878
return {name, static_cast<unsigned int>(numVariables_), ++numVariables_, ptr, onChange};
7979
}
8080

8181
RealVariable fmu_base::real(const std::string &name, const std::function<double()> &getter, const std::optional<std::function<void(double)>> &setter) {
8282
return {name, static_cast<unsigned int>(numVariables_), ++numVariables_, getter, setter};
8383
}
8484

85-
BoolVariable fmu_base::boolean(const std::string &name, bool *ptr, const std::function<void(bool)>& onChange) {
85+
BoolVariable fmu_base::boolean(const std::string &name, bool *ptr, const std::function<void(bool)> &onChange) {
8686
return {name, static_cast<unsigned int>(numVariables_), ++numVariables_, ptr, onChange};
8787
}
8888

8989
BoolVariable fmu_base::boolean(const std::string &name, const std::function<bool()> &getter, const std::optional<std::function<void(bool)>> &setter) {
9090
return {name, static_cast<unsigned int>(numVariables_), ++numVariables_, getter, setter};
9191
}
9292

93-
StringVariable fmu_base::string(const std::string &name, std::string *ptr, const std::function<void(std::string)>& onChange) {
93+
StringVariable fmu_base::string(const std::string &name, std::string *ptr, const std::function<void(std::string)> &onChange) {
9494
return {name, static_cast<unsigned int>(numVariables_), ++numVariables_, ptr, onChange};
9595
}
9696

@@ -149,6 +149,16 @@ namespace fmu4cpp {
149149
return std::to_string(fnv1a(ss.str()));
150150
}
151151

152+
std::vector<unsigned> fmu_base::get_value_refs() const {
153+
std::vector<unsigned int> indices;
154+
auto allVars = collect(integers_, reals_, booleans_, strings_);
155+
indices.reserve(allVars.size());
156+
for (auto v: allVars) {
157+
indices.emplace_back(v->value_reference());
158+
}
159+
160+
return indices;
161+
}
152162

153163
std::string fmu_base::make_description_v2() const {
154164

export/tests/fmi2/array_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <cstdarg>
99
#include <iostream>
10+
#include <unordered_set>
1011

1112
#include <fmu4cpp/fmu_base.hpp>
1213

@@ -61,6 +62,13 @@ TEST_CASE("test_array") {
6162
Model model({});
6263
const auto guid = model.guid();
6364

65+
auto vrs = model.get_value_refs();
66+
REQUIRE(vrs.size() == 4 + 1);// 4 variables + 1 for time
67+
std::unordered_set<unsigned int> unique_vrs(vrs.begin(), vrs.end());
68+
69+
// Check that all are unique
70+
REQUIRE(unique_vrs.size() == vrs.size());
71+
6472
std::cout << model.make_description_v2() << std::endl;
6573

6674
fmi2CallbackFunctions callbackFunctions;

export/tests/fmi2/identity_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <catch2/catch_approx.hpp>
77
#include <catch2/catch_test_macros.hpp>
88
#include <cstdarg>
9+
#include <unordered_set>
910

1011
#include <fmu4cpp/fmu_base.hpp>
1112

@@ -161,6 +162,14 @@ TEST_CASE("test_identity") {
161162
Model model({});
162163
const auto guid = model.guid();
163164

165+
auto vrs = model.get_value_refs();
166+
REQUIRE(vrs.size() == 8 + 1);// 8 variables + 1 for time
167+
std::unordered_set<unsigned int> unique_vrs(vrs.begin(), vrs.end());
168+
169+
// Check that all are unique
170+
REQUIRE(unique_vrs.size() == vrs.size());
171+
172+
164173
const auto realIn = model.get_real_variable("realIn");
165174
const auto stringIn = model.get_string_variable("stringIn");
166175
const auto integerIn = model.get_int_variable("integerIn");

export/tests/fmi3/array_test.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
#include "fmi3/fmi3Functions.h"
32
#include "fmi3/fmi3Functions.h"
43

54
#include <catch2/catch_approx.hpp>
@@ -8,6 +7,7 @@
87

98
#include <cstdarg>
109
#include <iostream>
10+
#include <unordered_set>
1111

1212
#include <fmu4cpp/fmu_base.hpp>
1313

@@ -57,13 +57,20 @@ TEST_CASE("test_array") {
5757

5858
Model model({});
5959
const auto guid = model.guid();
60-
60+
61+
auto vrs = model.get_value_refs();
62+
REQUIRE(vrs.size() == 4 + 1);// 4 variables + 1 for time
63+
std::unordered_set<unsigned int> unique_vrs(vrs.begin(), vrs.end());
64+
65+
// Check that all are unique
66+
REQUIRE(unique_vrs.size() == vrs.size());
67+
6168
auto c = fmi3InstantiateCoSimulation("array", guid.c_str(), "", false, true, false, false, nullptr, 0, nullptr, fmilogger, nullptr);
6269
REQUIRE(c);
63-
64-
REQUIRE(fmi3EnterInitializationMode(c,false, 0, 0, false, 0) == fmi3OK);
70+
71+
REQUIRE(fmi3EnterInitializationMode(c, false, 0, 0, false, 0) == fmi3OK);
6572
REQUIRE(fmi3ExitInitializationMode(c) == fmi3OK);
66-
73+
6774

6875
std::vector<double> values(4);
6976
for (int i = 1; i <= 4; i++) {// account for time

export/tests/fmi3/identity_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <catch2/catch_test_macros.hpp>
88
#include <cstdarg>
99
#include <iostream>
10+
#include <unordered_set>
1011

1112
#include <fmu4cpp/fmu_base.hpp>
1213

@@ -158,6 +159,13 @@ TEST_CASE("test_identity") {
158159
Model model({});
159160
const auto guid = model.guid();
160161

162+
auto vrs = model.get_value_refs();
163+
REQUIRE(vrs.size() == 8 + 1);// 8 variables + 1 for time
164+
std::unordered_set<unsigned int> unique_vrs(vrs.begin(), vrs.end());
165+
166+
// Check that all are unique
167+
REQUIRE(unique_vrs.size() == vrs.size());
168+
161169
const auto realIn = model.get_real_variable("realIn");
162170
const auto stringIn = model.get_string_variable("stringIn");
163171
const auto integerIn = model.get_int_variable("integerIn");

0 commit comments

Comments
 (0)