Skip to content

Commit

Permalink
Remove template, add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Cornu committed Jan 29, 2024
1 parent 316887b commit 4a00c66
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
3 changes: 1 addition & 2 deletions src/ivoc/ivocvect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3876,8 +3876,7 @@ void Vector_reg() {
#endif
}

template <typename F>
int nrn_mlh_gsort(double* vec, int* base_ptr, int total_elems, F cmp) {
int nrn_mlh_gsort(double* vec, int* base_ptr, int total_elems, int (*cmp)(double, double)) {
std::sort(base_ptr, base_ptr + total_elems, [&](const int& a, const int& b) {
return cmp(vec[a], vec[b]) < 0;
});
Expand Down
3 changes: 1 addition & 2 deletions src/oc/oc_ansi.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ std::FILE* hoc_obj_file_arg(int i);
void hoc_reg_nmodl_text(int type, const char* txt);
void hoc_reg_nmodl_filename(int type, const char* filename);
std::size_t nrn_mallinfo(int item);
template <typename F>
int nrn_mlh_gsort(double* vec, int* base_ptr, int total_elems, F cmp);
int nrn_mlh_gsort(double* vec, int* base_ptr, int total_elems, int (*cmp)(double, double));
void state_discontinuity(int i, double* pd, double d);

IvocVect* vector_arg(int);
Expand Down
42 changes: 30 additions & 12 deletions test/unit_tests/iovec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,41 @@

#include <catch2/catch.hpp>

// This function is the one that is used in all nrn-modeldb-ci
// Keep as is
int cmpdfn(double a, double b) {
return ((a) <= (b)) ? (((a) == (b)) ? 0 : -1) : 1;
}

TEST_CASE("Test nrn_mlh_gsort output", "[nrn_gsort]") {
std::vector<double> input{1.2, -2.5, 5.1};

std::vector<int> indices(input.size());
// all values from 0 to size - 1
std::iota(indices.begin(), indices.end(), 0);
{
std::vector<int> indices(input.size());
// all values from 0 to size - 1
std::iota(indices.begin(), indices.end(), 0);

// for comparison
auto sorted_input = input;
std::sort(sorted_input.begin(), sorted_input.end());

SECTION("Test sorting") {
nrn_mlh_gsort(input.data(), indices.data(), input.size(), cmpdfn);
for (auto i = 0; i < input.size(); ++i) {
REQUIRE(sorted_input[i] == input[indices[i]]);
}
}
}

// for comparison
auto sorted_input = input;
std::sort(sorted_input.begin(), sorted_input.end());
{
std::vector<int> indices{2, 1, 1};
std::vector<int> expected_result{1, 1, 2}; // as -2,5 < 5.1

SECTION("Test sorting") {
nrn_mlh_gsort(input.data(), indices.data(), input.size(), [](double a, double b) {
return a < b;
});
for (auto i = 0; i < input.size(); ++i) {
REQUIRE(sorted_input[i] == input[indices[i]]);
SECTION("Test sorting with repeted indices") {
nrn_mlh_gsort(input.data(), indices.data(), input.size(), cmpdfn);
for (auto i = 0; i < input.size(); ++i) {
REQUIRE(indices[i] == expected_result[i]);
}
}
}
}

0 comments on commit 4a00c66

Please sign in to comment.