From 573f110a8acdf7ca97d65c31b60564b6579e291e Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Sun, 29 Jan 2017 17:45:54 -0600 Subject: [PATCH] SymbolicAssignmentCollection: Fix name uniqueness (closes #13). --- sumpy/assignment_collection.py | 7 ++++ test/test_codegen.py | 59 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 test/test_codegen.py diff --git a/sumpy/assignment_collection.py b/sumpy/assignment_collection.py index 840b04da..c1e9197b 100644 --- a/sumpy/assignment_collection.py +++ b/sumpy/assignment_collection.py @@ -48,7 +48,14 @@ class _SymbolGenerator(object): from collections import defaultdict self.base_to_count = defaultdict(lambda: 0) + def _normalize(self, base): + # Strip off any _N suffix, to avoid generating conflicting names. + import re + base = re.split("_\d+$", base)[0] + return base if base != "" else "expr" + def __call__(self, base="expr"): + base = self._normalize(base) count = self.base_to_count[base] def make_id_str(base, count): diff --git a/test/test_codegen.py b/test/test_codegen.py new file mode 100644 index 00000000..59fe0340 --- /dev/null +++ b/test/test_codegen.py @@ -0,0 +1,59 @@ +from __future__ import division, absolute_import, print_function + +__copyright__ = "Copyright (C) 2017 Matt Wala" + +__license__ = """ +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +""" + + +import sys + +import logging +logger = logging.getLogger(__name__) + + +def test_symbolic_assignment_name_uniqueness(): + # https://gitlab.tiker.net/inducer/sumpy/issues/13 + from sumpy.assignment_collection import SymbolicAssignmentCollection + + sac = SymbolicAssignmentCollection({"s_0": 1}) + sac.assign_unique("s_", 1) + sac.assign_unique("s_", 1) + assert len(sac.assignments) == 3 + + sac = SymbolicAssignmentCollection() + sac.assign_unique("s_0", 1) + sac.assign_unique("s_", 1) + sac.assign_unique("s_", 1) + + assert len(sac.assignments) == 3 + + +# You can test individual routines by typing +# $ python test_fmm.py 'test_sumpy_fmm(cl.create_some_context)' + +if __name__ == "__main__": + if len(sys.argv) > 1: + exec(sys.argv[1]) + else: + from py.test.cmdline import main + main([__file__]) + +# vim: fdm=marker -- GitLab