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

Last change on this file since 7854 was 7720, checked in by James Hudson, 15 years ago

Fixed failing unit tests.

File size: 3.5 KB
Line 
1import unittest
2import numpy as num
3
4from aabb import AABB
5from quad import Cell
6
7import types, sys
8
9#-------------------------------------------------------------
10
11class Test_Geometry(unittest.TestCase):
12
13    def setUp(self):
14        pass
15
16    def tearDown(self):
17        pass
18
19    def test_AABB_contains(self):
20        box = AABB(1, 21, 1, 11)
21        assert box.contains([10, 5])
22        assert box.contains([1, 1])
23        assert box.contains([20, 6])
24        assert not box.contains([-1, -1])
25        assert not box.contains([5, 70])
26        assert not box.contains([6, -70])
27        assert not box.contains([-1, 6])
28        assert not box.contains([50, 6])       
29       
30    def test_AABB_split_vert(self):
31        parent = AABB(1, 21, 1, 11)
32       
33        child1, child2 = parent.split(0.6)
34
35        self.assertEqual(child1.xmin, 1)
36        self.assertEqual(child1.xmax, 13)
37        self.assertEqual(child1.ymin, 1)
38        self.assertEqual(child1.ymax, 11)
39       
40        self.assertEqual(child2.xmin, 9)
41        self.assertEqual(child2.xmax, 21)
42        self.assertEqual(child2.ymin, 1)
43        self.assertEqual(child2.ymax, 11)   
44
45    def test_AABB_split_horiz(self):
46        parent = AABB(1, 11, 1, 41)
47       
48        child1, child2 = parent.split(0.6)
49
50        self.assertEqual(child1.xmin, 1)
51        self.assertEqual(child1.xmax, 11)
52        self.assertEqual(child1.ymin, 1)
53        self.assertEqual(child1.ymax, 25)
54       
55        self.assertEqual(child2.xmin, 1)
56        self.assertEqual(child2.xmax, 11)
57        self.assertEqual(child2.ymin, 17)
58        self.assertEqual(child2.ymax, 41)         
59       
60    def test_add_data(self):
61        cell = Cell(AABB(0,10, 0,5), None)
62        cell.insert([(AABB(1,3, 1, 3), 111), (AABB(8,9, 1, 2), 222),  \
63                     (AABB(7, 8, 3, 4), 333), (AABB(1, 10, 0, 1), 444)])
64
65        result = cell.retrieve()
66        assert type(result) in [types.ListType,types.TupleType],\
67                            'should be a list'
68
69        self.assertEqual(len(result),4)
70       
71    def test_search(self):
72        test_tag = 222
73        cell = Cell(AABB(0,10, 0,5), None)
74        cell.insert([(AABB(1,3, 1, 3), 111), (AABB(8,9, 1, 2), test_tag),  \
75                     (AABB(7, 8, 3, 4), 333), (AABB(1, 10, 0, 1), 444)])
76
77        result = cell.search([8.5, 1.5])
78        assert type(result) in [types.ListType,types.TupleType],\
79                            'should be a list'
80        assert(len(result) == 1)
81        data, node = result[0]
82        self.assertEqual(data, test_tag, 'only 1 point should intersect')
83
84    def test_get_siblings(self):
85        """ Make sure children know their parent. """
86        cell = Cell(AABB(0,10, 0,5), None)
87        cell.insert([(AABB(1,3, 1, 3), 111), (AABB(8,9, 1, 2), 222)])
88
89        assert len(cell.children) == 2
90        assert cell.parent == None
91        assert cell.children[0].parent == cell
92        assert cell.children[1].parent == cell
93
94    def test_clear_1(self):
95        cell = Cell(AABB(0,10, 0,5), None)   
96        cell.insert([(AABB(1,3, 1, 3), 111), (AABB(8,9, 1, 2), 222),  \
97                     (AABB(7, 8, 3, 4), 333), (AABB(1, 10, 0, 1), 444)])
98                     
99        assert len(cell.retrieve()) == 4
100        cell.clear()
101
102        assert len(cell.retrieve()) == 0
103
104################################################################################
105
106if __name__ == "__main__":
107    mysuite = unittest.makeSuite(Test_Geometry, 'test')
108    runner = unittest.TextTestRunner()
109    runner.run(mysuite)
Note: See TracBrowser for help on using the repository browser.