Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Test the arraymodule. |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 3 | Roger E. Masse |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 4 | """ |
| 5 | import array |
Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame^] | 6 | from test_support import verbose, TESTFN, unlink, TestFailed |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +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 | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 13 | testtype(type, 1) |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | c9f8f14 | 1997-04-09 20:51:54 +0000 | [diff] [blame] | 15 | unlink(TESTFN) |
| 16 | |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 17 | |
Fred Drake | 7833447 | 2000-06-28 17:50:51 +0000 | [diff] [blame] | 18 | def testoverflow(type, lowerLimit, upperLimit): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 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 |
Fred Drake | 7833447 | 2000-06-28 17:50:51 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 56 | def testtype(type, example): |
| 57 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +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() |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 67 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +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") |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 71 | f.close() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 72 | f = open(TESTFN, 'r') |
| 73 | a.fromfile(f, 10) |
| 74 | f.close() |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 75 | if verbose: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 76 | print 'char array with 10 bytes of TESTFN appended: ', a |
| 77 | a.fromlist(['a', 'b', 'c']) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 78 | if verbose: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 79 | print 'char array with list appended: ', a |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 80 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +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() |
Tim Peters | cc78e47 | 2000-11-14 21:36:07 +0000 | [diff] [blame] | 87 | |
| 88 | # This block is just to verify that the operations don't blow up. |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 89 | a.tolist() |
| 90 | a.tostring() |
Tim Peters | cc78e47 | 2000-11-14 21:36:07 +0000 | [diff] [blame] | 91 | repr(a) |
| 92 | str(a) |
| 93 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 94 | if verbose: |
| 95 | print 'array of %s converted to a list: ' % a.typecode, a.tolist() |
| 96 | if verbose: |
| 97 | print 'array of %s converted to a string: ' \ |
| 98 | % a.typecode, `a.tostring()` |
Guido van Rossum | 7f1d3aa | 1998-07-16 15:31:43 +0000 | [diff] [blame] | 99 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 100 | if type == 'c': |
| 101 | a = array.array(type, "abcde") |
| 102 | a[:-1] = a |
| 103 | if a != array.array(type, "abcdee"): |
| 104 | raise TestFailed, "array(%s) self-slice-assign (head)" % `type` |
| 105 | a = array.array(type, "abcde") |
| 106 | a[1:] = a |
| 107 | if a != array.array(type, "aabcde"): |
| 108 | raise TestFailed, "array(%s) self-slice-assign (tail)" % `type` |
| 109 | a = array.array(type, "abcde") |
| 110 | a[1:-1] = a |
| 111 | if a != array.array(type, "aabcdee"): |
| 112 | raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type` |
| 113 | if a.index("e") != 5: |
| 114 | raise TestFailed, "array(%s) index-test" % `type` |
| 115 | if a.count("a") != 2: |
| 116 | raise TestFailed, "array(%s) count-test" % `type` |
| 117 | a.remove("e") |
| 118 | if a != array.array(type, "aabcde"): |
| 119 | raise TestFailed, "array(%s) remove-test" % `type` |
| 120 | if a.pop(0) != "a": |
| 121 | raise TestFailed, "array(%s) pop-test" % `type` |
| 122 | if a.pop(1) != "b": |
| 123 | raise TestFailed, "array(%s) pop-test" % `type` |
| 124 | a.extend(array.array(type, "xyz")) |
| 125 | if a != array.array(type, "acdexyz"): |
| 126 | raise TestFailed, "array(%s) extend-test" % `type` |
| 127 | a.pop() |
| 128 | a.pop() |
| 129 | a.pop() |
| 130 | x = a.pop() |
| 131 | if x != 'e': |
| 132 | raise TestFailed, "array(%s) pop-test" % `type` |
| 133 | if a != array.array(type, "acd"): |
| 134 | raise TestFailed, "array(%s) pop-test" % `type` |
| 135 | a.reverse() |
| 136 | if a != array.array(type, "dca"): |
| 137 | raise TestFailed, "array(%s) reverse-test" % `type` |
| 138 | else: |
| 139 | a = array.array(type, [1, 2, 3, 4, 5]) |
| 140 | a[:-1] = a |
| 141 | if a != array.array(type, [1, 2, 3, 4, 5, 5]): |
| 142 | raise TestFailed, "array(%s) self-slice-assign (head)" % `type` |
| 143 | a = array.array(type, [1, 2, 3, 4, 5]) |
| 144 | a[1:] = a |
| 145 | if a != array.array(type, [1, 1, 2, 3, 4, 5]): |
| 146 | raise TestFailed, "array(%s) self-slice-assign (tail)" % `type` |
| 147 | a = array.array(type, [1, 2, 3, 4, 5]) |
| 148 | a[1:-1] = a |
| 149 | if a != array.array(type, [1, 1, 2, 3, 4, 5, 5]): |
| 150 | raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type` |
| 151 | if a.index(5) != 5: |
| 152 | raise TestFailed, "array(%s) index-test" % `type` |
| 153 | if a.count(1) != 2: |
| 154 | raise TestFailed, "array(%s) count-test" % `type` |
| 155 | a.remove(5) |
| 156 | if a != array.array(type, [1, 1, 2, 3, 4, 5]): |
| 157 | raise TestFailed, "array(%s) remove-test" % `type` |
| 158 | if a.pop(0) != 1: |
| 159 | raise TestFailed, "array(%s) pop-test" % `type` |
| 160 | if a.pop(1) != 2: |
| 161 | raise TestFailed, "array(%s) pop-test" % `type` |
| 162 | a.extend(array.array(type, [7, 8, 9])) |
| 163 | if a != array.array(type, [1, 3, 4, 5, 7, 8, 9]): |
| 164 | raise TestFailed, "array(%s) extend-test" % `type` |
| 165 | a.pop() |
| 166 | a.pop() |
| 167 | a.pop() |
| 168 | x = a.pop() |
| 169 | if x != 5: |
| 170 | raise TestFailed, "array(%s) pop-test" % `type` |
| 171 | if a != array.array(type, [1, 3, 4]): |
| 172 | raise TestFailed, "array(%s) pop-test" % `type` |
| 173 | a.reverse() |
| 174 | if a != array.array(type, [4, 3, 1]): |
| 175 | raise TestFailed, "array(%s) reverse-test" % `type` |
| 176 | |
| 177 | # test that overflow exceptions are raised as expected for assignment |
| 178 | # to array of specific integral types |
| 179 | from math import pow |
| 180 | if type in ('b', 'h', 'i', 'l'): |
| 181 | # check signed and unsigned versions |
| 182 | a = array.array(type) |
| 183 | signedLowerLimit = -1 * long(pow(2, a.itemsize * 8 - 1)) |
| 184 | signedUpperLimit = long(pow(2, a.itemsize * 8 - 1)) - 1L |
| 185 | unsignedLowerLimit = 0 |
| 186 | unsignedUpperLimit = long(pow(2, a.itemsize * 8)) - 1L |
| 187 | testoverflow(type, signedLowerLimit, signedUpperLimit) |
| 188 | testoverflow(type.upper(), unsignedLowerLimit, unsignedUpperLimit) |
| 189 | |
| 190 | |
| 191 | |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 192 | main() |