#!/usr/bin/env python # #FIXME (Ole): I think we need a unit test of write_ASCII_trianglulation # in order to quash bug in merimbula data # - to quash the bug need a raised error in pyvolution for reading xya files # when the title attribute information is not consistent with the data in the # file - DSG import sys sys.path.append('..') import tempfile import unittest from mesh import * from loadASCII import * from Numeric import array class loadASCIITestCase(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_import_trianglulation(self): a_att = [5,2] d_att =[4,2] f_att = [3,2] e_att = [2,2] a_xy = [0.0, 0.0] a = Vertex ( a_xy[0],a_xy[1]) #, attributes =a_att) d = Vertex (0.0, 4.0) #, attributes =d_att) f = Vertex (4.0,0.0) #, attributes =f_att) e = Vertex (1.0,1.0) #, attributes =e_att) s1 = Segment(a,d, marker = "50") s2 = Segment(d,f, marker = "40") s3 = Segment(a,f, marker = "30") s4 = Segment(a,e, marker = "20") r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 45) m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4], regions=[r1]) m.generateMesh("QApza2.1") seg = m.getMeshSegments() points = m.getMeshVertices() fileName = tempfile.mktemp(".txt") #print "fileName",fileName m.exportASCIItrianglulationfile(fileName) dict = import_trianglulation(fileName) os.remove(fileName) #print dict #print "seg",seg for pimport,pactual,pimpatt in map(None,dict['generatedpointlist'],points,dict['generatedpointattributelist']): self.failUnless( pimport == [pactual.x,pactual.y], 'loadASCIITestCase failed. test 1') self.failUnless( pimpatt == pactual.attributes, 'loadASCIITestCase failed. test 1.1') self.failUnless( dict['segmentlist'][0] == [0,1], 'loadASCIITestCase failed. test 3') for segimp,segactual in map(None,dict['generatedsegmentmarkerlist'],seg): self.failUnless( segimp == segactual.marker, 'loadASCIITestCase failed. test 4') self.failUnless( dict['generatedtrianglelist'][0] == [3,2,4], 'loadASCIITestCase failed. test 5') self.failUnless( dict['generatedtriangleneighborlist'][0] == [-1,2,3], 'loadASCIITestCase failed. test 6') self.failUnless( dict['generatedtriangleattributelist'][0] == ["1.3"], 'loadASCIITestCase failed. test 7') def test_import_mesh(self): a_att = [5,2] d_att =[4,2] f_att = [3,2] e_att = [2,2] a_xy = [0.0, 0.0] a = Vertex ( a_xy[0],a_xy[1], attributes =a_att) d = Vertex (0.0, 4.0, attributes =d_att) f = Vertex (4.0,0.0, attributes =f_att) e = Vertex (1.0,1.0, attributes =e_att) s1 = Segment(a,d, marker = "50") s2 = Segment(d,f, marker = "40") s3 = Segment(a,f, marker = "30") s4 = Segment(a,e, marker = "20") r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 36) h1 = Hole(0.2,0.6) m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4], regions=[r1], holes=[h1]) seg = m.getUserSegments() points = m.getUserVertices() holes = m.getHoles() regions = m.getRegions() fileName = tempfile.mktemp(".txt") m.exportASCIImeshfile(fileName) #print "***************************fileName", fileName dict = import_mesh(fileName) os.remove(fileName) #print dict for pimport,pactual,pimpatt in map(None,dict['pointlist'],points,dict['pointattributelist']): self.failUnless( pimport == [pactual.x,pactual.y], 'loadASCIITestCase failed. test 1') self.failUnless( pimpatt == pactual.attributes, 'loadASCIITestCase failed. test 1.1') self.failUnless( dict['segmentlist'][0] == [0,1], 'loadASCIITestCase failed. test 3') for segimp,segactual in map(None,dict['segmentmarkerlist'],seg): self.failUnless( segimp == segactual.marker, 'loadASCIITestCase failed. test 4') for holeimp,holeactual in map(None,dict['holelist'],holes): self.failUnless( holeimp == [holeactual.x,holeactual.y], 'loadASCIITestCase failed. test 5') for regimp,regactual,regattimp, regmaxarea in map(None,dict['regionlist'],regions, dict['regionattributelist'], dict['regionmaxarealist']): self.failUnless( regimp == [regactual.x,regactual.y], 'loadASCIITestCase failed. test 6') self.failUnless( regattimp == regactual.getTag(), 'loadASCIITestCase failed. test 7') self.failUnless( regmaxarea == regactual.getMaxArea(), 'loadASCIITestCase failed. test 7') def test_export_trianglulation_file(self): import os import tempfile meshDict = {} meshDict['generatedpointlist'] = [(0.0, 0.0), (0.0, 4.0), (4.0, 0.0), (1.0, 1.0), (2.0, 2.0)] meshDict['generatedtrianglelist'] = [(3, 2, 4), (1, 0, 3), (3, 4,1), (2, 3, 0)] meshDict['generatedsegmentlist'] = [(0, 1), (1, 4), (2, 0), (0, 3), (4, 2)] meshDict['generatedtriangleattributelist'] = ['1.3', '1.3', '1.3', '1.3'] meshDict['generatedpointattributelist'] = [[1.2,2.], [1.2,2.], [1.2,2.], [1.2,2.], [1.2,3.]] meshDict['generatedtriangleneighborlist'] = [[-1, 2, 3], [3, 2, -1], [-1, 1, 0], [1, -1, 0]] meshDict['generatedsegmentmarkerlist'] = ['50', '40', '30', '20', '40'] meshDict['generatedpointattributetitlelist'] = ['bed elevation', 'height'] fileName = tempfile.mktemp(".tsh") export_trianglulation_file(fileName, meshDict) loadedDict = import_trianglulation(fileName) #print "*(*( meshDict" #print meshDict #print "*(*( loadedDcit" #print loadedDict #print "*(*(" self.failUnless(array(meshDict['generatedpointlist']) == array(loadedDict['generatedpointlist']), 'test_export_trianglulation_file failed. Test 1') self.failUnless(array(meshDict['generatedtrianglelist']) == array(loadedDict['generatedtrianglelist']), 'test_export_trianglulation_file failed. Test 2') self.failUnless(array(meshDict['generatedsegmentlist']) == array(loadedDict['generatedsegmentlist']), 'test_export_trianglulation_file failed. Test 3') self.failUnless(array(meshDict['generatedtriangleattributelist']) == array(loadedDict['generatedtriangleattributelist']), 'test_export_trianglulation_file failed. Test 4') self.failUnless(meshDict['generatedpointattributelist'] == loadedDict['generatedpointattributelist'], 'test_export_trianglulation_file failed. Test 5') self.failUnless(array(meshDict['generatedtriangleneighborlist']) == array(loadedDict['generatedtriangleneighborlist']), 'test_export_trianglulation_file failed. Test 6') self.failUnless(array(meshDict['generatedsegmentmarkerlist']) == array(loadedDict['generatedsegmentmarkerlist']), 'test_export_trianglulation_file failed. Test 7') self.failUnless(array(meshDict['generatedpointattributetitlelist']) == array(loadedDict['generatedpointattributetitlelist']), 'test_export_trianglulation_file failed. Test 8') #os.remove(fileName) def test_loadxy(self): import os import tempfile fileName = tempfile.mktemp(".xya") file = open(fileName,"w") file.write("title row \n\ 1.0 0.0 10.0 0.0\n\ 0.0 1.0 0.0 10.0\n\ 1.0 0.0 10.4 40.0\n") file.close() #print fileName m = importMeshFromFile(fileName) os.remove(fileName) self.failUnless(m.userVertices[0].x == 1.0, 'loadxy, test 1 failed') self.failUnless(m.userVertices[0].y == 0.0, 'loadxy, test 2 failed') #self.failUnless(m.userVertices[0].attributes == [10.0,0.0], # 'loadxy, test 2.2 failed') self.failUnless(m.userVertices[1].x == 0.0, 'loadxy, test 3 failed') self.failUnless(m.userVertices[1].y == 1.0, 'loadxy, test 4 failed') #self.failUnless(m.userVertices[1].attributes == [0.0,10.0], # 'loadxy, test 5 failed') #------------------------------------------------------------- if __name__ == "__main__": suite = unittest.makeSuite(loadASCIITestCase,'test') runner = unittest.TextTestRunner() runner.run(suite)