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

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

Generalising to work in EQRM

File size: 3.9 KB
Line 
1#!/usr/bin/env python
2
3
4import unittest
5from Numeric import zeros, array, allclose, Float
6from Scientific.IO.NetCDF import NetCDFFile
7import zlib
8from os.path import join, split, sep
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        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    def test_compute_checksum_real(self):
109        """test_compute_checksum(self):
110
111        Check that checksums on a png file is OK
112        """
113
114        # Get path where this test is run
115        try:
116            path = get_pathname_from_package('anuga.utilities')
117        except ImportError:
118            # I'm trying to keep this file general,
119            # so it works for EQRM and ANUGA
120            # EQRM also uses this file,
121            # but has a different directory structure
122            path = get_pathname_from_package('eqrm_code.ANUGA_utilities')
123       
124        filename = path + sep +  'crc_test_file.png'
125
126        ref_crc = 1203293305 # Computed on Windows box
127        checksum = compute_checksum(filename)
128
129        msg = 'Computed checksum = %s, should have been %s'\
130              %(checksum, ref_crc)
131        assert checksum == ref_crc, msg
132        #print checksum
133       
134       
135#-------------------------------------------------------------
136if __name__ == "__main__":
137    suite = unittest.makeSuite(Test_system_tools, 'test')
138    runner = unittest.TextTestRunner()
139    runner.run(suite)
140
141
142
143
Note: See TracBrowser for help on using the repository browser.