User Guide
cvode.cpp
Go to the documentation of this file.
1 #include <catch2/catch_test_macros.hpp>
2 
3 #include "ast/program.hpp"
11 
12 using namespace nmodl;
13 using namespace visitor;
14 using namespace test;
15 using namespace test_utils;
16 
18 
19 
20 auto run_cvode_visitor(const std::string& text) {
22  const auto& ast = driver.parse_string(text);
23  SymtabVisitor().visit_program(*ast);
24  CvodeVisitor().visit_program(*ast);
25 
26  return ast;
27 }
28 
29 
30 TEST_CASE("Make sure CVODE block is generated properly", "[visitor][cvode]") {
31  GIVEN("No DERIVATIVE block") {
32  auto nmodl_text = "NEURON { SUFFIX example }";
33  auto ast = run_cvode_visitor(nmodl_text);
34  THEN("No CVODE block is added") {
35  auto blocks = collect_nodes(*ast, {ast::AstNodeType::CVODE_BLOCK});
36  REQUIRE(blocks.empty());
37  }
38  }
39  GIVEN("DERIVATIVE block") {
40  auto nmodl_text = R"(
41  NEURON {
42  SUFFIX example
43  }
44 
45  STATE {X Y[2] Z}
46 
47  DERIVATIVE equation {
48  CONSERVE X + Z = 5
49  X' = -X + Z * Z
50  Z' = Z * X
51  Y'[1] = -Y[0]
52  Y'[0] = -Y[1]
53  }
54 )";
55  auto ast = run_cvode_visitor(nmodl_text);
56  THEN("CVODE block is added") {
57  auto blocks = collect_nodes(*ast, {ast::AstNodeType::CVODE_BLOCK});
58  REQUIRE(blocks.size() == 1);
59  THEN("No primed variables exist in the CVODE block") {
60  auto primed_vars = collect_nodes(*blocks[0], {ast::AstNodeType::PRIME_NAME});
61  REQUIRE(primed_vars.empty());
62  }
63  THEN("No CONSERVE statements are present in the CVODE block") {
64  auto conserved_stmts = collect_nodes(*blocks[0], {ast::AstNodeType::CONSERVE});
65  REQUIRE(conserved_stmts.empty());
66  }
67  }
68  }
69  GIVEN("Multiple DERIVATIVE blocks") {
70  auto nmodl_text = R"(
71  NEURON {
72  SUFFIX example
73  }
74 
75  STATE {X}
76 
77  DERIVATIVE equation {
78  X' = -X
79  }
80 
81  DERIVATIVE equation2 {
82  X' = -X * X
83  }
84 )";
85  THEN("An error is raised") {
86  REQUIRE_THROWS_AS(run_cvode_visitor(nmodl_text), std::runtime_error);
87  }
88  }
89 }
test_utils.hpp
nmodl::parser::NmodlDriver
Class that binds all pieces together for parsing nmodl file.
Definition: nmodl_driver.hpp:67
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::ast::AstNodeType::PRIME_NAME
@ PRIME_NAME
type of ast::PrimeName
nmodl::ast::AstNodeType::CONSERVE
@ CONSERVE
type of ast::Conserve
TEST_CASE
TEST_CASE("Make sure CVODE block is generated properly", "[visitor][cvode]")
Definition: cvode.cpp:30
run_cvode_visitor
auto run_cvode_visitor(const std::string &text)
Definition: cvode.cpp:20
visitor_utils.hpp
Utility functions for visitors implementation.
program.hpp
Auto generated AST classes declaration.
driver
nmodl::parser::UnitDriver driver
Definition: parser.cpp:28
nmodl::parser::UnitDriver::parse_string
bool parse_string(const std::string &input)
parser Units provided as string (used for testing)
Definition: unit_driver.cpp:40
nmodl::collect_nodes
std::vector< std::shared_ptr< const ast::Ast > > collect_nodes(const ast::Ast &node, const std::vector< ast::AstNodeType > &types)
traverse node recursively and collect nodes of given types
Definition: visitor_utils.cpp:206
checkparent_visitor.hpp
Visitor for checking parents of ast nodes
nmodl_driver.hpp
cvode_visitor.hpp
Visitor used for generating the necessary AST nodes for CVODE.
symtab_visitor.hpp
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
nmodl::ast::AstNodeType::CVODE_BLOCK
@ CVODE_BLOCK
type of ast::CvodeBlock
nmodl_visitor.hpp
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.