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

Last change on this file since 5589 was 5398, checked in by duncan, 16 years ago

Generalising to work in EQRM

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