1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | |
---|
4 | import tempfile |
---|
5 | import unittest |
---|
6 | |
---|
7 | import os |
---|
8 | import tempfile |
---|
9 | |
---|
10 | from os.path import splitext |
---|
11 | |
---|
12 | import Numeric as num |
---|
13 | |
---|
14 | from anuga.load_mesh.loadASCII import * |
---|
15 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
16 | import loadASCII |
---|
17 | |
---|
18 | class loadASCIITestCase(unittest.TestCase): |
---|
19 | def setUp(self): |
---|
20 | self.dict ={} |
---|
21 | self.dict['outline_segments'] = [(0, 1), (1, 2), (0, 2), (0, 3)] |
---|
22 | self.dict['outline_segment_tags'] = ['50', '40', '30', '20'] |
---|
23 | self.dict['holes'] = [(0.2, 0.6)] |
---|
24 | self.dict['point_attributes'] = [[5, 2], [4, 2], [3, 2], [2,2]] |
---|
25 | self.dict['regions'] = [(0.3, 0.3),(0.3, 0.4)] |
---|
26 | self.dict['region_tags'] = ['1.3', 'yeah'] |
---|
27 | self.dict['region_max_areas'] = [36.0,-7.1] |
---|
28 | self.dict['points'] = [(0.0, 0.0), (0.0, 4.0), (4.0, 0.0), (1.0, 1.0)] |
---|
29 | self.dict['vertices'] = [(0.0, 0.0), (0.0, 4.0), |
---|
30 | (4.0, 0.0), (1.0, 1.0), (2.0, 2.0)] |
---|
31 | self.dict['triangles'] = [(3, 2, 4), (1, 0, 3), |
---|
32 | (3, 4,1), (2, 3, 0)] |
---|
33 | self.dict['segments'] = [(0, 1), (1, 4), (2, 0), |
---|
34 | (0, 3), (4, 2)] |
---|
35 | self.dict['triangle_tags'] = ['1.3', '1.3', |
---|
36 | '1.3', '1.3'] |
---|
37 | self.dict['vertex_attributes'] = [[1.2,2.], [1.2,2.], |
---|
38 | [1.2,2.], [1.2,2.], [1.2,3.]] |
---|
39 | self.dict['triangle_neighbors'] = [[-1, 2, 3], [3, 2, -1], |
---|
40 | [-1, 1, 0], [1, -1, 0]] |
---|
41 | self.dict['segment_tags'] = ['50', '40', '30', '20', '40'] |
---|
42 | self.dict['vertex_attribute_titles'] = ['bed elevation', 'height'] |
---|
43 | self.dict['geo_reference'] = Geo_reference(56,1.9,1.9) |
---|
44 | |
---|
45 | self.sparse_dict ={} |
---|
46 | self.sparse_dict['outline_segments'] = [] |
---|
47 | self.sparse_dict['outline_segment_tags'] = [] |
---|
48 | self.sparse_dict['holes'] = [] |
---|
49 | self.sparse_dict['points'] = [(0.0, 0.0),(9,8)] |
---|
50 | self.sparse_dict['point_attributes'] = [[],[]] # points don't have to have |
---|
51 | # attributes |
---|
52 | self.sparse_dict['regions'] = [] |
---|
53 | self.sparse_dict['region_tags'] = [] |
---|
54 | self.sparse_dict['region_max_areas'] = [] |
---|
55 | |
---|
56 | self.sparse_dict['vertices'] = [] |
---|
57 | self.sparse_dict['triangles'] = [] |
---|
58 | self.sparse_dict['segments'] = [] |
---|
59 | self.sparse_dict['triangle_tags'] = [] |
---|
60 | self.sparse_dict['vertex_attributes'] = [] |
---|
61 | self.sparse_dict['triangle_neighbors'] = [] |
---|
62 | self.sparse_dict['segment_tags'] = [] |
---|
63 | self.sparse_dict['vertex_attribute_titles'] = [] |
---|
64 | |
---|
65 | self.blank_dict ={} |
---|
66 | self.blank_dict['outline_segments'] = [] |
---|
67 | self.blank_dict['outline_segment_tags'] = [] |
---|
68 | self.blank_dict['holes'] = [] |
---|
69 | self.blank_dict['points'] = [] |
---|
70 | self.blank_dict['point_attributes'] = [] |
---|
71 | self.blank_dict['regions'] = [] |
---|
72 | self.blank_dict['region_tags'] = [] |
---|
73 | self.blank_dict['region_max_areas'] = [] |
---|
74 | self.blank_dict['vertices'] = [] |
---|
75 | self.blank_dict['triangles'] = [] |
---|
76 | self.blank_dict['segments'] = [] |
---|
77 | self.blank_dict['triangle_tags'] = [] |
---|
78 | self.blank_dict['vertex_attributes'] = [] |
---|
79 | self.blank_dict['triangle_neighbors'] = [] |
---|
80 | self.blank_dict['segment_tags'] = [] |
---|
81 | self.blank_dict['vertex_attribute_titles'] = [] |
---|
82 | |
---|
83 | self.tri_dict ={} |
---|
84 | self.tri_dict['outline_segments'] = [[0,1]] |
---|
85 | self.tri_dict['outline_segment_tags'] = [''] |
---|
86 | self.tri_dict['holes'] = [] |
---|
87 | self.tri_dict['points'] = [(9,8),(7,8)] |
---|
88 | self.tri_dict['point_attributes'] = [[],[]] |
---|
89 | self.tri_dict['regions'] = [] |
---|
90 | self.tri_dict['region_tags'] = [] |
---|
91 | self.tri_dict['region_max_areas'] = [] |
---|
92 | self.tri_dict['vertices'] = [[9,8],[7,8], [4,5]] |
---|
93 | self.tri_dict['triangles'] = [[0,1,2]] |
---|
94 | self.tri_dict['segments'] = [[0,1]] |
---|
95 | self.tri_dict['triangle_tags'] = [''] |
---|
96 | self.tri_dict['vertex_attributes'] = None |
---|
97 | self.tri_dict['triangle_neighbors'] = [[0,0,0]] |
---|
98 | self.tri_dict['segment_tags'] = [''] |
---|
99 | self.tri_dict['vertex_attribute_titles'] = [] |
---|
100 | |
---|
101 | self.seg_dict ={} |
---|
102 | self.seg_dict['outline_segments'] = [[0,1]] |
---|
103 | self.seg_dict['outline_segment_tags'] = [''] |
---|
104 | self.seg_dict['holes'] = [] |
---|
105 | self.seg_dict['points'] = [(9,8),(7,8)] |
---|
106 | self.seg_dict['point_attributes'] = [[],[]] |
---|
107 | self.seg_dict['regions'] = [(5,4)] |
---|
108 | self.seg_dict['region_tags'] = [''] |
---|
109 | self.seg_dict['region_max_areas'] = [-999] |
---|
110 | self.seg_dict['vertices'] = [(9,8),(7,8)] |
---|
111 | self.seg_dict['triangles'] = [] |
---|
112 | self.seg_dict['segments'] = [[0,1]] |
---|
113 | self.seg_dict['triangle_tags'] = [] |
---|
114 | self.seg_dict['vertex_attributes'] = None |
---|
115 | self.seg_dict['triangle_neighbors'] = [] |
---|
116 | self.seg_dict['segment_tags'] = [''] |
---|
117 | self.seg_dict['vertex_attribute_titles'] = [] |
---|
118 | |
---|
119 | self.reg_dict ={} |
---|
120 | self.reg_dict['outline_segments'] = [[0,1]] |
---|
121 | self.reg_dict['outline_segment_tags'] = [''] |
---|
122 | self.reg_dict['holes'] = [] |
---|
123 | self.reg_dict['points'] = [(9,8),(7,8)] |
---|
124 | self.reg_dict['point_attributes'] = [[],[]] |
---|
125 | self.reg_dict['regions'] = [(5,4)] |
---|
126 | self.reg_dict['region_tags'] = [''] |
---|
127 | self.reg_dict['region_max_areas'] = [] |
---|
128 | self.reg_dict['vertices'] = [(9,8),(7,8)] |
---|
129 | self.reg_dict['triangles'] = [] |
---|
130 | self.reg_dict['segments'] = [[0,1]] |
---|
131 | self.reg_dict['triangle_tags'] = [] |
---|
132 | self.reg_dict['vertex_attributes'] = [[],[]] |
---|
133 | self.reg_dict['triangle_neighbors'] = [] |
---|
134 | self.reg_dict['segment_tags'] = [''] |
---|
135 | self.reg_dict['vertex_attribute_titles'] = [] |
---|
136 | |
---|
137 | self.triangle_tags_dict ={} |
---|
138 | self.triangle_tags_dict['outline_segments'] = [(0, 1), (1, 2), (0, 2), (0, 3)] |
---|
139 | self.triangle_tags_dict['outline_segment_tags'] = ['50', '40', '30', '20'] |
---|
140 | self.triangle_tags_dict['holes'] = [(0.2, 0.6)] |
---|
141 | self.triangle_tags_dict['point_attributes'] = [[5, 2], [4, 2], [3, 2], [2,2]] |
---|
142 | self.triangle_tags_dict['regions'] = [(0.3, 0.3),(0.3, 0.4)] |
---|
143 | self.triangle_tags_dict['region_tags'] = ['1.3', 'yeah'] |
---|
144 | self.triangle_tags_dict['region_max_areas'] = [36.0,-7.1] |
---|
145 | self.triangle_tags_dict['points'] = [(0.0, 0.0), (0.0, 4.0), (4.0, 0.0), (1.0, 1.0)] |
---|
146 | self.triangle_tags_dict['vertices'] = [(0.0, 0.0), (0.0, 4.0), |
---|
147 | (4.0, 0.0), (1.0, 1.0), (2.0, 2.0)] |
---|
148 | self.triangle_tags_dict['triangles'] = [(3, 2, 4), (1, 0, 3), |
---|
149 | (3, 4,1), (2, 3, 0)] |
---|
150 | self.triangle_tags_dict['segments'] = [(0, 1), (1, 4), (2, 0), |
---|
151 | (0, 3), (4, 2)] |
---|
152 | self.triangle_tags_dict['triangle_tags'] = ['yeah', '1.3', |
---|
153 | '1.3', ''] |
---|
154 | self.triangle_tags_dict['vertex_attributes'] = [[1.2,2.], [1.2,2.], |
---|
155 | [1.2,2.], [1.2,2.], [1.2,3.]] |
---|
156 | self.triangle_tags_dict['triangle_neighbors'] = [[-1, 2, 3], [3, 2, -1], |
---|
157 | [-1, 1, 0], [1, -1, 0]] |
---|
158 | self.triangle_tags_dict['segment_tags'] = ['50', '40', '30', '20', '40'] |
---|
159 | self.triangle_tags_dict['vertex_attribute_titles'] = ['bed elevation', 'height'] |
---|
160 | self.triangle_tags_dict['geo_reference'] = Geo_reference(56,1.9,1.9) |
---|
161 | |
---|
162 | def tearDown(self): |
---|
163 | pass |
---|
164 | |
---|
165 | ############### .TSH ########## |
---|
166 | def test_export_mesh_file(self): |
---|
167 | import os |
---|
168 | import tempfile |
---|
169 | |
---|
170 | meshDict = self.dict |
---|
171 | fileName = tempfile.mktemp(".tsh") |
---|
172 | export_mesh_file(fileName, meshDict) |
---|
173 | loadedDict = import_mesh_file(fileName) |
---|
174 | |
---|
175 | #print "*(*( meshDict" |
---|
176 | #print meshDict |
---|
177 | #print "*(*( loadedDcit" |
---|
178 | #print loadedDict |
---|
179 | #print "*(*(" |
---|
180 | |
---|
181 | self.failUnless(num.alltrue(num.array(meshDict['vertices']) == |
---|
182 | num.array(loadedDict['vertices'])), |
---|
183 | 'test_export_mesh_file failed. Test 1') |
---|
184 | self.failUnless(num.alltrue(num.array(meshDict['triangles']) == |
---|
185 | num.array(loadedDict['triangles'])), |
---|
186 | 'test_export_mesh_file failed. Test 2') |
---|
187 | self.failUnless(num.alltrue(num.array(meshDict['segments']) == |
---|
188 | num.array(loadedDict['segments'])), |
---|
189 | 'test_export_mesh_file failed. Test 3') |
---|
190 | self.failUnless(num.alltrue(num.array(meshDict['triangle_tags']) == |
---|
191 | num.array(loadedDict['triangle_tags'])), |
---|
192 | 'test_export_mesh_file failed. Test 4') |
---|
193 | |
---|
194 | self.failUnless(meshDict['vertex_attributes'] == |
---|
195 | loadedDict['vertex_attributes'], |
---|
196 | 'test_export_mesh_file failed. Test 5') |
---|
197 | self.failUnless(num.alltrue(num.array(meshDict['triangle_neighbors']) == |
---|
198 | num.array(loadedDict['triangle_neighbors'])), |
---|
199 | 'test_export_mesh_file failed. Test 6') |
---|
200 | self.failUnless(num.alltrue(num.array(meshDict['segment_tags']) == |
---|
201 | num.array(loadedDict['segment_tags'])), |
---|
202 | 'test_export_mesh_file failed. Test 7') |
---|
203 | self.failUnless(num.alltrue(num.array(meshDict['vertex_attribute_titles']) == |
---|
204 | num.array(loadedDict['vertex_attribute_titles'])), |
---|
205 | 'test_export_mesh_file failed. Test 8') |
---|
206 | self.failUnless(num.alltrue(num.array(meshDict['geo_reference']) == |
---|
207 | num.array(loadedDict['geo_reference'])), |
---|
208 | 'test_export_mesh_file failed. Test 9') |
---|
209 | |
---|
210 | os.remove(fileName) |
---|
211 | |
---|
212 | def test_read_write_tsh_file(self): |
---|
213 | dict = self.dict.copy() |
---|
214 | fileName = tempfile.mktemp(".tsh") |
---|
215 | export_mesh_file(fileName,dict) |
---|
216 | loaded_dict = import_mesh_file(fileName) |
---|
217 | os.remove(fileName) |
---|
218 | dict = self.dict |
---|
219 | #print "*********************" |
---|
220 | #print dict |
---|
221 | #print "**loaded_dict*******************" |
---|
222 | #print loaded_dict |
---|
223 | #print "*********************" |
---|
224 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file') |
---|
225 | |
---|
226 | def test_read_write_tsh_fileII(self): |
---|
227 | dict = self.sparse_dict.copy() |
---|
228 | fileName = tempfile.mktemp(".tsh") |
---|
229 | export_mesh_file(fileName,dict) |
---|
230 | loaded_dict = import_mesh_file(fileName) |
---|
231 | dict = self.sparse_dict |
---|
232 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file') |
---|
233 | os.remove(fileName) |
---|
234 | |
---|
235 | def test_read_write_tsh_fileIII(self): |
---|
236 | dict = self.blank_dict.copy() |
---|
237 | fileName = tempfile.mktemp(".tsh") |
---|
238 | export_mesh_file(fileName,dict) |
---|
239 | loaded_dict = import_mesh_file(fileName) |
---|
240 | os.remove(fileName) |
---|
241 | dict = self.blank_dict |
---|
242 | #print "*********************" |
---|
243 | #print dict |
---|
244 | #print "**loaded_dict*******************" |
---|
245 | #print loaded_dict |
---|
246 | #print "*********************" |
---|
247 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_fileIII') |
---|
248 | |
---|
249 | def test_read_write_tsh_file4(self): |
---|
250 | dict = self.seg_dict.copy() |
---|
251 | fileName = tempfile.mktemp(".tsh") |
---|
252 | export_mesh_file(fileName,dict) |
---|
253 | loaded_dict = import_mesh_file(fileName) |
---|
254 | os.remove(fileName) |
---|
255 | dict = self.seg_dict |
---|
256 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file4') |
---|
257 | |
---|
258 | def test_read_write_tsh_file5(self): |
---|
259 | dict = self.triangle_tags_dict.copy() |
---|
260 | fileName = tempfile.mktemp(".tsh") |
---|
261 | export_mesh_file(fileName,dict) |
---|
262 | loaded_dict = import_mesh_file(fileName) |
---|
263 | dict = self.triangle_tags_dict |
---|
264 | #print "*********************" |
---|
265 | #print dict |
---|
266 | #print "**loaded_dict*******************" |
---|
267 | #print loaded_dict |
---|
268 | #print "*********************" |
---|
269 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file5') |
---|
270 | os.remove(fileName) |
---|
271 | |
---|
272 | def test_read_write_tsh_file6(self): |
---|
273 | dict = self.tri_dict.copy() |
---|
274 | fileName = tempfile.mktemp(".tsh") |
---|
275 | export_mesh_file(fileName,dict) |
---|
276 | loaded_dict = import_mesh_file(fileName) |
---|
277 | dict = self.tri_dict |
---|
278 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file6') |
---|
279 | os.remove(fileName) |
---|
280 | |
---|
281 | ########################## BAD .TSH ########################## |
---|
282 | |
---|
283 | def test_load_bad_no_file_tsh(self): |
---|
284 | import os |
---|
285 | import tempfile |
---|
286 | |
---|
287 | fileName = tempfile.mktemp(".tsh") |
---|
288 | #print fileName |
---|
289 | try: |
---|
290 | dict = import_mesh_file(fileName) |
---|
291 | except IOError: |
---|
292 | pass |
---|
293 | else: |
---|
294 | self.failUnless(0 ==1, |
---|
295 | 'imaginary file did not raise error!') |
---|
296 | |
---|
297 | def test_read_write_tsh_file_bad(self): |
---|
298 | dict = self.tri_dict.copy() |
---|
299 | fileName = tempfile.mktemp(".xxx") |
---|
300 | try: |
---|
301 | export_mesh_file(fileName,dict) |
---|
302 | except IOError: |
---|
303 | pass |
---|
304 | else: |
---|
305 | self.failUnless(0 ==1, |
---|
306 | 'bad tsh file did not raise error!') |
---|
307 | |
---|
308 | def test_import_tsh_bad(self): |
---|
309 | import os |
---|
310 | import tempfile |
---|
311 | |
---|
312 | fileName = tempfile.mktemp(".tsh") |
---|
313 | file = open(fileName,"w") |
---|
314 | # this is a bad tsh file |
---|
315 | file.write("elevn\n\ |
---|
316 | 1.0 what \n\ |
---|
317 | 0.0 the \n\ |
---|
318 | 1.0 !!! \n") |
---|
319 | file.close() |
---|
320 | #print fileName |
---|
321 | try: |
---|
322 | dict = import_mesh_file(fileName) |
---|
323 | except IOError: |
---|
324 | pass |
---|
325 | else: |
---|
326 | self.failUnless(0 ==1, |
---|
327 | 'bad tsh file did not raise error!') |
---|
328 | os.remove(fileName) |
---|
329 | |
---|
330 | def test_import_tsh3(self): |
---|
331 | import os |
---|
332 | import tempfile |
---|
333 | |
---|
334 | fileName = tempfile.mktemp(".tsh") |
---|
335 | file = open(fileName,"w") |
---|
336 | file.write("1.0 \n\ |
---|
337 | showme1.0 0.0 10.0 \n\ |
---|
338 | 0.0 1.0\n\ |
---|
339 | 13.0 \n") |
---|
340 | file.close() |
---|
341 | #print fileName |
---|
342 | try: |
---|
343 | dict = import_mesh_file(fileName) |
---|
344 | except IOError: |
---|
345 | pass |
---|
346 | else: |
---|
347 | self.failUnless(0 ==1, |
---|
348 | 'bad tsh file did not raise error!') |
---|
349 | |
---|
350 | os.remove(fileName) |
---|
351 | |
---|
352 | |
---|
353 | ############### .MSH ########## |
---|
354 | |
---|
355 | def test_read_write_msh_file(self): |
---|
356 | dict = self.dict.copy() |
---|
357 | fileName = tempfile.mktemp(".msh") |
---|
358 | export_mesh_file(fileName,dict) |
---|
359 | loaded_dict = loadASCII._read_msh_file(fileName) |
---|
360 | os.remove(fileName) |
---|
361 | dict = self.dict |
---|
362 | #print "*********************" |
---|
363 | #print dict |
---|
364 | #print "**loaded_dict*******************" |
---|
365 | #print loaded_dict |
---|
366 | #print "*********************" |
---|
367 | self.check_mesh_dicts(loaded_dict,dict,'test_read_write_msh_file') |
---|
368 | |
---|
369 | def test_read_write_msh_fileII(self): |
---|
370 | dict = self.sparse_dict.copy() |
---|
371 | fileName = tempfile.mktemp(".msh") |
---|
372 | export_mesh_file(fileName,dict) |
---|
373 | loaded_dict = loadASCII._read_msh_file(fileName) |
---|
374 | os.remove(fileName) |
---|
375 | dict = self.sparse_dict |
---|
376 | #print "*********************" |
---|
377 | #print dict |
---|
378 | #print "**loaded_dict*******************" |
---|
379 | #print loaded_dict |
---|
380 | #print "*********************" |
---|
381 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_fileII') |
---|
382 | |
---|
383 | def test_read_write_msh_fileIII(self): |
---|
384 | dict = self.blank_dict.copy() |
---|
385 | fileName = tempfile.mktemp(".msh") |
---|
386 | export_mesh_file(fileName,dict) |
---|
387 | loaded_dict = loadASCII._read_msh_file(fileName) |
---|
388 | os.remove(fileName) |
---|
389 | dict = self.blank_dict |
---|
390 | #print "*********************" |
---|
391 | #print dict |
---|
392 | #print "**loaded_dict*******************" |
---|
393 | #print loaded_dict |
---|
394 | #print "*********************" |
---|
395 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_fileIII') |
---|
396 | |
---|
397 | def test_read_write_msh_file4(self): |
---|
398 | dict = self.seg_dict.copy() |
---|
399 | fileName = tempfile.mktemp(".msh") |
---|
400 | export_mesh_file(fileName,dict) |
---|
401 | loaded_dict = loadASCII._read_msh_file(fileName) |
---|
402 | os.remove(fileName) |
---|
403 | dict = self.seg_dict |
---|
404 | #print "*********************" |
---|
405 | #print dict |
---|
406 | #print "**loaded_dict*******************" |
---|
407 | #print loaded_dict |
---|
408 | #print "*********************" |
---|
409 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_fileIII') |
---|
410 | |
---|
411 | def test_read_write_msh_file5(self): |
---|
412 | dict = self.triangle_tags_dict.copy() |
---|
413 | fileName = tempfile.mktemp(".msh") |
---|
414 | export_mesh_file(fileName,dict) |
---|
415 | loaded_dict = loadASCII._read_msh_file(fileName) |
---|
416 | os.remove(fileName) |
---|
417 | dict = self.triangle_tags_dict |
---|
418 | #print "msh_file5*********************" |
---|
419 | #print dict |
---|
420 | #print "**loaded_dict*******************" |
---|
421 | #print loaded_dict |
---|
422 | #print "*********************" |
---|
423 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_fileIII') |
---|
424 | |
---|
425 | |
---|
426 | def test_read_write_msh_file6(self): |
---|
427 | dict = self.tri_dict.copy() |
---|
428 | fileName = tempfile.mktemp(".msh") |
---|
429 | export_mesh_file(fileName,dict) |
---|
430 | loaded_dict = loadASCII._read_msh_file(fileName) |
---|
431 | os.remove(fileName) |
---|
432 | dict = self.tri_dict |
---|
433 | #print "*********************" |
---|
434 | #print dict |
---|
435 | #print "**loaded_dict*******************" |
---|
436 | #print loaded_dict |
---|
437 | #print "*********************" |
---|
438 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_fileIII') |
---|
439 | |
---|
440 | def check_mesh_dicts(self, loaded_dict, dict, fail_string ): |
---|
441 | assert num.allclose(num.array(loaded_dict['points']), |
---|
442 | num.array(dict['points'])) |
---|
443 | |
---|
444 | assert num.allclose(num.array(loaded_dict['point_attributes']), |
---|
445 | num.array(dict['point_attributes'])) |
---|
446 | assert num.allclose(num.array(loaded_dict['outline_segments']), |
---|
447 | num.array(dict['outline_segments'])) |
---|
448 | |
---|
449 | self.failUnless(loaded_dict['outline_segment_tags'] == |
---|
450 | dict['outline_segment_tags'], |
---|
451 | fail_string + ' failed!! Test 4') |
---|
452 | |
---|
453 | |
---|
454 | assert num.allclose(num.array(loaded_dict['regions']), |
---|
455 | num.array(dict['regions'])) |
---|
456 | |
---|
457 | self.failUnless(loaded_dict['region_tags'] == |
---|
458 | dict['region_tags'], |
---|
459 | fail_string + ' failed!! Test 5') |
---|
460 | |
---|
461 | assert num.allclose(num.array(loaded_dict['region_max_areas']), |
---|
462 | num.array(dict['region_max_areas'])) |
---|
463 | |
---|
464 | assert num.allclose(num.array(loaded_dict['holes']), |
---|
465 | num.array(dict['holes'])) |
---|
466 | |
---|
467 | assert num.allclose(num.array(dict['vertices']), |
---|
468 | num.array(loaded_dict['vertices'])) |
---|
469 | |
---|
470 | assert num.allclose(num.array(dict['triangles']), |
---|
471 | num.array(loaded_dict['triangles'])) |
---|
472 | |
---|
473 | assert num.allclose(num.array(dict['segments']), |
---|
474 | num.array(loaded_dict['segments'])) |
---|
475 | for ob, ldob in map(None,dict['triangle_tags'], |
---|
476 | loaded_dict['triangle_tags']): |
---|
477 | self.failUnless(ob == ldob, |
---|
478 | fail_string + ' failed!! Test triangle_tags') |
---|
479 | # A bit hacky |
---|
480 | self.failUnless((loaded_dict['vertex_attributes'] == |
---|
481 | dict['vertex_attributes']) or \ |
---|
482 | (loaded_dict['vertex_attributes'] == None and \ |
---|
483 | dict['vertex_attributes'] == []), |
---|
484 | fail_string + ' failed!! Test vertex_attributes') |
---|
485 | |
---|
486 | assert num.allclose(num.array(dict['triangle_neighbors']), |
---|
487 | num.array(loaded_dict['triangle_neighbors'])) |
---|
488 | |
---|
489 | for seg, ldseg in map(None,dict['segment_tags'], |
---|
490 | loaded_dict['segment_tags']): |
---|
491 | self.failUnless(seg == ldseg, |
---|
492 | fail_string + ' failed!! Test 8') |
---|
493 | try: |
---|
494 | assert num.allclose(num.array(dict['vertex_attribute_titles']), |
---|
495 | num.array(loaded_dict['vertex_attribute_titles'])) |
---|
496 | except TypeError: |
---|
497 | self.failUnless(num.alltrue(num.array(loaded_dict['vertex_attribute_titles']) == num.array(dict['vertex_attribute_titles'])), |
---|
498 | fail_string + ' failed!! Test 8') |
---|
499 | try: |
---|
500 | self.failUnless(loaded_dict['geo_reference'] == |
---|
501 | dict['geo_reference'] , |
---|
502 | fail_string + ' failed!! Test geo_reference') |
---|
503 | except KeyError: |
---|
504 | self.failUnless(not dict.has_key('geo_reference' and |
---|
505 | loaded_dict['geo_reference'] == None) , |
---|
506 | fail_string + ' failed!! Test geo_reference') |
---|
507 | |
---|
508 | ########################## BAD .MSH ########################## |
---|
509 | |
---|
510 | def test_load_bad_no_file_msh(self): |
---|
511 | import os |
---|
512 | import tempfile |
---|
513 | |
---|
514 | fileName = tempfile.mktemp(".msh") |
---|
515 | #print fileName |
---|
516 | try: |
---|
517 | dict = import_mesh_file(fileName) |
---|
518 | except IOError: |
---|
519 | pass |
---|
520 | else: |
---|
521 | self.failUnless(0 ==1, |
---|
522 | 'imaginary file did not raise error!') |
---|
523 | |
---|
524 | def throws_error_2_screen_test_import_mesh_bad(self): |
---|
525 | import os |
---|
526 | import tempfile |
---|
527 | |
---|
528 | fileName = tempfile.mktemp(".msh") |
---|
529 | file = open(fileName,"w") |
---|
530 | # this is a bad tsh file |
---|
531 | file.write("elevn\n\ |
---|
532 | 1.0 what \n\ |
---|
533 | 0.0 the \n\ |
---|
534 | 1.0 !!! \n") |
---|
535 | file.close() |
---|
536 | #print fileName |
---|
537 | try: |
---|
538 | dict = import_mesh_file(fileName) |
---|
539 | except IOError: |
---|
540 | pass |
---|
541 | else: |
---|
542 | self.failUnless(0 ==1, |
---|
543 | 'bad msh file did not raise error!') |
---|
544 | os.remove(fileName) |
---|
545 | |
---|
546 | ###### |
---|
547 | # Test the clean_line() utility function. |
---|
548 | ###### |
---|
549 | |
---|
550 | def test_clean_line_01(self): |
---|
551 | test_line = 'abc, ,,xyz,123' |
---|
552 | result = clean_line(test_line, ',') |
---|
553 | self.failUnless(result == ['abc', '', 'xyz', '123']) |
---|
554 | |
---|
555 | def test_clean_line_02(self): |
---|
556 | test_line = ' abc , ,, xyz , 123 ' |
---|
557 | result = clean_line(test_line, ',') |
---|
558 | self.failUnless(result == ['abc', '', 'xyz', '123']) |
---|
559 | |
---|
560 | def test_clean_line_03(self): |
---|
561 | test_line = '1||||2' |
---|
562 | result = clean_line(test_line, '|') |
---|
563 | self.failUnless(result == ['1', '2']) |
---|
564 | |
---|
565 | #------------------------------------------------------------- |
---|
566 | if __name__ == "__main__": |
---|
567 | |
---|
568 | suite = unittest.makeSuite(loadASCIITestCase,'test') |
---|
569 | #suite = unittest.makeSuite(loadASCIITestCase,'test_read_write_tsh_file4') |
---|
570 | runner = unittest.TextTestRunner() #verbosity=2) |
---|
571 | runner.run(suite) |
---|
572 | |
---|