source: branches/numpy/anuga/utilities/test_system_tools.py @ 6304

Last change on this file since 6304 was 6304, checked in by rwilson, 14 years ago

Initial commit of numpy changes. Still a long way to go.

File size: 4.2 KB
Line 
1#!/usr/bin/env python
2
3
4import unittest
5import numpy as num
6import zlib
7from os.path import join, split, sep
8from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a
9from anuga.config import netcdf_float
10
11
12# Please, don't add anuga.utilities to these imports.
13# I'm trying to keep this file general, so it works for EQRM and ANUGA
14# EQRM also uses this file, but has a different directory structure
15from system_tools import *
16
17class Test_system_tools(unittest.TestCase):
18    def setUp(self):
19        pass
20
21    def tearDown(self):
22        pass
23
24    def test_user_name(self):
25        user = get_user_name()
26
27        # print user
28        assert isinstance(user, basestring), 'User name should be a string'
29
30    def test_host_name(self):
31        host = get_host_name()
32
33        # print host
34        assert isinstance(host, basestring), 'User name should be a string'       
35
36    def test_compute_checksum(self):
37        """test_compute_checksum(self):
38
39        Check that checksums on files are OK
40        """
41
42        from tempfile import mkstemp, mktemp
43
44        # Generate a text file
45        tmp_fd , tmp_name = mkstemp(suffix='.tmp', dir='.')
46        fid = os.fdopen(tmp_fd, 'w+b')
47        string = 'My temp file with textual content. AAAABBBBCCCC1234'
48        fid.write(string)
49        fid.close()
50
51        # Have to apply the 64 bit fix here since we aren't comparing two
52        # files, but rather a string and a file.
53        ref_crc = safe_crc(string)
54
55        checksum = compute_checksum(tmp_name)
56        assert checksum == ref_crc
57
58        os.remove(tmp_name)
59       
60
61
62        # Binary file
63        tmp_fd , tmp_name = mkstemp(suffix='.tmp', dir='.')
64        fid = os.fdopen(tmp_fd, 'w+b')
65
66        string = 'My temp file with binary content. AAAABBBBCCCC1234'
67        fid.write(string)
68        fid.close()
69
70        ref_crc = safe_crc(string)
71        checksum = compute_checksum(tmp_name)
72
73        assert checksum == ref_crc
74
75        os.remove(tmp_name)       
76       
77        # Binary NetCDF File X 2 (use mktemp's name)
78
79        try:
80            from Scientific.IO.NetCDF import NetCDFFile
81        except ImportError:
82            # This code is also used by EQRM which does not require NetCDF
83            pass
84        else:
85            test_array = num.array([[7.0, 3.14], [-31.333, 0.0]],
86                                   dtype=num.float)
87
88            # First file
89            filename1 = mktemp(suffix='.nc', dir='.')
90            fid = NetCDFFile(filename1, netcdf_mode_w)
91            fid.createDimension('two', 2)
92            fid.createVariable('test_array', netcdf_float,
93                               ('two', 'two'))
94            fid.variables['test_array'][:] = test_array
95            fid.close()
96           
97            # Second file
98            filename2 = mktemp(suffix='.nc', dir='.')
99            fid = NetCDFFile(filename2, netcdf_mode_w)
100            fid.createDimension('two', 2)
101            fid.createVariable('test_array', netcdf_float,
102                               ('two', 'two'))
103            fid.variables['test_array'][:] = test_array
104            fid.close()
105           
106           
107            checksum1 = compute_checksum(filename1)
108            checksum2 = compute_checksum(filename2)       
109            assert checksum1 == checksum2
110
111
112            os.remove(filename1)
113            os.remove(filename2)
114
115
116    def test_compute_checksum_real(self):
117        """test_compute_checksum(self):
118
119        Check that checksums on a png file is OK
120        """
121
122        # Get path where this test is run
123        # I'm trying to keep this file general, so it works for EQRM and ANUGA
124        path, tail = split(__file__)
125        if path == '':
126            path = '.' + sep
127       
128        filename = path + sep +  'crc_test_file.png'
129
130        ref_crc = 1203293305 # Computed on Windows box
131        checksum = compute_checksum(filename)
132
133        msg = 'Computed checksum = %s, should have been %s'\
134              %(checksum, ref_crc)
135        assert checksum == ref_crc, msg
136        #print checksum
137       
138       
139#-------------------------------------------------------------
140if __name__ == "__main__":
141    suite = unittest.makeSuite(Test_system_tools, 'test')
142    runner = unittest.TextTestRunner()
143    runner.run(suite)
144
145
146
147
Note: See TracBrowser for help on using the repository browser.