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 | e03c050 | 1998-08-12 02:38:11 +0000 | [diff] [blame] | 6 | from test_support import verbose, TESTFN, unlink, TestFailed |
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']): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 13 | testtype(type, 1) |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 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 | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 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() |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 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()` |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | e03c050 | 1998-08-12 02:38:11 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 85 | main() |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 86 | |