source: anuga_core/source/anuga/utilities/test_system_tools.py @ 4986

Last change on this file since 4986 was 4986, checked in by steve, 17 years ago

Fixed problem with zlib.crc32 and binascii.crc32 on 64 bit machine,
they gave different answers.

File size: 2.8 KB
Line 
1#!/usr/bin/env python
2
3
4import unittest
5from Numeric import zeros, array, allclose, Float
6from Scientific.IO.NetCDF import NetCDFFile
7
8from system_tools import *
9
10class Test_system_tools(unittest.TestCase):
11    def setUp(self):
12        pass
13
14    def tearDown(self):
15        pass
16
17    def test_user_name(self):
18        user = get_user_name()
19
20        # print user
21        assert isinstance(user, basestring), 'User name should be a string'
22
23    def test_host_name(self):
24        host = get_host_name()
25
26        # print host
27        assert isinstance(host, basestring), 'User name should be a string'       
28
29    def test_compute_checksum(self):
30        """test_compute_checksum(self):
31
32        Check that checksums on files are OK
33        """
34
35        import zlib
36        from tempfile import mkstemp, mktemp
37
38        # Generate a text file
39        tmp_fd , tmp_name = mkstemp(suffix='.tmp',dir='.')
40        fid = os.fdopen(tmp_fd,'w')
41        string = 'My temp file with textual content. AAAABBBBCCCC1234'
42        fid.write(string)
43        fid.flush()
44        fid.close()
45
46        ref_crc = zlib.crc32(string)
47
48        checksum = compute_checksum(tmp_name)
49
50
51        assert checksum == ref_crc
52
53        os.remove(tmp_name)
54       
55
56
57        # Binary file
58        tmp_fd , tmp_name = mkstemp(suffix='.tmp',dir='.')
59        fid = os.fdopen(tmp_fd,'w+b')
60       
61
62        string = 'My temp file with binary content. AAAABBBBCCCC1234'
63        fid.write(string)
64        fid.flush()
65
66
67        ref_crc = zlib.crc32(string)
68        checksum = compute_checksum(tmp_name)
69
70        assert checksum == ref_crc
71
72        # Close and remove temporary file
73        fid.close()
74        os.remove(tmp_name)       
75       
76        # Binary NetCDF File X 2 (use mktemp's name)
77
78        test_array = array([[7.0, 3.14], [-31.333, 0.0]])
79
80        # First file
81        filename1 = mktemp(suffix='.nc', dir='.')
82        fid = NetCDFFile(filename1, 'w')
83        fid.createDimension('two', 2)
84        fid.createVariable('test_array', Float,
85                           ('two', 'two'))
86        fid.variables['test_array'][:] = test_array
87        fid.close()
88
89        # Second file
90        filename2 = mktemp(suffix='.nc', dir='.')
91        fid = NetCDFFile(filename2, 'w')
92        fid.createDimension('two', 2)
93        fid.createVariable('test_array', Float,
94                           ('two', 'two'))
95        fid.variables['test_array'][:] = test_array
96        fid.close()
97
98       
99        checksum1 = compute_checksum(filename1)
100        checksum2 = compute_checksum(filename2)       
101        assert checksum1 == checksum2
102
103
104        os.remove(filename1)
105        os.remove(filename2)
106
107       
108#-------------------------------------------------------------
109if __name__ == "__main__":
110    suite = unittest.makeSuite(Test_system_tools, 'test')
111    runner = unittest.TextTestRunner()
112    runner.run(suite)
113
114
115
116
Note: See TracBrowser for help on using the repository browser.