blob: 7474a27207a278a8ea54c0e4e836a16238660085 [file] [log] [blame]
Guido van Rossum5de1f8d1996-12-10 16:02:14 +00001#! /usr/bin/env python
2"""Test the arraymodule.
Guido van Rossum228b8e81997-04-02 06:13:34 +00003 Roger E. Masse
Guido van Rossum5de1f8d1996-12-10 16:02:14 +00004"""
5import array
Guido van Rossum228b8e81997-04-02 06:13:34 +00006from test_support import verbose
7
8def main():
9
10 testtype('c', 'c')
11
12 for type in (['b', 'h', 'i', 'l', 'f', 'd']):
13 testtype(type, 1)
14
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000015
16def testtype(type, example):
17
18
19 a = array.array(type)
20 a.append(example)
Guido van Rossum228b8e81997-04-02 06:13:34 +000021 if verbose:
22 print 40*'*'
23 print 'array after append: ', a
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000024 a.typecode
25 a.itemsize
26 if a.typecode in ('i', 'b', 'h', 'l'):
27 a.byteswap()
28
29 if a.typecode == 'c':
30 f = open('/etc/passwd', 'r')
31 a.fromfile(f, 10)
Guido van Rossum228b8e81997-04-02 06:13:34 +000032 if verbose:
33 print 'char array with 10 bytes of /etc/passwd appended: ', a
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000034 a.fromlist(['a', 'b', 'c'])
Guido van Rossum228b8e81997-04-02 06:13:34 +000035 if verbose:
36 print 'char array with list appended: ', a
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000037
38 a.insert(0, example)
Guido van Rossum228b8e81997-04-02 06:13:34 +000039 if verbose:
40 print 'array of %s after inserting another:' % a.typecode, a
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000041 f = open('/dev/null', 'w')
42 a.tofile(f)
43 a.tolist()
44 a.tostring()
Guido van Rossum228b8e81997-04-02 06:13:34 +000045 if verbose:
46 print 'array of %s converted to a list: ' % a.typecode, a.tolist()
47 if verbose:
48 print 'array of %s converted to a string: ' \
49 % a.typecode, a.tostring()
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000050
Guido van Rossum228b8e81997-04-02 06:13:34 +000051main()
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000052