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 |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame^] | 6 | from test.test_support import verbose, TESTFN, unlink, TestFailed,\ |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 7 | have_unicode, vereq |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 8 | |
| 9 | def main(): |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 10 | testtype('c', 'c') |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 11 | if have_unicode: |
| 12 | testtype('u', unicode(r'\u263a', 'unicode-escape')) |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 13 | for type in (['b', 'h', 'i', 'l', 'f', 'd']): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 14 | testtype(type, 1) |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 15 | if have_unicode: |
| 16 | testunicode() |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 17 | testsubclassing() |
Guido van Rossum | c9f8f14 | 1997-04-09 20:51:54 +0000 | [diff] [blame] | 18 | unlink(TESTFN) |
| 19 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 20 | def testunicode(): |
| 21 | try: |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 22 | array.array('b', unicode('foo', 'ascii')) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 23 | except TypeError: |
| 24 | pass |
| 25 | else: |
| 26 | raise TestFailed("creating a non-unicode array from " |
| 27 | "a Unicode string should fail") |
| 28 | |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 29 | x = array.array('u', unicode(r'\xa0\xc2\u1234', 'unicode-escape')) |
| 30 | x.fromunicode(unicode(' ', 'ascii')) |
| 31 | x.fromunicode(unicode('', 'ascii')) |
| 32 | x.fromunicode(unicode('', 'ascii')) |
| 33 | x.fromunicode(unicode(r'\x11abc\xff\u1234', 'unicode-escape')) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 34 | s = x.tounicode() |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 35 | if s != unicode(r'\xa0\xc2\u1234 \x11abc\xff\u1234', 'unicode-escape'): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 36 | raise TestFailed("fromunicode()/tounicode()") |
| 37 | |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 38 | s = unicode(r'\x00="\'a\\b\x80\xff\u0000\u0001\u1234', 'unicode-escape') |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 39 | a = array.array('u', s) |
| 40 | if verbose: |
| 41 | print "repr of type 'u' array:", repr(a) |
| 42 | print " expected: array('u', %r)" % s |
| 43 | |
| 44 | def testsubclassing(): |
| 45 | class EditableString(array.array): |
| 46 | def __new__(cls, s, *args, **kwargs): |
| 47 | return array.array.__new__(cls, 'c', s) |
| 48 | |
| 49 | def __init__(self, s, color='blue'): |
| 50 | array.array.__init__(self, 'c', s) |
| 51 | self.color = color |
| 52 | |
| 53 | def strip(self): |
| 54 | self[:] = array.array('c', self.tostring().strip()) |
| 55 | |
| 56 | def __repr__(self): |
| 57 | return 'EditableString(%r)' % self.tostring() |
| 58 | |
| 59 | s = EditableString("\ttest\r\n") |
| 60 | s.strip() |
| 61 | if s.tostring() != 'test': |
| 62 | raise TestFailed, "subclassing array.array failed somewhere" |
| 63 | if s.color != 'blue': |
| 64 | raise TestFailed, "assigning attributes to instance of array subclass" |
| 65 | s.color = 'red' |
| 66 | if s.color != 'red': |
| 67 | raise TestFailed, "assigning attributes to instance of array subclass" |
| 68 | if s.__dict__.keys() != ['color']: |
| 69 | raise TestFailed, "array subclass __dict__" |
| 70 | |
| 71 | class ExaggeratingArray(array.array): |
| 72 | __slots__ = ['offset'] |
| 73 | |
| 74 | def __new__(cls, typecode, data, offset): |
| 75 | return array.array.__new__(cls, typecode, data) |
| 76 | |
| 77 | def __init__(self, typecode, data, offset): |
| 78 | self.offset = offset |
| 79 | |
| 80 | def __getitem__(self, i): |
| 81 | return array.array.__getitem__(self, i) + self.offset |
| 82 | |
| 83 | a = ExaggeratingArray('i', [3, 6, 7, 11], 4) |
| 84 | if a[0] != 7: |
| 85 | raise TestFailed, "array subclass overriding __getitem__" |
| 86 | try: |
| 87 | a.color = 'blue' |
| 88 | except AttributeError: |
| 89 | pass |
| 90 | else: |
| 91 | raise TestFailed, "array subclass __slots__ was ignored" |
| 92 | |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 93 | |
Fred Drake | 7833447 | 2000-06-28 17:50:51 +0000 | [diff] [blame] | 94 | def testoverflow(type, lowerLimit, upperLimit): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 95 | # should not overflow assigning lower limit |
| 96 | if verbose: |
| 97 | print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit`) |
| 98 | try: |
| 99 | a = array.array(type, [lowerLimit]) |
| 100 | except: |
| 101 | raise TestFailed, "array(%s) overflowed assigning %s" %\ |
| 102 | (`type`, `lowerLimit`) |
| 103 | # should overflow assigning less than lower limit |
| 104 | if verbose: |
| 105 | print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit-1`) |
| 106 | try: |
| 107 | a = array.array(type, [lowerLimit-1]) |
| 108 | raise TestFailed, "array(%s) did not overflow assigning %s" %\ |
| 109 | (`type`, `lowerLimit-1`) |
| 110 | except OverflowError: |
| 111 | pass |
| 112 | # should not overflow assigning upper limit |
| 113 | if verbose: |
| 114 | print "overflow test: array(%s, [%s])" % (`type`, `upperLimit`) |
| 115 | try: |
| 116 | a = array.array(type, [upperLimit]) |
| 117 | except: |
| 118 | raise TestFailed, "array(%s) overflowed assigning %s" %\ |
| 119 | (`type`, `upperLimit`) |
| 120 | # should overflow assigning more than upper limit |
| 121 | if verbose: |
| 122 | print "overflow test: array(%s, [%s])" % (`type`, `upperLimit+1`) |
| 123 | try: |
| 124 | a = array.array(type, [upperLimit+1]) |
| 125 | raise TestFailed, "array(%s) did not overflow assigning %s" %\ |
| 126 | (`type`, `upperLimit+1`) |
| 127 | except OverflowError: |
| 128 | pass |
Fred Drake | 7833447 | 2000-06-28 17:50:51 +0000 | [diff] [blame] | 129 | |
| 130 | |
| 131 | |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 132 | def testtype(type, example): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 133 | a = array.array(type) |
| 134 | a.append(example) |
| 135 | if verbose: |
| 136 | print 40*'*' |
| 137 | print 'array after append: ', a |
| 138 | a.typecode |
| 139 | a.itemsize |
| 140 | if a.typecode in ('i', 'b', 'h', 'l'): |
| 141 | a.byteswap() |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 142 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 143 | if a.typecode == 'c': |
| 144 | f = open(TESTFN, "w") |
| 145 | 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] | 146 | f.close() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 147 | f = open(TESTFN, 'r') |
| 148 | a.fromfile(f, 10) |
| 149 | f.close() |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 150 | if verbose: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 151 | print 'char array with 10 bytes of TESTFN appended: ', a |
| 152 | a.fromlist(['a', 'b', 'c']) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 153 | if verbose: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 154 | print 'char array with list appended: ', a |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 155 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 156 | a.insert(0, example) |
| 157 | if verbose: |
| 158 | print 'array of %s after inserting another:' % a.typecode, a |
| 159 | f = open(TESTFN, 'w') |
| 160 | a.tofile(f) |
| 161 | f.close() |
Tim Peters | cc78e47 | 2000-11-14 21:36:07 +0000 | [diff] [blame] | 162 | |
| 163 | # 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] | 164 | a.tolist() |
| 165 | a.tostring() |
Tim Peters | cc78e47 | 2000-11-14 21:36:07 +0000 | [diff] [blame] | 166 | repr(a) |
| 167 | str(a) |
| 168 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 169 | if verbose: |
| 170 | print 'array of %s converted to a list: ' % a.typecode, a.tolist() |
| 171 | if verbose: |
| 172 | print 'array of %s converted to a string: ' \ |
| 173 | % a.typecode, `a.tostring()` |
Guido van Rossum | 7f1d3aa | 1998-07-16 15:31:43 +0000 | [diff] [blame] | 174 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 175 | # Try out inplace addition and multiplication |
| 176 | a = array.array(type, [example]) |
| 177 | b = a |
| 178 | a += array.array(type, [example]*2) |
| 179 | if a is not b: |
| 180 | raise TestFailed, "array(%s) inplace addition" % `type` |
| 181 | if a != array.array(type, [example] * 3): |
| 182 | raise TestFailed, "array(%s) inplace addition" % `type` |
| 183 | |
| 184 | a *= 5 |
| 185 | if a is not b: |
| 186 | raise TestFailed, "array(%s) inplace multiplication" % `type` |
| 187 | if a != array.array(type, [example] * 15): |
| 188 | raise TestFailed, "array(%s) inplace multiplication" % `type` |
| 189 | |
| 190 | a *= 0 |
| 191 | if a is not b: |
| 192 | raise TestFailed, "array(%s) inplace multiplication by 0" % `type` |
| 193 | if a != array.array(type, []): |
| 194 | raise TestFailed, "array(%s) inplace multiplication by 0" % `type` |
| 195 | |
| 196 | a *= 1000 |
| 197 | if a is not b: |
| 198 | raise TestFailed, "empty array(%s) inplace multiplication" % `type` |
| 199 | if a != array.array(type, []): |
| 200 | raise TestFailed, "empty array(%s) inplace multiplication" % `type` |
| 201 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 202 | if type == 'c': |
| 203 | a = array.array(type, "abcde") |
| 204 | a[:-1] = a |
| 205 | if a != array.array(type, "abcdee"): |
| 206 | raise TestFailed, "array(%s) self-slice-assign (head)" % `type` |
| 207 | a = array.array(type, "abcde") |
| 208 | a[1:] = a |
| 209 | if a != array.array(type, "aabcde"): |
| 210 | raise TestFailed, "array(%s) self-slice-assign (tail)" % `type` |
| 211 | a = array.array(type, "abcde") |
| 212 | a[1:-1] = a |
| 213 | if a != array.array(type, "aabcdee"): |
| 214 | raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type` |
| 215 | if a.index("e") != 5: |
| 216 | raise TestFailed, "array(%s) index-test" % `type` |
| 217 | if a.count("a") != 2: |
| 218 | raise TestFailed, "array(%s) count-test" % `type` |
| 219 | a.remove("e") |
| 220 | if a != array.array(type, "aabcde"): |
| 221 | raise TestFailed, "array(%s) remove-test" % `type` |
| 222 | if a.pop(0) != "a": |
| 223 | raise TestFailed, "array(%s) pop-test" % `type` |
| 224 | if a.pop(1) != "b": |
| 225 | raise TestFailed, "array(%s) pop-test" % `type` |
| 226 | a.extend(array.array(type, "xyz")) |
| 227 | if a != array.array(type, "acdexyz"): |
| 228 | raise TestFailed, "array(%s) extend-test" % `type` |
| 229 | a.pop() |
| 230 | a.pop() |
| 231 | a.pop() |
| 232 | x = a.pop() |
| 233 | if x != 'e': |
| 234 | raise TestFailed, "array(%s) pop-test" % `type` |
| 235 | if a != array.array(type, "acd"): |
| 236 | raise TestFailed, "array(%s) pop-test" % `type` |
| 237 | a.reverse() |
| 238 | if a != array.array(type, "dca"): |
| 239 | raise TestFailed, "array(%s) reverse-test" % `type` |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 240 | elif type == 'u': |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 241 | a = array.array(type, unicode("abcde", 'ascii')) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 242 | a[:-1] = a |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 243 | if a != array.array(type, unicode("abcdee", 'ascii')): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 244 | raise TestFailed, "array(%s) self-slice-assign (head)" % `type` |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 245 | a = array.array(type, unicode("abcde", 'ascii')) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 246 | a[1:] = a |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 247 | if a != array.array(type, unicode("aabcde", 'ascii')): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 248 | raise TestFailed, "array(%s) self-slice-assign (tail)" % `type` |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 249 | a = array.array(type, unicode("abcde", 'ascii')) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 250 | a[1:-1] = a |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 251 | if a != array.array(type, unicode("aabcdee", 'ascii')): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 252 | raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type` |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 253 | if a.index(unicode("e", 'ascii')) != 5: |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 254 | raise TestFailed, "array(%s) index-test" % `type` |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 255 | if a.count(unicode("a", 'ascii')) != 2: |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 256 | raise TestFailed, "array(%s) count-test" % `type` |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 257 | a.remove(unicode("e", 'ascii')) |
| 258 | if a != array.array(type, unicode("aabcde", 'ascii')): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 259 | raise TestFailed, "array(%s) remove-test" % `type` |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 260 | if a.pop(0) != unicode("a", 'ascii'): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 261 | raise TestFailed, "array(%s) pop-test" % `type` |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 262 | if a.pop(1) != unicode("b", 'ascii'): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 263 | raise TestFailed, "array(%s) pop-test" % `type` |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 264 | a.extend(array.array(type, unicode("xyz", 'ascii'))) |
| 265 | if a != array.array(type, unicode("acdexyz", 'ascii')): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 266 | raise TestFailed, "array(%s) extend-test" % `type` |
| 267 | a.pop() |
| 268 | a.pop() |
| 269 | a.pop() |
| 270 | x = a.pop() |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 271 | if x != unicode('e', 'ascii'): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 272 | raise TestFailed, "array(%s) pop-test" % `type` |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 273 | if a != array.array(type, unicode("acd", 'ascii')): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 274 | raise TestFailed, "array(%s) pop-test" % `type` |
| 275 | a.reverse() |
Michael W. Hudson | 8bf46e4 | 2002-05-15 13:04:53 +0000 | [diff] [blame] | 276 | if a != array.array(type, unicode("dca", 'ascii')): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 277 | raise TestFailed, "array(%s) reverse-test" % `type` |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 278 | else: |
| 279 | a = array.array(type, [1, 2, 3, 4, 5]) |
| 280 | a[:-1] = a |
| 281 | if a != array.array(type, [1, 2, 3, 4, 5, 5]): |
| 282 | raise TestFailed, "array(%s) self-slice-assign (head)" % `type` |
| 283 | a = array.array(type, [1, 2, 3, 4, 5]) |
| 284 | a[1:] = a |
| 285 | if a != array.array(type, [1, 1, 2, 3, 4, 5]): |
| 286 | raise TestFailed, "array(%s) self-slice-assign (tail)" % `type` |
| 287 | a = array.array(type, [1, 2, 3, 4, 5]) |
| 288 | a[1:-1] = a |
| 289 | if a != array.array(type, [1, 1, 2, 3, 4, 5, 5]): |
| 290 | raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type` |
| 291 | if a.index(5) != 5: |
| 292 | raise TestFailed, "array(%s) index-test" % `type` |
| 293 | if a.count(1) != 2: |
| 294 | raise TestFailed, "array(%s) count-test" % `type` |
| 295 | a.remove(5) |
| 296 | if a != array.array(type, [1, 1, 2, 3, 4, 5]): |
| 297 | raise TestFailed, "array(%s) remove-test" % `type` |
| 298 | if a.pop(0) != 1: |
| 299 | raise TestFailed, "array(%s) pop-test" % `type` |
| 300 | if a.pop(1) != 2: |
| 301 | raise TestFailed, "array(%s) pop-test" % `type` |
| 302 | a.extend(array.array(type, [7, 8, 9])) |
| 303 | if a != array.array(type, [1, 3, 4, 5, 7, 8, 9]): |
| 304 | raise TestFailed, "array(%s) extend-test" % `type` |
| 305 | a.pop() |
| 306 | a.pop() |
| 307 | a.pop() |
| 308 | x = a.pop() |
| 309 | if x != 5: |
| 310 | raise TestFailed, "array(%s) pop-test" % `type` |
| 311 | if a != array.array(type, [1, 3, 4]): |
| 312 | raise TestFailed, "array(%s) pop-test" % `type` |
| 313 | a.reverse() |
| 314 | if a != array.array(type, [4, 3, 1]): |
| 315 | raise TestFailed, "array(%s) reverse-test" % `type` |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 316 | # extended slicing |
| 317 | # subscription |
| 318 | a = array.array(type, [0,1,2,3,4]) |
| 319 | vereq(a[::], a) |
| 320 | vereq(a[::2], array.array(type, [0,2,4])) |
| 321 | vereq(a[1::2], array.array(type, [1,3])) |
| 322 | vereq(a[::-1], array.array(type, [4,3,2,1,0])) |
| 323 | vereq(a[::-2], array.array(type, [4,2,0])) |
| 324 | vereq(a[3::-2], array.array(type, [3,1])) |
| 325 | vereq(a[-100:100:], a) |
| 326 | vereq(a[100:-100:-1], a[::-1]) |
| 327 | vereq(a[-100L:100L:2L], array.array(type, [0,2,4])) |
| 328 | vereq(a[1000:2000:2], array.array(type, [])) |
| 329 | vereq(a[-1000:-2000:-2], array.array(type, [])) |
| 330 | # deletion |
| 331 | del a[::2] |
| 332 | vereq(a, array.array(type, [1,3])) |
| 333 | a = array.array(type, range(5)) |
| 334 | del a[1::2] |
| 335 | vereq(a, array.array(type, [0,2,4])) |
| 336 | a = array.array(type, range(5)) |
| 337 | del a[1::-2] |
| 338 | vereq(a, array.array(type, [0,2,3,4])) |
| 339 | # assignment |
| 340 | a = array.array(type, range(10)) |
| 341 | a[::2] = array.array(type, [-1]*5) |
| 342 | vereq(a, array.array(type, [-1, 1, -1, 3, -1, 5, -1, 7, -1, 9])) |
| 343 | a = array.array(type, range(10)) |
| 344 | a[::-4] = array.array(type, [10]*3) |
| 345 | vereq(a, array.array(type, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])) |
| 346 | a = array.array(type, range(4)) |
| 347 | a[::-1] = a |
| 348 | vereq(a, array.array(type, [3, 2, 1, 0])) |
| 349 | a = array.array(type, range(10)) |
| 350 | b = a[:] |
| 351 | c = a[:] |
| 352 | ins = array.array(type, range(2)) |
| 353 | a[2:3] = ins |
| 354 | b[slice(2,3)] = ins |
| 355 | c[2:3:] = ins |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 356 | |
| 357 | # test that overflow exceptions are raised as expected for assignment |
| 358 | # to array of specific integral types |
| 359 | from math import pow |
| 360 | if type in ('b', 'h', 'i', 'l'): |
| 361 | # check signed and unsigned versions |
| 362 | a = array.array(type) |
| 363 | signedLowerLimit = -1 * long(pow(2, a.itemsize * 8 - 1)) |
| 364 | signedUpperLimit = long(pow(2, a.itemsize * 8 - 1)) - 1L |
| 365 | unsignedLowerLimit = 0 |
| 366 | unsignedUpperLimit = long(pow(2, a.itemsize * 8)) - 1L |
| 367 | testoverflow(type, signedLowerLimit, signedUpperLimit) |
| 368 | testoverflow(type.upper(), unsignedLowerLimit, unsignedUpperLimit) |
| 369 | |
| 370 | |
| 371 | |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 372 | main() |