User Guide
var_usage.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"
14 
15 using namespace nmodl;
16 using namespace visitor;
17 
18 //=============================================================================
19 // Variable usage visitor tests
20 //=============================================================================
21 
22 static bool run_var_usage_visitor(const std::shared_ptr<ast::Node>& node,
23  const std::string& variable) {
24  return VarUsageVisitor().variable_used(*node, variable);
25 }
26 
27 SCENARIO("Searching for variable name using VarUsageVisitor", "[visitor][var_usage]") {
28  auto to_ast = [](const std::string& text) {
30  return driver.parse_string(text);
31  };
32 
33  GIVEN("A simple NMODL block") {
34  std::string nmodl_text = R"(
35  DERIVATIVE states {
36  tau = 11.1
37  exp(tau)
38  {
39  h' = h + 2 + n
40  }
41  }
42  )";
43 
44  auto ast = to_ast(nmodl_text);
45  auto node = ast->get_blocks().front();
46 
47  WHEN("Looking for existing variable") {
48  THEN("Can find variables") {
49  REQUIRE(run_var_usage_visitor(node, "tau"));
50  REQUIRE(run_var_usage_visitor(node, "h"));
51  REQUIRE(run_var_usage_visitor(node, "n"));
52  }
53  }
54 
55  WHEN("Looking for missing variable") {
56  THEN("Can not find variable") {
57  REQUIRE_FALSE(run_var_usage_visitor(node, "tauu"));
58  REQUIRE_FALSE(run_var_usage_visitor(node, "my_var"));
59  }
60  }
61  }
62 
63  GIVEN("A nested NMODL block") {
64  std::string nmodl_text = R"(
65  NET_RECEIVE (weight,weight_AMPA, weight_NMDA, R){
66  LOCAL result
67  weight_AMPA = weight
68  weight_NMDA = weight * NMDA_ratio
69  INITIAL {
70  R = 1
71  u = u0
72  {
73  tsyn = t
74  }
75  }
76  }
77  )";
78 
79  auto ast = to_ast(nmodl_text);
80  auto node = ast->get_blocks().front();
81 
82  WHEN("Looking for existing variable in outer block") {
83  THEN("Can find variables") {
84  REQUIRE(run_var_usage_visitor(node, "weight"));
85  REQUIRE(run_var_usage_visitor(node, "NMDA_ratio"));
86  }
87  }
88 
89  WHEN("Looking for existing variable in inner block") {
90  THEN("Can find variables") {
91  REQUIRE(run_var_usage_visitor(node, "R"));
92  REQUIRE(run_var_usage_visitor(node, "u0"));
93  REQUIRE(run_var_usage_visitor(node, "tsyn"));
94  }
95  }
96  }
97 }
test_utils.hpp
nmodl::parser::NmodlDriver
Class that binds all pieces together for parsing nmodl file.
Definition: nmodl_driver.hpp:67
SCENARIO
SCENARIO("Searching for variable name using VarUsageVisitor", "[visitor][var_usage]")
Definition: var_usage.cpp:27
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
var_usage_visitor.hpp
Check if variable is used in given block.
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
run_var_usage_visitor
static bool run_var_usage_visitor(const std::shared_ptr< ast::Node > &node, const std::string &variable)
Definition: var_usage.cpp:22
nmodl_driver.hpp