Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support |
| 2 | from test.support import bigmemtest, _1G, _2G |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3 | |
| 4 | import unittest |
| 5 | import operator |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6 | import sys |
| 7 | |
| 8 | # Bigmem testing houserules: |
| 9 | # |
| 10 | # - Try not to allocate too many large objects. It's okay to rely on |
| 11 | # refcounting semantics, but don't forget that 's = create_largestring()' |
| 12 | # doesn't release the old 's' (if it exists) until well after its new |
| 13 | # value has been created. Use 'del s' before the create_largestring call. |
| 14 | # |
| 15 | # - Do *not* compare large objects using assertEquals or similar. It's a |
| 16 | # lengty operation and the errormessage will be utterly useless due to |
| 17 | # its size. To make sure whether a result has the right contents, better |
| 18 | # to use the strip or count methods, or compare meaningful slices. |
| 19 | # |
| 20 | # - Don't forget to test for large indices, offsets and results and such, |
| 21 | # in addition to large sizes. |
| 22 | # |
| 23 | # - When repeating an object (say, a substring, or a small list) to create |
| 24 | # a large object, make the subobject of a length that is not a power of |
| 25 | # 2. That way, int-wrapping problems are more easily detected. |
| 26 | # |
| 27 | # - While the bigmemtest decorator speaks of 'minsize', all tests will |
| 28 | # actually be called with a much smaller number too, in the normal |
| 29 | # test run (5Kb currently.) This is so the tests themselves get frequent |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 30 | # testing. Consequently, always make all large allocations based on the |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 31 | # passed-in 'size', and don't rely on the size being very large. Also, |
| 32 | # memuse-per-size should remain sane (less than a few thousand); if your |
| 33 | # test uses more, adjust 'size' upward, instead. |
| 34 | |
| 35 | class StrTest(unittest.TestCase): |
| 36 | @bigmemtest(minsize=_2G, memuse=2) |
| 37 | def test_capitalize(self, size): |
| 38 | SUBSTR = ' abc def ghi' |
| 39 | s = '-' * size + SUBSTR |
| 40 | caps = s.capitalize() |
| 41 | self.assertEquals(caps[-len(SUBSTR):], |
| 42 | SUBSTR.capitalize()) |
| 43 | self.assertEquals(caps.lstrip('-'), SUBSTR) |
| 44 | |
| 45 | @bigmemtest(minsize=_2G + 10, memuse=1) |
| 46 | def test_center(self, size): |
| 47 | SUBSTR = ' abc def ghi' |
| 48 | s = SUBSTR.center(size) |
| 49 | self.assertEquals(len(s), size) |
| 50 | lpadsize = rpadsize = (len(s) - len(SUBSTR)) // 2 |
| 51 | if len(s) % 2: |
| 52 | lpadsize += 1 |
| 53 | self.assertEquals(s[lpadsize:-rpadsize], SUBSTR) |
| 54 | self.assertEquals(s.strip(), SUBSTR.strip()) |
| 55 | |
| 56 | @bigmemtest(minsize=_2G, memuse=2) |
| 57 | def test_count(self, size): |
| 58 | SUBSTR = ' abc def ghi' |
| 59 | s = '.' * size + SUBSTR |
| 60 | self.assertEquals(s.count('.'), size) |
| 61 | s += '.' |
| 62 | self.assertEquals(s.count('.'), size + 1) |
| 63 | self.assertEquals(s.count(' '), 3) |
| 64 | self.assertEquals(s.count('i'), 1) |
| 65 | self.assertEquals(s.count('j'), 0) |
| 66 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 67 | @bigmemtest(minsize=_2G + 2, memuse=3) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 68 | def test_decode(self, size): |
Amaury Forgeot d'Arc | 8598422 | 2007-11-30 23:45:17 +0000 | [diff] [blame] | 69 | s = b'.' * size |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 70 | self.assertEquals(len(s.decode('utf-8')), size) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 71 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 72 | @bigmemtest(minsize=_2G + 2, memuse=3) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 73 | def test_encode(self, size): |
Amaury Forgeot d'Arc | 8598422 | 2007-11-30 23:45:17 +0000 | [diff] [blame] | 74 | s = '.' * size |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 75 | self.assertEquals(len(s.encode('utf-8')), size) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 76 | |
| 77 | @bigmemtest(minsize=_2G, memuse=2) |
| 78 | def test_endswith(self, size): |
| 79 | SUBSTR = ' abc def ghi' |
| 80 | s = '-' * size + SUBSTR |
| 81 | self.failUnless(s.endswith(SUBSTR)) |
| 82 | self.failUnless(s.endswith(s)) |
| 83 | s2 = '...' + s |
| 84 | self.failUnless(s2.endswith(s)) |
| 85 | self.failIf(s.endswith('a' + SUBSTR)) |
| 86 | self.failIf(SUBSTR.endswith(s)) |
| 87 | |
| 88 | @bigmemtest(minsize=_2G + 10, memuse=2) |
| 89 | def test_expandtabs(self, size): |
| 90 | s = '-' * size |
| 91 | tabsize = 8 |
| 92 | self.assertEquals(s.expandtabs(), s) |
| 93 | del s |
| 94 | slen, remainder = divmod(size, tabsize) |
| 95 | s = ' \t' * slen |
| 96 | s = s.expandtabs(tabsize) |
| 97 | self.assertEquals(len(s), size - remainder) |
| 98 | self.assertEquals(len(s.strip(' ')), 0) |
| 99 | |
| 100 | @bigmemtest(minsize=_2G, memuse=2) |
| 101 | def test_find(self, size): |
| 102 | SUBSTR = ' abc def ghi' |
| 103 | sublen = len(SUBSTR) |
| 104 | s = ''.join([SUBSTR, '-' * size, SUBSTR]) |
| 105 | self.assertEquals(s.find(' '), 0) |
| 106 | self.assertEquals(s.find(SUBSTR), 0) |
| 107 | self.assertEquals(s.find(' ', sublen), sublen + size) |
| 108 | self.assertEquals(s.find(SUBSTR, len(SUBSTR)), sublen + size) |
| 109 | self.assertEquals(s.find('i'), SUBSTR.find('i')) |
| 110 | self.assertEquals(s.find('i', sublen), |
| 111 | sublen + size + SUBSTR.find('i')) |
| 112 | self.assertEquals(s.find('i', size), |
| 113 | sublen + size + SUBSTR.find('i')) |
| 114 | self.assertEquals(s.find('j'), -1) |
| 115 | |
| 116 | @bigmemtest(minsize=_2G, memuse=2) |
| 117 | def test_index(self, size): |
| 118 | SUBSTR = ' abc def ghi' |
| 119 | sublen = len(SUBSTR) |
| 120 | s = ''.join([SUBSTR, '-' * size, SUBSTR]) |
| 121 | self.assertEquals(s.index(' '), 0) |
| 122 | self.assertEquals(s.index(SUBSTR), 0) |
| 123 | self.assertEquals(s.index(' ', sublen), sublen + size) |
| 124 | self.assertEquals(s.index(SUBSTR, sublen), sublen + size) |
| 125 | self.assertEquals(s.index('i'), SUBSTR.index('i')) |
| 126 | self.assertEquals(s.index('i', sublen), |
| 127 | sublen + size + SUBSTR.index('i')) |
| 128 | self.assertEquals(s.index('i', size), |
| 129 | sublen + size + SUBSTR.index('i')) |
| 130 | self.assertRaises(ValueError, s.index, 'j') |
| 131 | |
| 132 | @bigmemtest(minsize=_2G, memuse=2) |
| 133 | def test_isalnum(self, size): |
| 134 | SUBSTR = '123456' |
| 135 | s = 'a' * size + SUBSTR |
| 136 | self.failUnless(s.isalnum()) |
| 137 | s += '.' |
| 138 | self.failIf(s.isalnum()) |
| 139 | |
| 140 | @bigmemtest(minsize=_2G, memuse=2) |
| 141 | def test_isalpha(self, size): |
| 142 | SUBSTR = 'zzzzzzz' |
| 143 | s = 'a' * size + SUBSTR |
| 144 | self.failUnless(s.isalpha()) |
| 145 | s += '.' |
| 146 | self.failIf(s.isalpha()) |
| 147 | |
| 148 | @bigmemtest(minsize=_2G, memuse=2) |
| 149 | def test_isdigit(self, size): |
| 150 | SUBSTR = '123456' |
| 151 | s = '9' * size + SUBSTR |
| 152 | self.failUnless(s.isdigit()) |
| 153 | s += 'z' |
| 154 | self.failIf(s.isdigit()) |
| 155 | |
| 156 | @bigmemtest(minsize=_2G, memuse=2) |
| 157 | def test_islower(self, size): |
| 158 | chars = ''.join([ chr(c) for c in range(255) if not chr(c).isupper() ]) |
| 159 | repeats = size // len(chars) + 2 |
| 160 | s = chars * repeats |
| 161 | self.failUnless(s.islower()) |
| 162 | s += 'A' |
| 163 | self.failIf(s.islower()) |
| 164 | |
| 165 | @bigmemtest(minsize=_2G, memuse=2) |
| 166 | def test_isspace(self, size): |
| 167 | whitespace = ' \f\n\r\t\v' |
| 168 | repeats = size // len(whitespace) + 2 |
| 169 | s = whitespace * repeats |
| 170 | self.failUnless(s.isspace()) |
| 171 | s += 'j' |
| 172 | self.failIf(s.isspace()) |
| 173 | |
| 174 | @bigmemtest(minsize=_2G, memuse=2) |
| 175 | def test_istitle(self, size): |
| 176 | SUBSTR = '123456' |
| 177 | s = ''.join(['A', 'a' * size, SUBSTR]) |
| 178 | self.failUnless(s.istitle()) |
| 179 | s += 'A' |
| 180 | self.failUnless(s.istitle()) |
| 181 | s += 'aA' |
| 182 | self.failIf(s.istitle()) |
| 183 | |
| 184 | @bigmemtest(minsize=_2G, memuse=2) |
| 185 | def test_isupper(self, size): |
| 186 | chars = ''.join([ chr(c) for c in range(255) if not chr(c).islower() ]) |
| 187 | repeats = size // len(chars) + 2 |
| 188 | s = chars * repeats |
| 189 | self.failUnless(s.isupper()) |
| 190 | s += 'a' |
| 191 | self.failIf(s.isupper()) |
| 192 | |
| 193 | @bigmemtest(minsize=_2G, memuse=2) |
| 194 | def test_join(self, size): |
| 195 | s = 'A' * size |
| 196 | x = s.join(['aaaaa', 'bbbbb']) |
| 197 | self.assertEquals(x.count('a'), 5) |
| 198 | self.assertEquals(x.count('b'), 5) |
| 199 | self.failUnless(x.startswith('aaaaaA')) |
| 200 | self.failUnless(x.endswith('Abbbbb')) |
| 201 | |
| 202 | @bigmemtest(minsize=_2G + 10, memuse=1) |
| 203 | def test_ljust(self, size): |
| 204 | SUBSTR = ' abc def ghi' |
| 205 | s = SUBSTR.ljust(size) |
| 206 | self.failUnless(s.startswith(SUBSTR + ' ')) |
| 207 | self.assertEquals(len(s), size) |
| 208 | self.assertEquals(s.strip(), SUBSTR.strip()) |
| 209 | |
| 210 | @bigmemtest(minsize=_2G + 10, memuse=2) |
| 211 | def test_lower(self, size): |
| 212 | s = 'A' * size |
| 213 | s = s.lower() |
| 214 | self.assertEquals(len(s), size) |
| 215 | self.assertEquals(s.count('a'), size) |
| 216 | |
| 217 | @bigmemtest(minsize=_2G + 10, memuse=1) |
| 218 | def test_lstrip(self, size): |
| 219 | SUBSTR = 'abc def ghi' |
| 220 | s = SUBSTR.rjust(size) |
| 221 | self.assertEquals(len(s), size) |
| 222 | self.assertEquals(s.lstrip(), SUBSTR.lstrip()) |
| 223 | del s |
| 224 | s = SUBSTR.ljust(size) |
| 225 | self.assertEquals(len(s), size) |
| 226 | stripped = s.lstrip() |
| 227 | self.failUnless(stripped is s) |
| 228 | |
| 229 | @bigmemtest(minsize=_2G + 10, memuse=2) |
| 230 | def test_replace(self, size): |
| 231 | replacement = 'a' |
| 232 | s = ' ' * size |
| 233 | s = s.replace(' ', replacement) |
| 234 | self.assertEquals(len(s), size) |
| 235 | self.assertEquals(s.count(replacement), size) |
| 236 | s = s.replace(replacement, ' ', size - 4) |
| 237 | self.assertEquals(len(s), size) |
| 238 | self.assertEquals(s.count(replacement), 4) |
| 239 | self.assertEquals(s[-10:], ' aaaa') |
| 240 | |
| 241 | @bigmemtest(minsize=_2G, memuse=2) |
| 242 | def test_rfind(self, size): |
| 243 | SUBSTR = ' abc def ghi' |
| 244 | sublen = len(SUBSTR) |
| 245 | s = ''.join([SUBSTR, '-' * size, SUBSTR]) |
| 246 | self.assertEquals(s.rfind(' '), sublen + size + SUBSTR.rfind(' ')) |
| 247 | self.assertEquals(s.rfind(SUBSTR), sublen + size) |
| 248 | self.assertEquals(s.rfind(' ', 0, size), SUBSTR.rfind(' ')) |
| 249 | self.assertEquals(s.rfind(SUBSTR, 0, sublen + size), 0) |
| 250 | self.assertEquals(s.rfind('i'), sublen + size + SUBSTR.rfind('i')) |
| 251 | self.assertEquals(s.rfind('i', 0, sublen), SUBSTR.rfind('i')) |
| 252 | self.assertEquals(s.rfind('i', 0, sublen + size), |
| 253 | SUBSTR.rfind('i')) |
| 254 | self.assertEquals(s.rfind('j'), -1) |
| 255 | |
| 256 | @bigmemtest(minsize=_2G, memuse=2) |
| 257 | def test_rindex(self, size): |
| 258 | SUBSTR = ' abc def ghi' |
| 259 | sublen = len(SUBSTR) |
| 260 | s = ''.join([SUBSTR, '-' * size, SUBSTR]) |
| 261 | self.assertEquals(s.rindex(' '), |
| 262 | sublen + size + SUBSTR.rindex(' ')) |
| 263 | self.assertEquals(s.rindex(SUBSTR), sublen + size) |
| 264 | self.assertEquals(s.rindex(' ', 0, sublen + size - 1), |
| 265 | SUBSTR.rindex(' ')) |
| 266 | self.assertEquals(s.rindex(SUBSTR, 0, sublen + size), 0) |
| 267 | self.assertEquals(s.rindex('i'), |
| 268 | sublen + size + SUBSTR.rindex('i')) |
| 269 | self.assertEquals(s.rindex('i', 0, sublen), SUBSTR.rindex('i')) |
| 270 | self.assertEquals(s.rindex('i', 0, sublen + size), |
| 271 | SUBSTR.rindex('i')) |
| 272 | self.assertRaises(ValueError, s.rindex, 'j') |
| 273 | |
| 274 | @bigmemtest(minsize=_2G + 10, memuse=1) |
| 275 | def test_rjust(self, size): |
| 276 | SUBSTR = ' abc def ghi' |
| 277 | s = SUBSTR.ljust(size) |
| 278 | self.failUnless(s.startswith(SUBSTR + ' ')) |
| 279 | self.assertEquals(len(s), size) |
| 280 | self.assertEquals(s.strip(), SUBSTR.strip()) |
| 281 | |
| 282 | @bigmemtest(minsize=_2G + 10, memuse=1) |
| 283 | def test_rstrip(self, size): |
| 284 | SUBSTR = ' abc def ghi' |
| 285 | s = SUBSTR.ljust(size) |
| 286 | self.assertEquals(len(s), size) |
| 287 | self.assertEquals(s.rstrip(), SUBSTR.rstrip()) |
| 288 | del s |
| 289 | s = SUBSTR.rjust(size) |
| 290 | self.assertEquals(len(s), size) |
| 291 | stripped = s.rstrip() |
| 292 | self.failUnless(stripped is s) |
| 293 | |
| 294 | # The test takes about size bytes to build a string, and then about |
| 295 | # sqrt(size) substrings of sqrt(size) in size and a list to |
| 296 | # hold sqrt(size) items. It's close but just over 2x size. |
| 297 | @bigmemtest(minsize=_2G, memuse=2.1) |
| 298 | def test_split_small(self, size): |
| 299 | # Crudely calculate an estimate so that the result of s.split won't |
| 300 | # take up an inordinate amount of memory |
| 301 | chunksize = int(size ** 0.5 + 2) |
| 302 | SUBSTR = 'a' + ' ' * chunksize |
| 303 | s = SUBSTR * chunksize |
| 304 | l = s.split() |
| 305 | self.assertEquals(len(l), chunksize) |
| 306 | self.assertEquals(set(l), set(['a'])) |
| 307 | del l |
| 308 | l = s.split('a') |
| 309 | self.assertEquals(len(l), chunksize + 1) |
| 310 | self.assertEquals(set(l), set(['', ' ' * chunksize])) |
| 311 | |
| 312 | # Allocates a string of twice size (and briefly two) and a list of |
| 313 | # size. Because of internal affairs, the s.split() call produces a |
| 314 | # list of size times the same one-character string, so we only |
| 315 | # suffer for the list size. (Otherwise, it'd cost another 48 times |
| 316 | # size in bytes!) Nevertheless, a list of size takes |
| 317 | # 8*size bytes. |
| 318 | @bigmemtest(minsize=_2G + 5, memuse=10) |
| 319 | def test_split_large(self, size): |
| 320 | s = ' a' * size + ' ' |
| 321 | l = s.split() |
| 322 | self.assertEquals(len(l), size) |
| 323 | self.assertEquals(set(l), set(['a'])) |
| 324 | del l |
| 325 | l = s.split('a') |
| 326 | self.assertEquals(len(l), size + 1) |
| 327 | self.assertEquals(set(l), set([' '])) |
| 328 | |
| 329 | @bigmemtest(minsize=_2G, memuse=2.1) |
| 330 | def test_splitlines(self, size): |
| 331 | # Crudely calculate an estimate so that the result of s.split won't |
| 332 | # take up an inordinate amount of memory |
| 333 | chunksize = int(size ** 0.5 + 2) // 2 |
| 334 | SUBSTR = ' ' * chunksize + '\n' + ' ' * chunksize + '\r\n' |
| 335 | s = SUBSTR * chunksize |
| 336 | l = s.splitlines() |
| 337 | self.assertEquals(len(l), chunksize * 2) |
| 338 | self.assertEquals(set(l), set([' ' * chunksize])) |
| 339 | |
| 340 | @bigmemtest(minsize=_2G, memuse=2) |
| 341 | def test_startswith(self, size): |
| 342 | SUBSTR = ' abc def ghi' |
| 343 | s = '-' * size + SUBSTR |
| 344 | self.failUnless(s.startswith(s)) |
| 345 | self.failUnless(s.startswith('-' * size)) |
| 346 | self.failIf(s.startswith(SUBSTR)) |
| 347 | |
| 348 | @bigmemtest(minsize=_2G, memuse=1) |
| 349 | def test_strip(self, size): |
| 350 | SUBSTR = ' abc def ghi ' |
| 351 | s = SUBSTR.rjust(size) |
| 352 | self.assertEquals(len(s), size) |
| 353 | self.assertEquals(s.strip(), SUBSTR.strip()) |
| 354 | del s |
| 355 | s = SUBSTR.ljust(size) |
| 356 | self.assertEquals(len(s), size) |
| 357 | self.assertEquals(s.strip(), SUBSTR.strip()) |
| 358 | |
| 359 | @bigmemtest(minsize=_2G, memuse=2) |
| 360 | def test_swapcase(self, size): |
| 361 | SUBSTR = "aBcDeFG12.'\xa9\x00" |
| 362 | sublen = len(SUBSTR) |
| 363 | repeats = size // sublen + 2 |
| 364 | s = SUBSTR * repeats |
| 365 | s = s.swapcase() |
| 366 | self.assertEquals(len(s), sublen * repeats) |
| 367 | self.assertEquals(s[:sublen * 3], SUBSTR.swapcase() * 3) |
| 368 | self.assertEquals(s[-sublen * 3:], SUBSTR.swapcase() * 3) |
| 369 | |
| 370 | @bigmemtest(minsize=_2G, memuse=2) |
| 371 | def test_title(self, size): |
| 372 | SUBSTR = 'SpaaHAaaAaham' |
| 373 | s = SUBSTR * (size // len(SUBSTR) + 2) |
| 374 | s = s.title() |
| 375 | self.failUnless(s.startswith((SUBSTR * 3).title())) |
| 376 | self.failUnless(s.endswith(SUBSTR.lower() * 3)) |
| 377 | |
| 378 | @bigmemtest(minsize=_2G, memuse=2) |
| 379 | def test_translate(self, size): |
Georg Brandl | 7f13e6b | 2007-08-31 10:37:15 +0000 | [diff] [blame] | 380 | trans = {ord('.'):'-', ord('a'):'!', ord('Z'):'$'} |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 381 | SUBSTR = 'aZz.z.Aaz.' |
| 382 | sublen = len(SUBSTR) |
| 383 | repeats = size // sublen + 2 |
| 384 | s = SUBSTR * repeats |
| 385 | s = s.translate(trans) |
| 386 | self.assertEquals(len(s), repeats * sublen) |
| 387 | self.assertEquals(s[:sublen], SUBSTR.translate(trans)) |
| 388 | self.assertEquals(s[-sublen:], SUBSTR.translate(trans)) |
| 389 | self.assertEquals(s.count('.'), 0) |
| 390 | self.assertEquals(s.count('!'), repeats * 2) |
| 391 | self.assertEquals(s.count('z'), repeats * 3) |
| 392 | |
| 393 | @bigmemtest(minsize=_2G + 5, memuse=2) |
| 394 | def test_upper(self, size): |
| 395 | s = 'a' * size |
| 396 | s = s.upper() |
| 397 | self.assertEquals(len(s), size) |
| 398 | self.assertEquals(s.count('A'), size) |
| 399 | |
| 400 | @bigmemtest(minsize=_2G + 20, memuse=1) |
| 401 | def test_zfill(self, size): |
| 402 | SUBSTR = '-568324723598234' |
| 403 | s = SUBSTR.zfill(size) |
| 404 | self.failUnless(s.endswith('0' + SUBSTR[1:])) |
| 405 | self.failUnless(s.startswith('-0')) |
| 406 | self.assertEquals(len(s), size) |
| 407 | self.assertEquals(s.count('0'), size - len(SUBSTR)) |
| 408 | |
| 409 | @bigmemtest(minsize=_2G + 10, memuse=2) |
| 410 | def test_format(self, size): |
| 411 | s = '-' * size |
| 412 | sf = '%s' % (s,) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 413 | self.assertEqual(s, sf) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 414 | del sf |
| 415 | sf = '..%s..' % (s,) |
| 416 | self.assertEquals(len(sf), len(s) + 4) |
| 417 | self.failUnless(sf.startswith('..-')) |
| 418 | self.failUnless(sf.endswith('-..')) |
| 419 | del s, sf |
| 420 | |
| 421 | size //= 2 |
| 422 | edge = '-' * size |
| 423 | s = ''.join([edge, '%s', edge]) |
| 424 | del edge |
| 425 | s = s % '...' |
| 426 | self.assertEquals(len(s), size * 2 + 3) |
| 427 | self.assertEquals(s.count('.'), 3) |
| 428 | self.assertEquals(s.count('-'), size * 2) |
| 429 | |
| 430 | @bigmemtest(minsize=_2G + 10, memuse=2) |
| 431 | def test_repr_small(self, size): |
| 432 | s = '-' * size |
| 433 | s = repr(s) |
| 434 | self.assertEquals(len(s), size + 2) |
| 435 | self.assertEquals(s[0], "'") |
| 436 | self.assertEquals(s[-1], "'") |
| 437 | self.assertEquals(s.count('-'), size) |
| 438 | del s |
| 439 | # repr() will create a string four times as large as this 'binary |
| 440 | # string', but we don't want to allocate much more than twice |
| 441 | # size in total. (We do extra testing in test_repr_large()) |
| 442 | size = size // 5 * 2 |
| 443 | s = '\x00' * size |
| 444 | s = repr(s) |
| 445 | self.assertEquals(len(s), size * 4 + 2) |
| 446 | self.assertEquals(s[0], "'") |
| 447 | self.assertEquals(s[-1], "'") |
| 448 | self.assertEquals(s.count('\\'), size) |
| 449 | self.assertEquals(s.count('0'), size * 2) |
| 450 | |
| 451 | @bigmemtest(minsize=_2G + 10, memuse=5) |
| 452 | def test_repr_large(self, size): |
| 453 | s = '\x00' * size |
| 454 | s = repr(s) |
| 455 | self.assertEquals(len(s), size * 4 + 2) |
| 456 | self.assertEquals(s[0], "'") |
| 457 | self.assertEquals(s[-1], "'") |
| 458 | self.assertEquals(s.count('\\'), size) |
| 459 | self.assertEquals(s.count('0'), size * 2) |
| 460 | |
| 461 | # This test is meaningful even with size < 2G, as long as the |
| 462 | # doubled string is > 2G (but it tests more if both are > 2G :) |
| 463 | @bigmemtest(minsize=_1G + 2, memuse=3) |
| 464 | def test_concat(self, size): |
| 465 | s = '.' * size |
| 466 | self.assertEquals(len(s), size) |
| 467 | s = s + s |
| 468 | self.assertEquals(len(s), size * 2) |
| 469 | self.assertEquals(s.count('.'), size * 2) |
| 470 | |
| 471 | # This test is meaningful even with size < 2G, as long as the |
| 472 | # repeated string is > 2G (but it tests more if both are > 2G :) |
| 473 | @bigmemtest(minsize=_1G + 2, memuse=3) |
| 474 | def test_repeat(self, size): |
| 475 | s = '.' * size |
| 476 | self.assertEquals(len(s), size) |
| 477 | s = s * 2 |
| 478 | self.assertEquals(len(s), size * 2) |
| 479 | self.assertEquals(s.count('.'), size * 2) |
| 480 | |
| 481 | @bigmemtest(minsize=_2G + 20, memuse=1) |
| 482 | def test_slice_and_getitem(self, size): |
| 483 | SUBSTR = '0123456789' |
| 484 | sublen = len(SUBSTR) |
| 485 | s = SUBSTR * (size // sublen) |
| 486 | stepsize = len(s) // 100 |
| 487 | stepsize = stepsize - (stepsize % sublen) |
| 488 | for i in range(0, len(s) - stepsize, stepsize): |
| 489 | self.assertEquals(s[i], SUBSTR[0]) |
| 490 | self.assertEquals(s[i:i + sublen], SUBSTR) |
| 491 | self.assertEquals(s[i:i + sublen:2], SUBSTR[::2]) |
| 492 | if i > 0: |
| 493 | self.assertEquals(s[i + sublen - 1:i - 1:-3], |
| 494 | SUBSTR[sublen::-3]) |
| 495 | # Make sure we do some slicing and indexing near the end of the |
| 496 | # string, too. |
| 497 | self.assertEquals(s[len(s) - 1], SUBSTR[-1]) |
| 498 | self.assertEquals(s[-1], SUBSTR[-1]) |
| 499 | self.assertEquals(s[len(s) - 10], SUBSTR[0]) |
| 500 | self.assertEquals(s[-sublen], SUBSTR[0]) |
| 501 | self.assertEquals(s[len(s):], '') |
| 502 | self.assertEquals(s[len(s) - 1:], SUBSTR[-1]) |
| 503 | self.assertEquals(s[-1:], SUBSTR[-1]) |
| 504 | self.assertEquals(s[len(s) - sublen:], SUBSTR) |
| 505 | self.assertEquals(s[-sublen:], SUBSTR) |
| 506 | self.assertEquals(len(s[:]), len(s)) |
| 507 | self.assertEquals(len(s[:len(s) - 5]), len(s) - 5) |
| 508 | self.assertEquals(len(s[5:-5]), len(s) - 10) |
| 509 | |
| 510 | self.assertRaises(IndexError, operator.getitem, s, len(s)) |
| 511 | self.assertRaises(IndexError, operator.getitem, s, len(s) + 1) |
| 512 | self.assertRaises(IndexError, operator.getitem, s, len(s) + 1<<31) |
| 513 | |
| 514 | @bigmemtest(minsize=_2G, memuse=2) |
| 515 | def test_contains(self, size): |
| 516 | SUBSTR = '0123456789' |
| 517 | edge = '-' * (size // 2) |
| 518 | s = ''.join([edge, SUBSTR, edge]) |
| 519 | del edge |
| 520 | self.failUnless(SUBSTR in s) |
| 521 | self.failIf(SUBSTR * 2 in s) |
| 522 | self.failUnless('-' in s) |
| 523 | self.failIf('a' in s) |
| 524 | s += 'a' |
| 525 | self.failUnless('a' in s) |
| 526 | |
| 527 | @bigmemtest(minsize=_2G + 10, memuse=2) |
| 528 | def test_compare(self, size): |
| 529 | s1 = '-' * size |
| 530 | s2 = '-' * size |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 531 | self.assertEqual(s1, s2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 532 | del s2 |
| 533 | s2 = s1 + 'a' |
| 534 | self.failIf(s1 == s2) |
| 535 | del s2 |
| 536 | s2 = '.' * size |
| 537 | self.failIf(s1 == s2) |
| 538 | |
| 539 | @bigmemtest(minsize=_2G + 10, memuse=1) |
| 540 | def test_hash(self, size): |
| 541 | # Not sure if we can do any meaningful tests here... Even if we |
| 542 | # start relying on the exact algorithm used, the result will be |
| 543 | # different depending on the size of the C 'long int'. Even this |
| 544 | # test is dodgy (there's no *guarantee* that the two things should |
| 545 | # have a different hash, even if they, in the current |
| 546 | # implementation, almost always do.) |
| 547 | s = '\x00' * size |
| 548 | h1 = hash(s) |
| 549 | del s |
| 550 | s = '\x00' * (size + 1) |
| 551 | self.failIf(h1 == hash(s)) |
| 552 | |
| 553 | class TupleTest(unittest.TestCase): |
| 554 | |
| 555 | # Tuples have a small, fixed-sized head and an array of pointers to |
| 556 | # data. Since we're testing 64-bit addressing, we can assume that the |
| 557 | # pointers are 8 bytes, and that thus that the tuples take up 8 bytes |
| 558 | # per size. |
| 559 | |
| 560 | # As a side-effect of testing long tuples, these tests happen to test |
| 561 | # having more than 2<<31 references to any given object. Hence the |
| 562 | # use of different types of objects as contents in different tests. |
| 563 | |
| 564 | @bigmemtest(minsize=_2G + 2, memuse=16) |
| 565 | def test_compare(self, size): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 566 | t1 = ('',) * size |
| 567 | t2 = ('',) * size |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 568 | self.assertEqual(t1, t2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 569 | del t2 |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 570 | t2 = ('',) * (size + 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 571 | self.failIf(t1 == t2) |
| 572 | del t2 |
| 573 | t2 = (1,) * size |
| 574 | self.failIf(t1 == t2) |
| 575 | |
| 576 | # Test concatenating into a single tuple of more than 2G in length, |
| 577 | # and concatenating a tuple of more than 2G in length separately, so |
| 578 | # the smaller test still gets run even if there isn't memory for the |
| 579 | # larger test (but we still let the tester know the larger test is |
| 580 | # skipped, in verbose mode.) |
| 581 | def basic_concat_test(self, size): |
| 582 | t = ((),) * size |
| 583 | self.assertEquals(len(t), size) |
| 584 | t = t + t |
| 585 | self.assertEquals(len(t), size * 2) |
| 586 | |
| 587 | @bigmemtest(minsize=_2G // 2 + 2, memuse=24) |
| 588 | def test_concat_small(self, size): |
| 589 | return self.basic_concat_test(size) |
| 590 | |
| 591 | @bigmemtest(minsize=_2G + 2, memuse=24) |
| 592 | def test_concat_large(self, size): |
| 593 | return self.basic_concat_test(size) |
| 594 | |
| 595 | @bigmemtest(minsize=_2G // 5 + 10, memuse=8 * 5) |
| 596 | def test_contains(self, size): |
| 597 | t = (1, 2, 3, 4, 5) * size |
| 598 | self.assertEquals(len(t), size * 5) |
| 599 | self.failUnless(5 in t) |
| 600 | self.failIf((1, 2, 3, 4, 5) in t) |
| 601 | self.failIf(0 in t) |
| 602 | |
| 603 | @bigmemtest(minsize=_2G + 10, memuse=8) |
| 604 | def test_hash(self, size): |
| 605 | t1 = (0,) * size |
| 606 | h1 = hash(t1) |
| 607 | del t1 |
| 608 | t2 = (0,) * (size + 1) |
| 609 | self.failIf(h1 == hash(t2)) |
| 610 | |
| 611 | @bigmemtest(minsize=_2G + 10, memuse=8) |
| 612 | def test_index_and_slice(self, size): |
| 613 | t = (None,) * size |
| 614 | self.assertEquals(len(t), size) |
| 615 | self.assertEquals(t[-1], None) |
| 616 | self.assertEquals(t[5], None) |
| 617 | self.assertEquals(t[size - 1], None) |
| 618 | self.assertRaises(IndexError, operator.getitem, t, size) |
| 619 | self.assertEquals(t[:5], (None,) * 5) |
| 620 | self.assertEquals(t[-5:], (None,) * 5) |
| 621 | self.assertEquals(t[20:25], (None,) * 5) |
| 622 | self.assertEquals(t[-25:-20], (None,) * 5) |
| 623 | self.assertEquals(t[size - 5:], (None,) * 5) |
| 624 | self.assertEquals(t[size - 5:size], (None,) * 5) |
| 625 | self.assertEquals(t[size - 6:size - 2], (None,) * 4) |
| 626 | self.assertEquals(t[size:size], ()) |
| 627 | self.assertEquals(t[size:size+5], ()) |
| 628 | |
| 629 | # Like test_concat, split in two. |
| 630 | def basic_test_repeat(self, size): |
| 631 | t = ('',) * size |
| 632 | self.assertEquals(len(t), size) |
| 633 | t = t * 2 |
| 634 | self.assertEquals(len(t), size * 2) |
| 635 | |
| 636 | @bigmemtest(minsize=_2G // 2 + 2, memuse=24) |
| 637 | def test_repeat_small(self, size): |
| 638 | return self.basic_test_repeat(size) |
| 639 | |
| 640 | @bigmemtest(minsize=_2G + 2, memuse=24) |
| 641 | def test_repeat_large(self, size): |
| 642 | return self.basic_test_repeat(size) |
| 643 | |
| 644 | # Like test_concat, split in two. |
| 645 | def basic_test_repr(self, size): |
| 646 | t = (0,) * size |
| 647 | s = repr(t) |
| 648 | # The repr of a tuple of 0's is exactly three times the tuple length. |
| 649 | self.assertEquals(len(s), size * 3) |
| 650 | self.assertEquals(s[:5], '(0, 0') |
| 651 | self.assertEquals(s[-5:], '0, 0)') |
| 652 | self.assertEquals(s.count('0'), size) |
| 653 | |
| 654 | @bigmemtest(minsize=_2G // 3 + 2, memuse=8 + 3) |
| 655 | def test_repr_small(self, size): |
| 656 | return self.basic_test_repr(size) |
| 657 | |
| 658 | @bigmemtest(minsize=_2G + 2, memuse=8 + 3) |
| 659 | def test_repr_large(self, size): |
| 660 | return self.basic_test_repr(size) |
| 661 | |
| 662 | class ListTest(unittest.TestCase): |
| 663 | |
| 664 | # Like tuples, lists have a small, fixed-sized head and an array of |
| 665 | # pointers to data, so 8 bytes per size. Also like tuples, we make the |
| 666 | # lists hold references to various objects to test their refcount |
| 667 | # limits. |
| 668 | |
| 669 | @bigmemtest(minsize=_2G + 2, memuse=16) |
| 670 | def test_compare(self, size): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 671 | l1 = [''] * size |
| 672 | l2 = [''] * size |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 673 | self.assertEqual(l1, l2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 674 | del l2 |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 675 | l2 = [''] * (size + 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 676 | self.failIf(l1 == l2) |
| 677 | del l2 |
| 678 | l2 = [2] * size |
| 679 | self.failIf(l1 == l2) |
| 680 | |
| 681 | # Test concatenating into a single list of more than 2G in length, |
| 682 | # and concatenating a list of more than 2G in length separately, so |
| 683 | # the smaller test still gets run even if there isn't memory for the |
| 684 | # larger test (but we still let the tester know the larger test is |
| 685 | # skipped, in verbose mode.) |
| 686 | def basic_test_concat(self, size): |
| 687 | l = [[]] * size |
| 688 | self.assertEquals(len(l), size) |
| 689 | l = l + l |
| 690 | self.assertEquals(len(l), size * 2) |
| 691 | |
| 692 | @bigmemtest(minsize=_2G // 2 + 2, memuse=24) |
| 693 | def test_concat_small(self, size): |
| 694 | return self.basic_test_concat(size) |
| 695 | |
| 696 | @bigmemtest(minsize=_2G + 2, memuse=24) |
| 697 | def test_concat_large(self, size): |
| 698 | return self.basic_test_concat(size) |
| 699 | |
| 700 | def basic_test_inplace_concat(self, size): |
| 701 | l = [sys.stdout] * size |
| 702 | l += l |
| 703 | self.assertEquals(len(l), size * 2) |
| 704 | self.failUnless(l[0] is l[-1]) |
| 705 | self.failUnless(l[size - 1] is l[size + 1]) |
| 706 | |
| 707 | @bigmemtest(minsize=_2G // 2 + 2, memuse=24) |
| 708 | def test_inplace_concat_small(self, size): |
| 709 | return self.basic_test_inplace_concat(size) |
| 710 | |
| 711 | @bigmemtest(minsize=_2G + 2, memuse=24) |
| 712 | def test_inplace_concat_large(self, size): |
| 713 | return self.basic_test_inplace_concat(size) |
| 714 | |
| 715 | @bigmemtest(minsize=_2G // 5 + 10, memuse=8 * 5) |
| 716 | def test_contains(self, size): |
| 717 | l = [1, 2, 3, 4, 5] * size |
| 718 | self.assertEquals(len(l), size * 5) |
| 719 | self.failUnless(5 in l) |
| 720 | self.failIf([1, 2, 3, 4, 5] in l) |
| 721 | self.failIf(0 in l) |
| 722 | |
| 723 | @bigmemtest(minsize=_2G + 10, memuse=8) |
| 724 | def test_hash(self, size): |
| 725 | l = [0] * size |
| 726 | self.failUnlessRaises(TypeError, hash, l) |
| 727 | |
| 728 | @bigmemtest(minsize=_2G + 10, memuse=8) |
| 729 | def test_index_and_slice(self, size): |
| 730 | l = [None] * size |
| 731 | self.assertEquals(len(l), size) |
| 732 | self.assertEquals(l[-1], None) |
| 733 | self.assertEquals(l[5], None) |
| 734 | self.assertEquals(l[size - 1], None) |
| 735 | self.assertRaises(IndexError, operator.getitem, l, size) |
| 736 | self.assertEquals(l[:5], [None] * 5) |
| 737 | self.assertEquals(l[-5:], [None] * 5) |
| 738 | self.assertEquals(l[20:25], [None] * 5) |
| 739 | self.assertEquals(l[-25:-20], [None] * 5) |
| 740 | self.assertEquals(l[size - 5:], [None] * 5) |
| 741 | self.assertEquals(l[size - 5:size], [None] * 5) |
| 742 | self.assertEquals(l[size - 6:size - 2], [None] * 4) |
| 743 | self.assertEquals(l[size:size], []) |
| 744 | self.assertEquals(l[size:size+5], []) |
| 745 | |
| 746 | l[size - 2] = 5 |
| 747 | self.assertEquals(len(l), size) |
| 748 | self.assertEquals(l[-3:], [None, 5, None]) |
| 749 | self.assertEquals(l.count(5), 1) |
| 750 | self.assertRaises(IndexError, operator.setitem, l, size, 6) |
| 751 | self.assertEquals(len(l), size) |
| 752 | |
| 753 | l[size - 7:] = [1, 2, 3, 4, 5] |
| 754 | size -= 2 |
| 755 | self.assertEquals(len(l), size) |
| 756 | self.assertEquals(l[-7:], [None, None, 1, 2, 3, 4, 5]) |
| 757 | |
| 758 | l[:7] = [1, 2, 3, 4, 5] |
| 759 | size -= 2 |
| 760 | self.assertEquals(len(l), size) |
| 761 | self.assertEquals(l[:7], [1, 2, 3, 4, 5, None, None]) |
| 762 | |
| 763 | del l[size - 1] |
| 764 | size -= 1 |
| 765 | self.assertEquals(len(l), size) |
| 766 | self.assertEquals(l[-1], 4) |
| 767 | |
| 768 | del l[-2:] |
| 769 | size -= 2 |
| 770 | self.assertEquals(len(l), size) |
| 771 | self.assertEquals(l[-1], 2) |
| 772 | |
| 773 | del l[0] |
| 774 | size -= 1 |
| 775 | self.assertEquals(len(l), size) |
| 776 | self.assertEquals(l[0], 2) |
| 777 | |
| 778 | del l[:2] |
| 779 | size -= 2 |
| 780 | self.assertEquals(len(l), size) |
| 781 | self.assertEquals(l[0], 4) |
| 782 | |
| 783 | # Like test_concat, split in two. |
| 784 | def basic_test_repeat(self, size): |
| 785 | l = [] * size |
| 786 | self.failIf(l) |
| 787 | l = [''] * size |
| 788 | self.assertEquals(len(l), size) |
| 789 | l = l * 2 |
| 790 | self.assertEquals(len(l), size * 2) |
| 791 | |
| 792 | @bigmemtest(minsize=_2G // 2 + 2, memuse=24) |
| 793 | def test_repeat_small(self, size): |
| 794 | return self.basic_test_repeat(size) |
| 795 | |
| 796 | @bigmemtest(minsize=_2G + 2, memuse=24) |
| 797 | def test_repeat_large(self, size): |
| 798 | return self.basic_test_repeat(size) |
| 799 | |
| 800 | def basic_test_inplace_repeat(self, size): |
| 801 | l = [''] |
| 802 | l *= size |
| 803 | self.assertEquals(len(l), size) |
| 804 | self.failUnless(l[0] is l[-1]) |
| 805 | del l |
| 806 | |
| 807 | l = [''] * size |
| 808 | l *= 2 |
| 809 | self.assertEquals(len(l), size * 2) |
| 810 | self.failUnless(l[size - 1] is l[-1]) |
| 811 | |
| 812 | @bigmemtest(minsize=_2G // 2 + 2, memuse=16) |
| 813 | def test_inplace_repeat_small(self, size): |
| 814 | return self.basic_test_inplace_repeat(size) |
| 815 | |
| 816 | @bigmemtest(minsize=_2G + 2, memuse=16) |
| 817 | def test_inplace_repeat_large(self, size): |
| 818 | return self.basic_test_inplace_repeat(size) |
| 819 | |
| 820 | def basic_test_repr(self, size): |
| 821 | l = [0] * size |
| 822 | s = repr(l) |
| 823 | # The repr of a list of 0's is exactly three times the list length. |
| 824 | self.assertEquals(len(s), size * 3) |
| 825 | self.assertEquals(s[:5], '[0, 0') |
| 826 | self.assertEquals(s[-5:], '0, 0]') |
| 827 | self.assertEquals(s.count('0'), size) |
| 828 | |
| 829 | @bigmemtest(minsize=_2G // 3 + 2, memuse=8 + 3) |
| 830 | def test_repr_small(self, size): |
| 831 | return self.basic_test_repr(size) |
| 832 | |
| 833 | @bigmemtest(minsize=_2G + 2, memuse=8 + 3) |
| 834 | def test_repr_large(self, size): |
| 835 | return self.basic_test_repr(size) |
| 836 | |
| 837 | # list overallocates ~1/8th of the total size (on first expansion) so |
| 838 | # the single list.append call puts memuse at 9 bytes per size. |
| 839 | @bigmemtest(minsize=_2G, memuse=9) |
| 840 | def test_append(self, size): |
| 841 | l = [object()] * size |
| 842 | l.append(object()) |
| 843 | self.assertEquals(len(l), size+1) |
| 844 | self.failUnless(l[-3] is l[-2]) |
| 845 | self.failIf(l[-2] is l[-1]) |
| 846 | |
| 847 | @bigmemtest(minsize=_2G // 5 + 2, memuse=8 * 5) |
| 848 | def test_count(self, size): |
| 849 | l = [1, 2, 3, 4, 5] * size |
| 850 | self.assertEquals(l.count(1), size) |
| 851 | self.assertEquals(l.count("1"), 0) |
| 852 | |
| 853 | def basic_test_extend(self, size): |
Guido van Rossum | a5d0c26 | 2007-07-12 08:11:23 +0000 | [diff] [blame] | 854 | l = [object] * size |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 855 | l.extend(l) |
| 856 | self.assertEquals(len(l), size * 2) |
| 857 | self.failUnless(l[0] is l[-1]) |
| 858 | self.failUnless(l[size - 1] is l[size + 1]) |
| 859 | |
| 860 | @bigmemtest(minsize=_2G // 2 + 2, memuse=16) |
| 861 | def test_extend_small(self, size): |
| 862 | return self.basic_test_extend(size) |
| 863 | |
| 864 | @bigmemtest(minsize=_2G + 2, memuse=16) |
| 865 | def test_extend_large(self, size): |
| 866 | return self.basic_test_extend(size) |
| 867 | |
| 868 | @bigmemtest(minsize=_2G // 5 + 2, memuse=8 * 5) |
| 869 | def test_index(self, size): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 870 | l = [1, 2, 3, 4, 5] * size |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 871 | size *= 5 |
| 872 | self.assertEquals(l.index(1), 0) |
| 873 | self.assertEquals(l.index(5, size - 5), size - 1) |
| 874 | self.assertEquals(l.index(5, size - 5, size), size - 1) |
| 875 | self.assertRaises(ValueError, l.index, 1, size - 4, size) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 876 | self.assertRaises(ValueError, l.index, 6) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 877 | |
| 878 | # This tests suffers from overallocation, just like test_append. |
| 879 | @bigmemtest(minsize=_2G + 10, memuse=9) |
| 880 | def test_insert(self, size): |
| 881 | l = [1.0] * size |
| 882 | l.insert(size - 1, "A") |
| 883 | size += 1 |
| 884 | self.assertEquals(len(l), size) |
| 885 | self.assertEquals(l[-3:], [1.0, "A", 1.0]) |
| 886 | |
| 887 | l.insert(size + 1, "B") |
| 888 | size += 1 |
| 889 | self.assertEquals(len(l), size) |
| 890 | self.assertEquals(l[-3:], ["A", 1.0, "B"]) |
| 891 | |
| 892 | l.insert(1, "C") |
| 893 | size += 1 |
| 894 | self.assertEquals(len(l), size) |
| 895 | self.assertEquals(l[:3], [1.0, "C", 1.0]) |
| 896 | self.assertEquals(l[size - 3:], ["A", 1.0, "B"]) |
| 897 | |
| 898 | @bigmemtest(minsize=_2G // 5 + 4, memuse=8 * 5) |
| 899 | def test_pop(self, size): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 900 | l = ["a", "b", "c", "d", "e"] * size |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 901 | size *= 5 |
| 902 | self.assertEquals(len(l), size) |
| 903 | |
| 904 | item = l.pop() |
| 905 | size -= 1 |
| 906 | self.assertEquals(len(l), size) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 907 | self.assertEquals(item, "e") |
| 908 | self.assertEquals(l[-2:], ["c", "d"]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 909 | |
| 910 | item = l.pop(0) |
| 911 | size -= 1 |
| 912 | self.assertEquals(len(l), size) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 913 | self.assertEquals(item, "a") |
| 914 | self.assertEquals(l[:2], ["b", "c"]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 915 | |
| 916 | item = l.pop(size - 2) |
| 917 | size -= 1 |
| 918 | self.assertEquals(len(l), size) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 919 | self.assertEquals(item, "c") |
| 920 | self.assertEquals(l[-2:], ["b", "d"]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 921 | |
| 922 | @bigmemtest(minsize=_2G + 10, memuse=8) |
| 923 | def test_remove(self, size): |
| 924 | l = [10] * size |
| 925 | self.assertEquals(len(l), size) |
| 926 | |
| 927 | l.remove(10) |
| 928 | size -= 1 |
| 929 | self.assertEquals(len(l), size) |
| 930 | |
| 931 | # Because of the earlier l.remove(), this append doesn't trigger |
| 932 | # a resize. |
| 933 | l.append(5) |
| 934 | size += 1 |
| 935 | self.assertEquals(len(l), size) |
| 936 | self.assertEquals(l[-2:], [10, 5]) |
| 937 | l.remove(5) |
| 938 | size -= 1 |
| 939 | self.assertEquals(len(l), size) |
| 940 | self.assertEquals(l[-2:], [10, 10]) |
| 941 | |
| 942 | @bigmemtest(minsize=_2G // 5 + 2, memuse=8 * 5) |
| 943 | def test_reverse(self, size): |
| 944 | l = [1, 2, 3, 4, 5] * size |
| 945 | l.reverse() |
| 946 | self.assertEquals(len(l), size * 5) |
| 947 | self.assertEquals(l[-5:], [5, 4, 3, 2, 1]) |
| 948 | self.assertEquals(l[:5], [5, 4, 3, 2, 1]) |
| 949 | |
| 950 | @bigmemtest(minsize=_2G // 5 + 2, memuse=8 * 5) |
| 951 | def test_sort(self, size): |
| 952 | l = [1, 2, 3, 4, 5] * size |
| 953 | l.sort() |
| 954 | self.assertEquals(len(l), size * 5) |
| 955 | self.assertEquals(l.count(1), size) |
| 956 | self.assertEquals(l[:10], [1] * 10) |
| 957 | self.assertEquals(l[-10:], [5] * 10) |
| 958 | |
| 959 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 960 | support.run_unittest(StrTest, TupleTest, ListTest) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 961 | |
| 962 | if __name__ == '__main__': |
| 963 | if len(sys.argv) > 1: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 964 | support.set_memlimit(sys.argv[1]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 965 | test_main() |