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 | |
Guido van Rossum | 3e06ab1 | 2000-06-29 19:35:29 +0000 | [diff] [blame] | 18 | def testoverflow(type, lowerLimit, upperLimit): |
| 19 | # should not overflow assigning lower limit |
| 20 | if verbose: |
| 21 | print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit`) |
| 22 | try: |
| 23 | a = array.array(type, [lowerLimit]) |
| 24 | except: |
| 25 | raise TestFailed, "array(%s) overflowed assigning %s" %\ |
| 26 | (`type`, `lowerLimit`) |
| 27 | # should overflow assigning less than lower limit |
| 28 | if verbose: |
| 29 | print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit-1`) |
| 30 | try: |
| 31 | a = array.array(type, [lowerLimit-1]) |
| 32 | raise TestFailed, "array(%s) did not overflow assigning %s" %\ |
| 33 | (`type`, `lowerLimit-1`) |
| 34 | except OverflowError: |
| 35 | pass |
| 36 | # should not overflow assigning upper limit |
| 37 | if verbose: |
| 38 | print "overflow test: array(%s, [%s])" % (`type`, `upperLimit`) |
| 39 | try: |
| 40 | a = array.array(type, [upperLimit]) |
| 41 | except: |
| 42 | raise TestFailed, "array(%s) overflowed assigning %s" %\ |
| 43 | (`type`, `upperLimit`) |
| 44 | # should overflow assigning more than upper limit |
| 45 | if verbose: |
| 46 | print "overflow test: array(%s, [%s])" % (`type`, `upperLimit+1`) |
| 47 | try: |
| 48 | a = array.array(type, [upperLimit+1]) |
| 49 | raise TestFailed, "array(%s) did not overflow assigning %s" %\ |
| 50 | (`type`, `upperLimit+1`) |
| 51 | except OverflowError: |
| 52 | pass |
| 53 | |
| 54 | |
| 55 | |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 56 | def testtype(type, example): |
| 57 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 58 | a = array.array(type) |
| 59 | a.append(example) |
| 60 | if verbose: |
| 61 | print 40*'*' |
| 62 | print 'array after append: ', a |
| 63 | a.typecode |
| 64 | a.itemsize |
| 65 | if a.typecode in ('i', 'b', 'h', 'l'): |
| 66 | a.byteswap() |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 68 | if a.typecode == 'c': |
| 69 | f = open(TESTFN, "w") |
| 70 | f.write("The quick brown fox jumps over the lazy dog.\n") |
| 71 | f.close() |
| 72 | f = open(TESTFN, 'r') |
| 73 | a.fromfile(f, 10) |
| 74 | f.close() |
| 75 | if verbose: |
| 76 | print 'char array with 10 bytes of TESTFN appended: ', a |
| 77 | a.fromlist(['a', 'b', 'c']) |
| 78 | if verbose: |
| 79 | print 'char array with list appended: ', a |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 81 | a.insert(0, example) |
| 82 | if verbose: |
| 83 | print 'array of %s after inserting another:' % a.typecode, a |
| 84 | f = open(TESTFN, 'w') |
| 85 | a.tofile(f) |
| 86 | f.close() |
| 87 | a.tolist() |
| 88 | a.tostring() |
| 89 | if verbose: |
| 90 | print 'array of %s converted to a list: ' % a.typecode, a.tolist() |
| 91 | if verbose: |
| 92 | print 'array of %s converted to a string: ' \ |
| 93 | % a.typecode, `a.tostring()` |
Guido van Rossum | 5de1f8d | 1996-12-10 16:02:14 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | e03c050 | 1998-08-12 02:38:11 +0000 | [diff] [blame] | 95 | if type == 'c': |
| 96 | a = array.array(type, "abcde") |
| 97 | a[:-1] = a |
| 98 | if a != array.array(type, "abcdee"): |
| 99 | raise TestFailed, "array(%s) self-slice-assign (head)" % `type` |
| 100 | a = array.array(type, "abcde") |
| 101 | a[1:] = a |
| 102 | if a != array.array(type, "aabcde"): |
| 103 | raise TestFailed, "array(%s) self-slice-assign (tail)" % `type` |
| 104 | a = array.array(type, "abcde") |
| 105 | a[1:-1] = a |
| 106 | if a != array.array(type, "aabcdee"): |
| 107 | raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type` |
| 108 | else: |
| 109 | a = array.array(type, [1, 2, 3, 4, 5]) |
| 110 | a[:-1] = a |
| 111 | if a != array.array(type, [1, 2, 3, 4, 5, 5]): |
| 112 | raise TestFailed, "array(%s) self-slice-assign (head)" % `type` |
| 113 | a = array.array(type, [1, 2, 3, 4, 5]) |
| 114 | a[1:] = a |
| 115 | if a != array.array(type, [1, 1, 2, 3, 4, 5]): |
| 116 | raise TestFailed, "array(%s) self-slice-assign (tail)" % `type` |
| 117 | a = array.array(type, [1, 2, 3, 4, 5]) |
| 118 | a[1:-1] = a |
| 119 | if a != array.array(type, [1, 1, 2, 3, 4, 5, 5]): |
| 120 | raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type` |
| 121 | |
Guido van Rossum | 3e06ab1 | 2000-06-29 19:35:29 +0000 | [diff] [blame] | 122 | # test that overflow exceptions are raised as expected for assignment |
| 123 | # to array of specific integral types |
| 124 | from math import pow |
| 125 | if type in ('b', 'h', 'i', 'l'): |
| 126 | # check signed and unsigned versions |
| 127 | a = array.array(type) |
| 128 | signedLowerLimit = -1 * long(pow(2, a.itemsize * 8 - 1)) |
| 129 | signedUpperLimit = long(pow(2, a.itemsize * 8 - 1)) - 1L |
| 130 | unsignedLowerLimit = 0 |
| 131 | unsignedUpperLimit = long(pow(2, a.itemsize * 8)) - 1L |
| 132 | testoverflow(type, signedLowerLimit, signedUpperLimit) |
| 133 | testoverflow(type.upper(), unsignedLowerLimit, unsignedUpperLimit) |
| 134 | |
| 135 | |
| 136 | |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 137 | main() |
Guido van Rossum | 3e06ab1 | 2000-06-29 19:35:29 +0000 | [diff] [blame] | 138 | |