NEURON
test_mech_mapping.cpp
Go to the documentation of this file.
1 /*
2 # =============================================================================
3 # Copyright (c) 2025 Open Brain Institute
4 # =============================================================================.
5 */
6 #include <catch2/catch_test_macros.hpp>
7 
12 
13 using namespace coreneuron;
14 
15 // Note: Catch2 does not natively support death tests. I cannot test malformed report configurations
16 // (Katta)
17 TEST_CASE("register_all_variables_offsets and get_var_location_from_var_name") {
18  // Ensure mech_data_layout has enough entries
20  const int mech_id = 42;
21 
22  const char* names[] = {"var0", "var1", "var2", "var3_mechX", nullptr, nullptr, nullptr};
23  SerializedNames serialized_names = names;
24 
25  register_all_variables_offsets(mech_id, serialized_names);
26 
27  Memb_list ml;
28  ml._nodecount_padded = 2;
29  const int nvars = 4;
30  // In order to have values that are not all the same and do not follow i I pick a random value
31  // here. Feel free to change this to any other integer (maybe not NaN or +-Inf). It just changes
32  // the values that we store in the container (and later check). 0 is not wrong but makes the
33  // test more trivial and slightly increases the chances that the test passes even if there are
34  // problems. (Katta)
35  const int random_value_offset = 123;
36  ml.data = new double[nvars * ml._nodecount_padded];
37 
38  for (int i = 0; i < nvars * ml._nodecount_padded; ++i) {
39  ml.data[i] = i + random_value_offset;
40  }
41 
42  const auto mech_name = "mechX";
43 
44  SECTION("basic variable retrieval") {
45  for (int var_idx = 0; var_idx < nvars; ++var_idx) {
46  std::string var_name = names[var_idx];
47 
48  double* val = get_var_location_from_var_name(mech_id, mech_name, var_name, &ml, 1);
49 
50  int expected_ix = var_idx * ml._nodecount_padded + 1;
51  REQUIRE(val == &ml.data[expected_ix]);
52  REQUIRE(*val == random_value_offset + expected_ix);
53  }
54  }
55 
56  SECTION("fallback variable retrieval (var3 -> var3_mechX)") {
57  double* val = get_var_location_from_var_name(mech_id, mech_name, "var3", &ml, 1);
58 
59  int expected_ix = 3 * ml._nodecount_padded + 1;
60  REQUIRE(val == &ml.data[expected_ix]);
61  REQUIRE(*val == random_value_offset + expected_ix);
62  }
63 
64  delete[] ml.data;
65 }
#define i
Definition: md1redef.h:19
#define SOA_LAYOUT
Definition: data_layout.hpp:11
THIS FILE IS AUTO GENERATED DONT MODIFY IT.
double * get_var_location_from_var_name(int mech_id, const std::string_view mech_name, const std::string_view variable_name, Memb_list *ml, int node_index)
const char ** SerializedNames
CoreNeuron corenrn
Definition: multicore.cpp:53
void register_all_variables_offsets(int mech_id, SerializedNames variable_names)
static char * names
Definition: units.cpp:81
TEST_CASE("register_all_variables_offsets and get_var_location_from_var_name")