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

Fix mock_slave and lots of magic numbers #539

Merged
merged 1 commit into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions test/cpp/async_slave_mockup_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ void run_test(std::shared_ptr<cse::async_slave> (*make_async)(std::shared_ptr<cs
// slave's sole real output variable.
std::vector<future<cse::async_slave::variable_values>> getResults;
for (const auto& slave : asyncSlaves) {
const cse::value_reference realOutIndex = 0;
getResults.push_back(
slave->get_variables({&realOutIndex, 1}, {}, {}, {}));
slave->get_variables({&mock_slave::real_out_reference, 1}, {}, {}, {}));
}
std::vector<double> values; // To be filled with one value per slave
for (auto& r : getResults) {
Expand All @@ -90,10 +89,9 @@ void run_test(std::shared_ptr<cse::async_slave> (*make_async)(std::shared_ptr<cs

std::vector<future<void>> setResults;
for (int i = 0; i < numSlaves; ++i) {
const cse::value_reference realInIndex = 1;
setResults.push_back(
asyncSlaves[i]->set_variables(
{&realInIndex, 1}, {&values[i], 1},
{&mock_slave::real_in_reference, 1}, {&values[i], 1},
{}, {},
{}, {},
{}, {}));
Expand Down
80 changes: 39 additions & 41 deletions test/cpp/fixed_step_algorithm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main()
constexpr cse::time_point startTime;
constexpr cse::time_point midTime = cse::to_time_point(0.6);
constexpr cse::time_point endTime = cse::to_time_point(1.0);
constexpr cse::duration stepSize = cse::to_duration(0.1);
constexpr cse::duration stepSize = cse::to_duration(0.05);

// Set up execution
auto execution = cse::execution(
Expand All @@ -40,26 +40,38 @@ int main()
auto observer = std::make_shared<cse::last_value_observer>();
execution.add_observer(observer);

const cse::value_reference realOutRef = 0;
const cse::value_reference realInRef = 1;
const cse::value_reference realOutRef = mock_slave::real_out_reference;
const cse::value_reference realInRef = mock_slave::real_in_reference;


// Add slaves to it
for (int i = 0; i < numSlaves; ++i) {
std::vector<cse::simulator_index> slaves;
slaves.push_back(
execution.add_slave(
cse::make_pseudo_async(std::make_unique<mock_slave>([](double x) { return x + 1.234; })),
"slave" + std::to_string(i));
if (i > 0) {
execution.add_connection(
std::make_shared<cse::scalar_connection>(
cse::variable_id{i - 1, cse::variable_type::real, realOutRef},
cse::variable_id{i, cse::variable_type::real, realInRef}));
}
cse::make_pseudo_async(
std::make_unique<mock_slave>([](cse::time_point t, double) {
return cse::to_double_time_point(t);
})),
"clock_slave"));
for (int i = 1; i < numSlaves; ++i) {
slaves.push_back(
execution.add_slave(
cse::make_pseudo_async(
std::make_unique<mock_slave>([](double x) {
return x + 1.234;
})),
"adder_slave" + std::to_string(i)));
execution.add_connection(
std::make_shared<cse::scalar_connection>(
cse::variable_id{slaves[i - 1], cse::variable_type::real, realOutRef},
cse::variable_id{slaves[i], cse::variable_type::real, realInRef}));
}

// Add an observer that watches the last slave
auto observer2 = std::make_shared<cse::time_series_observer>();
execution.add_observer(observer2);
observer2->start_observing(cse::variable_id{9, cse::variable_type::real, realOutRef});
observer2->start_observing(
cse::variable_id{slaves.back(), cse::variable_type::real, realOutRef});

// Run simulation
auto simResult = execution.simulate_until(midTime);
Expand All @@ -70,40 +82,26 @@ int main()
simResult = execution.simulate_until(endTime);
REQUIRE(simResult.get());


double realOutValue = -1.0;
double realInValue = -1.0;

for (int j = 0; j < numSlaves; j++) {
double lastRealOutValue = realOutValue;
observer->get_real(j, gsl::make_span(&realOutRef, 1), gsl::make_span(&realOutValue, 1));
observer->get_real(j, gsl::make_span(&realInRef, 1), gsl::make_span(&realInValue, 1));
if (j > 0) {
// Check that real input of slave j has same value as real output of slave j - 1
REQUIRE(std::fabs(realInValue - lastRealOutValue) < 1.0e-9);
}
}

// Check that time, step number and output values increase monotonically
const int numSamples = 10;
double realValues[numSamples];
cse::step_number steps[numSamples];
cse::time_point timeValues[numSamples];
observer2->get_real_samples(9, realOutRef, 1, gsl::make_span(realValues, numSamples), gsl::make_span(steps, numSamples), gsl::make_span(timeValues, numSamples));
cse::step_number lastStep = -1;
double lastValue = -1.0;
for (int k = 0; k < numSamples; k++) {
REQUIRE(steps[k] > lastStep);
lastStep = steps[k];

REQUIRE(realValues[k] > lastValue);
lastValue = realValues[k];

if (k > 0) {
cse::duration diff = timeValues[k] - timeValues[k - 1];
REQUIRE(diff == stepSize);
}
observer2->get_real_samples(
slaves.back(),
realOutRef,
numSlaves, // changes won't propagate to the last slave until the numSlaves'th step
gsl::make_span(realValues, numSamples),
gsl::make_span(steps, numSamples),
gsl::make_span(timeValues, numSamples));

for (int k = 1; k < numSamples; k++) {
REQUIRE(steps[k] > steps[k-1]);
REQUIRE(realValues[k] > realValues[k-1]);
REQUIRE(timeValues[k] - timeValues[k-1] == stepSize);
}

// Run for another period with an RTF target > 1
constexpr auto finalTime = cse::to_time_point(2.0);
constexpr double rtfTarget = 2.25;
execution.enable_real_time_simulation();
Expand Down
62 changes: 34 additions & 28 deletions test/cpp/last_value_observer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,64 +45,70 @@ int main()

execution.step();

const cse::value_reference outIndex = 0;
const cse::value_reference inIndex = 1;
const cse::value_reference realOutRef = mock_slave::real_out_reference;
const cse::value_reference realInRef = mock_slave::real_in_reference;
const cse::value_reference intOutRef = mock_slave::integer_out_reference;
const cse::value_reference intInRef = mock_slave::integer_in_reference;
const cse::value_reference boolOutRef = mock_slave::boolean_out_reference;
const cse::value_reference boolInRef = mock_slave::boolean_in_reference;
const cse::value_reference stringOutRef = mock_slave::string_out_reference;
const cse::value_reference stringInRef = mock_slave::string_in_reference;

double realInValue = -1.0;
double realOutValue = -1.0;
observer->get_real(sim, gsl::make_span(&inIndex, 1), gsl::make_span(&realInValue, 1));
observer->get_real(sim, gsl::make_span(&outIndex, 1), gsl::make_span(&realOutValue, 1));
observer->get_real(sim, gsl::make_span(&realInRef, 1), gsl::make_span(&realInValue, 1));
observer->get_real(sim, gsl::make_span(&realOutRef, 1), gsl::make_span(&realOutValue, 1));
REQUIRE(std::fabs(realInValue - 0.0) < 1.0e-9);
REQUIRE(std::fabs(realOutValue - 1.234) < 1.0e-9);

int intInValue = -1;
int intOutValue = -1;
observer->get_integer(sim, gsl::make_span(&inIndex, 1), gsl::make_span(&intInValue, 1));
observer->get_integer(sim, gsl::make_span(&outIndex, 1), gsl::make_span(&intOutValue, 1));
observer->get_integer(sim, gsl::make_span(&intInRef, 1), gsl::make_span(&intInValue, 1));
observer->get_integer(sim, gsl::make_span(&intOutRef, 1), gsl::make_span(&intOutValue, 1));
REQUIRE(intInValue == 0);
REQUIRE(intOutValue == 1);

bool boolInValue = false;
bool boolOutValue = true;
observer->get_boolean(sim, gsl::make_span(&inIndex, 1), gsl::make_span(&boolInValue, 1));
observer->get_boolean(sim, gsl::make_span(&outIndex, 1), gsl::make_span(&boolOutValue, 1));
REQUIRE(boolInValue == true);
REQUIRE(boolOutValue == false);
bool boolInValue = true;
bool boolOutValue = false;
observer->get_boolean(sim, gsl::make_span(&boolInRef, 1), gsl::make_span(&boolInValue, 1));
observer->get_boolean(sim, gsl::make_span(&boolOutRef, 1), gsl::make_span(&boolOutValue, 1));
REQUIRE(boolInValue == false);
REQUIRE(boolOutValue == true);

std::string stringInValue;
std::string stringOutValue;
observer->get_string(sim, gsl::make_span(&inIndex, 1), gsl::make_span(&stringInValue, 1));
observer->get_string(sim, gsl::make_span(&outIndex, 1), gsl::make_span(&stringOutValue, 1));
observer->get_string(sim, gsl::make_span(&stringInRef, 1), gsl::make_span(&stringInValue, 1));
observer->get_string(sim, gsl::make_span(&stringOutRef, 1), gsl::make_span(&stringOutValue, 1));
REQUIRE(stringInValue == "");
REQUIRE(stringOutValue == "bar");

auto manipulator = std::make_shared<cse::override_manipulator>();
execution.add_manipulator(manipulator);

manipulator->override_real_variable(sim, inIndex, 2.0);
manipulator->override_integer_variable(sim, inIndex, 2);
manipulator->override_boolean_variable(sim, inIndex, false);
manipulator->override_string_variable(sim, inIndex, "foo");
manipulator->override_real_variable(sim, realInRef, 2.0);
manipulator->override_integer_variable(sim, intInRef, 2);
manipulator->override_boolean_variable(sim, boolInRef, true);
manipulator->override_string_variable(sim, stringInRef, "foo");

execution.step();

observer->get_real(sim, gsl::make_span(&inIndex, 1), gsl::make_span(&realInValue, 1));
observer->get_real(sim, gsl::make_span(&outIndex, 1), gsl::make_span(&realOutValue, 1));
observer->get_real(sim, gsl::make_span(&realInRef, 1), gsl::make_span(&realInValue, 1));
observer->get_real(sim, gsl::make_span(&realOutRef, 1), gsl::make_span(&realOutValue, 1));
REQUIRE(std::fabs(realInValue - 2.0) < 1.0e-9);
REQUIRE(std::fabs(realOutValue - 3.234) < 1.0e-9);

observer->get_integer(sim, gsl::make_span(&inIndex, 1), gsl::make_span(&intInValue, 1));
observer->get_integer(sim, gsl::make_span(&outIndex, 1), gsl::make_span(&intOutValue, 1));
observer->get_integer(sim, gsl::make_span(&intInRef, 1), gsl::make_span(&intInValue, 1));
observer->get_integer(sim, gsl::make_span(&intOutRef, 1), gsl::make_span(&intOutValue, 1));
REQUIRE(intInValue == 2);
REQUIRE(intOutValue == 3);

observer->get_boolean(sim, gsl::make_span(&inIndex, 1), gsl::make_span(&boolInValue, 1));
observer->get_boolean(sim, gsl::make_span(&outIndex, 1), gsl::make_span(&boolOutValue, 1));
REQUIRE(boolInValue == false);
REQUIRE(boolOutValue == true);
observer->get_boolean(sim, gsl::make_span(&boolInRef, 1), gsl::make_span(&boolInValue, 1));
observer->get_boolean(sim, gsl::make_span(&boolOutRef, 1), gsl::make_span(&boolOutValue, 1));
REQUIRE(boolInValue == true);
REQUIRE(boolOutValue == false);

observer->get_string(sim, gsl::make_span(&inIndex, 1), gsl::make_span(&stringInValue, 1));
observer->get_string(sim, gsl::make_span(&outIndex, 1), gsl::make_span(&stringOutValue, 1));
observer->get_string(sim, gsl::make_span(&stringInRef, 1), gsl::make_span(&stringInValue, 1));
observer->get_string(sim, gsl::make_span(&stringOutRef, 1), gsl::make_span(&stringOutValue, 1));
REQUIRE(stringInValue == "foo");
REQUIRE(stringOutValue == "foobar");

Expand Down
Loading