/[escript]/trunk/escript/test/python/test_util_base.py
ViewVC logotype

Diff of /trunk/escript/test/python/test_util_base.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2414 by jfenwick, Mon Mar 30 02:13:58 2009 UTC revision 2415 by gross, Wed May 13 02:48:39 2009 UTC
# Line 36  __author__="Lutz Gross, l.gross@uq.edu.a Line 36  __author__="Lutz Gross, l.gross@uq.edu.a
36    
37  import unittest  import unittest
38  import numarray  import numarray
39    import os
40  from esys.escript import *  from esys.escript import *
41    
42  class Test_util_base(unittest.TestCase):  class Test_util_base(unittest.TestCase):
# Line 45  class Test_util_base(unittest.TestCase): Line 46  class Test_util_base(unittest.TestCase):
46     RES_TOL=1.e-7 # RES_TOLerance to compare results     RES_TOL=1.e-7 # RES_TOLerance to compare results
47     DIFF_TOL=1.e-7 # RES_TOLerance to derivatices     DIFF_TOL=1.e-7 # RES_TOLerance to derivatices
48  #=========================================================  #=========================================================
49    #  File writer
50    #=========================================================
51       def __checkContent(self,fn,ref_cont):
52            cont=open(fn,'r').readlines()
53            self.failUnless(len(cont)==len(ref_cont),"wrong number of records")
54            for i in xrange(len(cont)):
55               self.failUnless(cont[i].strip()==ref_cont[i],"wrong records %s"%i)
56       def test_FileWriter_W(self):
57            fn="filewriter_w.txt"
58            self.failUnlessRaises(IOError,FileWriter,fn="",append=False)
59            f=FileWriter(fn,append=False)
60            self.failUnless(f.name==fn, "wrong file name.")
61            self.failUnless(f.mode=='w', "wrong mode")
62            self.failUnless(f.newlines==os.linesep, "wrong line seps")
63            self.failUnless(not f.closed,"file shuold not be closed.")
64            f.write("line1"+f.newlines)
65            f.flush()
66            self.__checkContent(fn,["line1"])
67            f.writelines(["line2"+f.newlines, "line3"+f.newlines])
68            f.close()
69            self.failUnless(f.closed,"file shuold be closed.")
70            self.__checkContent(fn,["line1", "line2", "line3"])
71    
72       def test_FileWriter_A(self):
73            fn="filewriter_a.txt"
74            if getMPIRankWorld()==0: open(fn,'w').write("line1"+os.linesep)
75            self.failUnlessRaises(IOError,FileWriter,fn="",append=True)
76            f=FileWriter(fn,append=True)
77            self.failUnless(f.name==fn, "wrong file name.")
78            self.failUnless(f.mode=='a', "wrong mode")
79            self.failUnless(f.newlines==os.linesep, "wrong line seps")
80            self.failUnless(not f.closed,"file shuold not be closed.")
81            f.write("line2"+f.newlines)
82            f.flush()
83            self.__checkContent(fn,["line1", "line2"])
84            f.writelines(["line3"+f.newlines, "line4"+f.newlines])
85            f.close()
86            self.failUnless(f.closed,"file shuold be closed.")
87            self.__checkContent(fn,["line1", "line2", "line3", "line4"])
88    
89       def test_FileWriter_A_loc(self):
90            fn="filewriter_a_loc.txt"
91            if getMPIRankWorld()>0:
92                fn2=fn+".%s"%getMPIRankWorld()
93            else:
94                fn2=fn
95            open(fn2,'w').write("line1"+os.linesep)
96            self.failUnlessRaises(IOError,FileWriter,fn="",append=True, createLocalFiles=True)
97            f=FileWriter(fn,append=True,createLocalFiles=True)
98            self.failUnless(f.name==fn, "wrong file name.")
99            self.failUnless(f.mode=='a', "wrong mode")
100            self.failUnless(f.newlines==os.linesep, "wrong line seps")
101            self.failUnless(not f.closed,"file shuold not be closed.")
102            f.write("line2"+f.newlines)
103            f.flush()
104            self.__checkContent(fn2,["line1", "line2"])
105            f.writelines(["line3"+f.newlines, "line4"+f.newlines])
106            f.close()
107            self.failUnless(f.closed,"file shuold be closed.")
108            self.__checkContent(fn2,["line1", "line2", "line3", "line4"])
109    
110       def test_FileWriter_W_loc(self):
111            fn="filewriter_w_loc.txt"
112            if getMPIRankWorld()>0:
113                fn2=fn+".%s"%getMPIRankWorld()
114            else:
115                fn2=fn
116            self.failUnlessRaises(IOError,FileWriter,fn="",append=True, createLocalFiles=True)
117            f=FileWriter(fn,append=False,createLocalFiles=True)
118            self.failUnless(f.name==fn, "wrong file name.")
119            self.failUnless(f.mode=='w', "wrong mode")
120            self.failUnless(f.newlines==os.linesep, "wrong line seps")
121            self.failUnless(not f.closed,"file shuold not be closed.")
122            f.write("line1"+f.newlines)
123            f.flush()
124            self.__checkContent(fn2,["line1"])
125            f.writelines(["line2"+f.newlines, "line3"+f.newlines])
126            f.close()
127            self.failUnless(f.closed,"file shuold be closed.")
128            self.__checkContent(fn2,["line1", "line2", "line3"])
129    #=========================================================
130  #  constants  #  constants
131  #=========================================================  #=========================================================
132     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Legend:
Removed from v.2414  
changed lines
  Added in v.2415

  ViewVC Help
Powered by ViewVC 1.1.26