home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / Python / Lib / Python1.5 / test / test_array.py < prev    next >
Encoding:
Python Source  |  1998-07-16  |  2.7 KB  |  87 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, TestFailed
  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.         if type == 'c':
  58.             a = array.array(type, "abcde")
  59.             a[:-1] = a
  60.             if a != array.array(type, "abcdee"):
  61.                 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
  62.             a = array.array(type, "abcde")
  63.             a[1:] = a
  64.             if a != array.array(type, "aabcde"):
  65.                 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
  66.             a = array.array(type, "abcde")
  67.             a[1:-1] = a
  68.             if a != array.array(type, "aabcdee"):
  69.                 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
  70.         else:
  71.             a = array.array(type, [1, 2, 3, 4, 5])
  72.             a[:-1] = a
  73.             if a != array.array(type, [1, 2, 3, 4, 5, 5]):
  74.                 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
  75.             a = array.array(type, [1, 2, 3, 4, 5])
  76.             a[1:] = a
  77.             if a != array.array(type, [1, 1, 2, 3, 4, 5]):
  78.                 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
  79.             a = array.array(type, [1, 2, 3, 4, 5])
  80.             a[1:-1] = a
  81.             if a != array.array(type, [1, 1, 2, 3, 4, 5, 5]):
  82.                 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
  83.  
  84.  
  85. main()
  86.         
  87.