1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | |
---|
4 | import os |
---|
5 | import unittest |
---|
6 | import tempfile |
---|
7 | |
---|
8 | from anuga.pmesh.mesh import Vertex, Segment, Mesh |
---|
9 | from ungenerate import load_ungenerate |
---|
10 | |
---|
11 | class ungenerateTestCase(unittest.TestCase): |
---|
12 | def setUp(self): |
---|
13 | pass |
---|
14 | |
---|
15 | def tearDown(self): |
---|
16 | pass |
---|
17 | |
---|
18 | def test_ungenerateFileLoading(self): |
---|
19 | |
---|
20 | fileName = tempfile.mktemp(".txt") |
---|
21 | file = open(fileName,"w") |
---|
22 | file.write(" 1 ?? ??\n\ |
---|
23 | 0.0 0.0\n\ |
---|
24 | 1.0 0.0\n\ |
---|
25 | 1.0 1.0\n\ |
---|
26 | 0.0 1.0\n\ |
---|
27 | 0.0 0.0\n\ |
---|
28 | END\n\ |
---|
29 | 2 ?? ??\n\ |
---|
30 | 10.0 10.0\n\ |
---|
31 | 10.0 20.0\n\ |
---|
32 | 20.0 20.0\n\ |
---|
33 | 10.0 10.0\n\ |
---|
34 | END\n\ |
---|
35 | END\n") |
---|
36 | file.close() |
---|
37 | |
---|
38 | |
---|
39 | a = Vertex (0.0, 0.0) #, attributes = [1.1]) |
---|
40 | b = Vertex (0.0, 40.0) #, attributes = [1.2]) |
---|
41 | c = Vertex (40.0,40.0) #, attributes = [1.3]) |
---|
42 | d = Vertex (40.0,0.0) #, attributes = [1.4]) |
---|
43 | |
---|
44 | s1 = Segment(a,b) |
---|
45 | s2 = Segment(b,c) |
---|
46 | s3 = Segment(c,d) |
---|
47 | s4 = Segment(d,a) |
---|
48 | |
---|
49 | m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4]) |
---|
50 | dict = load_ungenerate(fileName) |
---|
51 | #os.remove(fileName) |
---|
52 | |
---|
53 | tag = "DSG" |
---|
54 | Segment.set_default_tag(tag) |
---|
55 | m.addVertsSegs(dict) |
---|
56 | |
---|
57 | # have to reset this , since it's a class attribute |
---|
58 | Segment.set_default_tag("") |
---|
59 | |
---|
60 | self.failUnless(len(m.userSegments) ==11, |
---|
61 | 'Wrong segment list length.') |
---|
62 | self.failUnless(len(m.userVertices) == 11, |
---|
63 | 'Wrong vertex list length.') |
---|
64 | self.failUnless(m.userSegments[10].vertices[0] == m.userVertices[10], |
---|
65 | 'bad vertex on segment.') |
---|
66 | self.failUnless(m.userSegments[10].vertices[1] == m.userVertices[8], |
---|
67 | 'Bad segment.') |
---|
68 | self.failUnless(m.userSegments[10].tag == tag, |
---|
69 | 'wrong tag.') |
---|
70 | |
---|
71 | ## let's test the method |
---|
72 | a = Vertex (0.0, 0.0) #, attributes = [1.1]) |
---|
73 | b = Vertex (0.0, 40.0) #, attributes = [1.2]) |
---|
74 | c = Vertex (40.0,40.0) #, attributes = [1.3]) |
---|
75 | d = Vertex (40.0,0.0) #, attributes = [1.4]) |
---|
76 | |
---|
77 | s1 = Segment(a,b) |
---|
78 | s2 = Segment(b,c) |
---|
79 | s3 = Segment(c,d) |
---|
80 | s4 = Segment(d,a) |
---|
81 | |
---|
82 | m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4]) |
---|
83 | |
---|
84 | tag = "DSG" |
---|
85 | initial_tag = "PIG" |
---|
86 | Segment.set_default_tag(initial_tag) |
---|
87 | m.import_ungenerate_file(fileName, tag=tag) |
---|
88 | |
---|
89 | os.remove(fileName) |
---|
90 | |
---|
91 | self.failUnless(Segment.get_default_tag() == initial_tag, |
---|
92 | 'Wrong segment list length.') |
---|
93 | |
---|
94 | |
---|
95 | # have to reset this , since it's a class attribute |
---|
96 | Segment.set_default_tag("") |
---|
97 | |
---|
98 | self.failUnless(len(m.userSegments) ==11, |
---|
99 | 'Wrong segment list length.') |
---|
100 | self.failUnless(len(m.userVertices) == 11, |
---|
101 | 'Wrong vertex list length.') |
---|
102 | self.failUnless(m.userSegments[10].vertices[0] == m.userVertices[10], |
---|
103 | 'bad vertex on segment.') |
---|
104 | self.failUnless(m.userSegments[10].vertices[1] == m.userVertices[8], |
---|
105 | 'Bad segment.') |
---|
106 | self.failUnless(m.userSegments[10].tag == tag, |
---|
107 | 'wrong tag.') |
---|
108 | |
---|
109 | def test_import_ungenerate_file(self): |
---|
110 | |
---|
111 | fileName = tempfile.mktemp(".txt") |
---|
112 | file = open(fileName,"w") |
---|
113 | file.write(" 1 ?? ??\n\ |
---|
114 | 10.0 10.0\n\ |
---|
115 | 11.0 10.0\n\ |
---|
116 | 11.0 11.0\n\ |
---|
117 | 10.0 11.0\n\ |
---|
118 | 10.0 10.0\n\ |
---|
119 | END\n\ |
---|
120 | 2 ?? ??\n\ |
---|
121 | 20.0 20.0\n\ |
---|
122 | 20.0 30.0\n\ |
---|
123 | 30.0 30.0\n\ |
---|
124 | END\n\ |
---|
125 | END\n") |
---|
126 | file.close() |
---|
127 | |
---|
128 | |
---|
129 | a = Vertex (0.0, 0.0) #, attributes = [1.1]) |
---|
130 | b = Vertex (0.0, 40.0) #, attributes = [1.2]) |
---|
131 | c = Vertex (40.0,40.0) #, attributes = [1.3]) |
---|
132 | d = Vertex (40.0,0.0) #, attributes = [1.4]) |
---|
133 | |
---|
134 | s1 = Segment(a,b) |
---|
135 | s2 = Segment(b,c) |
---|
136 | s3 = Segment(c,d) |
---|
137 | s4 = Segment(d,a) |
---|
138 | |
---|
139 | m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4]) |
---|
140 | dict = load_ungenerate(fileName) |
---|
141 | #os.remove(fileName) |
---|
142 | |
---|
143 | tag = "DSG" |
---|
144 | Segment.set_default_tag(tag) |
---|
145 | m.addVertsSegs(dict) |
---|
146 | |
---|
147 | self.failUnless(len(m.userSegments) ==11, |
---|
148 | 'Wrong segment list length.') |
---|
149 | self.failUnless(len(m.userVertices) == 11, |
---|
150 | 'Wrong vertex list length.') |
---|
151 | |
---|
152 | # Test the method |
---|
153 | a = Vertex (0.0, 0.0) #, attributes = [1.1]) |
---|
154 | b = Vertex (0.0, 40.0) #, attributes = [1.2]) |
---|
155 | c = Vertex (40.0,40.0) #, attributes = [1.3]) |
---|
156 | d = Vertex (40.0,0.0) #, attributes = [1.4]) |
---|
157 | |
---|
158 | s1 = Segment(a,b) |
---|
159 | s2 = Segment(b,c) |
---|
160 | s3 = Segment(c,d) |
---|
161 | s4 = Segment(d,a) |
---|
162 | |
---|
163 | m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4]) |
---|
164 | tag = "DSG" |
---|
165 | initial_tag = "PIG" |
---|
166 | Segment.set_default_tag(initial_tag) |
---|
167 | m.import_ungenerate_file(fileName, tag=tag, region_tag="swamp") |
---|
168 | |
---|
169 | os.remove(fileName) |
---|
170 | |
---|
171 | self.failUnless(Segment.get_default_tag() == initial_tag, |
---|
172 | 'Wrong segment list length.') |
---|
173 | m.export_mesh_file("swamp.tsh") |
---|
174 | #print "m.userSegments",m.userSegments |
---|
175 | self.failUnless(len(m.userSegments) ==11, |
---|
176 | 'Wrong segment list length.') |
---|
177 | self.failUnless(len(m.userVertices) == 11, |
---|
178 | 'Wrong vertex list length.') |
---|
179 | self.failUnless(len(m.regions) == 2, |
---|
180 | 'Wrong regions list length.') |
---|
181 | self.failUnless(m.regions[0].getTag() == "swamp", |
---|
182 | 'Wrong regions tag.') |
---|
183 | self.failUnless(m.regions[1].getTag() == "swamp", |
---|
184 | 'Wrong regions 1 tag.') |
---|
185 | |
---|
186 | # have to reset this , since it's a class attribute |
---|
187 | Segment.set_default_tag("") |
---|
188 | |
---|
189 | |
---|
190 | def test_import_ungenerate_file_different_region_tags(self): |
---|
191 | |
---|
192 | fileName = tempfile.mktemp(".txt") |
---|
193 | file = open(fileName,"w") |
---|
194 | file.write(" 1 ?? ??\n\ |
---|
195 | 10.0 10.0\n\ |
---|
196 | 11.0 10.0\n\ |
---|
197 | 11.0 11.0\n\ |
---|
198 | 10.0 11.0\n\ |
---|
199 | 10.0 10.0\n\ |
---|
200 | END\n\ |
---|
201 | 2 ?? ??\n\ |
---|
202 | 20.0 20.0\n\ |
---|
203 | 20.0 30.0\n\ |
---|
204 | 30.0 30.0\n\ |
---|
205 | END\n\ |
---|
206 | END\n") |
---|
207 | file.close() |
---|
208 | |
---|
209 | |
---|
210 | a = Vertex (0.0, 0.0) #, attributes = [1.1]) |
---|
211 | b = Vertex (0.0, 40.0) #, attributes = [1.2]) |
---|
212 | c = Vertex (40.0,40.0) #, attributes = [1.3]) |
---|
213 | d = Vertex (40.0,0.0) #, attributes = [1.4]) |
---|
214 | |
---|
215 | s1 = Segment(a,b) |
---|
216 | s2 = Segment(b,c) |
---|
217 | s3 = Segment(c,d) |
---|
218 | s4 = Segment(d,a) |
---|
219 | |
---|
220 | m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4]) |
---|
221 | dict = load_ungenerate(fileName) |
---|
222 | #os.remove(fileName) |
---|
223 | |
---|
224 | tag = "DSG" |
---|
225 | Segment.set_default_tag(tag) |
---|
226 | m.addVertsSegs(dict) |
---|
227 | |
---|
228 | self.failUnless(len(m.userSegments) ==11, |
---|
229 | 'Wrong segment list length.') |
---|
230 | self.failUnless(len(m.userVertices) == 11, |
---|
231 | 'Wrong vertex list length.') |
---|
232 | |
---|
233 | # Test the method |
---|
234 | a = Vertex (0.0, 0.0) #, attributes = [1.1]) |
---|
235 | b = Vertex (0.0, 40.0) #, attributes = [1.2]) |
---|
236 | c = Vertex (40.0,40.0) #, attributes = [1.3]) |
---|
237 | d = Vertex (40.0,0.0) #, attributes = [1.4]) |
---|
238 | |
---|
239 | s1 = Segment(a,b) |
---|
240 | s2 = Segment(b,c) |
---|
241 | s3 = Segment(c,d) |
---|
242 | s4 = Segment(d,a) |
---|
243 | |
---|
244 | m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4]) |
---|
245 | tag = "DSG" |
---|
246 | initial_tag = "PIG" |
---|
247 | Segment.set_default_tag(initial_tag) |
---|
248 | m.import_ungenerate_file(fileName, tag=tag, region_tag=["swamp","coastalp"]) |
---|
249 | |
---|
250 | os.remove(fileName) |
---|
251 | |
---|
252 | self.failUnless(Segment.get_default_tag() == initial_tag, |
---|
253 | 'Wrong segment list length.') |
---|
254 | m.export_mesh_file("swamp.tsh") |
---|
255 | #print "m.userSegments",m.userSegments |
---|
256 | self.failUnless(len(m.userSegments) ==11, |
---|
257 | 'Wrong segment list length.') |
---|
258 | self.failUnless(len(m.userVertices) == 11, |
---|
259 | 'Wrong vertex list length.') |
---|
260 | self.failUnless(len(m.regions) == 2, |
---|
261 | 'Wrong regions list length.') |
---|
262 | self.failUnless(m.regions[0].getTag() == "swamp", |
---|
263 | 'Wrong regions tag.') |
---|
264 | self.failUnless(m.regions[1].getTag() == "coastalp", |
---|
265 | 'Wrong regions 1 tag.') |
---|
266 | |
---|
267 | |
---|
268 | # have to reset this , since it's a class attribute |
---|
269 | Segment.set_default_tag("") |
---|
270 | |
---|
271 | |
---|
272 | |
---|
273 | ################################################################################ |
---|
274 | |
---|
275 | if __name__ == "__main__": |
---|
276 | suite = unittest.makeSuite(ungenerateTestCase,'test') |
---|
277 | runner = unittest.TextTestRunner() #verbosity=2) |
---|
278 | runner.run(suite) |
---|
279 | |
---|