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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*************************************************************************
* 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/checkparent_visitor.hpp"
#include "visitors/constant_folder_visitor.hpp"
#include "visitors/nmodl_visitor.hpp"
#include "visitors/symtab_visitor.hpp"
using namespace nmodl;
using namespace visitor;
using namespace test;
using namespace test_utils;
using nmodl::parser::NmodlDriver;
//=============================================================================
// Constant folding tests
//=============================================================================
std::string run_constant_folding_visitor(const std::string& text) {
NmodlDriver driver;
const auto& ast = driver.parse_string(text);
SymtabVisitor().visit_program(*ast);
ConstantFolderVisitor().visit_program(*ast);
std::stringstream stream;
NmodlPrintVisitor(stream).visit_program(*ast);
// check that, after visitor rearrangement, parents are still up-to-date
CheckParentVisitor().check_ast(*ast);
return stream.str();
}
SCENARIO("Perform constant folder on NMODL constructs") {
GIVEN("Simple integer expression") {
std::string nmodl_text = R"(
PROCEDURE dummy() {
a = 1 + 2
}
)";
std::string expected_text = R"(
PROCEDURE dummy() {
a = 3
}
)";
THEN("successfully folds") {
auto result = run_constant_folding_visitor(nmodl_text);
REQUIRE(reindent_text(result) == reindent_text(expected_text));
}
}
GIVEN("Simple double expression") {
std::string nmodl_text = R"(
PROCEDURE dummy() {
a = 1.1 + 2e-10
}
)";
std::string expected_text = R"(
PROCEDURE dummy() {
a = 1.1000000002
}
)";
THEN("successfully folds") {
auto result = run_constant_folding_visitor(nmodl_text);
REQUIRE(reindent_text(result) == reindent_text(expected_text));
}
}
GIVEN("Complex expression") {
std::string nmodl_text = R"(
PROCEDURE dummy() {
a = 1 + (2) + (2 / 2) + (((1+((2)))))
}
)";
std::string expected_text = R"(
PROCEDURE dummy() {
a = 7
}
)";
THEN("successfully folds") {
auto result = run_constant_folding_visitor(nmodl_text);
REQUIRE(reindent_text(result) == reindent_text(expected_text));
}
}
GIVEN("Integer expression with define statement") {
std::string nmodl_text = R"(
DEFINE N 10
PROCEDURE dummy() {
a = N + (2*N) + (N / 2) + (((1+((N)))))
FROM i = 0 TO N-2 {
}
}
)";
std::string expected_text = R"(
DEFINE N 10
PROCEDURE dummy() {
a = 46
FROM i = 0 TO 8 {
}
}
)";
THEN("successfully folds") {
auto result = run_constant_folding_visitor(nmodl_text);
REQUIRE(reindent_text(result) == reindent_text(expected_text));
}
}
GIVEN("Only fold part of the statement") {
std::string nmodl_text = R"(
DEFINE N 10
PROCEDURE dummy() {
a = N + 2.0 + b
c = a + d
d = 2^3
e = 2 || 3
}
)";
std::string expected_text = R"(
DEFINE N 10
PROCEDURE dummy() {
a = 12+b
c = a+d
d = 2^3
e = 2 || 3
}
)";
THEN("successfully folds and keep other statements untouched") {
auto result = run_constant_folding_visitor(nmodl_text);
REQUIRE(reindent_text(result) == reindent_text(expected_text));
}
}
GIVEN("Don't remove parentheses if not simplifying") {
std::string nmodl_text = R"(
DEFINE N 10
PROCEDURE dummy() {
a = ((N+1)+5)*(c+1+N)/(b - 2)
}
)";
std::string expected_text = R"(
DEFINE N 10
PROCEDURE dummy() {
a = 16*(c+1+10)/(b-2)
}
)";
THEN("successfully folds and keep other statements untouched") {
auto result = run_constant_folding_visitor(nmodl_text);
REQUIRE(reindent_text(result) == reindent_text(expected_text));
}
}
}
|