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

Last change on this file since 6556 was 6556, checked in by rwilson, 15 years ago

Changes to sww2dem() to minimize memory footprint.

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