blob: 7474a27207a278a8ea54c0e4e836a16238660085 [file] [log] [blame]
Roger E. Masse8db1b071996-12-09 20:09:16 +00001#! /usr/bin/env python
2"""Test the arraymodule.
Roger E. Massefab8ab81996-12-20 22:36:52 +00003 Roger E. Masse
Roger E. Masse8db1b071996-12-09 20:09:16 +00004"""
5import array
Roger E. Massefab8ab81996-12-20 22:36:52 +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
Roger E. Masse8db1b071996-12-09 20:09:16 +000015
16def testtype(type, example):
17
18
19 a = array.array(type)
20 a.append(example)
Roger E. Massefab8ab81996-12-20 22:36:52 +000021 if verbose:
22 print 40*'*'
23 print 'array after append: ', a
Roger E. Masse8db1b071996-12-09 20:09:16 +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)
Roger E. Massefab8ab81996-12-20 22:36:52 +000032 if verbose:
33 print 'char array with 10 bytes of /etc/passwd appended: ', a
Roger E. Masse8db1b071996-12-09 20:09:16 +000034 a.fromlist(['a', 'b', 'c'])
Roger E. Massefab8ab81996-12-20 22:36:52 +000035 if verbose:
36 print 'char array with list appended: ', a
Roger E. Masse8db1b071996-12-09 20:09:16 +000037
38 a.insert(0, example)
Roger E. Massefab8ab81996-12-20 22:36:52 +000039 if verbose:
40 print 'array of %s after inserting another:' % a.typecode, a
Roger E. Masse8db1b071996-12-09 20:09:16 +000041 f = open('/dev/null', 'w')
42 a.tofile(f)
43 a.tolist()
44 a.tostring()
Roger E. Massefab8ab81996-12-20 22:36:52 +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()
Roger E. Masse8db1b071996-12-09 20:09:16 +000050
Roger E. Massefab8ab81996-12-20 22:36:52 +000051main()
Roger E. Masse8db1b071996-12-09 20:09:16 +000052