Skip to content

Commit

Permalink
Added unit test for std::function<double()> elements
Browse files Browse the repository at this point in the history
  • Loading branch information
jorblancoa committed Apr 28, 2023
1 parent ede01d9 commit 48b3526
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/data/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@ void Node::fill_data(std::vector<float>::iterator it) {
}

void Node::refresh_pointers(std::function<double*(double*)> refresh_function) {
std::transform(elements_.begin(), elements_.end(), elements_.begin(), refresh_function);
if (!elements_.empty()) {
std::transform(elements_.begin(), elements_.end(), elements_.begin(), refresh_function);
} else if (!element_handles_.empty()) {
std::transform(element_handles_.begin(),
element_handles_.end(),
element_handles_.begin(),
[&refresh_function](auto const& elem) -> std::function<double()> {
return [elem, refresh_function]() -> double {
double value = elem();
double* refreshed_value = refresh_function(&value);
return *refreshed_value;
};
});
}
}

} // namespace sonata
Expand Down
35 changes: 33 additions & 2 deletions tests/unit/test_node.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <catch2/catch.hpp>
#include <memory>
#include <data/node.h>
#include <data/soma_node.h>
#include <memory>
#include <spdlog/spdlog.h>

using namespace bbp::sonata;
Expand All @@ -26,7 +26,7 @@ SCENARIO("Test Node class", "[Node]") {
REQUIRE_NOTHROW(node.refresh_pointers(&square));
}

WHEN("We add a element") {
WHEN("We add a raw pointer element") {
std::vector<double> elements = {10, 11, 12, 13, 14};
size_t i = 0;
for (auto& element : elements) {
Expand All @@ -53,6 +53,37 @@ SCENARIO("Test Node class", "[Node]") {
REQUIRE(result == compare);
}
}

WHEN("We add a std::function<double()> element") {
std::vector<std::function<double()>> elements = {[]() { return 10.0; },
[]() { return 11.0; },
[]() { return 12.0; }};
size_t i = 0;
for (auto& element_handle : elements) {
node.add_element(element_handle, i);
++i;
}
THEN("Number of elements is 3") {
REQUIRE(node.get_num_elements() == 3);
}
THEN("The element_ids are") {
std::vector<uint32_t> compare = {0, 1, 2};
REQUIRE(node.get_element_ids() == compare);
}
THEN("fill_data will return something correct") {
std::vector<float> result(3, -1.0);
node.fill_data(result.begin());
std::vector<float> compare = {10.0, 11.0, 12.0};
REQUIRE(result == compare);
}
THEN("refresh_pointers will call the function on all elements") {
node.refresh_pointers(&square);
std::vector<float> compare{100, 121, 144};
std::vector<float> result(3, -1);
node.fill_data(result.begin());
REQUIRE(result == compare);
}
}
}

GIVEN("An instance of a soma node") {
Expand Down

0 comments on commit 48b3526

Please sign in to comment.