1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | import unittest |
---|
5 | import numpy as num |
---|
6 | import zlib |
---|
7 | import os |
---|
8 | from os.path import join, split, sep |
---|
9 | from Scientific.IO.NetCDF import NetCDFFile |
---|
10 | from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a |
---|
11 | from anuga.config import netcdf_float, netcdf_char, netcdf_int |
---|
12 | |
---|
13 | |
---|
14 | # Please, don't add anuga.utilities to these imports. |
---|
15 | # I'm trying to keep this file general, so it works for EQRM and ANUGA |
---|
16 | # EQRM also uses this file, but has a different directory structure |
---|
17 | from system_tools import * |
---|
18 | |
---|
19 | class Test_system_tools(unittest.TestCase): |
---|
20 | def setUp(self): |
---|
21 | pass |
---|
22 | |
---|
23 | def tearDown(self): |
---|
24 | pass |
---|
25 | |
---|
26 | def test_user_name(self): |
---|
27 | user = get_user_name() |
---|
28 | |
---|
29 | # print user |
---|
30 | assert isinstance(user, basestring), 'User name should be a string' |
---|
31 | |
---|
32 | def test_host_name(self): |
---|
33 | host = get_host_name() |
---|
34 | |
---|
35 | # print host |
---|
36 | assert isinstance(host, basestring), 'User name should be a string' |
---|
37 | |
---|
38 | def test_compute_checksum(self): |
---|
39 | """test_compute_checksum(self): |
---|
40 | |
---|
41 | Check that checksums on files are OK |
---|
42 | """ |
---|
43 | |
---|
44 | from tempfile import mkstemp, mktemp |
---|
45 | |
---|
46 | # Generate a text file |
---|
47 | tmp_fd , tmp_name = mkstemp(suffix='.tmp', dir='.') |
---|
48 | fid = os.fdopen(tmp_fd, 'w+b') |
---|
49 | string = 'My temp file with textual content. AAAABBBBCCCC1234' |
---|
50 | fid.write(string) |
---|
51 | fid.close() |
---|
52 | |
---|
53 | # Have to apply the 64 bit fix here since we aren't comparing two |
---|
54 | # files, but rather a string and a file. |
---|
55 | ref_crc = safe_crc(string) |
---|
56 | |
---|
57 | checksum = compute_checksum(tmp_name) |
---|
58 | assert checksum == ref_crc |
---|
59 | |
---|
60 | os.remove(tmp_name) |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | # Binary file |
---|
65 | tmp_fd , tmp_name = mkstemp(suffix='.tmp', dir='.') |
---|
66 | fid = os.fdopen(tmp_fd, 'w+b') |
---|
67 | |
---|
68 | string = 'My temp file with binary content. AAAABBBBCCCC1234' |
---|
69 | fid.write(string) |
---|
70 | fid.close() |
---|
71 | |
---|
72 | ref_crc = safe_crc(string) |
---|
73 | checksum = compute_checksum(tmp_name) |
---|
74 | |
---|
75 | assert checksum == ref_crc |
---|
76 | |
---|
77 | os.remove(tmp_name) |
---|
78 | |
---|
79 | # Binary NetCDF File X 2 (use mktemp's name) |
---|
80 | |
---|
81 | try: |
---|
82 | from Scientific.IO.NetCDF import NetCDFFile |
---|
83 | except ImportError: |
---|
84 | # This code is also used by EQRM which does not require NetCDF |
---|
85 | pass |
---|
86 | else: |
---|
87 | test_array = num.array([[7.0, 3.14], [-31.333, 0.0]], |
---|
88 | dtype=num.float) |
---|
89 | |
---|
90 | # First file |
---|
91 | filename1 = mktemp(suffix='.nc', dir='.') |
---|
92 | fid = NetCDFFile(filename1, netcdf_mode_w) |
---|
93 | fid.createDimension('two', 2) |
---|
94 | fid.createVariable('test_array', netcdf_float, |
---|
95 | ('two', 'two')) |
---|
96 | fid.variables['test_array'][:] = test_array |
---|
97 | fid.close() |
---|
98 | |
---|
99 | # Second file |
---|
100 | filename2 = mktemp(suffix='.nc', dir='.') |
---|
101 | fid = NetCDFFile(filename2, netcdf_mode_w) |
---|
102 | fid.createDimension('two', 2) |
---|
103 | fid.createVariable('test_array', netcdf_float, |
---|
104 | ('two', 'two')) |
---|
105 | fid.variables['test_array'][:] = test_array |
---|
106 | fid.close() |
---|
107 | |
---|
108 | |
---|
109 | checksum1 = compute_checksum(filename1) |
---|
110 | checksum2 = compute_checksum(filename2) |
---|
111 | assert checksum1 == checksum2 |
---|
112 | |
---|
113 | |
---|
114 | os.remove(filename1) |
---|
115 | os.remove(filename2) |
---|
116 | |
---|
117 | |
---|
118 | def test_compute_checksum_real(self): |
---|
119 | """test_compute_checksum(self): |
---|
120 | |
---|
121 | Check that checksums on a png file is OK |
---|
122 | """ |
---|
123 | |
---|
124 | # Get path where this test is run |
---|
125 | # I'm trying to keep this file general, so it works for EQRM and ANUGA |
---|
126 | path, tail = split(__file__) |
---|
127 | if path == '': |
---|
128 | path = '.' + sep |
---|
129 | |
---|
130 | filename = path + sep + 'crc_test_file.png' |
---|
131 | |
---|
132 | ref_crc = 1203293305 # Computed on Windows box |
---|
133 | checksum = compute_checksum(filename) |
---|
134 | |
---|
135 | msg = 'Computed checksum = %s, should have been %s'\ |
---|
136 | %(checksum, ref_crc) |
---|
137 | assert checksum == ref_crc, msg |
---|
138 | #print checksum |
---|
139 | |
---|
140 | ################################################################################ |
---|
141 | # Test the clean_line() utility function. |
---|
142 | ################################################################################ |
---|
143 | |
---|
144 | # helper routine to test clean_line() |
---|
145 | def clean_line_test(self, instr, delim, expected): |
---|
146 | result = clean_line(instr, delim) |
---|
147 | self.failUnless(result == expected, |
---|
148 | "clean_line('%s', '%s'), expected %s, got %s" |
---|
149 | % (str(instr), str(delim), str(expected), str(result))) |
---|
150 | |
---|
151 | def test_clean_line_01(self): |
---|
152 | self.clean_line_test('abc, ,,xyz,123', ',', ['abc', '', 'xyz', '123']) |
---|
153 | |
---|
154 | def test_clean_line_02(self): |
---|
155 | self.clean_line_test(' abc , ,, xyz , 123 ', ',', |
---|
156 | ['abc', '', 'xyz', '123']) |
---|
157 | |
---|
158 | def test_clean_line_03(self): |
---|
159 | self.clean_line_test('1||||2', '|', ['1', '2']) |
---|
160 | |
---|
161 | def test_clean_line_04(self): |
---|
162 | self.clean_line_test('abc, ,,xyz,123, ', ',', |
---|
163 | ['abc', '', 'xyz', '123']) |
---|
164 | |
---|
165 | def test_clean_line_05(self): |
---|
166 | self.clean_line_test('abc, ,,xyz,123, , ', ',', |
---|
167 | ['abc', '', 'xyz', '123', '']) |
---|
168 | |
---|
169 | def test_clean_line_06(self): |
---|
170 | self.clean_line_test(',,abc, ,,xyz,123, , ', ',', |
---|
171 | ['abc', '', 'xyz', '123', '']) |
---|
172 | |
---|
173 | def test_clean_line_07(self): |
---|
174 | self.clean_line_test('|1||||2', '|', ['1', '2']) |
---|
175 | |
---|
176 | def test_clean_line_08(self): |
---|
177 | self.clean_line_test(' ,a,, , ,b,c , ,, , ', ',', |
---|
178 | ['a', '', '', 'b', 'c', '', '']) |
---|
179 | |
---|
180 | def test_clean_line_09(self): |
---|
181 | self.clean_line_test('a:b:c', ':', ['a', 'b', 'c']) |
---|
182 | |
---|
183 | def test_clean_line_10(self): |
---|
184 | self.clean_line_test('a:b:c:', ':', ['a', 'b', 'c']) |
---|
185 | |
---|
186 | ################################################################################ |
---|
187 | # Test the string_to_char() and char_to_string() utility functions. |
---|
188 | ################################################################################ |
---|
189 | |
---|
190 | def test_string_to_char(self): |
---|
191 | import random |
---|
192 | |
---|
193 | MAX_CHARS = 10 |
---|
194 | MAX_ENTRIES = 10000 |
---|
195 | A_INT = ord('a') |
---|
196 | Z_INT = ord('z') |
---|
197 | |
---|
198 | # generate some random strings in a list, with guaranteed lengths |
---|
199 | str_list = ['x' * MAX_CHARS] # make first maximum length |
---|
200 | for entry in xrange(MAX_ENTRIES): |
---|
201 | length = random.randint(1, MAX_CHARS) |
---|
202 | s = '' |
---|
203 | for c in range(length): |
---|
204 | s += chr(random.randint(A_INT, Z_INT)) |
---|
205 | str_list.append(s) |
---|
206 | |
---|
207 | x = string_to_char(str_list) |
---|
208 | new_str_list = char_to_string(x) |
---|
209 | |
---|
210 | self.failUnlessEqual(new_str_list, str_list) |
---|
211 | |
---|
212 | # special test - input list is [''] |
---|
213 | def test_string_to_char2(self): |
---|
214 | # generate a special list shown bad in load_mesh testing |
---|
215 | str_list = [''] |
---|
216 | |
---|
217 | x = string_to_char(str_list) |
---|
218 | new_str_list = char_to_string(x) |
---|
219 | |
---|
220 | self.failUnlessEqual(new_str_list, str_list) |
---|
221 | |
---|
222 | |
---|
223 | ################################################################################ |
---|
224 | # Test the raw I/O to NetCDF files of string data encoded/decoded with |
---|
225 | # string_to_char() and char_to_string(). |
---|
226 | ################################################################################ |
---|
227 | |
---|
228 | ## |
---|
229 | # @brief Helper function to write a list of strings to a NetCDF file. |
---|
230 | # @param filename Path to the file to write. |
---|
231 | # @param l The list of strings to write. |
---|
232 | def helper_write_msh_file(self, filename, l): |
---|
233 | # open the NetCDF file |
---|
234 | fd = NetCDFFile(filename, netcdf_mode_w) |
---|
235 | fd.description = 'Test file - string arrays' |
---|
236 | |
---|
237 | # convert list of strings to num.array |
---|
238 | al = num.array(string_to_char(l), num.character) |
---|
239 | |
---|
240 | # write the list |
---|
241 | fd.createDimension('num_of_strings', al.shape[0]) |
---|
242 | fd.createDimension('size_of_strings', al.shape[1]) |
---|
243 | |
---|
244 | var = fd.createVariable('strings', netcdf_char, |
---|
245 | ('num_of_strings', 'size_of_strings')) |
---|
246 | var[:] = al |
---|
247 | |
---|
248 | fd.close() |
---|
249 | |
---|
250 | |
---|
251 | ## |
---|
252 | # @brief Helper function to read a NetCDF file and return a list of strings. |
---|
253 | # @param filename Path to the file to read. |
---|
254 | # @return A list of strings from the file. |
---|
255 | def helper_read_msh_file(self, filename): |
---|
256 | fid = NetCDFFile(filename, netcdf_mode_r) |
---|
257 | mesh = {} |
---|
258 | |
---|
259 | # Get the 'strings' variable |
---|
260 | strings = fid.variables['strings'][:] |
---|
261 | |
---|
262 | fid.close() |
---|
263 | |
---|
264 | return char_to_string(strings) |
---|
265 | |
---|
266 | |
---|
267 | # test random strings to a NetCDF file |
---|
268 | def test_string_to_netcdf(self): |
---|
269 | import random |
---|
270 | |
---|
271 | MAX_CHARS = 10 |
---|
272 | MAX_ENTRIES = 10000 |
---|
273 | |
---|
274 | A_INT = ord('a') |
---|
275 | Z_INT = ord('z') |
---|
276 | |
---|
277 | FILENAME = 'test.msh' |
---|
278 | |
---|
279 | # generate some random strings in a list, with guaranteed lengths |
---|
280 | str_list = ['x' * MAX_CHARS] # make first maximum length |
---|
281 | for entry in xrange(MAX_ENTRIES): |
---|
282 | length = random.randint(1, MAX_CHARS) |
---|
283 | s = '' |
---|
284 | for c in range(length): |
---|
285 | s += chr(random.randint(A_INT, Z_INT)) |
---|
286 | str_list.append(s) |
---|
287 | |
---|
288 | self.helper_write_msh_file(FILENAME, str_list) |
---|
289 | new_str_list = self.helper_read_msh_file(FILENAME) |
---|
290 | |
---|
291 | self.failUnlessEqual(new_str_list, str_list) |
---|
292 | os.remove(FILENAME) |
---|
293 | |
---|
294 | # special test - list [''] to a NetCDF file |
---|
295 | def test_string_to_netcdf2(self): |
---|
296 | FILENAME = 'test.msh' |
---|
297 | |
---|
298 | # generate some random strings in a list, with guaranteed lengths |
---|
299 | str_list = [''] |
---|
300 | |
---|
301 | self.helper_write_msh_file(FILENAME, str_list) |
---|
302 | new_str_list = self.helper_read_msh_file(FILENAME) |
---|
303 | |
---|
304 | self.failUnlessEqual(new_str_list, str_list) |
---|
305 | os.remove(FILENAME) |
---|
306 | |
---|
307 | # print ('test_string_to_char: str_list=%s, new_str_list=%s' |
---|
308 | # % (str(str_list), str(new_str_list))) |
---|
309 | ################################################################################ |
---|
310 | |
---|
311 | if __name__ == "__main__": |
---|
312 | suite = unittest.makeSuite(Test_system_tools, 'test') |
---|
313 | runner = unittest.TextTestRunner() |
---|
314 | runner.run(suite) |
---|
315 | |
---|