Skip to content

Commit f80bf7b

Browse files
committed
Add validation for field's dimensionality (#56)
1 parent 68e1cdb commit f80bf7b

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

tests/core/topology/test_lattice.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
from xentica.core.topology.lattice import (
66
OrthogonalLattice,
77
)
8+
from examples.game_of_life import GameOfLife, GOLExperiment
9+
10+
11+
class GOLExperimentBroken(GOLExperiment):
12+
"""Experiment with broken field size."""
13+
size = (23, )
814

915

1016
class TestOrthogonalLattice(unittest.TestCase):
@@ -15,3 +21,8 @@ def test_incorrect_dimensions(self):
1521
lattice = OrthogonalLattice()
1622
with self.assertRaises(XenticaException):
1723
lattice.dimensions = 0
24+
25+
def test_wrong_field_size(self):
26+
"""Test wrong field size set in experiment."""
27+
with self.assertRaises(XenticaException):
28+
GameOfLife(GOLExperimentBroken)

xentica/core/topology/lattice.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ class Topology:
1616
"""
1717
import abc
1818

19-
from xentica.core.mixins import DimensionsMixin
20-
21-
from xentica.core.mixins import BscaDetectorMixin
19+
from xentica.core.mixins import DimensionsMixin, BscaDetectorMixin
2220
from xentica.core.variables import Constant
21+
from xentica.core.exceptions import XenticaException
2322

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

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

4948
def _define_constants_once(self):
5049
"""Define field size conctants in C code."""
51-
for i in range(self.bsca.topology.dimensions):
52-
size = 1
53-
if hasattr(self.bsca, "size") and i < len(self.bsca.size):
54-
size = self.bsca.size[i]
50+
num_dimensions = self.bsca.topology.dimensions
51+
for i in range(num_dimensions):
52+
if not hasattr(self.bsca, "size") or i >= len(self.bsca.size):
53+
msg = "Wrong field's dimensionality ({} instead of {})."
54+
msg = msg.format(len(self.bsca.size), num_dimensions)
55+
raise XenticaException(msg)
56+
size = self.bsca.size[i]
5557
constant = Constant("%s%d" % (self.width_prefix, i), size)
5658
self.bsca.define_constant(constant)
5759

0 commit comments

Comments
 (0)