neuropercolation/test/test_neighborhood.py

75 lines
3.5 KiB
Python
Raw Normal View History

2019-02-23 15:20:48 +00:00
"""
Copyright 2019 Richard Feistenauer
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import sys
2019-02-23 15:20:48 +00:00
sys.path.append('../..')
2019-02-17 10:11:27 +00:00
from cellular_automaton import cellular_automaton as csn
import unittest
2019-02-09 17:16:19 +00:00
class TestNeighborhood(unittest.TestCase):
@staticmethod
2019-02-16 17:05:26 +00:00
def check_neighbors(neighborhood, neighborhood_sets, dimension=(3, 3)):
for neighborhood_set in neighborhood_sets:
2019-02-16 17:05:26 +00:00
neighbors = neighborhood.calculate_cell_neighbor_coordinates(neighborhood_set[0], dimension)
if neighborhood_set[1] != neighbors:
2019-02-16 17:05:26 +00:00
print("\nrel_n:", neighborhood._rel_neighbors)
print("\nWrong neighbors (expected, real): ", (neighborhood_set[1]), neighbors)
return False
return True
def test_ignore_missing_neighbors(self):
neighborhood = csn.MooreNeighborhood(csn.EdgeRule.IGNORE_MISSING_NEIGHBORS_OF_EDGE_CELLS)
n00 = [[0, 0], [[1, 0], [0, 1], [1, 1]]]
n11 = [[1, 1], [[0, 0], [1, 0], [2, 0], [0, 1], [2, 1], [0, 2], [1, 2], [2, 2]]]
n22 = [[2, 2], [[1, 1], [2, 1], [1, 2]]]
self.assertTrue(self.check_neighbors(neighborhood, [n00, n11, n22]))
def test_ignore_edge_cells(self):
neighborhood = csn.MooreNeighborhood(csn.EdgeRule.IGNORE_EDGE_CELLS)
n00 = [[0, 0], []]
n11 = [[1, 1], [[0, 0], [1, 0], [2, 0], [0, 1], [2, 1], [0, 2], [1, 2], [2, 2]]]
n22 = [[2, 2], []]
self.assertTrue(self.check_neighbors(neighborhood, [n00, n11, n22]))
def test_cyclic_dimensions(self):
neighborhood = csn.MooreNeighborhood(csn.EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS)
n00 = [[0, 0], [[2, 2], [0, 2], [1, 2], [2, 0], [1, 0], [2, 1], [0, 1], [1, 1]]]
n11 = [[1, 1], [[0, 0], [1, 0], [2, 0], [0, 1], [2, 1], [0, 2], [1, 2], [2, 2]]]
n22 = [[2, 2], [[1, 1], [2, 1], [0, 1], [1, 2], [0, 2], [1, 0], [2, 0], [0, 0]]]
self.assertTrue(self.check_neighbors(neighborhood, [n00, n11, n22]))
2019-02-16 17:05:26 +00:00
def test_von_neumann_r1(self):
neighborhood = csn.VonNeumannNeighborhood(csn.EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS)
n1 = [[1, 1], [[1, 0], [0, 1], [2, 1], [1, 2]]]
self.assertTrue(self.check_neighbors(neighborhood, [n1]))
def test_von_neumann_r2(self):
neighborhood = csn.VonNeumannNeighborhood(csn.EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS, range_=2)
n1 = [[2, 2], [[2, 0], [1, 1], [2, 1], [3, 1], [0, 2], [1, 2], [3, 2], [4, 2], [1, 3], [2, 3], [3, 3], [2, 4]]]
self.assertTrue(self.check_neighbors(neighborhood, [n1], dimension=[5, 5]))
def test_von_neumann_d3(self):
neighborhood = csn.VonNeumannNeighborhood(csn.EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS,
dimension=3)
n1 = [[1, 1, 1], [[1, 1, 0], [1, 0, 1], [0, 1, 1], [2, 1, 1], [1, 2, 1], [1, 1, 2]]]
self.assertTrue(self.check_neighbors(neighborhood, [n1], dimension=[3, 3, 3]))
if __name__ == '__main__':
unittest.main()