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