Changeset 4956


Ignore:
Timestamp:
Jan 18, 2008, 3:58:24 PM (16 years ago)
Author:
ole
Message:

Work on CRC32 for IP tracking

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/utilities/test_system_tools.py

    r4955 r4956  
    33
    44import unittest
    5 from Numeric import zeros, array, allclose
     5from Numeric import zeros, array, allclose, Float
     6from Scientific.IO.NetCDF import NetCDFFile
    67
    78from system_tools import *
     
    2526        # print host
    2627        assert isinstance(host, basestring), 'User name should be a string'       
     28
    2729    def test_compute_checksum(self):
    2830        """test_compute_checksum(self):
     
    3032        Check that checksums on files are OK
    3133        """
    32         # FIXME: Not Done Yet
    3334
    34         from tempfile import mkstemp
     35        import zlib
     36        from tempfile import NamedTemporaryFile, mkstemp, mktemp
    3537
    3638        # Generate a text file
    37         fd, pathname = mkstemp('.tmp', '', '.', '')
     39        fid = open(mktemp(suffix='.tmp',
     40                          dir='.'), 'w')
     41        string = 'My temp file with textual content. AAAABBBBCCCC1234'
     42        fid.write(string)
     43        fid.close()
     44
     45
     46        ref_crc = zlib.crc32(string)
     47        checksum = compute_checksum(fid.name)
     48
     49        assert checksum == ref_crc
     50
     51        os.remove(fid.name)
     52
     53        # Binary file
     54        fid = open(mktemp(suffix='.tmp',
     55                          dir='.'), 'wb')
     56        string = 'My temp file with textual content. AAAABBBBCCCC1234'
     57        fid.write(string)
     58        fid.close()
     59
     60        ref_crc = zlib.crc32(string)
     61        checksum = compute_checksum(fid.name)
     62
     63        assert checksum == ref_crc
     64        os.remove(fid.name)
     65       
     66        # Binary NetCDF File X 2
     67
     68        test_array = array([[7.0, 3.14], [-31.333, 0.0]])
     69
     70        # First file
     71        filename1 = mktemp(suffix='.nc', dir='.')
     72        fid = NetCDFFile(filename1, 'w')
     73        fid.createDimension('two', 2)
     74        fid.createVariable('test_array', Float,
     75                           ('two', 'two'))
     76        fid.variables['test_array'][:] = test_array
     77        fid.close()
     78
     79        # Second file
     80        filename2 = mktemp(suffix='.nc', dir='.')
     81        fid = NetCDFFile(filename2, 'w')
     82        fid.createDimension('two', 2)
     83        fid.createVariable('test_array', Float,
     84                           ('two', 'two'))
     85        fid.variables['test_array'][:] = test_array
     86        fid.close()
    3887
    3988       
    40        
     89        checksum1 = compute_checksum(filename1)
     90        checksum2 = compute_checksum(filename2)       
     91        assert checksum1 == checksum2
     92
     93
     94        os.remove(filename1)
     95        os.remove(filename2)
    4196
    4297       
Note: See TracChangeset for help on using the changeset viewer.