Skip to content

Commit

Permalink
Add test for string handling (#904)
Browse files Browse the repository at this point in the history
* add test for string handling

* make test NEST2 compatible

---------

Co-authored-by: C.A.P. Linssen <[email protected]>
  • Loading branch information
clinssen and C.A.P. Linssen authored Jul 10, 2023
1 parent 72570fc commit 8695329
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 13 deletions.
28 changes: 15 additions & 13 deletions pynestml/visitors/ast_comparison_operator_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@
# You should have received a copy of the GNU General Public License
# along with NEST. If not, see <http://www.gnu.org/licenses/>.

"""
rhs : left=rhs comparisonOperator right=rhs
"""
from pynestml.symbols.boolean_type_symbol import BooleanTypeSymbol
from pynestml.symbols.error_type_symbol import ErrorTypeSymbol
from pynestml.symbols.predefined_types import PredefinedTypes
from pynestml.symbols.string_type_symbol import StringTypeSymbol
from pynestml.symbols.unit_type_symbol import UnitTypeSymbol
from pynestml.utils.error_strings import ErrorStrings
from pynestml.utils.logger import Logger, LoggingLevel
from pynestml.utils.messages import MessageCode
from pynestml.visitors.ast_visitor import ASTVisitor
from pynestml.symbols.boolean_type_symbol import BooleanTypeSymbol
from pynestml.symbols.error_type_symbol import ErrorTypeSymbol


class ASTComparisonOperatorVisitor(ASTVisitor):
Expand All @@ -49,6 +47,11 @@ def visit_expression(self, expr):
lhs_type.referenced_object = expr.get_lhs()
rhs_type.referenced_object = expr.get_rhs()

# both are string types
if lhs_type.is_primitive() and rhs_type.is_primitive() and isinstance(lhs_type, StringTypeSymbol) and isinstance(rhs_type, StringTypeSymbol):
expr.type = PredefinedTypes.get_boolean_type()
return

if (lhs_type.is_numeric_primitive() and rhs_type.is_numeric_primitive()) \
or (lhs_type.equals(rhs_type) and lhs_type.is_numeric()) or (
isinstance(lhs_type, BooleanTypeSymbol) and isinstance(rhs_type, BooleanTypeSymbol)):
Expand All @@ -65,11 +68,10 @@ def visit_expression(self, expr):
error_position=expr.get_source_position(),
log_level=LoggingLevel.WARNING)
return
else:
# hard incompatibility, cannot recover in c++, ERROR
error_msg = ErrorStrings.message_comparison(self, expr.get_source_position())
expr.type = ErrorTypeSymbol()
Logger.log_message(code=MessageCode.HARD_INCOMPATIBILITY,
error_position=expr.get_source_position(),
message=error_msg, log_level=LoggingLevel.ERROR)
return

# hard incompatibility, cannot recover in c++, ERROR
error_msg = ErrorStrings.message_comparison(self, expr.get_source_position())
expr.type = ErrorTypeSymbol()
Logger.log_message(code=MessageCode.HARD_INCOMPATIBILITY,
error_position=expr.get_source_position(),
message=error_msg, log_level=LoggingLevel.ERROR)
46 changes: 46 additions & 0 deletions tests/nest_tests/resources/StringHandlingTest.nestml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
StringHandlingTest.nestml
#########################


Copyright statement
+++++++++++++++++++

This file is part of NEST.

Copyright (C) 2004 The NEST Initiative

NEST is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

NEST is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with NEST. If not, see <http://www.gnu.org/licenses/>.
"""
neuron string_handling_test:
state:
s1 string = "abc"
s2 string = "def"
b1 boolean = false
b2 boolean = false

parameters:
s3 string = "ghi"
s4 string = "klm"

internals:
s5 string = "ghi"

update:
s7 string = s1 + s2
if s7 == "abcdef":
b1 = true

if s3 + s4 == s5 + "klm":
b2 = true
57 changes: 57 additions & 0 deletions tests/nest_tests/test_string_handling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
#
# test_string_handling.py
#
# This file is part of NEST.
#
# Copyright (C) 2004 The NEST Initiative
#
# NEST is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# NEST is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NEST. If not, see <http://www.gnu.org/licenses/>.

import nest
import numpy as np
import scipy as sp
import os

from pynestml.frontend.pynestml_frontend import generate_nest_target
from pynestml.codegeneration.nest_tools import NESTTools


class TestStringHandling:
"""Test string handling"""

def test_string_handling(self):
input_path = os.path.join(os.path.realpath(os.path.join(os.path.dirname(__file__), "resources", "StringHandlingTest.nestml")))
target_path = "target"
logging_level = "INFO"
module_name = "nestmlmodule"
suffix = "_nestml"

nest_version = NESTTools.detect_nest_version()

nest.set_verbosity("M_ALL")
generate_nest_target(input_path,
target_path=target_path,
logging_level=logging_level,
module_name=module_name,
suffix=suffix)
nest.ResetKernel()
nest.Install("nestmlmodule")

nrn = nest.Create("string_handling_test_nestml")

nest.Simulate(100.)

assert nest.GetStatus(nrn, "b1")
assert nest.GetStatus(nrn, "b2")

0 comments on commit 8695329

Please sign in to comment.