source: trunk/anuga_core/source/anuga/geometry/test_geometry.py @ 7866

Last change on this file since 7866 was 7866, checked in by hudson, 14 years ago

More swb tests passing. Cleaned up some pylint errors.

File size: 3.7 KB
Line 
1""" Test for the geometry classes.
2
3    Pylint quality rating as of June 2010: 8.51/10.
4"""
5
6import unittest
7
8from aabb import AABB
9from quad import Cell
10
11import types
12
13#-------------------------------------------------------------
14
15class Test_Geometry(unittest.TestCase):
16    """ Test geometry classes. """
17    def setUp(self):
18        """ Generic set up for geometry tests. """
19        pass
20
21    def tearDown(self):
22        """ Generic shut down for geometry tests. """
23        pass
24
25    def test_aabb_contains(self):
26        """ Test if point is correctly classified as falling inside or
27            outside of bounding box. """
28        box = AABB(1, 21, 1, 11)
29        assert box.contains([10, 5])
30        assert box.contains([1, 1])
31        assert box.contains([20, 6])
32        assert not box.contains([-1, -1])
33        assert not box.contains([5, 70])
34        assert not box.contains([6, -70])
35        assert not box.contains([-1, 6])
36        assert not box.contains([50, 6])       
37       
38    def test_aabb_split_vert(self):
39        """ Test that a bounding box can be split correctly along an axis.
40        """
41        parent = AABB(1, 21, 1, 11)
42       
43        child1, child2 = parent.split(0.6)
44
45        self.assertEqual(child1.xmin, 1)
46        self.assertEqual(child1.xmax, 13)
47        self.assertEqual(child1.ymin, 1)
48        self.assertEqual(child1.ymax, 11)
49       
50        self.assertEqual(child2.xmin, 9)
51        self.assertEqual(child2.xmax, 21)
52        self.assertEqual(child2.ymin, 1)
53        self.assertEqual(child2.ymax, 11)   
54
55    def test_aabb_split_horiz(self):
56        """ Test that a bounding box will be split along the horizontal axis
57        correctly. """
58        parent = AABB(1, 11, 1, 41)
59       
60        child1, child2 = parent.split(0.6)
61
62        self.assertEqual(child1.xmin, 1)
63        self.assertEqual(child1.xmax, 11)
64        self.assertEqual(child1.ymin, 1)
65        self.assertEqual(child1.ymax, 25)
66       
67        self.assertEqual(child2.xmin, 1)
68        self.assertEqual(child2.xmax, 11)
69        self.assertEqual(child2.ymin, 17)
70        self.assertEqual(child2.ymax, 41)         
71       
72    def test_add_data(self):
73        """ Test add and retrieve arbitrary data from tree structure. """
74        cell = Cell(AABB(0, 10, 0, 5), None)
75        cell.insert([(AABB(1, 3, 1, 3), 111), (AABB(8, 9, 1, 2), 222),  \
76                     (AABB(7, 8, 3, 4), 333), (AABB(1, 10, 0, 1), 444)])
77
78        result = cell.retrieve()
79        assert type(result) in [types.ListType,types.TupleType], \
80                            'should be a list'
81
82        self.assertEqual(len(result), 4)
83       
84    def test_search(self):
85        """ Test search tree for an intersection. """
86        test_tag = 222
87        cell = Cell(AABB(0, 10, 0,5), None)
88        cell.insert([(AABB(1, 3, 1, 3), 111), (AABB(8, 9, 1, 2), test_tag),  \
89                     (AABB(7, 8, 3, 4), 333), (AABB(1, 10, 0, 1), 444)])
90
91        result = cell.search([8.5, 1.5])
92        assert type(result) in [types.ListType, types.TupleType], \
93                            'should be a list'
94        assert(len(result) == 1)
95        data, _ = result[0]
96        self.assertEqual(data, test_tag, 'only 1 point should intersect')
97
98    def test_get_siblings(self):
99        """ Make sure children know their parent. """
100        cell = Cell(AABB(0, 10, 0, 5), None)
101        cell.insert([(AABB(1, 3, 1, 3), 111), (AABB(8, 9, 1, 2), 222)])
102
103        assert len(cell.children) == 2
104        assert cell.parent == None
105        assert cell.children[0].parent == cell
106        assert cell.children[1].parent == cell
107
108
109################################################################################
110
111if __name__ == "__main__":
112    mysuite = unittest.makeSuite(Test_Geometry, 'test')
113    runner = unittest.TextTestRunner()
114    runner.run(mysuite)
Note: See TracBrowser for help on using the repository browser.