File: node_index.cpp

package info (click to toggle)
nmodl 0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 5,992 kB
  • sloc: cpp: 28,492; javascript: 9,841; yacc: 2,804; python: 1,967; lex: 1,674; xml: 181; sh: 136; ansic: 37; makefile: 18; pascal: 7
file content (80 lines) | stat: -rw-r--r-- 3,129 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*************************************************************************
 * Copyright (C) 2018-2022 Blue Brain Project
 *
 * This file is part of NMODL distributed under the terms of the GNU
 * Lesser General Public License. See top-level LICENSE file for details.
 *************************************************************************/

#include <catch2/catch_test_macros.hpp>

#include "ast/program.hpp"
#include "parser/nmodl_driver.hpp"
#include "test/unit/utils/test_utils.hpp"
#include "visitors/indexedname_visitor.hpp"
#include "visitors/visitor_utils.hpp"

using namespace nmodl;
using namespace visitor;

//=============================================================================
// Get the indexed node name and the dependencies of differential equations
//=============================================================================
std::pair<std::string, std::pair<std::string, std::unordered_set<std::string>>>
get_indexedname_dependencies(ast::Program& node) {
    IndexedNameVisitor testvisitor;
    testvisitor.visit_program(node);
    return std::make_pair(testvisitor.get_indexed_name(), testvisitor.get_dependencies());
}

SCENARIO("Get node name with index TestVisitor", "[visitor][node_index]") {
    auto to_ast = [](const std::string& text) {
        parser::NmodlDriver driver;
        return driver.parse_string(text);
    };

    GIVEN("A simple NMODL block") {
        std::string nmodl_text_a = R"(
            STATE {
                m[1]
            }
            BREAKPOINT  {
                SOLVE states METHOD euler
            }
            DERIVATIVE states {
                m'[0] = mInf/mTau
            }
        )";
        std::string nmodl_text_b = R"(
            BREAKPOINT  {
                SOLVE states STEADYSTATE sparse
            }
            DERIVATIVE states {
                m' = m + h
            }
        )";

        WHEN("Get node name with index") {
            THEN("Get node name with index") {
                auto ast = to_ast(nmodl_text_a);
                std::unordered_set<std::string> vars{"mInf", "mTau"};
                std::string var("m[0]");
                auto expect = std::make_pair(var, vars);
                auto result_name = get_indexedname_dependencies(*ast).first;
                auto result_dependencies = get_indexedname_dependencies(*ast).second;
                REQUIRE(result_name == var);
                REQUIRE(result_dependencies.first == expect.first);
                REQUIRE(result_dependencies.second == expect.second);
            }
            THEN("Get dependencies") {
                auto ast = to_ast(nmodl_text_b);
                std::unordered_set<std::string> vars{"m", "h"};
                std::string var("m");
                auto expect = std::make_pair(var, vars);
                auto result_name = get_indexedname_dependencies(*ast).first;
                auto result_dependencies = get_indexedname_dependencies(*ast).second;
                REQUIRE(result_dependencies.first == expect.first);
                REQUIRE(result_dependencies.second == expect.second);
            }
        }
    }
}