Skip to content
Permalink
Browse files

Add validation for field's dimensionality (#56)

  • Loading branch information...
a5kin committed May 14, 2019
1 parent 68e1cdb commit f80bf7b7caea96e625e3975666768042a465ba15
Showing with 20 additions and 7 deletions.
  1. +11 −0 tests/core/topology/test_lattice.py
  2. +9 −7 xentica/core/topology/lattice.py
@@ -5,6 +5,12 @@
from xentica.core.topology.lattice import (
OrthogonalLattice,
)
from examples.game_of_life import GameOfLife, GOLExperiment


class GOLExperimentBroken(GOLExperiment):
"""Experiment with broken field size."""
size = (23, )


class TestOrthogonalLattice(unittest.TestCase):
@@ -15,3 +21,8 @@ def test_incorrect_dimensions(self):
lattice = OrthogonalLattice()
with self.assertRaises(XenticaException):
lattice.dimensions = 0

def test_wrong_field_size(self):
"""Test wrong field size set in experiment."""
with self.assertRaises(XenticaException):
GameOfLife(GOLExperimentBroken)
@@ -16,10 +16,9 @@ class Topology:
"""
import abc

from xentica.core.mixins import DimensionsMixin

from xentica.core.mixins import BscaDetectorMixin
from xentica.core.mixins import DimensionsMixin, BscaDetectorMixin
from xentica.core.variables import Constant
from xentica.core.exceptions import XenticaException

__all__ = ['Lattice', 'OrthogonalLattice', ]

@@ -48,10 +47,13 @@ class Lattice(DimensionsMixin, BscaDetectorMixin, metaclass=abc.ABCMeta):

def _define_constants_once(self):
"""Define field size conctants in C code."""
for i in range(self.bsca.topology.dimensions):
size = 1
if hasattr(self.bsca, "size") and i < len(self.bsca.size):
size = self.bsca.size[i]
num_dimensions = self.bsca.topology.dimensions
for i in range(num_dimensions):
if not hasattr(self.bsca, "size") or i >= len(self.bsca.size):
msg = "Wrong field's dimensionality ({} instead of {})."
msg = msg.format(len(self.bsca.size), num_dimensions)
raise XenticaException(msg)
size = self.bsca.size[i]
constant = Constant("%s%d" % (self.width_prefix, i), size)
self.bsca.define_constant(constant)

0 comments on commit f80bf7b

Please sign in to comment.
You can’t perform that action at this time.