Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Test the arraymodule. |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 3 | Roger E. Masse |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 4 | """ |
| 5 | import array |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 6 | from test_support import verbose, TESTFN, unlink |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 15 | unlink(TESTFN) |
| 16 | |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 17 | |
| 18 | def testtype(type, example): |
| 19 | |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 20 | a = array.array(type) |
| 21 | a.append(example) |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 22 | if verbose: |
| 23 | print 40*'*' |
| 24 | print 'array after append: ', a |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 25 | a.typecode |
| 26 | a.itemsize |
| 27 | if a.typecode in ('i', 'b', 'h', 'l'): |
| 28 | a.byteswap() |
| 29 | |
| 30 | if a.typecode == 'c': |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 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') |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 35 | a.fromfile(f, 10) |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 36 | f.close() |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 37 | if verbose: |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 38 | print 'char array with 10 bytes of TESTFN appended: ', a |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 39 | a.fromlist(['a', 'b', 'c']) |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 40 | if verbose: |
| 41 | print 'char array with list appended: ', a |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 42 | |
| 43 | a.insert(0, example) |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 44 | if verbose: |
| 45 | print 'array of %s after inserting another:' % a.typecode, a |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 46 | f = open(TESTFN, 'w') |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 47 | a.tofile(f) |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 48 | f.close() |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 49 | a.tolist() |
| 50 | a.tostring() |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 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() |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 58 | main() |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 59 | |