Browse Source

begin to add some tests

olinox 9 years ago
parent
commit
f13e584678

+ 14 - 0
.travis.yml

@@ -0,0 +1,14 @@
+language: python
+python:
+  - "3.3"
+  - "3.4"
+  - "3.5"
+install:
+  - pip install nose2
+  - pip install coveralls
+  - pip install pylint
+script: 
+  - nose2 --with-coverage
+  - pylint --errors-only
+after_success:
+  coveralls

+ 2 - 1
README.md

@@ -1,4 +1,5 @@
 ** pypog **
 
-[GNU License]()
+[GNU License](https://github.com/olinox14/pypog/blob/master/LICENSE.txt) 
 
+[![Build Status](https://travis-ci.org/olinox14/pypog.svg?branch=master)](https://travis-ci.org/olinox14/pypog) [![Coverage Status](https://coveralls.io/repos/github/olinox14/pypog/badge.svg?branch=master)](https://coveralls.io/github/olinox14/pypog?branch=master)

+ 2 - 2
core/Cell.py

@@ -47,12 +47,12 @@ class Cell(object):
     def __update_neighbours(self):
         """update the tuple of neighbours cells"""
         x, y = self._x, self._y
-        if self._geometry == constants.HEXGRID:
+        if self._geometry == constants.HEX:
             if 1 == (x % 2):
                 self._neighbours = ( (x, y-1), (x+1, y), (x+1, y+1), (x,  y+1), (x-1, y+1), (x-1, y) )
             else:
                 self._neighbours = ( (x, y-1), (x+1, y-1), (x+1, y), (x,  y+1), (x-1, y), (x-1, y-1) )
-        elif self._geometry == constants.SQUAREGRID:
+        elif self._geometry == constants.SQUARE:
             self._neighbours = ( (x-1, y-1), (x, y-1), (x+1, y-1), \
                                 (x-1, y)  , (x, y-1), (x+1, y)  , \
                                 (x-1, y+1), (x, y+1),(x+1, y+1) )

+ 26 - 36
core/Grid.py

@@ -5,15 +5,20 @@ Created on 7 nov. 2016
 '''
 from core.Cell import Cell
 from core.constants import GRID_GEOMETRIES
-from core.geometry import triangle, rectangle, line, zone
+from core import geometry
 from core.pathfinder import pathfinder
 
 
 class Grid(object):
     def __init__(self, geometry, width, height):
-        self._geometry = geometry
-        self._width = width
-        self._height = height
+        self._geometry = None
+        self.geometry = geometry
+        
+        self._width = 0
+        self.width = width
+        self._height = 0
+        self.height = height
+        
         self._cells = {}
         
     # properties
@@ -47,6 +52,9 @@ class Grid(object):
             raise ValueError("'width' has to be a strictly positive integer")
         self._height = height    
     
+    
+    
+    
     def cell(self, coord):
         # temporary
         try:
@@ -66,57 +74,39 @@ class Grid(object):
         return (x > 0 and x <= self._width and y > 0 and y <= self._height)
     
     def line(self, x1, y1, x2, y2):
-        return line.line2d(self.geometry, x1, y1, x2, y2)
+        return geometry.gline.line2d(self.geometry, x1, y1, x2, y2)
     
     def line3d(self, x1, y1, z1, x2, y2, z2):
-        return line.line3d(self.geometry, x1, y1, z1, x2, y2, z2)
+        return geometry.gline.line3d(self.geometry, x1, y1, z1, x2, y2, z2)
+    
+    def zone(self, x, y):
+        return geometry.gzone.zone(self.geometry, x, y)
     
     def triangle(self, xa, ya, xh, yh, iAngle):
-        return triangle.triangle(self.geometry, xa, ya, xh, yh, iAngle)
+        return geometry.gtriangle.triangle(self.geometry, xa, ya, xh, yh, iAngle)
     
     def triangle3d(self, xa, ya, za, xh, yh, zh, iAngle):
-        return triangle.triangle3d(self.geometry, xa, ya, za, xh, yh, zh, iAngle)
+        return geometry.gtriangle.triangle3d(self.geometry, xa, ya, za, xh, yh, zh, iAngle)
 
     def rect(self, x1, y1, x2, y2):
-        return rectangle.rect(x1, y1, x2, y2)
+        return geometry.grectangle.rect(x1, y1, x2, y2)
     
     def hollow_rect(self, x1, y1, x2, y2):
-        return rectangle.hollow_rect(x1, y1, x2, y2)
+        return geometry.grectangle.hollow_rect(x1, y1, x2, y2)
 
     def path(self, x1, y1, x2, y2):
         return pathfinder.path( self, (x1, y1), (x2,y2) )
+
+    
+    
     
-    def zone(self, x, y):
-        return zone.zone(self.geometry, x, y)
     
 class HexGrid(Grid):
     def __init__(self, width, height):
-        Grid.__init__(5, width, height)
+        Grid.__init__(self, 5, width, height)
 
 class SquareGrid(Grid):
     def __init__(self, width, height):
-        Grid.__init__(4, width, height)
-
+        Grid.__init__(self, 4, width, height)
 
-
-
-    
-    
-if __name__ == '__main__':
-    gr = Grid(5, 100, 100)
-    print(gr.cases_number())
-    
-    print(gr.line(1,1,5,10))
-    print(gr.line3d(1,1,1,5,10,10))
-    
-    print(gr.triangle(1,1,5,10,triangle.ANGLES[1]))
-    print(gr.triangle3d(1,1,1,5,10,10, triangle.ANGLES[1]))
-    
-    print(gr.path(1,1,5,10))
-    
-    
-    
-    
-    
-    
     

+ 2 - 2
core/constants.py

@@ -6,5 +6,5 @@ Created on 7 nov. 2016
 
 # geometry
 GRID_GEOMETRIES = [4, 5]
-SQUAREGRID = 4
-HEXGRID = 5
+SQUARE = 4
+HEX = 5

+ 6 - 12
core/geometry/line.py → core/geometry/gline.py

@@ -15,10 +15,12 @@ def line2d(geometry, x1, y1, x2, y2):
     grid could be one of the GRIDTYPES values"""
     if not all(isinstance(c, int) for c in [x1, y1, x2, y2]):
         raise TypeError("x1, y1, x2, y2 have to be integers")
-    if geometry == constants.SQUAREGRID:
-        return _brH(x1, y1, x2, y2)
-    elif geometry == constants.HEXGRID: 
-        return _brC(x1, y1, x2, y2)
+    if (x1, y1) == (x2, y2):
+        return [(x1, y1)]
+    if geometry == constants.HEX:
+        return hex_2d_line(x1, y1, x2, y2)
+    elif geometry == constants.SQUARE: 
+        return squ_2d_line(x1, y1, x2, y2)
     else:
         raise ValueError("'geometry' has to be a value from GRID_GEOMETRIES")
 
@@ -37,8 +39,6 @@ def line3d(geometry, x1, y1, z1, x2, y2, z2):
 def hex_2d_line(x1, y1, x2, y2):
     """returns a line from x1,y1 to x2,y2 on an hexagonal grid
     line is a list of coordinates"""
-    if not all(isinstance(c, int) for c in [x1, y1, x2, y2]):
-        raise TypeError("x1, y1, x2, y2 have to be integers")
     if (x1, y1) == (x2, y2):
         return [(x1, y1)]
     return _brH(x1, y1, x2, y2)
@@ -46,8 +46,6 @@ def hex_2d_line(x1, y1, x2, y2):
 def hex_3d_line(x1, y1, z1, x2, y2, z2):
     """returns a line from x1,y1,z1 to x2,y2,z2 on an hexagonal grid
     line is a list of coordinates"""
-    if not all(isinstance(c, int) for c in [x1, y1, z1, x2, y2, z2]):
-        raise TypeError("x1, y1, z1, x2, y2, z2 have to be integers")
     hoLine = hex_2d_line(x1, y1, x2, y2)
     if z1 == z2:
         return [(x, y, z1) for x, y in hoLine]
@@ -62,8 +60,6 @@ def squ_2d_line(x1, y1, x2, y2):
     """returns a line from x1,y1 to x2,y2 on an square grid
     line is a list of coordinates
     """
-    if not all(isinstance(c, int) for c in [x1, y1, x2, y2]):
-        raise TypeError("x1, y1, x2, y2 have to be integers")
     if (x1, y1) == (x2, y2):
         return [(x1, y1)]
     return _brC(x1, y1, x2, y2)
@@ -71,8 +67,6 @@ def squ_2d_line(x1, y1, x2, y2):
 def squ_3d_line(x1, y1, z1, x2, y2, z2):
     """returns a line from x1,y1,z1 to x2,y2,z2 on an square grid
     line is a list of coordinates"""
-    if not all(isinstance(c, int) for c in [x1, y1, z1, x2, y2, z2]):
-        raise TypeError("x1, y1, z1, x2, y2, z2 have to be integers")
     hoLine = squ_2d_line(x1, y1, x2, y2)
     if z1 == z2:
         return [(x, y, z1) for x, y in hoLine]

+ 0 - 0
core/geometry/neighbours.py → core/geometry/gneighbours.py


+ 0 - 0
core/geometry/rectangle.py → core/geometry/grectangle.py


+ 4 - 4
core/geometry/triangle.py → core/geometry/gtriangle.py

@@ -14,17 +14,17 @@ from core.geometry.cube_coords import cv_off_cube, cube_round, cv_cube_off
 ANGLES = [1, 2, 3]
 
 def triangle(geometry, xa, ya, xh, yh, iAngle):
-    if geometry == constants.SQUAREGRID:
+    if geometry == constants.SQUARE:
         return triangle_sq(xa, ya, xh, yh, iAngle)
-    elif geometry == constants.HEXGRID: 
+    elif geometry == constants.HEX: 
         return triangle_hex(xa, ya, xh, yh, iAngle)
     else:
         raise ValueError("'geometry' has to be a value from GRID_GEOMETRIES")
 
 def triangle3d(geometry, xa, ya, za, xh, yh, zh, iAngle):
-    if geometry == constants.SQUAREGRID:
+    if geometry == constants.SQUARE:
         return triangle_sq_3d(xa, ya, za, xh, yh, zh, iAngle)
-    elif geometry == constants.HEXGRID: 
+    elif geometry == constants.HEX: 
         return triangle_hex_3d(xa, ya, za, xh, yh, zh, iAngle)
     else:
         raise ValueError("'geometry' has to be a value from GRID_GEOMETRIES")

+ 7 - 3
core/geometry/zone.py → core/geometry/gzone.py

@@ -3,17 +3,21 @@ Created on 19 nov. 2016
 
 @author: olinox
 '''
-from core.geometry import neighbours
+from core.geometry import gneighbours
 
 
 def zone(geometry, x0, y0, radius):
     """ returns the list of the coordinates of the cells in the zone around (x0, y0)
     """
+    if not all(isinstance(c, int) for c in [x0, y0, radius]):
+        raise TypeError("x0, y0, radius have to be integers")
+    if not radius >= 0:
+        raise ValueError("radius has to be positive")
     buffer = frozenset( [ (x0, y0) ] )
-        
+
     for _ in range(0, radius):
         current = buffer
         for x, y in current:
-            buffer |= frozenset( neighbours.neighbours_of( geometry, x, y ) )
+            buffer |= frozenset( gneighbours.neighbours_of( geometry, x, y ) )
 
     return list(buffer)

+ 0 - 0
tests/geometry/__init__.py


+ 96 - 0
tests/geometry/test_line.py

@@ -0,0 +1,96 @@
+'''
+Created on 20 nov. 2016
+
+@author: olinox
+'''
+import unittest
+
+from core import constants
+from core.geometry import gline
+
+
+class Test(unittest.TestCase):
+    """test line algorithms"""
+
+    def test_hex_line(self):
+        """ 2d line on hexagonal grid """
+        geometry = constants.HEX
+        line = gline.line2d(geometry, 1,1,1,1)
+        self.assertEqual(line, [(1,1)])
+        
+        line = gline.line2d(geometry, 0,0,1,1)
+        self.assertEqual(line, [(0,0), (0,1), (1,1)])
+ 
+        line = gline.line2d(geometry, 0,0,7,3)
+        self.assertEqual(line, [(0,0), (1,0), (2,1), (3,1), (4,2), (5,2), (6,3), (7,3)] )
+ 
+        line = gline.line2d(geometry, 4,3,0,3)
+        self.assertEqual(line, [(4,3), (3,2), (2,3), (1,2), (0,3)] )
+ 
+        line = gline.line2d(geometry, 3,0,3,3)
+        self.assertEqual(line, [(3,0), (3,1), (3,2), (3,3)] )
+ 
+        
+    def test_squ_line(self):
+        """ 2d line on square grid """
+        geometry = constants.SQUARE
+        line = gline.line2d(geometry,0,0,0,1)
+        self.assertEqual(line, [(0,0), (0,1)])
+        
+        line = gline.line2d(geometry,0,0,1,1)
+        self.assertEqual(line, [(0,0), (1,1)])
+        
+        line = gline.line2d(geometry,0,0,7,3)
+        self.assertEqual(line, [(0,0), (1,0), (2,1), (3,1), (4,2), (5,2), (6,3), (7,3)] )
+ 
+        line = gline.line2d(geometry,4,3,0,3)
+        self.assertEqual(line, [(4,3), (3,3), (2,3), (1,3), (0,3)] )
+ 
+        line = gline.line2d(geometry,3,0,3,3)
+        self.assertEqual(line, [(3,0), (3,1), (3,2), (3,3)] )
+    
+    def test_hex_line_3d(self):
+        """ 3d line on hexagonal grid """
+        geometry = constants.HEX
+        line = gline.line3d(geometry,1,1,1,1,1,1)
+        self.assertEqual(line, [(1,1,1)])
+    
+        line = gline.line3d(geometry,1,1,0,1,1,1)
+        self.assertEqual(line, [(1,1,0), (1,1,1)])
+    
+        line = gline.line3d(geometry,0,0,0,1,1,1)
+        self.assertEqual(line, [(0,0,0), (0,1,0), (1,1,1)])
+    
+        line = gline.line3d(geometry,0,0,0,7,3,7)
+        self.assertEqual(line, [(0,0,0), (1,0,1), (2,1,2), (3,1,3), (4,2,4), (5,2,5), (6,3,6), (7,3,7)] )
+ 
+        line = gline.line3d(geometry,4,3,10,0,3,3)
+        self.assertEqual(line, [(4,3,10), (3,2,9), (3,2,8), (2,3,7), (2,3,6), (1,2,5), (1,2,4), (0,3,3)] )
+ 
+        line = gline.line3d(geometry,3,0,0,3,3,0)
+        self.assertEqual(line, [(3,0,0), (3,1,0), (3,2,0), (3,3,0)] )
+        
+    def test_squ_line_3d(self):
+        """ 3d line on square grid """
+        geometry = constants.SQUARE
+        line = gline.line3d(geometry,1,1,1,1,1,1)
+        self.assertEqual(line, [(1,1,1)])
+    
+        line = gline.line3d(geometry,1,1,0,1,1,1)
+        self.assertEqual(line, [(1,1,0), (1,1,1)])
+    
+        line = gline.line3d(geometry,0,0,0,1,1,1)
+        self.assertEqual(line, [(0,0,0), (1,1,1)])
+    
+        line = gline.line3d(geometry,0,0,0,7,3,7)
+        self.assertEqual(line, [(0,0,0), (1,0,1), (2,1,2), (3,1,3), (4,2,4), (5,2,5), (6,3,6), (7,3,7)] )
+ 
+        line = gline.line3d(geometry,4,3,10,0,3,3)
+        self.assertEqual(line, [(4,3,10), (3,3,9), (3,3,8), (2,3,7), (2,3,6), (1,3,5), (1,3,4), (0,3,3)] )
+ 
+        line = gline.line3d(geometry,3,0,0,3,3,0)
+        self.assertEqual(line, [(3,0,0), (3,1,0), (3,2,0), (3,3,0)] )
+
+
+if __name__ == "__main__":
+    unittest.main()

+ 43 - 0
tests/geometry/test_zone.py

@@ -0,0 +1,43 @@
+'''
+Created on 20 nov. 2016
+
+@author: olinox
+'''
+import unittest
+
+from core import constants
+from core.geometry import gzone
+
+
+class Test(unittest.TestCase):
+    """ test the zone algorithm """
+
+    def test_hex_zone(self):
+        """ test the zone algo for hexagonal grid """
+        geometry = constants.HEX
+        self.assertEqual( gzone.zone( geometry, 3, 3, 0 ), [(3,3)])
+        self.assertEqual( gzone.zone( geometry, 3, 3, 1 ), [(3, 2), (2, 3), (3, 3), (4, 3), (4, 4), (3, 4), (2, 4)])
+        self.assertEqual( gzone.zone( geometry, 3, 3, 2 ), [(3, 2), (1, 3), (5, 4), (4, 5), (1, 4), (2, 3), (4, 2), \
+                                                            (2, 5), (5, 3), (1, 2), (3, 5), (3, 3), (4, 4), (3, 1), \
+                                                            (4, 3), (2, 2), (3, 4), (2, 4), (5, 2)] )
+
+    def test_squ_zone(self):
+        """ test the zone algo for square grid """
+        geometry = constants.SQUARE
+        self.assertEqual( gzone.zone( geometry, 3, 3, 0 ), [(3,3)])
+        self.assertEqual( gzone.zone( geometry, 3, 3, 1 ), [(3, 2), (3, 3), (4, 4), (2, 3), (4, 3), (2, 2), (4, 2), (3, 4), (2, 4)])
+        self.assertEqual( gzone.zone( geometry, 3, 3, 2 ), [(2, 4), (3, 2), (5, 4), (1, 3), (4, 5), (2, 1), (1, 4), (2, 3), (4, 2), \
+                                                            (5, 1), (2, 5), (3, 5), (5, 3), (1, 2), (3, 3), (5, 5), (4, 4), (3, 1), \
+                                                            (1, 5), (4, 3), (2, 2), (4, 1), (5, 2), (3, 4), (1, 1)])
+        
+    def test_errors(self):
+        """test the errors due to bad parameters"""
+        self.assertRaises( TypeError, gzone.zone, 5, 0, 0, "a")
+        self.assertRaises( TypeError, gzone.zone, 5, "a", 0, 1)
+        self.assertRaises( TypeError, gzone.zone, 5, 0, "a", 1)
+        self.assertRaises( ValueError, gzone.zone, 5, 0, 0, -1)
+        self.assertRaises( ValueError, gzone.zone, -1, 0, 0, 1)
+        self.assertRaises( ValueError, gzone.zone, "a", 0, 0, 1)
+
+if __name__ == "__main__":
+    unittest.main()

+ 101 - 0
tests/test_grid.py

@@ -0,0 +1,101 @@
+'''
+Created on 20 nov. 2016
+
+@author: olinox
+'''
+import unittest
+
+from core import constants
+from core.Grid import Grid, SquareGrid, HexGrid
+
+
+class Test(unittest.TestCase):
+
+    def test_init(self):
+        #square grid
+        _ = Grid( constants.SQUARE, 1, 1 )
+        _ = SquareGrid( 1, 1 )
+        
+        #hex grid
+        _ = Grid( constants.HEX, 1, 1 )
+        _ = HexGrid( 1, 1 )
+
+    def test_geometry(self):
+        grid = Grid( constants.SQUARE, 1, 1 )
+        self.assertEqual( grid.geometry, constants.SQUARE )
+        
+        grid.geometry = constants.HEX
+        self.assertEqual( grid.geometry, constants.HEX )
+
+        def _set_invalid_geometry():
+            grid.geometry = -1
+        self.assertRaises( ValueError, _set_invalid_geometry )
+
+    def test_dimensions(self):
+        
+        for cls in (SquareGrid, HexGrid):
+            grid = cls( 1, 1 )
+            self.assertEqual( grid.height, 1 )
+            self.assertEqual( grid.width, 1 )
+    
+            grid.height = 1000
+            self.assertEqual( grid.height, 1000 )
+            grid.width = 1000
+            self.assertEqual( grid.width, 1000 )
+    
+            def _set_invalid_height():
+                grid.height = -1
+            self.assertRaises( ValueError, _set_invalid_height )
+            
+            def _set_invalid_width():
+                grid.height = -1
+            self.assertRaises( ValueError, _set_invalid_width )
+        
+    def test_cases_number(self):
+        for cls in (SquareGrid, HexGrid):
+            grid = cls( 1, 1 )
+            self.assertEqual( grid.cases_number(), 1 )
+            
+            grid.width = 100
+            grid.height = 100
+            
+            self.assertEqual( grid.cases_number(), 10000 )
+    
+    def test_in_grid(self):
+        
+        for cls in (SquareGrid, HexGrid):
+            grid = cls( 10, 10 )
+            self.assertTrue( grid.in_grid(5, 5) )
+            self.assertFalse( grid.in_grid(11, 5) )
+            self.assertFalse( grid.in_grid(5, 11) )
+
+    def test_line(self):
+         
+        #line algorithm is tested in tests.geometry.test_line
+        
+        grid = SquareGrid(10, 10)
+         
+        line = grid.line(0,0,0,1)
+        self.assertEqual(line, [(0,0), (0,1)])
+        
+    def test_line_3d(self):
+        
+        #line algorithm is tested in tests.geometry.test_line
+        
+        grid = HexGrid(10, 10)
+    
+        line = grid.line3d(1,1,1,1,1,1)
+        self.assertEqual(line, [(1,1,1)])
+    
+
+    def test_hex_zone(self):
+        
+        #zone algorithm is tested in tests.geometry.test_zone
+        
+        grid = HexGrid(10,10)
+        zone = grid.zone( 0, 0, 0 )
+        self.assertEqual(zone, [(0,0,0)])
+
+
+if __name__ == "__main__":
+    unittest.main()