|
| 1 | +// NOTE: this assumes neuronapi.h is on your CPLUS_INCLUDE_PATH |
| 2 | +// Exercises the top-level objref and strdef accessors: nrn_symbol_object_get/set |
| 3 | +// and nrn_symbol_str_get/set. These read and write the Object* / char* that a |
| 4 | +// top-level `objref`/`strdef` stores in the top-level object-data array, which |
| 5 | +// nrn_symbol_dataptr cannot return (it is not a double*). |
| 6 | +#include <array> |
| 7 | +#include <cstring> |
| 8 | +#include <iostream> |
| 9 | +#include "neuronapi.h" |
| 10 | + |
| 11 | +using std::cerr; |
| 12 | +using std::endl; |
| 13 | + |
| 14 | +extern "C" void modl_reg(){/* No modl_reg */}; |
| 15 | + |
| 16 | +static bool check(bool cond, const char* msg) { |
| 17 | + if (!cond) { |
| 18 | + cerr << "FAIL: " << msg << endl; |
| 19 | + } |
| 20 | + return cond; |
| 21 | +} |
| 22 | + |
| 23 | +int main(void) { |
| 24 | + static std::array<const char*, 4> argv = {"symbol_object_string", |
| 25 | + "-nogui", |
| 26 | + "-nopython", |
| 27 | + nullptr}; |
| 28 | + nrn_init(3, argv.data()); |
| 29 | + |
| 30 | + bool ok = true; |
| 31 | + |
| 32 | + // --- strdef get/set --- |
| 33 | + nrn_hoc_call("strdef s"); |
| 34 | + nrn_hoc_call("s = \"hello\""); |
| 35 | + Symbol* s = nrn_symbol("s"); |
| 36 | + ok &= check(s != nullptr, "strdef symbol resolves"); |
| 37 | + const char* sval = nrn_symbol_str_get(s); |
| 38 | + ok &= check(sval != nullptr && std::strcmp(sval, "hello") == 0, |
| 39 | + "str_get reads the strdef value"); |
| 40 | + |
| 41 | + ok &= check(nrn_symbol_str_set(s, "world") == true, "str_set succeeds on a strdef"); |
| 42 | + ok &= check(std::strcmp(nrn_symbol_str_get(s), "world") == 0, "str_set updated the value"); |
| 43 | + // Confirm HOC sees the written value too (aliases the same storage). |
| 44 | + nrn_hoc_call("hoc_ac_ = strcmp(s, \"world\")"); |
| 45 | + ok &= check(*nrn_symbol_dataptr(nrn_symbol("hoc_ac_")) == 0.0, "HOC reads the str_set value"); |
| 46 | + |
| 47 | + // --- objref get/set --- |
| 48 | + nrn_hoc_call("objref o"); |
| 49 | + nrn_hoc_call("o = new Vector(3)"); |
| 50 | + Symbol* o = nrn_symbol("o"); |
| 51 | + ok &= check(o != nullptr, "objref symbol resolves"); |
| 52 | + Object* vec = nrn_symbol_object_get(o); |
| 53 | + ok &= check(vec != nullptr, "object_get reads the bound object"); |
| 54 | + ok &= check(nrn_vector_capacity(vec) == 3, "the bound object is the Vector(3)"); |
| 55 | + |
| 56 | + // Rebind the objref to a different Vector via object_set. |
| 57 | + Object* vec5 = nrn_object_new(nrn_symbol("Vector"), 0); // empty Vector |
| 58 | + nrn_object_ref(vec5); |
| 59 | + ok &= check(nrn_symbol_object_set(o, vec5) == true, "object_set succeeds on an objref"); |
| 60 | + ok &= check(nrn_symbol_object_get(o) == vec5, "object_set rebound the objref"); |
| 61 | + // HOC sees the new binding. |
| 62 | + nrn_hoc_call("hoc_ac_ = o.size()"); |
| 63 | + ok &= check(*nrn_symbol_dataptr(nrn_symbol("hoc_ac_")) == 0.0, "HOC sees the rebound object"); |
| 64 | + |
| 65 | + // Clearing to nil. |
| 66 | + ok &= check(nrn_symbol_object_set(o, nullptr) == true, "object_set(NULL) clears the objref"); |
| 67 | + ok &= check(nrn_symbol_object_get(o) == nullptr, "objref is nil after clear"); |
| 68 | + |
| 69 | + // --- type mismatches return NULL / false, not a crash --- |
| 70 | + ok &= check(nrn_symbol_object_get(s) == nullptr, "object_get on a strdef returns NULL"); |
| 71 | + ok &= check(nrn_symbol_str_get(o) == nullptr, "str_get on an objref returns NULL"); |
| 72 | + ok &= check(nrn_symbol_object_set(s, vec5) == false, "object_set on a strdef is rejected"); |
| 73 | + ok &= check(nrn_symbol_str_set(o, "x") == false, "str_set on an objref is rejected"); |
| 74 | + |
| 75 | + return ok ? 0 : 1; |
| 76 | +} |
0 commit comments