home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / test / test_array.py < prev    next >
Text File  |  1997-10-07  |  1KB  |  60 lines

  1. #! /usr/bin/env python
  2. """Test the arraymodule.
  3.    Roger E. Masse
  4. """
  5. import array
  6. from test_support import verbose, TESTFN, unlink
  7.  
  8. def main():
  9.  
  10.     testtype('c', 'c')
  11.  
  12.     for type in (['b', 'h', 'i', 'l', 'f', 'd']):
  13.     testtype(type, 1)
  14.  
  15.     unlink(TESTFN)
  16.  
  17.  
  18. def testtype(type, example):
  19.  
  20.     a = array.array(type)
  21.     a.append(example)
  22.     if verbose:
  23.         print 40*'*'
  24.         print 'array after append: ', a
  25.     a.typecode
  26.     a.itemsize
  27.     if a.typecode in ('i', 'b', 'h', 'l'):
  28.         a.byteswap()
  29.  
  30.     if a.typecode == 'c':
  31.         f = open(TESTFN, "w")
  32.         f.write("The quick brown fox jumps over the lazy dog.\n")
  33.         f.close()
  34.         f = open(TESTFN, 'r')
  35.         a.fromfile(f, 10)
  36.         f.close()
  37.         if verbose:
  38.         print 'char array with 10 bytes of TESTFN appended: ', a
  39.         a.fromlist(['a', 'b', 'c'])
  40.         if verbose:
  41.         print 'char array with list appended: ', a
  42.  
  43.     a.insert(0, example)
  44.     if verbose:
  45.         print 'array of %s after inserting another:' % a.typecode, a
  46.     f = open(TESTFN, 'w')
  47.     a.tofile(f)
  48.     f.close()
  49.     a.tolist()
  50.     a.tostring()
  51.     if verbose:
  52.         print 'array of %s converted to a list: ' % a.typecode, a.tolist()
  53.     if verbose:
  54.         print 'array of %s converted to a string: ' \
  55.                % a.typecode, `a.tostring()`
  56.  
  57.  
  58. main()
  59.     
  60.