NEURON
iovec.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <numeric>
3 #include <vector>
4 
5 #include "oc_ansi.h"
6 
7 #include <catch2/catch_test_macros.hpp>
8 
9 // This function is the one that is used in all nrn-modeldb-ci
10 // Keep as is
11 int cmpdfn(double a, double b) {
12  return ((a) <= (b)) ? (((a) == (b)) ? 0 : -1) : 1;
13 }
14 
15 TEST_CASE("Test nrn_mlh_gsort output", "[nrn_gsort]") {
16  std::vector<double> input{1.2, -2.5, 5.1};
17 
18  {
19  std::vector<int> indices(input.size());
20  // all values from 0 to size - 1
21  std::iota(indices.begin(), indices.end(), 0);
22 
23  // for comparison
24  auto sorted_input = input;
25  std::sort(sorted_input.begin(), sorted_input.end());
26 
27  SECTION("Test sorting") {
28  nrn_mlh_gsort(input.data(), indices.data(), input.size(), cmpdfn);
29  for (auto i = 0; i < input.size(); ++i) {
30  REQUIRE(sorted_input[i] == input[indices[i]]);
31  }
32  }
33  }
34 
35  {
36  std::vector<int> indices{2, 1, 1};
37  std::vector<int> expected_result{1, 1, 2}; // as -2,5 < 5.1
38 
39  SECTION("Test sorting with repeated indices") {
40  nrn_mlh_gsort(input.data(), indices.data(), input.size(), cmpdfn);
41  for (auto i = 0; i < input.size(); ++i) {
42  REQUIRE(indices[i] == expected_result[i]);
43  }
44  }
45  }
46 }
#define i
Definition: md1redef.h:19
int nrn_mlh_gsort(double *vec, int *base_ptr, int total_elems, int(*cmp)(double, double))
Definition: ivocvect.cpp:3877
TEST_CASE("Test nrn_mlh_gsort output", "[nrn_gsort]")
Definition: iovec.cpp:15
int cmpdfn(double a, double b)
Definition: iovec.cpp:11
HOC interpreter function declarations (included by hocdec.h)