1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | from maxasc import * |
---|
4 | import anuga.utilities.system_tools as aust |
---|
5 | |
---|
6 | import exceptions |
---|
7 | class TestError(exceptions.Exception): pass |
---|
8 | import unittest |
---|
9 | |
---|
10 | import sys |
---|
11 | import os |
---|
12 | import re |
---|
13 | import glob |
---|
14 | |
---|
15 | HEADER_SIZE = 6 |
---|
16 | |
---|
17 | # pattern string used to split multimax data |
---|
18 | SpacesPatternString = ' +' |
---|
19 | |
---|
20 | # generate 're' pattern for 'any number of spaces' |
---|
21 | SpacesPattern = re.compile(SpacesPatternString) |
---|
22 | |
---|
23 | |
---|
24 | def FilesEqual(file1, file2): |
---|
25 | """ |
---|
26 | Compare two ASC files for equality. |
---|
27 | """ |
---|
28 | |
---|
29 | def alltrue(a, b): |
---|
30 | return a and b |
---|
31 | |
---|
32 | def do_list(prefix, l): |
---|
33 | print prefix, '#'*100 |
---|
34 | for (i, x) in enumerate(l): |
---|
35 | print '%05d: %s\t' % (i, str(x)), |
---|
36 | if (i+1) % 5 == 0: |
---|
37 | print |
---|
38 | print |
---|
39 | print |
---|
40 | |
---|
41 | # get both files into memory |
---|
42 | fd = open(file1, 'r') |
---|
43 | data1 = fd.readlines() |
---|
44 | fd.close() |
---|
45 | fd = open(file2, 'r') |
---|
46 | data2 = fd.readlines() |
---|
47 | fd.close() |
---|
48 | |
---|
49 | # check we have same number of lines in each file |
---|
50 | if len(data1) != len(data2): |
---|
51 | print '# lines differs: len(data1)=%d, len(data2)=%d' % (len(data1), len(data2)) |
---|
52 | return False |
---|
53 | |
---|
54 | # read header lines, check identical |
---|
55 | for i in range(HEADER_SIZE): |
---|
56 | if data1[i] != data2[i]: |
---|
57 | print 'headers differ:' |
---|
58 | print data1[i] |
---|
59 | print data2[i] |
---|
60 | return False |
---|
61 | |
---|
62 | # read data lines, check same *values* |
---|
63 | for line_num in range(HEADER_SIZE, len(data1)): |
---|
64 | d1 = SpacesPattern.split(data1[line_num].strip()) |
---|
65 | d2 = SpacesPattern.split(data2[line_num].strip()) |
---|
66 | |
---|
67 | if len(d1) != len(d2): |
---|
68 | print '# columns differs, len(d1)=%d, len(d2)=%d on line %d' % (len(d1), len(d2), line_num) |
---|
69 | return False |
---|
70 | |
---|
71 | fd1 = [float(value) for value in d1] |
---|
72 | fd2 = [float(value) for value in d2] |
---|
73 | |
---|
74 | vec = map(lambda a,b: a==b, fd1, fd2) |
---|
75 | |
---|
76 | if not reduce(lambda a,b: a and b, vec): |
---|
77 | print 'line number = %d (out of %d)' % (line_num, len(data1)) |
---|
78 | do_list('fd1', fd1) |
---|
79 | do_list('fd2', fd2) |
---|
80 | do_list('vec', vec) |
---|
81 | return False |
---|
82 | |
---|
83 | return True |
---|
84 | |
---|
85 | |
---|
86 | class Test_MaxAsc(unittest.TestCase): |
---|
87 | def tearDown(self): |
---|
88 | # delete all output files |
---|
89 | files = glob.glob('*.out.asc') |
---|
90 | for file in files: |
---|
91 | os.remove(file) |
---|
92 | |
---|
93 | def test_unequal_lines(self): |
---|
94 | test_path = aust.get_pathname_from_package('anuga.lib.maxasc') |
---|
95 | in_file = os.path.join(test_path, 'test1.asc') |
---|
96 | expected_file = os.path.join(test_path, 'test1_bad_num_lines.asc') |
---|
97 | self.failUnlessRaises(RuntimeError, MaxAsc, |
---|
98 | 'test.out.asc', |
---|
99 | [in_file, expected_file]) |
---|
100 | |
---|
101 | def test_headers_differ(self): |
---|
102 | test_path = aust.get_pathname_from_package('anuga.lib.maxasc') |
---|
103 | in_file = os.path.join(test_path, 'test1.asc') |
---|
104 | expected_file = os.path.join(test_path, 'test1_bad_num_lines.asc') |
---|
105 | self.failUnlessRaises(RuntimeError, MaxAsc, |
---|
106 | 'test.out.asc', |
---|
107 | [in_file, expected_file]) |
---|
108 | |
---|
109 | def test_wrong_num_columns(self): |
---|
110 | test_path = aust.get_pathname_from_package('anuga.lib.maxasc') |
---|
111 | in_file = os.path.join(test_path, 'test1.asc') |
---|
112 | expected_file = os.path.join(test_path, 'test1_wrong_num_columns.asc') |
---|
113 | self.failUnlessRaises(RuntimeError, MaxAsc, |
---|
114 | 'test.out.asc', |
---|
115 | [in_file, expected_file]) |
---|
116 | |
---|
117 | def test_same_input_equals_output1(self): |
---|
118 | test_path = aust.get_pathname_from_package('anuga.lib.maxasc') |
---|
119 | in_file = os.path.join(test_path, 'test1.asc') |
---|
120 | MaxAsc('test1.out.asc', [in_file]) |
---|
121 | self.failUnless(FilesEqual('test1.out.asc', in_file)) |
---|
122 | |
---|
123 | def test_same_input_equals_bigA(self): |
---|
124 | test_path = aust.get_pathname_from_package('anuga.lib.maxasc') |
---|
125 | in_file = os.path.join(test_path, 'perthAll_stage_250m.asc') |
---|
126 | MaxAsc('perth.out.asc', [in_file]) |
---|
127 | self.failUnless(FilesEqual('perth.out.asc', in_file)) |
---|
128 | |
---|
129 | def test_same_input_equals_bigB(self): |
---|
130 | test_path = aust.get_pathname_from_package('anuga.lib.maxasc') |
---|
131 | in_file = os.path.join(test_path, 'perthAll_stage_250m_all.asc') |
---|
132 | MaxAsc('perth.out.asc', [in_file]) |
---|
133 | self.failUnless(FilesEqual('perth.out.asc', in_file)) |
---|
134 | |
---|
135 | def test_same_input_equals_bigC(self): |
---|
136 | test_path = aust.get_pathname_from_package('anuga.lib.maxasc') |
---|
137 | in_file = os.path.join(test_path, 'perthAll_stage_original.asc') |
---|
138 | MaxAsc('perth.out.asc', [in_file]) |
---|
139 | self.failUnless(FilesEqual('perth.out.asc', in_file)) |
---|
140 | |
---|
141 | def test_same_input_equals_big3(self): |
---|
142 | test_path = aust.get_pathname_from_package('anuga.lib.maxasc') |
---|
143 | in_file = os.path.join(test_path, 'perthAll_stage_250m.asc') |
---|
144 | MaxAsc('perth.out.asc', [in_file, in_file, in_file]) |
---|
145 | self.failUnless(FilesEqual('perth.out.asc', in_file)) |
---|
146 | |
---|
147 | def test_same_input_equals_outputN(self): |
---|
148 | test_path = aust.get_pathname_from_package('anuga.lib.maxasc') |
---|
149 | in_file = os.path.join(test_path, 'test1.asc') |
---|
150 | MaxAsc('test1.out.asc', [in_file] * 30) |
---|
151 | self.failUnless(FilesEqual('test1.out.asc', in_file)) |
---|
152 | |
---|
153 | def test_different_input2(self): |
---|
154 | test_path = aust.get_pathname_from_package('anuga.lib.maxasc') |
---|
155 | in_file = os.path.join(test_path, 'test1.asc') |
---|
156 | in_file2 = os.path.join(test_path, 'test2.asc') |
---|
157 | expected_file = os.path.join(test_path, 'test2.expected.asc') |
---|
158 | MaxAsc('test2.out.asc', [in_file, in_file2]) |
---|
159 | self.failUnless(FilesEqual('test2.out.asc', expected_file)) |
---|
160 | |
---|
161 | def test_different_input3(self): |
---|
162 | test_path = aust.get_pathname_from_package('anuga.lib.maxasc') |
---|
163 | in_file = os.path.join(test_path, 'test1.asc') |
---|
164 | in_file2 = os.path.join(test_path, 'test2.asc') |
---|
165 | in_file3 = os.path.join(test_path, 'test3.asc') |
---|
166 | expected_file = os.path.join(test_path, 'test3.expected.asc') |
---|
167 | MaxAsc('test3.out.asc', [in_file, in_file2, in_file3]) |
---|
168 | self.failUnless(FilesEqual('test3.out.asc', expected_file)) |
---|
169 | |
---|
170 | if __name__ == '__main__': |
---|
171 | suite = unittest.makeSuite(Test_MaxAsc,'test') |
---|
172 | runner = unittest.TextTestRunner(verbosity=1) |
---|
173 | runner.run(suite) |
---|