2019-02-03 16:17:57 +00:00
|
|
|
import sys
|
|
|
|
sys.path.append('../src')
|
|
|
|
|
|
|
|
import cellular_automaton.ca_neighborhood 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)):
|
2019-02-03 16:17:57 +00:00
|
|
|
for neighborhood_set in neighborhood_sets:
|
2019-02-16 17:05:26 +00:00
|
|
|
neighbors = neighborhood.calculate_cell_neighbor_coordinates(neighborhood_set[0], dimension)
|
2019-02-03 16:17:57 +00:00
|
|
|
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)
|
2019-02-03 16:17:57 +00:00
|
|
|
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]))
|
|
|
|
|
2019-02-03 16:17:57 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|