|
| 1 | +// NOTE: this assumes neuronapi.h is on your CPLUS_INCLUDE_PATH |
| 2 | +// Exercises nrn_symbol_pop and nrn_object_pop's nil handling, the stack-pop |
| 3 | +// primitives used to unwind an interpreter frame from a binding (the |
| 4 | +// HOC-to-Python component read/write-back path). nrn_symbol_pop pops a Symbol |
| 5 | +// the interpreter left on the stack; nrn_object_pop pops an object but returns |
| 6 | +// NULL for a nil objref instead of dereferencing it to take a reference. |
| 7 | +#include <array> |
| 8 | +#include <iostream> |
| 9 | +#include "neuronapi.h" |
| 10 | + |
| 11 | +using std::cerr; |
| 12 | +using std::endl; |
| 13 | + |
| 14 | +// No public API leaves a bare Symbol (STACK_IS_SYM) on the stack -- the |
| 15 | +// interpreter pushes one during object-component access -- so the test uses the |
| 16 | +// internal push to put a Symbol there, the way nrniv would mid-expression. |
| 17 | +extern void hoc_pushs(Symbol*); |
| 18 | + |
| 19 | +extern "C" void modl_reg() {} |
| 20 | + |
| 21 | +static bool check(bool cond, const char* msg) { |
| 22 | + if (!cond) { |
| 23 | + cerr << "FAIL: " << msg << endl; |
| 24 | + } |
| 25 | + return cond; |
| 26 | +} |
| 27 | + |
| 28 | +int main(void) { |
| 29 | + static std::array<const char*, 4> argv = {"stack_pops", "-nogui", "-nopython", nullptr}; |
| 30 | + nrn_init(3, argv.data()); |
| 31 | + |
| 32 | + bool ok = true; |
| 33 | + |
| 34 | + // Push a sentinel FIRST, below everything else the test does. The stack is |
| 35 | + // LIFO, so if every push below is matched by exactly one pop the sentinel |
| 36 | + // resurfaces on top at the end; recovering it then proves the pops neither |
| 37 | + // over-popped into what lay beneath them nor left anything behind. A |
| 38 | + // sentinel pushed *after* the pops could prove neither -- it would only ever |
| 39 | + // probe the current top. |
| 40 | + const double SENTINEL = 424242.0; |
| 41 | + nrn_double_push(SENTINEL); |
| 42 | + |
| 43 | + // nrn_symbol_pop returns the Symbol on top of the stack, LIFO. |
| 44 | + Symbol* v = nrn_symbol("v"); |
| 45 | + Symbol* t = nrn_symbol("t"); |
| 46 | + ok &= check(v != nullptr && t != nullptr, "symbols v and t resolve"); |
| 47 | + hoc_pushs(v); |
| 48 | + hoc_pushs(t); |
| 49 | + ok &= check(nrn_stack_type() == STACK_IS_SYM, "stack top is a symbol before nrn_symbol_pop"); |
| 50 | + ok &= check(nrn_symbol_pop() == t, "symbol pop returns the last pushed symbol"); |
| 51 | + ok &= check(nrn_symbol_pop() == v, "symbol pop returns the earlier symbol (LIFO)"); |
| 52 | + |
| 53 | + // nrn_object_pop returns a real object (reference-counted). |
| 54 | + Object* vec = nrn_object_new(nrn_symbol("Vector"), 0); |
| 55 | + ok &= check(vec != nullptr, "Vector constructed"); |
| 56 | + nrn_object_push(vec); |
| 57 | + Object* got = nrn_object_pop(); |
| 58 | + ok &= check(got == vec, "object pop returns the pushed object"); |
| 59 | + if (got) { |
| 60 | + nrn_object_unref(got); |
| 61 | + } |
| 62 | + |
| 63 | + // nrn_object_pop returns NULL for a nil object reference instead of |
| 64 | + // crashing -- a naive pop would dereference the NULL to take a reference and |
| 65 | + // segfault here. |
| 66 | + nrn_object_push(nullptr); |
| 67 | + ok &= check(nrn_object_pop() == nullptr, "object pop returns NULL for a nil objref"); |
| 68 | + |
| 69 | + // Balance: with every push above consumed by exactly one pop, the sentinel |
| 70 | + // from the very start is what remains on top. |
| 71 | + ok &= check(nrn_double_pop() == SENTINEL, |
| 72 | + "sentinel from before the pops is intact -- the stack is balanced"); |
| 73 | + |
| 74 | + return ok ? 0 : 1; |
| 75 | +} |
0 commit comments