NEURON
node_index.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <catch2/catch_test_macros.hpp>
9 
10 #include "ast/program.hpp"
11 #include "parser/nmodl_driver.hpp"
12 #include "utils/test_utils.hpp"
15 
16 using namespace nmodl;
17 using namespace visitor;
18 
19 //=============================================================================
20 // Get the indexed node name and the dependencies of differential equations
21 //=============================================================================
22 std::pair<std::string, std::pair<std::string, std::unordered_set<std::string>>>
24  IndexedNameVisitor testvisitor;
25  testvisitor.visit_program(node);
26  return std::make_pair(testvisitor.get_indexed_name(), testvisitor.get_dependencies());
27 }
28 
29 SCENARIO("Get node name with index TestVisitor", "[visitor][node_index]") {
30  auto to_ast = [](const std::string& text) {
32  return driver.parse_string(text);
33  };
34 
35  GIVEN("A simple NMODL block") {
36  std::string nmodl_text_a = R"(
37  STATE {
38  m[1]
39  }
40  BREAKPOINT {
41  SOLVE states METHOD euler
42  }
43  DERIVATIVE states {
44  m'[0] = mInf/mTau
45  }
46  )";
47  std::string nmodl_text_b = R"(
48  BREAKPOINT {
49  SOLVE states STEADYSTATE sparse
50  }
51  DERIVATIVE states {
52  m' = m + h
53  }
54  )";
55 
56  WHEN("Get node name with index") {
57  THEN("Get node name with index") {
58  auto ast = to_ast(nmodl_text_a);
59  std::unordered_set<std::string> vars{"mInf", "mTau"};
60  std::string var("m[0]");
61  auto expect = std::make_pair(var, vars);
62  auto result_name = get_indexedname_dependencies(*ast).first;
63  auto result_dependencies = get_indexedname_dependencies(*ast).second;
64  REQUIRE(result_name == var);
65  REQUIRE(result_dependencies.first == expect.first);
66  REQUIRE(result_dependencies.second == expect.second);
67  }
68  THEN("Get dependencies") {
69  auto ast = to_ast(nmodl_text_b);
70  std::unordered_set<std::string> vars{"m", "h"};
71  std::string var("m");
72  auto expect = std::make_pair(var, vars);
73  auto result_name = get_indexedname_dependencies(*ast).first;
74  auto result_dependencies = get_indexedname_dependencies(*ast).second;
75  REQUIRE(result_dependencies.first == expect.first);
76  REQUIRE(result_dependencies.second == expect.second);
77  }
78  }
79  }
80 }
Represents top level AST node for whole NMODL input.
Definition: program.hpp:39
Class that binds all pieces together for parsing nmodl file.
Get node name with indexed for the IndexedName node and the dependencies of DiffEqExpression node.
void visit_program(ast::Program &node) override
visit node of type ast::Program
std::pair< std::string, std::unordered_set< std::string > > get_dependencies()
get the attribute dependencies
std::string get_indexed_name()
get the attribute indexed_name
bool parse_string(const std::string &input)
parser Units provided as string (used for testing)
Definition: unit_driver.cpp:40
Get node name with indexed for the IndexedName node and the dependencies of DiffEqExpression node.
double var(InputIterator begin, InputIterator end)
Definition: ivocvect.h:108
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
SCENARIO("Get node name with index TestVisitor", "[visitor][node_index]")
Definition: node_index.cpp:29
std::pair< std::string, std::pair< std::string, std::unordered_set< std::string > > > get_indexedname_dependencies(ast::Program &node)
Definition: node_index.cpp:23
static Node * node(Object *)
Definition: netcvode.cpp:291
#define text
Definition: plot.cpp:60
Auto generated AST classes declaration.
nmodl::parser::UnitDriver driver
Definition: parser.cpp:28
Utility functions for visitors implementation.