test_bigmem.py 45 KB

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