1 | import unittest |
---|
2 | from quad import Cell, build_quadtree |
---|
3 | |
---|
4 | #from domain import * |
---|
5 | from general_mesh import General_mesh as Mesh |
---|
6 | |
---|
7 | import types, sys |
---|
8 | |
---|
9 | #------------------------------------------------------------- |
---|
10 | |
---|
11 | class Test_Quad(unittest.TestCase): |
---|
12 | |
---|
13 | def setUp(self): |
---|
14 | self.cell = Cell(100, 140, 0, 40, 'cell') |
---|
15 | |
---|
16 | a = [3, 107] |
---|
17 | b = [5, 107] |
---|
18 | c = [5, 105] |
---|
19 | d = [7, 107] |
---|
20 | e = [15, 115] |
---|
21 | f = [15, 130] |
---|
22 | g = [30, 110] |
---|
23 | h = [30, 130] |
---|
24 | |
---|
25 | points = [a, b, c, d, e, f, g, h] |
---|
26 | #bac, bce, ecf, dbe, daf, dae |
---|
27 | vertices = [[1,0,2], [1,3,4], [1,2,3], [5,4,7], [4,6,7]] |
---|
28 | |
---|
29 | mesh = Mesh(points, vertices) |
---|
30 | self.mesh = mesh |
---|
31 | Cell.initialise(mesh) |
---|
32 | |
---|
33 | def tearDown(self): |
---|
34 | pass |
---|
35 | |
---|
36 | def test_add_points_2_cell(self): |
---|
37 | self.cell.insert(0) |
---|
38 | self.cell.insert(1) |
---|
39 | |
---|
40 | result = self.cell.retrieve() |
---|
41 | assert type(result) in [types.ListType,types.TupleType],\ |
---|
42 | 'should be a list' |
---|
43 | self.assertEqual(len(result),2) |
---|
44 | |
---|
45 | def test_add_points_2_cellII(self): |
---|
46 | self.cell.insert([0,1,2,3,4,5,6,7]) |
---|
47 | |
---|
48 | result = self.cell.retrieve() |
---|
49 | assert type(result) in [types.ListType,types.TupleType],\ |
---|
50 | 'should be a list' |
---|
51 | self.assertEqual(len(result),8) |
---|
52 | |
---|
53 | |
---|
54 | def test_search(self): |
---|
55 | self.cell.insert([0,1,2,3,4,5,6,7]) |
---|
56 | self.cell.split(4) |
---|
57 | |
---|
58 | result = self.cell.search(x = 1, y = 101) |
---|
59 | assert type(result) in [types.ListType,types.TupleType],\ |
---|
60 | 'should be a list' |
---|
61 | self.assertEqual(result, [0,1,2,3]) |
---|
62 | |
---|
63 | |
---|
64 | def test_clear_1(self): |
---|
65 | self.cell.insert([0,1,2,3,4,5,6,7]) |
---|
66 | assert self.cell.count() == 8 |
---|
67 | self.cell.clear() |
---|
68 | |
---|
69 | #This one actually revealed a bug :-) |
---|
70 | assert self.cell.count() == 0 |
---|
71 | |
---|
72 | def test_clear_2(self): |
---|
73 | self.cell.insert([0,1,2,3,4,5,6,7]) |
---|
74 | assert self.cell.count() == 8 |
---|
75 | self.cell.split(2) |
---|
76 | assert self.cell.count() == 8 |
---|
77 | |
---|
78 | self.cell.clear() |
---|
79 | assert self.cell.count() == 0 |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | def test_split(self): |
---|
84 | self.cell.insert([0,1,2,3,4,5,6,7], split = False) |
---|
85 | |
---|
86 | #No children yet |
---|
87 | assert self.cell.children is None |
---|
88 | assert self.cell.count() == 8 |
---|
89 | |
---|
90 | #Split |
---|
91 | self.cell.split(4) |
---|
92 | #self.cell.show() |
---|
93 | #self.cell.show_all() |
---|
94 | |
---|
95 | |
---|
96 | #Now there are children |
---|
97 | assert self.cell.children is not None |
---|
98 | assert self.cell.count() == 8 |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | def test_collapse(self): |
---|
103 | self.cell.insert([0,1,2,3,4,5,6,7], split = False) |
---|
104 | |
---|
105 | #Split maximally |
---|
106 | self.cell.split(1) |
---|
107 | |
---|
108 | #Now there are children |
---|
109 | assert self.cell.children is not None |
---|
110 | assert self.cell.count() == 8 |
---|
111 | |
---|
112 | #Collapse |
---|
113 | self.cell.collapse(8) |
---|
114 | |
---|
115 | #No children |
---|
116 | assert self.cell.children is None |
---|
117 | assert self.cell.count() == 8 |
---|
118 | |
---|
119 | def test_build_quadtree(self): |
---|
120 | |
---|
121 | Q = build_quadtree(self.mesh) |
---|
122 | assert Q.count() == 8 |
---|
123 | |
---|
124 | #Q.show() |
---|
125 | |
---|
126 | result = Q.search(3, 105) |
---|
127 | assert type(result) in [types.ListType,types.TupleType],\ |
---|
128 | 'should be a list' |
---|
129 | #print "result",result |
---|
130 | self.assertEqual(result, [0,1,2,3]) |
---|
131 | |
---|
132 | |
---|
133 | def test_build_quadtreeII(self): |
---|
134 | |
---|
135 | self.cell = Cell(100, 140, 0, 40, 'cell') |
---|
136 | |
---|
137 | p0 = [34.6292076111,-7999.92529297] |
---|
138 | p1 = [8000.0, 7999.0] |
---|
139 | p2 = [-7999.96630859, 7999.0] |
---|
140 | p3 = [34, 7999.97021484] |
---|
141 | |
---|
142 | points = [p0,p1,p2, p3] |
---|
143 | #bac, bce, ecf, dbe, daf, dae |
---|
144 | vertices = [[0,1,2],[0,2,3]] |
---|
145 | |
---|
146 | mesh = Mesh(points, vertices) |
---|
147 | |
---|
148 | #This was causing round off error |
---|
149 | Q = build_quadtree(mesh) |
---|
150 | |
---|
151 | #------------------------------------------------------------- |
---|
152 | if __name__ == "__main__": |
---|
153 | |
---|
154 | mysuite = unittest.makeSuite(Test_Quad,'test') |
---|
155 | runner = unittest.TextTestRunner() |
---|
156 | runner.run(mysuite) |
---|