comparison tests/swig_tests/location_tests.py @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 81641655bc38
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 #!/usr/bin/env python
2 from swig_test_utils import *
3 import fifelog
4
5 P = fife.ModelCoordinate
6 D = fife.ExactModelCoordinate
7
8 NEAR_TRESHOLD = 0.001
9 def is_near(x, y):
10 return (y-NEAR_TRESHOLD) < x < (y+NEAR_TRESHOLD)
11
12 def pt_is_near(p1, p2):
13 return ((p2.x-NEAR_TRESHOLD) < p1.x < (p2.x+NEAR_TRESHOLD)) and \
14 ((p2.y-NEAR_TRESHOLD) < p1.y < (p2.y+NEAR_TRESHOLD))
15
16 class TestLocation(unittest.TestCase):
17 def setUp(self):
18 self.squaregrid1 = fife.SquareGrid()
19 self.squaregrid2 = fife.SquareGrid()
20 self.layer1 = fife.Layer("layer1", None, self.squaregrid1)
21 self.layer2 = fife.Layer("layer2", None, self.squaregrid2)
22 self.loc1 = fife.Location(self.layer1)
23 self.loc2 = fife.Location(self.layer2)
24
25 def testBasicMapping(self):
26 self.loc1.setLayerCoordinates(P(5,5))
27 pt = self.loc1.getLayerCoordinates()
28 self.assertEqual(pt, P(5,5))
29 pt = self.loc1.getExactLayerCoordinates()
30 self.assertEqual(pt, D(5,5))
31 pt = self.loc1.getMapCoordinates()
32 self.assertEqual(pt, D(5,5))
33 pt = self.loc1.getLayerCoordinates(self.layer2)
34 self.assertEqual(pt, P(5,5))
35 pt = self.loc1.getExactLayerCoordinates(self.layer2)
36 self.assertEqual(pt, D(5,5))
37
38 def testSquareGridScale(self):
39 self.squaregrid2.setXScale(5)
40 self.squaregrid2.setYScale(5)
41 self.loc1.setLayerCoordinates(P(5,5))
42 pt = self.loc1.getLayerCoordinates(self.layer2)
43 self.assertEqual(pt, P(1,1))
44 pt = self.loc1.getExactLayerCoordinates(self.layer2)
45 self.assertEqual(pt, D(5,5))
46
47 def testSquareGridRotation(self):
48 self.squaregrid1.setRotation(90)
49 self.loc1.setLayerCoordinates(P(3,3))
50 pt = self.loc1.getMapCoordinates()
51 self.assertEqual(pt, D(-3,3))
52
53 def testSquareGridShifts(self):
54 self.squaregrid1.setXShift(-3)
55 self.squaregrid1.setYShift(-3)
56 self.loc1.setLayerCoordinates(P(3,3))
57 pt = self.loc1.getMapCoordinates()
58 self.assertEqual(pt, D(0,0))
59
60 def testSquareCombinations(self):
61 # correct order in combinations = translate, rotate, scale
62 self.squaregrid1.setXShift(2)
63 self.squaregrid1.setYShift(2)
64 self.squaregrid1.setRotation(90)
65 self.squaregrid1.setXScale(5)
66 self.squaregrid1.setYScale(5)
67 self.loc1.setLayerCoordinates(P(1,1))
68 pt = self.loc1.getMapCoordinates()
69 print pt
70 self.assert_(is_near(pt.x, -3))
71 self.assert_(is_near(pt.y, 7))
72
73 class TestHexGrid(unittest.TestCase):
74 def setUp(self):
75 self.hexgrid1 = fife.HexGrid()
76 self.squaregrid1 = fife.SquareGrid()
77 self.hex_layer1 = fife.Layer("hexlayer", None, self.hexgrid1)
78 self.square_layer1 = fife.Layer("squarelayer", None, self.squaregrid1)
79 self.loc1 = fife.Location(self.hex_layer1)
80 self.loc2 = fife.Location(self.square_layer1)
81 self.engine = getEngine(True)
82 self.log = fifelog.LogManager(self.engine)
83 #self.log.setVisibleModules('hexgrid', 'cellgrid')
84
85 def tearDown(self):
86 del self.engine
87 del self.log
88
89 def testHexGrid0row(self):
90 self.loc1.setMapCoordinates(D(-2,0))
91 self.assert_(self.loc1.getLayerCoordinates() == P(-2,0))
92
93 self.loc1.setMapCoordinates(D(-1,0))
94 self.assert_(self.loc1.getLayerCoordinates() == P(-1,0))
95
96 self.loc1.setMapCoordinates(D(0,0))
97 self.assert_(self.loc1.getLayerCoordinates() == P(0,0))
98
99 self.loc1.setMapCoordinates(D(1,0))
100 self.assert_(self.loc1.getLayerCoordinates() == P(1,0))
101
102 self.loc1.setMapCoordinates(D(2,0))
103 self.assert_(self.loc1.getLayerCoordinates() == P(2,0))
104
105 def testHexGrid1row(self):
106 self.loc1.setMapCoordinates(D(-1.1,1))
107 self.assert_(self.loc1.getLayerCoordinates() == P(-2,1))
108
109 self.loc1.setMapCoordinates(D(-0.5,1))
110 self.assert_(self.loc1.getLayerCoordinates() == P(-1,1))
111
112 self.loc1.setMapCoordinates(D(-0.1,1))
113 self.assert_(self.loc1.getLayerCoordinates() == P(-1,1))
114
115 self.loc1.setMapCoordinates(D(0.1,1))
116 self.assert_(self.loc1.getLayerCoordinates() == P(0,1))
117
118 def testHexGridm1row(self):
119 self.loc1.setMapCoordinates(D(-1.1,-1))
120 self.assert_(self.loc1.getLayerCoordinates() == P(-2,-1))
121
122 self.loc1.setMapCoordinates(D(-0.5,-1))
123 self.assert_(self.loc1.getLayerCoordinates() == P(-1,-1))
124
125 self.loc1.setMapCoordinates(D(-0.1,-1))
126 self.assert_(self.loc1.getLayerCoordinates() == P(-1,-1))
127
128 self.loc1.setMapCoordinates(D(0.1,-1))
129 self.assert_(self.loc1.getLayerCoordinates() == P(0,-1))
130
131 def testEdgeHits(self):
132 self.loc1.setMapCoordinates(D(0.5,0.5))
133 self.assert_(self.loc1.getLayerCoordinates() == P(0,1))
134
135 self.loc1.setMapCoordinates(D(0.1,0.4))
136 #print "______''___" + str(self.loc1.getLayerCoordinates())
137 self.assert_(self.loc1.getLayerCoordinates() == P(0,0))
138
139 self.loc1.setMapCoordinates(D(0.1,-0.4))
140 self.assert_(self.loc1.getLayerCoordinates() == P(0,0))
141
142 self.loc1.setMapCoordinates(D(-0.5,-0.5))
143 self.assert_(self.loc1.getLayerCoordinates() == P(-1,-1))
144
145 TEST_CLASSES = [TestHexGrid, TestLocation]
146
147 if __name__ == '__main__':
148 unittest.main()