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 | """ |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 5 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 6 | import unittest |
| 7 | from test import test_support |
| 8 | import array, cStringIO, math |
Guido van Rossum | c9f8f14 | 1997-04-09 20:51:54 +0000 | [diff] [blame] | 9 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 10 | tests = [] # list to accumulate all tests |
| 11 | typecodes = "cubBhHiIlLfd" |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 12 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 13 | class BadConstructorTest(unittest.TestCase): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 14 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 15 | def test_constructor(self): |
| 16 | self.assertRaises(TypeError, array.array) |
| 17 | self.assertRaises(TypeError, array.array, spam=42) |
| 18 | self.assertRaises(TypeError, array.array, 'xx') |
| 19 | self.assertRaises(ValueError, array.array, 'x') |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 20 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 21 | tests.append(BadConstructorTest) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 22 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 23 | class BaseTest(unittest.TestCase): |
| 24 | # Required class attributes (provided by subclasses |
| 25 | # typecode: the typecode to test |
| 26 | # example: an initializer usable in the constructor for this type |
| 27 | # smallerexample: the same length as example, but smaller |
| 28 | # biggerexample: the same length as example, but bigger |
| 29 | # outside: An entry that is not in example |
| 30 | # minitemsize: the minimum guaranteed itemsize |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 31 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 32 | def assertEntryEqual(self, entry1, entry2): |
| 33 | self.assertEqual(entry1, entry2) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 34 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 35 | def badtypecode(self): |
| 36 | # Return a typecode that is different from our own |
| 37 | return typecodes[(typecodes.index(self.typecode)+1) % len(typecodes)] |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 38 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 39 | def test_constructor(self): |
| 40 | a = array.array(self.typecode) |
| 41 | self.assertEqual(a.typecode, self.typecode) |
| 42 | self.assert_(a.itemsize>=self.minitemsize) |
| 43 | self.assertRaises(TypeError, array.array, self.typecode, None) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 44 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 45 | def test_len(self): |
| 46 | a = array.array(self.typecode) |
| 47 | a.append(self.example[0]) |
| 48 | self.assertEqual(len(a), 1) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 49 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 50 | a = array.array(self.typecode, self.example) |
| 51 | self.assertEqual(len(a), len(self.example)) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 52 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 53 | def test_buffer_info(self): |
| 54 | a = array.array(self.typecode, self.example) |
| 55 | self.assertRaises(TypeError, a.buffer_info, 42) |
| 56 | bi = a.buffer_info() |
| 57 | self.assert_(isinstance(bi, tuple)) |
| 58 | self.assertEqual(len(bi), 2) |
| 59 | self.assert_(isinstance(bi[0], int)) |
| 60 | self.assert_(isinstance(bi[1], int)) |
| 61 | self.assertEqual(bi[1], len(a)) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 62 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 63 | def test_byteswap(self): |
| 64 | a = array.array(self.typecode, self.example) |
| 65 | self.assertRaises(TypeError, a.byteswap, 42) |
| 66 | if a.itemsize in (1, 2, 4, 8): |
| 67 | b = array.array(self.typecode, self.example) |
| 68 | b.byteswap() |
| 69 | if a.itemsize==1: |
| 70 | self.assertEqual(a, b) |
| 71 | else: |
| 72 | self.assertNotEqual(a, b) |
| 73 | b.byteswap() |
| 74 | self.assertEqual(a, b) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 75 | |
Raymond Hettinger | 3aa82c0 | 2004-03-13 18:18:51 +0000 | [diff] [blame] | 76 | def test_copy(self): |
| 77 | import copy |
| 78 | a = array.array(self.typecode, self.example) |
| 79 | b = copy.copy(a) |
| 80 | self.assertNotEqual(id(a), id(b)) |
| 81 | self.assertEqual(a, b) |
| 82 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 83 | def test_insert(self): |
| 84 | a = array.array(self.typecode, self.example) |
| 85 | a.insert(0, self.example[0]) |
| 86 | self.assertEqual(len(a), 1+len(self.example)) |
| 87 | self.assertEqual(a[0], a[1]) |
| 88 | self.assertRaises(TypeError, a.insert) |
| 89 | self.assertRaises(TypeError, a.insert, None) |
Walter Dörwald | ba39d9c | 2003-05-18 01:56:25 +0000 | [diff] [blame] | 90 | self.assertRaises(TypeError, a.insert, 0, None) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 91 | |
Walter Dörwald | 9e46abe | 2003-05-18 03:15:10 +0000 | [diff] [blame] | 92 | a = array.array(self.typecode, self.example) |
| 93 | a.insert(-1, self.example[0]) |
| 94 | self.assertEqual( |
| 95 | a, |
| 96 | array.array( |
| 97 | self.typecode, |
| 98 | self.example[:-1] + self.example[:1] + self.example[-1:] |
| 99 | ) |
| 100 | ) |
| 101 | |
| 102 | a = array.array(self.typecode, self.example) |
| 103 | a.insert(-1000, self.example[0]) |
| 104 | self.assertEqual( |
| 105 | a, |
| 106 | array.array(self.typecode, self.example[:1] + self.example) |
| 107 | ) |
| 108 | |
| 109 | a = array.array(self.typecode, self.example) |
| 110 | a.insert(1000, self.example[0]) |
| 111 | self.assertEqual( |
| 112 | a, |
| 113 | array.array(self.typecode, self.example + self.example[:1]) |
| 114 | ) |
| 115 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 116 | def test_tofromfile(self): |
| 117 | a = array.array(self.typecode, 2*self.example) |
| 118 | self.assertRaises(TypeError, a.tofile) |
| 119 | self.assertRaises(TypeError, a.tofile, cStringIO.StringIO()) |
| 120 | f = open(test_support.TESTFN, 'wb') |
| 121 | try: |
| 122 | a.tofile(f) |
| 123 | f.close() |
| 124 | b = array.array(self.typecode) |
| 125 | f = open(test_support.TESTFN, 'rb') |
| 126 | self.assertRaises(TypeError, b.fromfile) |
| 127 | self.assertRaises( |
| 128 | TypeError, |
| 129 | b.fromfile, |
| 130 | cStringIO.StringIO(), len(self.example) |
| 131 | ) |
| 132 | b.fromfile(f, len(self.example)) |
| 133 | self.assertEqual(b, array.array(self.typecode, self.example)) |
| 134 | self.assertNotEqual(a, b) |
| 135 | b.fromfile(f, len(self.example)) |
| 136 | self.assertEqual(a, b) |
| 137 | self.assertRaises(EOFError, b.fromfile, f, 1) |
| 138 | f.close() |
| 139 | finally: |
| 140 | if not f.closed: |
| 141 | f.close() |
| 142 | test_support.unlink(test_support.TESTFN) |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 143 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 144 | def test_tofromlist(self): |
| 145 | a = array.array(self.typecode, 2*self.example) |
| 146 | b = array.array(self.typecode) |
| 147 | self.assertRaises(TypeError, a.tolist, 42) |
| 148 | self.assertRaises(TypeError, b.fromlist) |
| 149 | self.assertRaises(TypeError, b.fromlist, 42) |
| 150 | self.assertRaises(TypeError, b.fromlist, [None]) |
| 151 | b.fromlist(a.tolist()) |
| 152 | self.assertEqual(a, b) |
Fred Drake | 7833447 | 2000-06-28 17:50:51 +0000 | [diff] [blame] | 153 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 154 | def test_tofromstring(self): |
| 155 | a = array.array(self.typecode, 2*self.example) |
| 156 | b = array.array(self.typecode) |
| 157 | self.assertRaises(TypeError, a.tostring, 42) |
| 158 | self.assertRaises(TypeError, b.fromstring) |
| 159 | self.assertRaises(TypeError, b.fromstring, 42) |
| 160 | b.fromstring(a.tostring()) |
| 161 | self.assertEqual(a, b) |
| 162 | if a.itemsize>1: |
| 163 | self.assertRaises(ValueError, b.fromstring, "x") |
Fred Drake | 7833447 | 2000-06-28 17:50:51 +0000 | [diff] [blame] | 164 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 165 | def test_repr(self): |
| 166 | a = array.array(self.typecode, 2*self.example) |
| 167 | self.assertEqual(a, eval(repr(a), {"array": array.array})) |
Fred Drake | 7833447 | 2000-06-28 17:50:51 +0000 | [diff] [blame] | 168 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 169 | a = array.array(self.typecode) |
| 170 | self.assertEqual(repr(a), "array('%s')" % self.typecode) |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 171 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 172 | def test_str(self): |
| 173 | a = array.array(self.typecode, 2*self.example) |
| 174 | str(a) |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 175 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 176 | def test_cmp(self): |
| 177 | a = array.array(self.typecode, self.example) |
| 178 | self.assert_((a == 42) is False) |
| 179 | self.assert_((a != 42) is True) |
Tim Peters | cc78e47 | 2000-11-14 21:36:07 +0000 | [diff] [blame] | 180 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 181 | self.assert_((a == a) is True) |
| 182 | self.assert_((a != a) is False) |
| 183 | self.assert_((a < a) is False) |
| 184 | self.assert_((a <= a) is True) |
| 185 | self.assert_((a > a) is False) |
| 186 | self.assert_((a >= a) is True) |
Tim Peters | cc78e47 | 2000-11-14 21:36:07 +0000 | [diff] [blame] | 187 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 188 | as = array.array(self.typecode, self.smallerexample) |
| 189 | ab = array.array(self.typecode, self.biggerexample) |
Guido van Rossum | 7f1d3aa | 1998-07-16 15:31:43 +0000 | [diff] [blame] | 190 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 191 | self.assert_((a == 2*a) is False) |
| 192 | self.assert_((a != 2*a) is True) |
| 193 | self.assert_((a < 2*a) is True) |
| 194 | self.assert_((a <= 2*a) is True) |
| 195 | self.assert_((a > 2*a) is False) |
| 196 | self.assert_((a >= 2*a) is False) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 197 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 198 | self.assert_((a == as) is False) |
| 199 | self.assert_((a != as) is True) |
| 200 | self.assert_((a < as) is False) |
| 201 | self.assert_((a <= as) is False) |
| 202 | self.assert_((a > as) is True) |
| 203 | self.assert_((a >= as) is True) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 204 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 205 | self.assert_((a == ab) is False) |
| 206 | self.assert_((a != ab) is True) |
| 207 | self.assert_((a < ab) is True) |
| 208 | self.assert_((a <= ab) is True) |
| 209 | self.assert_((a > ab) is False) |
| 210 | self.assert_((a >= ab) is False) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 211 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 212 | def test_add(self): |
| 213 | a = array.array(self.typecode, self.example) \ |
| 214 | + array.array(self.typecode, self.example[::-1]) |
| 215 | self.assertEqual( |
| 216 | a, |
| 217 | array.array(self.typecode, self.example + self.example[::-1]) |
| 218 | ) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 219 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 220 | b = array.array(self.badtypecode()) |
| 221 | self.assertRaises(TypeError, a.__add__, b) |
| 222 | |
| 223 | self.assertRaises(TypeError, a.__add__, "bad") |
| 224 | |
| 225 | def test_iadd(self): |
| 226 | a = array.array(self.typecode, self.example[::-1]) |
| 227 | b = a |
| 228 | a += array.array(self.typecode, 2*self.example) |
| 229 | self.assert_(a is b) |
| 230 | self.assertEqual( |
| 231 | a, |
| 232 | array.array(self.typecode, self.example[::-1]+2*self.example) |
| 233 | ) |
| 234 | |
| 235 | b = array.array(self.badtypecode()) |
| 236 | self.assertRaises(TypeError, a.__add__, b) |
| 237 | |
| 238 | self.assertRaises(TypeError, a.__iadd__, "bad") |
| 239 | |
| 240 | def test_mul(self): |
| 241 | a = 5*array.array(self.typecode, self.example) |
| 242 | self.assertEqual( |
| 243 | a, |
| 244 | array.array(self.typecode, 5*self.example) |
| 245 | ) |
| 246 | |
| 247 | a = array.array(self.typecode, self.example)*5 |
| 248 | self.assertEqual( |
| 249 | a, |
| 250 | array.array(self.typecode, self.example*5) |
| 251 | ) |
| 252 | |
| 253 | a = 0*array.array(self.typecode, self.example) |
| 254 | self.assertEqual( |
| 255 | a, |
| 256 | array.array(self.typecode) |
| 257 | ) |
| 258 | |
| 259 | a = (-1)*array.array(self.typecode, self.example) |
| 260 | self.assertEqual( |
| 261 | a, |
| 262 | array.array(self.typecode) |
| 263 | ) |
| 264 | |
| 265 | self.assertRaises(TypeError, a.__mul__, "bad") |
| 266 | |
| 267 | def test_imul(self): |
| 268 | a = array.array(self.typecode, self.example) |
| 269 | b = a |
| 270 | |
| 271 | a *= 5 |
| 272 | self.assert_(a is b) |
| 273 | self.assertEqual( |
| 274 | a, |
| 275 | array.array(self.typecode, 5*self.example) |
| 276 | ) |
| 277 | |
| 278 | a *= 0 |
| 279 | self.assert_(a is b) |
| 280 | self.assertEqual(a, array.array(self.typecode)) |
| 281 | |
| 282 | a *= 1000 |
| 283 | self.assert_(a is b) |
| 284 | self.assertEqual(a, array.array(self.typecode)) |
| 285 | |
| 286 | a *= -1 |
| 287 | self.assert_(a is b) |
| 288 | self.assertEqual(a, array.array(self.typecode)) |
| 289 | |
| 290 | a = array.array(self.typecode, self.example) |
| 291 | a *= -1 |
| 292 | self.assertEqual(a, array.array(self.typecode)) |
| 293 | |
| 294 | self.assertRaises(TypeError, a.__imul__, "bad") |
| 295 | |
| 296 | def test_getitem(self): |
| 297 | a = array.array(self.typecode, self.example) |
| 298 | self.assertEntryEqual(a[0], self.example[0]) |
| 299 | self.assertEntryEqual(a[0L], self.example[0]) |
| 300 | self.assertEntryEqual(a[-1], self.example[-1]) |
| 301 | self.assertEntryEqual(a[-1L], self.example[-1]) |
| 302 | self.assertEntryEqual(a[len(self.example)-1], self.example[-1]) |
| 303 | self.assertEntryEqual(a[-len(self.example)], self.example[0]) |
| 304 | self.assertRaises(TypeError, a.__getitem__) |
| 305 | self.assertRaises(IndexError, a.__getitem__, len(self.example)) |
| 306 | self.assertRaises(IndexError, a.__getitem__, -len(self.example)-1) |
| 307 | |
| 308 | def test_setitem(self): |
| 309 | a = array.array(self.typecode, self.example) |
| 310 | a[0] = a[-1] |
| 311 | self.assertEntryEqual(a[0], a[-1]) |
| 312 | |
| 313 | a = array.array(self.typecode, self.example) |
| 314 | a[0L] = a[-1] |
| 315 | self.assertEntryEqual(a[0], a[-1]) |
| 316 | |
| 317 | a = array.array(self.typecode, self.example) |
| 318 | a[-1] = a[0] |
| 319 | self.assertEntryEqual(a[0], a[-1]) |
| 320 | |
| 321 | a = array.array(self.typecode, self.example) |
| 322 | a[-1L] = a[0] |
| 323 | self.assertEntryEqual(a[0], a[-1]) |
| 324 | |
| 325 | a = array.array(self.typecode, self.example) |
| 326 | a[len(self.example)-1] = a[0] |
| 327 | self.assertEntryEqual(a[0], a[-1]) |
| 328 | |
| 329 | a = array.array(self.typecode, self.example) |
| 330 | a[-len(self.example)] = a[-1] |
| 331 | self.assertEntryEqual(a[0], a[-1]) |
| 332 | |
| 333 | self.assertRaises(TypeError, a.__setitem__) |
| 334 | self.assertRaises(TypeError, a.__setitem__, None) |
| 335 | self.assertRaises(TypeError, a.__setitem__, 0, None) |
| 336 | self.assertRaises( |
| 337 | IndexError, |
| 338 | a.__setitem__, |
| 339 | len(self.example), self.example[0] |
| 340 | ) |
| 341 | self.assertRaises( |
| 342 | IndexError, |
| 343 | a.__setitem__, |
| 344 | -len(self.example)-1, self.example[0] |
| 345 | ) |
| 346 | |
| 347 | def test_delitem(self): |
| 348 | a = array.array(self.typecode, self.example) |
| 349 | del a[0] |
| 350 | self.assertEqual( |
| 351 | a, |
| 352 | array.array(self.typecode, self.example[1:]) |
| 353 | ) |
| 354 | |
| 355 | a = array.array(self.typecode, self.example) |
| 356 | del a[-1] |
| 357 | self.assertEqual( |
| 358 | a, |
| 359 | array.array(self.typecode, self.example[:-1]) |
| 360 | ) |
| 361 | |
| 362 | a = array.array(self.typecode, self.example) |
| 363 | del a[len(self.example)-1] |
| 364 | self.assertEqual( |
| 365 | a, |
| 366 | array.array(self.typecode, self.example[:-1]) |
| 367 | ) |
| 368 | |
| 369 | a = array.array(self.typecode, self.example) |
| 370 | del a[-len(self.example)] |
| 371 | self.assertEqual( |
| 372 | a, |
| 373 | array.array(self.typecode, self.example[1:]) |
| 374 | ) |
| 375 | |
| 376 | self.assertRaises(TypeError, a.__delitem__) |
| 377 | self.assertRaises(TypeError, a.__delitem__, None) |
| 378 | self.assertRaises(IndexError, a.__delitem__, len(self.example)) |
| 379 | self.assertRaises(IndexError, a.__delitem__, -len(self.example)-1) |
| 380 | |
| 381 | def test_getslice(self): |
| 382 | a = array.array(self.typecode, self.example) |
| 383 | self.assertEqual(a[:], a) |
| 384 | |
| 385 | self.assertEqual( |
| 386 | a[1:], |
| 387 | array.array(self.typecode, self.example[1:]) |
| 388 | ) |
| 389 | |
| 390 | self.assertEqual( |
| 391 | a[:1], |
| 392 | array.array(self.typecode, self.example[:1]) |
| 393 | ) |
| 394 | |
| 395 | self.assertEqual( |
| 396 | a[:-1], |
| 397 | array.array(self.typecode, self.example[:-1]) |
| 398 | ) |
| 399 | |
| 400 | self.assertEqual( |
| 401 | a[-1:], |
| 402 | array.array(self.typecode, self.example[-1:]) |
| 403 | ) |
| 404 | |
| 405 | self.assertEqual( |
| 406 | a[-1:-1], |
| 407 | array.array(self.typecode) |
| 408 | ) |
| 409 | |
| 410 | self.assertEqual( |
| 411 | a[1000:], |
| 412 | array.array(self.typecode) |
| 413 | ) |
| 414 | self.assertEqual(a[-1000:], a) |
| 415 | self.assertEqual(a[:1000], a) |
| 416 | self.assertEqual( |
| 417 | a[:-1000], |
| 418 | array.array(self.typecode) |
| 419 | ) |
| 420 | self.assertEqual(a[-1000:1000], a) |
| 421 | self.assertEqual( |
| 422 | a[2000:1000], |
| 423 | array.array(self.typecode) |
| 424 | ) |
| 425 | |
| 426 | def test_setslice(self): |
| 427 | a = array.array(self.typecode, self.example) |
| 428 | a[:1] = a |
| 429 | self.assertEqual( |
| 430 | a, |
| 431 | array.array(self.typecode, self.example + self.example[1:]) |
| 432 | ) |
| 433 | |
| 434 | a = array.array(self.typecode, self.example) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 435 | a[:-1] = a |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 436 | self.assertEqual( |
| 437 | a, |
| 438 | array.array(self.typecode, self.example + self.example[-1:]) |
| 439 | ) |
| 440 | |
| 441 | a = array.array(self.typecode, self.example) |
| 442 | a[-1:] = a |
| 443 | self.assertEqual( |
| 444 | a, |
| 445 | array.array(self.typecode, self.example[:-1] + self.example) |
| 446 | ) |
| 447 | |
| 448 | a = array.array(self.typecode, self.example) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 449 | a[1:] = a |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 450 | self.assertEqual( |
| 451 | a, |
| 452 | array.array(self.typecode, self.example[:1] + self.example) |
| 453 | ) |
| 454 | |
| 455 | a = array.array(self.typecode, self.example) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 456 | a[1:-1] = a |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 457 | self.assertEqual( |
| 458 | a, |
| 459 | array.array( |
| 460 | self.typecode, |
| 461 | self.example[:1] + self.example + self.example[-1:] |
| 462 | ) |
| 463 | ) |
| 464 | |
| 465 | a = array.array(self.typecode, self.example) |
| 466 | a[1000:] = a |
| 467 | self.assertEqual( |
| 468 | a, |
| 469 | array.array(self.typecode, 2*self.example) |
| 470 | ) |
| 471 | |
| 472 | a = array.array(self.typecode, self.example) |
| 473 | a[-1000:] = a |
| 474 | self.assertEqual( |
| 475 | a, |
| 476 | array.array(self.typecode, self.example) |
| 477 | ) |
| 478 | |
| 479 | a = array.array(self.typecode, self.example) |
| 480 | a[:1000] = a |
| 481 | self.assertEqual( |
| 482 | a, |
| 483 | array.array(self.typecode, self.example) |
| 484 | ) |
| 485 | |
| 486 | a = array.array(self.typecode, self.example) |
| 487 | a[:-1000] = a |
| 488 | self.assertEqual( |
| 489 | a, |
| 490 | array.array(self.typecode, 2*self.example) |
| 491 | ) |
| 492 | |
| 493 | a = array.array(self.typecode, self.example) |
| 494 | a[1:0] = a |
| 495 | self.assertEqual( |
| 496 | a, |
| 497 | array.array(self.typecode, self.example[:1] + self.example + self.example[1:]) |
| 498 | ) |
| 499 | |
| 500 | a = array.array(self.typecode, self.example) |
| 501 | a[2000:1000] = a |
| 502 | self.assertEqual( |
| 503 | a, |
| 504 | array.array(self.typecode, 2*self.example) |
| 505 | ) |
| 506 | |
| 507 | a = array.array(self.typecode, self.example) |
| 508 | self.assertRaises(TypeError, a.__setslice__, 0, 0, None) |
| 509 | self.assertRaises(TypeError, a.__setitem__, slice(0, 1), None) |
| 510 | |
| 511 | b = array.array(self.badtypecode()) |
| 512 | self.assertRaises(TypeError, a.__setslice__, 0, 0, b) |
| 513 | self.assertRaises(TypeError, a.__setitem__, slice(0, 1), b) |
| 514 | |
| 515 | def test_index(self): |
| 516 | example = 2*self.example |
| 517 | a = array.array(self.typecode, example) |
| 518 | self.assertRaises(TypeError, a.index) |
| 519 | for x in example: |
| 520 | self.assertEqual(a.index(x), example.index(x)) |
| 521 | self.assertRaises(ValueError, a.index, None) |
| 522 | self.assertRaises(ValueError, a.index, self.outside) |
| 523 | |
| 524 | def test_count(self): |
| 525 | example = 2*self.example |
| 526 | a = array.array(self.typecode, example) |
| 527 | self.assertRaises(TypeError, a.count) |
| 528 | for x in example: |
| 529 | self.assertEqual(a.count(x), example.count(x)) |
| 530 | self.assertEqual(a.count(self.outside), 0) |
| 531 | self.assertEqual(a.count(None), 0) |
| 532 | |
| 533 | def test_remove(self): |
| 534 | for x in self.example: |
| 535 | example = 2*self.example |
| 536 | a = array.array(self.typecode, example) |
| 537 | pos = example.index(x) |
| 538 | example2 = example[:pos] + example[pos+1:] |
| 539 | a.remove(x) |
| 540 | self.assertEqual(a, array.array(self.typecode, example2)) |
| 541 | |
| 542 | a = array.array(self.typecode, self.example) |
| 543 | self.assertRaises(ValueError, a.remove, self.outside) |
| 544 | |
| 545 | self.assertRaises(ValueError, a.remove, None) |
| 546 | |
| 547 | def test_pop(self): |
| 548 | a = array.array(self.typecode) |
| 549 | self.assertRaises(IndexError, a.pop) |
| 550 | |
| 551 | a = array.array(self.typecode, 2*self.example) |
| 552 | self.assertRaises(TypeError, a.pop, 42, 42) |
| 553 | self.assertRaises(TypeError, a.pop, None) |
| 554 | self.assertRaises(IndexError, a.pop, len(a)) |
| 555 | self.assertRaises(IndexError, a.pop, -len(a)-1) |
| 556 | |
| 557 | self.assertEntryEqual(a.pop(0), self.example[0]) |
| 558 | self.assertEqual( |
| 559 | a, |
| 560 | array.array(self.typecode, self.example[1:]+self.example) |
| 561 | ) |
| 562 | self.assertEntryEqual(a.pop(1), self.example[2]) |
| 563 | self.assertEqual( |
| 564 | a, |
| 565 | array.array(self.typecode, self.example[1:2]+self.example[3:]+self.example) |
| 566 | ) |
| 567 | self.assertEntryEqual(a.pop(0), self.example[1]) |
| 568 | self.assertEntryEqual(a.pop(), self.example[-1]) |
| 569 | self.assertEqual( |
| 570 | a, |
| 571 | array.array(self.typecode, self.example[3:]+self.example[:-1]) |
| 572 | ) |
| 573 | |
| 574 | def test_reverse(self): |
| 575 | a = array.array(self.typecode, self.example) |
| 576 | self.assertRaises(TypeError, a.reverse, 42) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 577 | a.reverse() |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 578 | self.assertEqual( |
| 579 | a, |
| 580 | array.array(self.typecode, self.example[::-1]) |
| 581 | ) |
| 582 | |
| 583 | def test_extend(self): |
| 584 | a = array.array(self.typecode, self.example) |
| 585 | self.assertRaises(TypeError, a.extend) |
| 586 | a.extend(array.array(self.typecode, self.example[::-1])) |
| 587 | self.assertEqual( |
| 588 | a, |
| 589 | array.array(self.typecode, self.example+self.example[::-1]) |
| 590 | ) |
| 591 | |
| 592 | b = array.array(self.badtypecode()) |
| 593 | self.assertRaises(TypeError, a.extend, b) |
| 594 | |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 595 | a = array.array(self.typecode, self.example) |
| 596 | a.extend(self.example[::-1]) |
| 597 | self.assertEqual( |
| 598 | a, |
| 599 | array.array(self.typecode, self.example+self.example[::-1]) |
| 600 | ) |
| 601 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 602 | def test_coveritertraverse(self): |
| 603 | try: |
| 604 | import gc |
| 605 | except ImportError: |
| 606 | return |
| 607 | a = array.array(self.typecode) |
| 608 | l = [iter(a)] |
| 609 | l.append(l) |
| 610 | gc.collect() |
| 611 | |
| 612 | def test_buffer(self): |
| 613 | a = array.array(self.typecode, self.example) |
| 614 | b = buffer(a) |
| 615 | self.assertEqual(b[0], a.tostring()[0]) |
| 616 | |
Skip Montanaro | d5cf0b8 | 2003-08-03 23:02:10 +0000 | [diff] [blame] | 617 | def test_bug_782369(self): |
| 618 | import sys |
Skip Montanaro | ed9bf12 | 2003-08-03 23:30:40 +0000 | [diff] [blame] | 619 | if hasattr(sys, "getrefcount"): |
| 620 | for i in range(10): |
| 621 | b = array.array('B', range(64)) |
| 622 | rc = sys.getrefcount(10) |
| 623 | for i in range(10): |
| 624 | b = array.array('B', range(64)) |
| 625 | self.assertEqual(rc, sys.getrefcount(10)) |
Skip Montanaro | d5cf0b8 | 2003-08-03 23:02:10 +0000 | [diff] [blame] | 626 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 627 | class StringTest(BaseTest): |
| 628 | |
| 629 | def test_setitem(self): |
| 630 | super(StringTest, self).test_setitem() |
| 631 | a = array.array(self.typecode, self.example) |
| 632 | self.assertRaises(TypeError, a.__setitem__, 0, self.example[:2]) |
| 633 | |
| 634 | class CharacterTest(StringTest): |
| 635 | typecode = 'c' |
| 636 | example = '\x01azAZ\x00\xfe' |
| 637 | smallerexample = '\x01azAY\x00\xfe' |
| 638 | biggerexample = '\x01azAZ\x00\xff' |
| 639 | outside = '\x33' |
| 640 | minitemsize = 1 |
| 641 | |
| 642 | def test_subbclassing(self): |
| 643 | class EditableString(array.array): |
| 644 | def __new__(cls, s, *args, **kwargs): |
| 645 | return array.array.__new__(cls, 'c', s) |
| 646 | |
| 647 | def __init__(self, s, color='blue'): |
| 648 | array.array.__init__(self, 'c', s) |
| 649 | self.color = color |
| 650 | |
| 651 | def strip(self): |
| 652 | self[:] = array.array('c', self.tostring().strip()) |
| 653 | |
| 654 | def __repr__(self): |
| 655 | return 'EditableString(%r)' % self.tostring() |
| 656 | |
| 657 | s = EditableString("\ttest\r\n") |
| 658 | s.strip() |
| 659 | self.assertEqual(s.tostring(), "test") |
| 660 | |
| 661 | self.assertEqual(s.color, "blue") |
| 662 | s.color = "red" |
| 663 | self.assertEqual(s.color, "red") |
| 664 | self.assertEqual(s.__dict__.keys(), ["color"]) |
| 665 | |
| 666 | def test_nounicode(self): |
| 667 | a = array.array(self.typecode, self.example) |
| 668 | self.assertRaises(ValueError, a.fromunicode, unicode('')) |
| 669 | self.assertRaises(ValueError, a.tounicode) |
| 670 | |
| 671 | tests.append(CharacterTest) |
| 672 | |
| 673 | if test_support.have_unicode: |
| 674 | class UnicodeTest(StringTest): |
| 675 | typecode = 'u' |
| 676 | example = unicode(r'\x01\u263a\x00\ufeff', 'unicode-escape') |
| 677 | smallerexample = unicode(r'\x01\u263a\x00\ufefe', 'unicode-escape') |
| 678 | biggerexample = unicode(r'\x01\u263a\x01\ufeff', 'unicode-escape') |
| 679 | outside = unicode('\x33') |
| 680 | minitemsize = 2 |
| 681 | |
| 682 | def test_unicode(self): |
| 683 | self.assertRaises(TypeError, array.array, 'b', unicode('foo', 'ascii')) |
| 684 | |
| 685 | a = array.array('u', unicode(r'\xa0\xc2\u1234', 'unicode-escape')) |
| 686 | a.fromunicode(unicode(' ', 'ascii')) |
| 687 | a.fromunicode(unicode('', 'ascii')) |
| 688 | a.fromunicode(unicode('', 'ascii')) |
| 689 | a.fromunicode(unicode(r'\x11abc\xff\u1234', 'unicode-escape')) |
| 690 | s = a.tounicode() |
| 691 | self.assertEqual( |
| 692 | s, |
| 693 | unicode(r'\xa0\xc2\u1234 \x11abc\xff\u1234', 'unicode-escape') |
| 694 | ) |
| 695 | |
| 696 | s = unicode(r'\x00="\'a\\b\x80\xff\u0000\u0001\u1234', 'unicode-escape') |
| 697 | a = array.array('u', s) |
| 698 | self.assertEqual( |
| 699 | repr(a), |
| 700 | r"""array('u', u'\x00="\'a\\b\x80\xff\x00\x01\u1234')""" |
| 701 | ) |
| 702 | |
| 703 | self.assertRaises(TypeError, a.fromunicode) |
| 704 | |
| 705 | tests.append(UnicodeTest) |
| 706 | |
| 707 | class NumberTest(BaseTest): |
| 708 | |
| 709 | def test_extslice(self): |
| 710 | a = array.array(self.typecode, range(5)) |
| 711 | self.assertEqual(a[::], a) |
| 712 | self.assertEqual(a[::2], array.array(self.typecode, [0,2,4])) |
| 713 | self.assertEqual(a[1::2], array.array(self.typecode, [1,3])) |
| 714 | self.assertEqual(a[::-1], array.array(self.typecode, [4,3,2,1,0])) |
| 715 | self.assertEqual(a[::-2], array.array(self.typecode, [4,2,0])) |
| 716 | self.assertEqual(a[3::-2], array.array(self.typecode, [3,1])) |
| 717 | self.assertEqual(a[-100:100:], a) |
| 718 | self.assertEqual(a[100:-100:-1], a[::-1]) |
| 719 | self.assertEqual(a[-100L:100L:2L], array.array(self.typecode, [0,2,4])) |
| 720 | self.assertEqual(a[1000:2000:2], array.array(self.typecode, [])) |
| 721 | self.assertEqual(a[-1000:-2000:-2], array.array(self.typecode, [])) |
| 722 | |
| 723 | def test_delslice(self): |
| 724 | a = array.array(self.typecode, range(5)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 725 | del a[::2] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 726 | self.assertEqual(a, array.array(self.typecode, [1,3])) |
| 727 | a = array.array(self.typecode, range(5)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 728 | del a[1::2] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 729 | self.assertEqual(a, array.array(self.typecode, [0,2,4])) |
| 730 | a = array.array(self.typecode, range(5)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 731 | del a[1::-2] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 732 | self.assertEqual(a, array.array(self.typecode, [0,2,3,4])) |
| 733 | a = array.array(self.typecode, range(10)) |
Michael W. Hudson | 56796f6 | 2002-07-29 14:35:04 +0000 | [diff] [blame] | 734 | del a[::1000] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 735 | self.assertEqual(a, array.array(self.typecode, [1,2,3,4,5,6,7,8,9])) |
| 736 | |
| 737 | def test_assignment(self): |
| 738 | a = array.array(self.typecode, range(10)) |
| 739 | a[::2] = array.array(self.typecode, [42]*5) |
| 740 | self.assertEqual(a, array.array(self.typecode, [42, 1, 42, 3, 42, 5, 42, 7, 42, 9])) |
| 741 | a = array.array(self.typecode, range(10)) |
| 742 | a[::-4] = array.array(self.typecode, [10]*3) |
| 743 | self.assertEqual(a, array.array(self.typecode, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])) |
| 744 | a = array.array(self.typecode, range(4)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 745 | a[::-1] = a |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 746 | self.assertEqual(a, array.array(self.typecode, [3, 2, 1, 0])) |
| 747 | a = array.array(self.typecode, range(10)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 748 | b = a[:] |
| 749 | c = a[:] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 750 | ins = array.array(self.typecode, range(2)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 751 | a[2:3] = ins |
| 752 | b[slice(2,3)] = ins |
| 753 | c[2:3:] = ins |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 754 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 755 | def test_iterationcontains(self): |
| 756 | a = array.array(self.typecode, range(10)) |
| 757 | self.assertEqual(list(a), range(10)) |
| 758 | b = array.array(self.typecode, [20]) |
| 759 | self.assertEqual(a[-1] in a, True) |
| 760 | self.assertEqual(b[0] not in a, True) |
| 761 | |
| 762 | def check_overflow(self, lower, upper): |
| 763 | # method to be used by subclasses |
| 764 | |
| 765 | # should not overflow assigning lower limit |
| 766 | a = array.array(self.typecode, [lower]) |
| 767 | a[0] = lower |
| 768 | # should overflow assigning less than lower limit |
| 769 | self.assertRaises(OverflowError, array.array, self.typecode, [lower-1]) |
| 770 | self.assertRaises(OverflowError, a.__setitem__, 0, lower-1) |
| 771 | # should not overflow assigning upper limit |
| 772 | a = array.array(self.typecode, [upper]) |
| 773 | a[0] = upper |
| 774 | # should overflow assigning more than upper limit |
| 775 | self.assertRaises(OverflowError, array.array, self.typecode, [upper+1]) |
| 776 | self.assertRaises(OverflowError, a.__setitem__, 0, upper+1) |
| 777 | |
| 778 | def test_subclassing(self): |
| 779 | typecode = self.typecode |
| 780 | class ExaggeratingArray(array.array): |
| 781 | __slots__ = ['offset'] |
| 782 | |
| 783 | def __new__(cls, typecode, data, offset): |
| 784 | return array.array.__new__(cls, typecode, data) |
| 785 | |
| 786 | def __init__(self, typecode, data, offset): |
| 787 | self.offset = offset |
| 788 | |
| 789 | def __getitem__(self, i): |
| 790 | return array.array.__getitem__(self, i) + self.offset |
| 791 | |
| 792 | a = ExaggeratingArray(self.typecode, [3, 6, 7, 11], 4) |
| 793 | self.assertEntryEqual(a[0], 7) |
| 794 | |
| 795 | self.assertRaises(AttributeError, setattr, a, "color", "blue") |
| 796 | |
| 797 | class SignedNumberTest(NumberTest): |
| 798 | example = [-1, 0, 1, 42, 0x7f] |
| 799 | smallerexample = [-1, 0, 1, 42, 0x7e] |
| 800 | biggerexample = [-1, 0, 1, 43, 0x7f] |
| 801 | outside = 23 |
| 802 | |
| 803 | def test_overflow(self): |
| 804 | a = array.array(self.typecode) |
| 805 | lower = -1 * long(pow(2, a.itemsize * 8 - 1)) |
| 806 | upper = long(pow(2, a.itemsize * 8 - 1)) - 1L |
| 807 | self.check_overflow(lower, upper) |
| 808 | |
| 809 | class UnsignedNumberTest(NumberTest): |
| 810 | example = [0, 1, 17, 23, 42, 0xff] |
| 811 | smallerexample = [0, 1, 17, 23, 42, 0xfe] |
| 812 | biggerexample = [0, 1, 17, 23, 43, 0xff] |
| 813 | outside = 0xaa |
| 814 | |
| 815 | def test_overflow(self): |
| 816 | a = array.array(self.typecode) |
| 817 | lower = 0 |
| 818 | upper = long(pow(2, a.itemsize * 8)) - 1L |
| 819 | self.check_overflow(lower, upper) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 820 | |
| 821 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 822 | class ByteTest(SignedNumberTest): |
| 823 | typecode = 'b' |
| 824 | minitemsize = 1 |
| 825 | tests.append(ByteTest) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 826 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 827 | class UnsignedByteTest(UnsignedNumberTest): |
| 828 | typecode = 'B' |
| 829 | minitemsize = 1 |
| 830 | tests.append(UnsignedByteTest) |
| 831 | |
| 832 | class ShortTest(SignedNumberTest): |
| 833 | typecode = 'h' |
| 834 | minitemsize = 2 |
| 835 | tests.append(ShortTest) |
| 836 | |
| 837 | class UnsignedShortTest(UnsignedNumberTest): |
| 838 | typecode = 'H' |
| 839 | minitemsize = 2 |
| 840 | tests.append(UnsignedShortTest) |
| 841 | |
| 842 | class IntTest(SignedNumberTest): |
| 843 | typecode = 'i' |
| 844 | minitemsize = 2 |
| 845 | tests.append(IntTest) |
| 846 | |
| 847 | class UnsignedIntTest(UnsignedNumberTest): |
| 848 | typecode = 'I' |
| 849 | minitemsize = 2 |
| 850 | tests.append(UnsignedIntTest) |
| 851 | |
| 852 | class LongTest(SignedNumberTest): |
| 853 | typecode = 'l' |
| 854 | minitemsize = 4 |
| 855 | tests.append(LongTest) |
| 856 | |
| 857 | class UnsignedLongTest(UnsignedNumberTest): |
| 858 | typecode = 'L' |
| 859 | minitemsize = 4 |
| 860 | tests.append(UnsignedLongTest) |
| 861 | |
| 862 | class FPTest(NumberTest): |
| 863 | example = [-42.0, 0, 42, 1e5, -1e10] |
| 864 | smallerexample = [-42.0, 0, 42, 1e5, -2e10] |
| 865 | biggerexample = [-42.0, 0, 42, 1e5, 1e10] |
| 866 | outside = 23 |
| 867 | |
| 868 | def assertEntryEqual(self, entry1, entry2): |
| 869 | self.assertAlmostEqual(entry1, entry2) |
| 870 | |
Walter Dörwald | cf99b0a | 2003-05-22 13:15:31 +0000 | [diff] [blame] | 871 | def test_byteswap(self): |
| 872 | a = array.array(self.typecode, self.example) |
| 873 | self.assertRaises(TypeError, a.byteswap, 42) |
| 874 | if a.itemsize in (1, 2, 4, 8): |
| 875 | b = array.array(self.typecode, self.example) |
| 876 | b.byteswap() |
| 877 | if a.itemsize==1: |
| 878 | self.assertEqual(a, b) |
| 879 | else: |
| 880 | # On alphas treating the byte swapped bit patters as |
| 881 | # floats/doubles results in floating point exceptions |
| 882 | # => compare the 8bit string values instead |
| 883 | self.assertNotEqual(a.tostring(), b.tostring()) |
| 884 | b.byteswap() |
| 885 | self.assertEqual(a, b) |
| 886 | |
Neal Norwitz | 35c6cd0 | 2003-05-22 13:29:15 +0000 | [diff] [blame] | 887 | class FloatTest(FPTest): |
| 888 | typecode = 'f' |
| 889 | minitemsize = 4 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 890 | tests.append(FloatTest) |
| 891 | |
| 892 | class DoubleTest(FPTest): |
| 893 | typecode = 'd' |
| 894 | minitemsize = 8 |
| 895 | tests.append(DoubleTest) |
| 896 | |
| 897 | def test_main(): |
| 898 | test_support.run_unittest(*tests) |
| 899 | |
| 900 | if __name__=="__main__": |
| 901 | test_main() |