| 1 |
import sys |
| 2 |
from contextlib import contextmanager |
| 3 |
import signal |
| 4 |
from collections import OrderedDict |
| 5 |
|
| 6 |
from rpython.translator.translator import TranslationContext |
| 7 |
from rpython.annotator.model import ( |
| 8 |
SomeInteger, SomeString, SomeChar, SomeUnicodeString, SomeUnicodeCodePoint) |
| 9 |
from rpython.annotator.dictdef import DictKey, DictValue |
| 10 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 11 |
from rpython.rtyper.lltypesystem import rdict |
| 12 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 13 |
from rpython.rlib.objectmodel import r_dict |
| 14 |
from rpython.rlib.rarithmetic import r_int, r_uint, r_longlong, r_ulonglong |
| 15 |
|
| 16 |
import py |
| 17 |
from hypothesis import settings, assume |
| 18 |
from hypothesis.strategies import ( |
| 19 |
builds, sampled_from, binary, just, integers, text, characters, tuples, |
| 20 |
lists, sets, composite) |
| 21 |
from hypothesis.stateful import GenericStateMachine, run_state_machine_as_test |
| 22 |
|
| 23 |
def ann2strategy(s_value): |
| 24 |
if isinstance(s_value, SomeChar): |
| 25 |
return builds(chr, integers(min_value=0, max_value=255)) |
| 26 |
elif isinstance(s_value, SomeString): |
| 27 |
if s_value.can_be_None: |
| 28 |
return binary() | just(None) |
| 29 |
else: |
| 30 |
return binary() |
| 31 |
elif isinstance(s_value, SomeUnicodeCodePoint): |
| 32 |
return characters() |
| 33 |
elif isinstance(s_value, SomeUnicodeString): |
| 34 |
if s_value.can_be_None: |
| 35 |
return text() | just(None) |
| 36 |
else: |
| 37 |
return text() |
| 38 |
elif isinstance(s_value, SomeInteger): |
| 39 |
return integers(min_value=~sys.maxint, max_value=sys.maxint) |
| 40 |
else: |
| 41 |
raise TypeError("Cannot convert annotation %s to a strategy" % s_value) |
| 42 |
|
| 43 |
|
| 44 |
if hasattr(signal, 'alarm'): |
| 45 |
@contextmanager |
| 46 |
def signal_timeout(n): |
| 47 |
"""A flaky context manager that throws an exception if the body of the |
| 48 |
`with` block runs for longer than `n` seconds. |
| 49 |
""" |
| 50 |
def handler(signum, frame): |
| 51 |
raise RuntimeError('timeout') |
| 52 |
signal.signal(signal.SIGALRM, handler) |
| 53 |
signal.alarm(n) |
| 54 |
try: |
| 55 |
yield |
| 56 |
finally: |
| 57 |
signal.alarm(0) |
| 58 |
else: |
| 59 |
@contextmanager |
| 60 |
def signal_timeout(n): |
| 61 |
yield |
| 62 |
|
| 63 |
|
| 64 |
class BaseTestRDict(BaseRtypingTest): |
| 65 |
def test_dict_creation(self): |
| 66 |
def createdict(i): |
| 67 |
d = self.newdict() |
| 68 |
d['hello'] = i |
| 69 |
return d['hello'] |
| 70 |
|
| 71 |
res = self.interpret(createdict, [42]) |
| 72 |
assert res == 42 |
| 73 |
|
| 74 |
def test_dict_getitem_setitem(self): |
| 75 |
def func(i): |
| 76 |
d = self.newdict() |
| 77 |
d['hello'] = i |
| 78 |
d['world'] = i + 1 |
| 79 |
return d['hello'] * d['world'] |
| 80 |
res = self.interpret(func, [6]) |
| 81 |
assert res == 42 |
| 82 |
|
| 83 |
def test_dict_getitem_keyerror(self): |
| 84 |
def func(i): |
| 85 |
d = self.newdict() |
| 86 |
d['hello'] = i |
| 87 |
try: |
| 88 |
return d['world'] |
| 89 |
except KeyError: |
| 90 |
return 0 |
| 91 |
res = self.interpret(func, [6]) |
| 92 |
assert res == 0 |
| 93 |
|
| 94 |
def test_dict_del_simple(self): |
| 95 |
def func(i): |
| 96 |
d = self.newdict() |
| 97 |
d['hello'] = i |
| 98 |
d['world'] = i + 1 |
| 99 |
del d['hello'] |
| 100 |
return len(d) |
| 101 |
res = self.interpret(func, [6]) |
| 102 |
assert res == 1 |
| 103 |
|
| 104 |
def test_dict_clear(self): |
| 105 |
def func(i): |
| 106 |
d = self.newdict() |
| 107 |
d['abc'] = i |
| 108 |
d['def'] = i+1 |
| 109 |
d.clear() |
| 110 |
d['ghi'] = i+2 |
| 111 |
return ('abc' not in d and 'def' not in d |
| 112 |
and d['ghi'] == i+2 and len(d) == 1) |
| 113 |
res = self.interpret(func, [7]) |
| 114 |
assert res == True |
| 115 |
|
| 116 |
def test_empty_strings(self): |
| 117 |
def func(i): |
| 118 |
d = self.newdict() |
| 119 |
d[''] = i |
| 120 |
del d[''] |
| 121 |
try: |
| 122 |
d[''] |
| 123 |
return 0 |
| 124 |
except KeyError: |
| 125 |
pass |
| 126 |
return 1 |
| 127 |
res = self.interpret(func, [6]) |
| 128 |
assert res == 1 |
| 129 |
|
| 130 |
def func(i): |
| 131 |
d = self.newdict() |
| 132 |
d[''] = i |
| 133 |
del d[''] |
| 134 |
d[''] = i + 1 |
| 135 |
return len(d) |
| 136 |
res = self.interpret(func, [6]) |
| 137 |
assert res == 1 |
| 138 |
|
| 139 |
def test_dict_bool(self): |
| 140 |
def func(i): |
| 141 |
if i: |
| 142 |
d = self.newdict() |
| 143 |
else: |
| 144 |
d = self.newdict() |
| 145 |
d[i] = i+1 |
| 146 |
if d: |
| 147 |
return i |
| 148 |
else: |
| 149 |
return i+1 |
| 150 |
assert self.interpret(func, [42]) == 43 |
| 151 |
assert self.interpret(func, [0]) == 0 |
| 152 |
|
| 153 |
def test_contains(self): |
| 154 |
def func(x, y): |
| 155 |
d = self.newdict() |
| 156 |
d[x] = x+1 |
| 157 |
return y in d |
| 158 |
assert self.interpret(func, [42, 0]) == False |
| 159 |
assert self.interpret(func, [42, 42]) == True |
| 160 |
|
| 161 |
def test_contains_2(self): |
| 162 |
d = self.newdict() |
| 163 |
d['5'] = None |
| 164 |
d['7'] = None |
| 165 |
def func(x): |
| 166 |
return chr(x) in d |
| 167 |
assert self.interpret(func, [ord('5')]) == True |
| 168 |
assert self.interpret(func, [ord('6')]) == False |
| 169 |
|
| 170 |
def func(n): |
| 171 |
return str(n) in d |
| 172 |
assert self.interpret(func, [512]) == False |
| 173 |
|
| 174 |
def test_dict_iteration(self): |
| 175 |
def func(i, j): |
| 176 |
d = self.newdict() |
| 177 |
d['hello'] = i |
| 178 |
d['world'] = j |
| 179 |
k = 1 |
| 180 |
for key in d: |
| 181 |
k = k * d[key] |
| 182 |
return k |
| 183 |
res = self.interpret(func, [6, 7]) |
| 184 |
assert res == 42 |
| 185 |
|
| 186 |
def test_dict_itermethods(self): |
| 187 |
def func(): |
| 188 |
d = self.newdict() |
| 189 |
d['hello'] = 6 |
| 190 |
d['world'] = 7 |
| 191 |
k1 = k2 = k3 = 1 |
| 192 |
for key in d.iterkeys(): |
| 193 |
k1 = k1 * d[key] |
| 194 |
for value in d.itervalues(): |
| 195 |
k2 = k2 * value |
| 196 |
for key, value in d.iteritems(): |
| 197 |
assert d[key] == value |
| 198 |
k3 = k3 * value |
| 199 |
return k1 + k2 + k3 |
| 200 |
res = self.interpret(func, []) |
| 201 |
assert res == 42 + 42 + 42 |
| 202 |
|
| 203 |
def test_dict_get(self): |
| 204 |
def func(): |
| 205 |
dic = self.newdict() |
| 206 |
x1 = dic.get('hi', 42) |
| 207 |
dic['blah'] = 1 |
| 208 |
x2 = dic.get('blah', 2) |
| 209 |
return x1 * 10 + x2 |
| 210 |
res = self.interpret(func, ()) |
| 211 |
assert res == 421 |
| 212 |
|
| 213 |
def test_dict_get_no_second_arg(self): |
| 214 |
def func(): |
| 215 |
dic = self.newdict() |
| 216 |
x1 = dic.get('hi', 'a') |
| 217 |
x2 = dic.get('blah') |
| 218 |
return (x1 == 'a') * 10 + (x2 is None) |
| 219 |
return x1 * 10 + x2 |
| 220 |
res = self.interpret(func, ()) |
| 221 |
assert res == 11 |
| 222 |
|
| 223 |
def test_dict_get_empty(self): |
| 224 |
def func(): |
| 225 |
|
| 226 |
dic = self.newdict() |
| 227 |
x1 = dic.get('hi', 42) |
| 228 |
x2 = dic.get('blah', 2) |
| 229 |
return x1 * 10 + x2 |
| 230 |
res = self.interpret(func, ()) |
| 231 |
assert res == 422 |
| 232 |
|
| 233 |
def test_dict_setdefault(self): |
| 234 |
def f(): |
| 235 |
d = self.newdict() |
| 236 |
d.setdefault('a', 2) |
| 237 |
return d['a'] |
| 238 |
res = self.interpret(f, ()) |
| 239 |
assert res == 2 |
| 240 |
|
| 241 |
def f(): |
| 242 |
d = self.newdict() |
| 243 |
d.setdefault('a', 2) |
| 244 |
x = d.setdefault('a', -3) |
| 245 |
return x |
| 246 |
res = self.interpret(f, ()) |
| 247 |
assert res == 2 |
| 248 |
|
| 249 |
def test_dict_copy(self): |
| 250 |
def func(): |
| 251 |
dic = self.newdict() |
| 252 |
dic['a'] = 1 |
| 253 |
dic['b'] = 2 |
| 254 |
d2 = dic.copy() |
| 255 |
ok = 1 |
| 256 |
for key in d2: |
| 257 |
if dic[key] != d2[key]: |
| 258 |
ok = 0 |
| 259 |
ok &= len(dic) == len(d2) |
| 260 |
d2['c'] = 3 |
| 261 |
ok &= len(dic) == len(d2) - 1 |
| 262 |
return ok |
| 263 |
res = self.interpret(func, ()) |
| 264 |
assert res == 1 |
| 265 |
|
| 266 |
def test_dict_update(self): |
| 267 |
def func(): |
| 268 |
dic = self.newdict() |
| 269 |
dic['ab'] = 1000 |
| 270 |
dic['b'] = 200 |
| 271 |
d2 = self.newdict() |
| 272 |
d2['b'] = 30 |
| 273 |
d2['cb'] = 4 |
| 274 |
dic.update(d2) |
| 275 |
ok = len(dic) == 3 |
| 276 |
sum = ok |
| 277 |
for key in dic: |
| 278 |
sum += dic[key] |
| 279 |
return sum |
| 280 |
res = self.interpret(func, ()) |
| 281 |
assert res == 1035 |
| 282 |
|
| 283 |
def test_dict_keys(self): |
| 284 |
def func(): |
| 285 |
dic = self.newdict() |
| 286 |
dic[' 4'] = 1000 |
| 287 |
dic[' 8'] = 200 |
| 288 |
keys = dic.keys() |
| 289 |
return ord(keys[0][1]) + ord(keys[1][1]) - 2*ord('0') + len(keys) |
| 290 |
res = self.interpret(func, ()) |
| 291 |
assert res == 14 |
| 292 |
|
| 293 |
def test_list_dict(self): |
| 294 |
def func(): |
| 295 |
dic = self.newdict() |
| 296 |
dic[' 4'] = 1000 |
| 297 |
dic[' 8'] = 200 |
| 298 |
keys = list(dic) |
| 299 |
return ord(keys[0][1]) + ord(keys[1][1]) - 2*ord('0') + len(keys) |
| 300 |
res = self.interpret(func, ()) |
| 301 |
assert res == 14 |
| 302 |
|
| 303 |
def test_dict_inst_keys(self): |
| 304 |
class Empty: |
| 305 |
pass |
| 306 |
class A(Empty): |
| 307 |
pass |
| 308 |
def func(): |
| 309 |
dic0 = self.newdict() |
| 310 |
dic0[Empty()] = 2 |
| 311 |
dic = self.newdict() |
| 312 |
dic[A()] = 1 |
| 313 |
dic[A()] = 2 |
| 314 |
keys = dic.keys() |
| 315 |
return (isinstance(keys[1], A))*2+(isinstance(keys[0],A)) |
| 316 |
res = self.interpret(func, []) |
| 317 |
assert res == 3 |
| 318 |
|
| 319 |
def test_dict_inst_iterkeys(self): |
| 320 |
class Empty: |
| 321 |
pass |
| 322 |
class A(Empty): |
| 323 |
pass |
| 324 |
def func(): |
| 325 |
dic0 = self.newdict() |
| 326 |
dic0[Empty()] = 2 |
| 327 |
dic = self.newdict() |
| 328 |
dic[A()] = 1 |
| 329 |
dic[A()] = 2 |
| 330 |
a = 0 |
| 331 |
for k in dic.iterkeys(): |
| 332 |
a += isinstance(k, A) |
| 333 |
return a |
| 334 |
res = self.interpret(func, []) |
| 335 |
assert res == 2 |
| 336 |
|
| 337 |
def test_dict_values(self): |
| 338 |
def func(): |
| 339 |
dic = self.newdict() |
| 340 |
dic[' 4'] = 1000 |
| 341 |
dic[' 8'] = 200 |
| 342 |
values = dic.values() |
| 343 |
return values[0] + values[1] + len(values) |
| 344 |
res = self.interpret(func, ()) |
| 345 |
assert res == 1202 |
| 346 |
|
| 347 |
def test_dict_inst_values(self): |
| 348 |
class A: |
| 349 |
pass |
| 350 |
def func(): |
| 351 |
dic = self.newdict() |
| 352 |
dic[1] = A() |
| 353 |
dic[2] = A() |
| 354 |
vals = dic.values() |
| 355 |
return (isinstance(vals[1], A))*2+(isinstance(vals[0],A)) |
| 356 |
res = self.interpret(func, []) |
| 357 |
assert res == 3 |
| 358 |
|
| 359 |
def test_dict_inst_itervalues(self): |
| 360 |
class A: |
| 361 |
pass |
| 362 |
def func(): |
| 363 |
dic = self.newdict() |
| 364 |
dic[1] = A() |
| 365 |
dic[2] = A() |
| 366 |
a = 0 |
| 367 |
for v in dic.itervalues(): |
| 368 |
a += isinstance(v, A) |
| 369 |
return a |
| 370 |
res = self.interpret(func, []) |
| 371 |
assert res == 2 |
| 372 |
|
| 373 |
def test_dict_inst_items(self): |
| 374 |
class Empty: |
| 375 |
pass |
| 376 |
class A: |
| 377 |
pass |
| 378 |
class B(Empty): |
| 379 |
pass |
| 380 |
def func(): |
| 381 |
dic0 = self.newdict() |
| 382 |
dic0[Empty()] = A() |
| 383 |
dic = self.newdict() |
| 384 |
dic[B()] = A() |
| 385 |
dic[B()] = A() |
| 386 |
items = dic.items() |
| 387 |
b = 0 |
| 388 |
a = 0 |
| 389 |
for k, v in items: |
| 390 |
b += isinstance(k, B) |
| 391 |
a += isinstance(v, A) |
| 392 |
return 3*b+a |
| 393 |
res = self.interpret(func, []) |
| 394 |
assert res == 8 |
| 395 |
|
| 396 |
def test_dict_inst_iteritems(self): |
| 397 |
class Empty: |
| 398 |
pass |
| 399 |
class A: |
| 400 |
pass |
| 401 |
class B(Empty): |
| 402 |
pass |
| 403 |
def func(): |
| 404 |
dic0 = self.newdict() |
| 405 |
dic0[Empty()] = A() |
| 406 |
dic = self.newdict() |
| 407 |
dic[B()] = A() |
| 408 |
dic[B()] = A() |
| 409 |
b = 0 |
| 410 |
a = 0 |
| 411 |
for k, v in dic.iteritems(): |
| 412 |
b += isinstance(k, B) |
| 413 |
a += isinstance(v, A) |
| 414 |
return 3*b+a |
| 415 |
res = self.interpret(func, []) |
| 416 |
assert res == 8 |
| 417 |
|
| 418 |
def test_dict_items(self): |
| 419 |
def func(): |
| 420 |
dic = self.newdict() |
| 421 |
dic[' 4'] = 1000 |
| 422 |
dic[' 8'] = 200 |
| 423 |
items = dic.items() |
| 424 |
res = len(items) |
| 425 |
for key, value in items: |
| 426 |
res += ord(key[1]) - ord('0') + value |
| 427 |
return res |
| 428 |
res = self.interpret(func, ()) |
| 429 |
assert res == 1214 |
| 430 |
|
| 431 |
def test_dict_contains(self): |
| 432 |
def func(): |
| 433 |
dic = self.newdict() |
| 434 |
dic[' 4'] = 1000 |
| 435 |
dic[' 8'] = 200 |
| 436 |
return ' 4' in dic and ' 9' not in dic |
| 437 |
res = self.interpret(func, ()) |
| 438 |
assert res is True |
| 439 |
|
| 440 |
def test_dict_contains_with_constant_dict(self): |
| 441 |
dic = self.newdict() |
| 442 |
dic['4'] = 1000 |
| 443 |
dic['8'] = 200 |
| 444 |
def func(i): |
| 445 |
return chr(i) in dic |
| 446 |
res = self.interpret(func, [ord('4')]) |
| 447 |
assert res is True |
| 448 |
res = self.interpret(func, [1]) |
| 449 |
assert res is False |
| 450 |
|
| 451 |
def test_dict_or_none(self): |
| 452 |
class A: |
| 453 |
pass |
| 454 |
def negate(d): |
| 455 |
return not d |
| 456 |
def func(n): |
| 457 |
a = A() |
| 458 |
a.d = None |
| 459 |
if n > 0: |
| 460 |
a.d = self.newdict() |
| 461 |
a.d[str(n)] = 1 |
| 462 |
a.d["42"] = 2 |
| 463 |
del a.d["42"] |
| 464 |
return negate(a.d) |
| 465 |
res = self.interpret(func, [10]) |
| 466 |
assert res is False |
| 467 |
res = self.interpret(func, [0]) |
| 468 |
assert res is True |
| 469 |
res = self.interpret(func, [42]) |
| 470 |
assert res is True |
| 471 |
|
| 472 |
def test_int_dict(self): |
| 473 |
def func(a, b): |
| 474 |
dic = self.newdict() |
| 475 |
dic[12] = 34 |
| 476 |
dic[a] = 1000 |
| 477 |
return dic.get(b, -123) |
| 478 |
res = self.interpret(func, [12, 12]) |
| 479 |
assert res == 1000 |
| 480 |
res = self.interpret(func, [12, 13]) |
| 481 |
assert res == -123 |
| 482 |
res = self.interpret(func, [524, 12]) |
| 483 |
assert res == 34 |
| 484 |
res = self.interpret(func, [524, 524]) |
| 485 |
assert res == 1000 |
| 486 |
res = self.interpret(func, [524, 1036]) |
| 487 |
assert res == -123 |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
def test_id_instances_keys(self): |
| 492 |
class A: |
| 493 |
pass |
| 494 |
class B(A): |
| 495 |
pass |
| 496 |
def f(): |
| 497 |
a = A() |
| 498 |
b = B() |
| 499 |
d = self.newdict() |
| 500 |
d[b] = 7 |
| 501 |
d[a] = 3 |
| 502 |
return len(d) + d[a] + d[b] |
| 503 |
res = self.interpret(f, []) |
| 504 |
assert res == 12 |
| 505 |
|
| 506 |
def test_captured_get(self): |
| 507 |
d = self.newdict() |
| 508 |
d[1] = 2 |
| 509 |
get = d.get |
| 510 |
def f(): |
| 511 |
return get(1, 3)+get(2, 4) |
| 512 |
res = self.interpret(f, []) |
| 513 |
assert res == 6 |
| 514 |
|
| 515 |
def g(h): |
| 516 |
return h(1, 3) |
| 517 |
def f(): |
| 518 |
return g(get) |
| 519 |
|
| 520 |
res = self.interpret(f, []) |
| 521 |
assert res == 2 |
| 522 |
|
| 523 |
def test_specific_obscure_bug(self): |
| 524 |
class A: pass |
| 525 |
class B: pass |
| 526 |
def f(): |
| 527 |
lst = [A()] |
| 528 |
res1 = A() in lst |
| 529 |
d2 = self.newdict() |
| 530 |
d2[B()] = None |
| 531 |
d2[B()] = None |
| 532 |
return res1+len(d2) |
| 533 |
res = self.interpret(f, []) |
| 534 |
assert res == 2 |
| 535 |
|
| 536 |
def test_identity_hash_is_fast(self): |
| 537 |
class A(object): |
| 538 |
pass |
| 539 |
|
| 540 |
def f(): |
| 541 |
d = self.newdict() |
| 542 |
d[A()] = 1 |
| 543 |
return d |
| 544 |
|
| 545 |
t = TranslationContext() |
| 546 |
s = t.buildannotator().build_types(f, []) |
| 547 |
rtyper = t.buildrtyper() |
| 548 |
rtyper.specialize() |
| 549 |
|
| 550 |
r_dict = rtyper.getrepr(s) |
| 551 |
assert not hasattr(r_dict.lowleveltype.TO.entries.TO.OF, "f_hash") |
| 552 |
|
| 553 |
def test_r_dict_can_be_fast(self): |
| 554 |
def myeq(n, m): |
| 555 |
return n == m |
| 556 |
def myhash(n): |
| 557 |
return ~n |
| 558 |
def f(): |
| 559 |
d = self.new_r_dict(myeq, myhash, simple_hash_eq=True) |
| 560 |
d[5] = 7 |
| 561 |
d[12] = 19 |
| 562 |
return d |
| 563 |
|
| 564 |
t = TranslationContext() |
| 565 |
s = t.buildannotator().build_types(f, []) |
| 566 |
rtyper = t.buildrtyper() |
| 567 |
rtyper.specialize() |
| 568 |
|
| 569 |
r_dict = rtyper.getrepr(s) |
| 570 |
assert not hasattr(r_dict.lowleveltype.TO.entries.TO.OF, "f_hash") |
| 571 |
|
| 572 |
def test_tuple_dict(self): |
| 573 |
def f(i): |
| 574 |
d = self.newdict() |
| 575 |
d[(1, 4.5, (str(i), 2), 2)] = 4 |
| 576 |
d[(1, 4.5, (str(i), 2), 3)] = 6 |
| 577 |
return d[(1, 4.5, (str(i), 2), i)] |
| 578 |
|
| 579 |
res = self.interpret(f, [2]) |
| 580 |
assert res == f(2) |
| 581 |
|
| 582 |
def test_dict_of_dict(self): |
| 583 |
def f(n): |
| 584 |
d = self.newdict() |
| 585 |
d[5] = d |
| 586 |
d[6] = self.newdict() |
| 587 |
return len(d[n]) |
| 588 |
|
| 589 |
res = self.interpret(f, [5]) |
| 590 |
assert res == 2 |
| 591 |
res = self.interpret(f, [6]) |
| 592 |
assert res == 0 |
| 593 |
|
| 594 |
def test_cls_dict(self): |
| 595 |
class A(object): |
| 596 |
pass |
| 597 |
|
| 598 |
class B(A): |
| 599 |
pass |
| 600 |
|
| 601 |
def f(i): |
| 602 |
d = self.newdict() |
| 603 |
d[A] = 3 |
| 604 |
d[B] = 4 |
| 605 |
if i: |
| 606 |
cls = A |
| 607 |
else: |
| 608 |
cls = B |
| 609 |
return d[cls] |
| 610 |
|
| 611 |
res = self.interpret(f, [1]) |
| 612 |
assert res == 3 |
| 613 |
res = self.interpret(f, [0]) |
| 614 |
assert res == 4 |
| 615 |
|
| 616 |
def test_prebuilt_cls_dict(self): |
| 617 |
class A(object): |
| 618 |
pass |
| 619 |
|
| 620 |
class B(A): |
| 621 |
pass |
| 622 |
|
| 623 |
d = self.newdict() |
| 624 |
d[(A, 3)] = 3 |
| 625 |
d[(B, 0)] = 4 |
| 626 |
|
| 627 |
def f(i): |
| 628 |
if i: |
| 629 |
cls = A |
| 630 |
else: |
| 631 |
cls = B |
| 632 |
try: |
| 633 |
return d[cls, i] |
| 634 |
except KeyError: |
| 635 |
return -99 |
| 636 |
|
| 637 |
res = self.interpret(f, [0]) |
| 638 |
assert res == 4 |
| 639 |
res = self.interpret(f, [3]) |
| 640 |
assert res == 3 |
| 641 |
res = self.interpret(f, [10]) |
| 642 |
assert res == -99 |
| 643 |
|
| 644 |
def test_access_in_try(self): |
| 645 |
def f(d): |
| 646 |
try: |
| 647 |
return d[2] |
| 648 |
except ZeroDivisionError: |
| 649 |
return 42 |
| 650 |
return -1 |
| 651 |
def g(n): |
| 652 |
d = self.newdict() |
| 653 |
d[1] = n |
| 654 |
d[2] = 2*n |
| 655 |
return f(d) |
| 656 |
res = self.interpret(g, [3]) |
| 657 |
assert res == 6 |
| 658 |
|
| 659 |
def test_access_in_try_set(self): |
| 660 |
def f(d): |
| 661 |
try: |
| 662 |
d[2] = 77 |
| 663 |
except ZeroDivisionError: |
| 664 |
return 42 |
| 665 |
return -1 |
| 666 |
def g(n): |
| 667 |
d = self.newdict() |
| 668 |
d[1] = n |
| 669 |
f(d) |
| 670 |
return d[2] |
| 671 |
res = self.interpret(g, [3]) |
| 672 |
assert res == 77 |
| 673 |
|
| 674 |
def test_resize_during_iteration(self): |
| 675 |
def func(): |
| 676 |
d = self.newdict() |
| 677 |
d[5] = 1 |
| 678 |
d[6] = 2 |
| 679 |
d[7] = 3 |
| 680 |
try: |
| 681 |
for key, value in d.iteritems(): |
| 682 |
d[key^16] = value*2 |
| 683 |
except RuntimeError: |
| 684 |
pass |
| 685 |
total = 0 |
| 686 |
for key in d: |
| 687 |
total += key |
| 688 |
return total |
| 689 |
res = self.interpret(func, []) |
| 690 |
assert 5 + 6 + 7 <= res <= 5 + 6 + 7 + (5^16) + (6^16) + (7^16) |
| 691 |
|
| 692 |
def test_change_during_iteration(self): |
| 693 |
def func(): |
| 694 |
d = self.newdict() |
| 695 |
d['a'] = 1 |
| 696 |
d['b'] = 2 |
| 697 |
for key in d: |
| 698 |
d[key] = 42 |
| 699 |
return d['a'] |
| 700 |
assert self.interpret(func, []) == 42 |
| 701 |
|
| 702 |
def test_dict_of_floats(self): |
| 703 |
d = self.newdict() |
| 704 |
d[3.0] = 42 |
| 705 |
d[3.1] = 43 |
| 706 |
d[3.2] = 44 |
| 707 |
d[3.3] = 45 |
| 708 |
d[3.4] = 46 |
| 709 |
def fn(f): |
| 710 |
return d[f] |
| 711 |
|
| 712 |
res = self.interpret(fn, [3.0]) |
| 713 |
assert res == 42 |
| 714 |
|
| 715 |
def test_dict_of_r_uint(self): |
| 716 |
for r_t in [r_uint, r_longlong, r_ulonglong]: |
| 717 |
if r_t is r_int: |
| 718 |
continue |
| 719 |
d = self.newdict() |
| 720 |
d[r_t(2)] = 3 |
| 721 |
d[r_t(4)] = 5 |
| 722 |
def fn(x, y): |
| 723 |
d[r_t(x)] = 123 |
| 724 |
return d[r_t(y)] |
| 725 |
res = self.interpret(fn, [4, 2]) |
| 726 |
assert res == 3 |
| 727 |
res = self.interpret(fn, [3, 3]) |
| 728 |
assert res == 123 |
| 729 |
|
| 730 |
def test_dict_popitem(self): |
| 731 |
def func(): |
| 732 |
d = self.newdict() |
| 733 |
d[5] = 2 |
| 734 |
d[6] = 3 |
| 735 |
k1, v1 = d.popitem() |
| 736 |
assert len(d) == 1 |
| 737 |
k2, v2 = d.popitem() |
| 738 |
try: |
| 739 |
d.popitem() |
| 740 |
except KeyError: |
| 741 |
pass |
| 742 |
else: |
| 743 |
assert 0, "should have raised KeyError" |
| 744 |
assert len(d) == 0 |
| 745 |
return k1*1000 + v1*100 + k2*10 + v2 |
| 746 |
|
| 747 |
res = self.interpret(func, []) |
| 748 |
assert res in [5263, 6352] |
| 749 |
|
| 750 |
def test_dict_pop(self): |
| 751 |
def f(n, default): |
| 752 |
d = self.newdict() |
| 753 |
d[2] = 3 |
| 754 |
d[4] = 5 |
| 755 |
if default == -1: |
| 756 |
try: |
| 757 |
x = d.pop(n) |
| 758 |
except KeyError: |
| 759 |
x = -1 |
| 760 |
else: |
| 761 |
x = d.pop(n, default) |
| 762 |
return x * 10 + len(d) |
| 763 |
res = self.interpret(f, [2, -1]) |
| 764 |
assert res == 31 |
| 765 |
res = self.interpret(f, [3, -1]) |
| 766 |
assert res == -8 |
| 767 |
res = self.interpret(f, [2, 5]) |
| 768 |
assert res == 31 |
| 769 |
|
| 770 |
def test_dict_pop_instance(self): |
| 771 |
class A(object): |
| 772 |
pass |
| 773 |
def f(n): |
| 774 |
d = self.newdict() |
| 775 |
d[2] = A() |
| 776 |
x = d.pop(n, None) |
| 777 |
if x is None: |
| 778 |
return 12 |
| 779 |
else: |
| 780 |
return 15 |
| 781 |
res = self.interpret(f, [2]) |
| 782 |
assert res == 15 |
| 783 |
res = self.interpret(f, [700]) |
| 784 |
assert res == 12 |
| 785 |
|
| 786 |
def test_dict_but_not_with_char_keys(self): |
| 787 |
def func(i): |
| 788 |
d = self.newdict() |
| 789 |
d['h'] = i |
| 790 |
try: |
| 791 |
return d['hello'] |
| 792 |
except KeyError: |
| 793 |
return 0 |
| 794 |
res = self.interpret(func, [6]) |
| 795 |
assert res == 0 |
| 796 |
|
| 797 |
def test_dict_valid_resize(self): |
| 798 |
|
| 799 |
def func(): |
| 800 |
d = self.newdict() |
| 801 |
|
| 802 |
for i in range(10): |
| 803 |
d[str(i)] = 0 |
| 804 |
|
| 805 |
for i in range(10): |
| 806 |
del d[str(i)] |
| 807 |
res = 0 |
| 808 |
|
| 809 |
self.interpret(func, []) |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
def test_dict_of_addresses(self): |
| 814 |
from rpython.rtyper.lltypesystem import llmemory |
| 815 |
TP = lltype.Struct('x') |
| 816 |
a = lltype.malloc(TP, flavor='raw', immortal=True) |
| 817 |
b = lltype.malloc(TP, flavor='raw', immortal=True) |
| 818 |
|
| 819 |
def func(i): |
| 820 |
d = self.newdict() |
| 821 |
d[llmemory.cast_ptr_to_adr(a)] = 123 |
| 822 |
d[llmemory.cast_ptr_to_adr(b)] = 456 |
| 823 |
if i > 5: |
| 824 |
key = llmemory.cast_ptr_to_adr(a) |
| 825 |
else: |
| 826 |
key = llmemory.cast_ptr_to_adr(b) |
| 827 |
return d[key] |
| 828 |
|
| 829 |
assert self.interpret(func, [3]) == 456 |
| 830 |
|
| 831 |
def test_prebuilt_list_of_addresses(self): |
| 832 |
from rpython.rtyper.lltypesystem import llmemory |
| 833 |
|
| 834 |
TP = lltype.Struct('x', ('y', lltype.Signed)) |
| 835 |
a = lltype.malloc(TP, flavor='raw', immortal=True) |
| 836 |
b = lltype.malloc(TP, flavor='raw', immortal=True) |
| 837 |
c = lltype.malloc(TP, flavor='raw', immortal=True) |
| 838 |
a_a = llmemory.cast_ptr_to_adr(a) |
| 839 |
a0 = llmemory.cast_ptr_to_adr(a) |
| 840 |
assert a_a is not a0 |
| 841 |
assert a_a == a0 |
| 842 |
a_b = llmemory.cast_ptr_to_adr(b) |
| 843 |
a_c = llmemory.cast_ptr_to_adr(c) |
| 844 |
|
| 845 |
d = self.newdict() |
| 846 |
d[a_a] = 3 |
| 847 |
d[a_b] = 4 |
| 848 |
d[a_c] = 5 |
| 849 |
d[a0] = 8 |
| 850 |
|
| 851 |
def func(i): |
| 852 |
if i == 0: |
| 853 |
ptr = a |
| 854 |
else: |
| 855 |
ptr = b |
| 856 |
return d[llmemory.cast_ptr_to_adr(ptr)] |
| 857 |
|
| 858 |
py.test.raises(TypeError, self.interpret, func, [0]) |
| 859 |
|
| 860 |
def test_dict_of_voidp(self): |
| 861 |
def func(): |
| 862 |
d = self.newdict() |
| 863 |
handle = lltype.nullptr(rffi.VOIDP.TO) |
| 864 |
|
| 865 |
|
| 866 |
d[-1] = handle |
| 867 |
return len(d) |
| 868 |
|
| 869 |
assert self.interpret(func, []) == 1 |
| 870 |
from rpython.translator.c.test.test_genc import compile |
| 871 |
f = compile(func, []) |
| 872 |
res = f() |
| 873 |
assert res == 1 |
| 874 |
|
| 875 |
def test_dict_with_SHORT_keys(self): |
| 876 |
def func(x): |
| 877 |
d = self.newdict() |
| 878 |
d[rffi.cast(rffi.SHORT, 42)] = 123 |
| 879 |
d[rffi.cast(rffi.SHORT, -43)] = 321 |
| 880 |
return d[rffi.cast(rffi.SHORT, x)] |
| 881 |
|
| 882 |
assert self.interpret(func, [42]) == 123 |
| 883 |
assert self.interpret(func, [2**16 - 43]) == 321 |
| 884 |
|
| 885 |
def test_dict_with_bool_keys(self): |
| 886 |
def func(x): |
| 887 |
d = self.newdict() |
| 888 |
d[False] = 123 |
| 889 |
d[True] = 321 |
| 890 |
return d[x == 42] |
| 891 |
|
| 892 |
assert self.interpret(func, [5]) == 123 |
| 893 |
assert self.interpret(func, [42]) == 321 |
| 894 |
|
| 895 |
def test_memoryerror_should_not_insert(self): |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
def _check_small_range(self, n): |
| 904 |
if n >= 128: |
| 905 |
raise MemoryError |
| 906 |
return range(n) |
| 907 |
original_check_range = lltype._array._check_range |
| 908 |
try: |
| 909 |
lltype._array._check_range = _check_small_range |
| 910 |
|
| 911 |
def do_insert(d, i): |
| 912 |
d[i] = i |
| 913 |
def func(): |
| 914 |
d = self.newdict() |
| 915 |
i = 0 |
| 916 |
while True: |
| 917 |
try: |
| 918 |
do_insert(d, i) |
| 919 |
except MemoryError: |
| 920 |
return (i in d) |
| 921 |
i += 1 |
| 922 |
res = self.interpret(func, []) |
| 923 |
assert res == 0 |
| 924 |
|
| 925 |
finally: |
| 926 |
lltype._array._check_range = original_check_range |
| 927 |
|
| 928 |
def test_dict_with_none_key(self): |
| 929 |
def func(i): |
| 930 |
d = self.newdict() |
| 931 |
d[None] = i |
| 932 |
return d[None] |
| 933 |
res = self.interpret(func, [42]) |
| 934 |
assert res == 42 |
| 935 |
|
| 936 |
def test_externalvsinternal(self): |
| 937 |
class A: pass |
| 938 |
class B: pass |
| 939 |
class C: pass |
| 940 |
class D: pass |
| 941 |
def func(): |
| 942 |
d1 = self.newdict(); d1[A()] = B() |
| 943 |
d2 = self.newdict2(); d2[C()] = D() |
| 944 |
return (d1, d2) |
| 945 |
res = self.interpret(func, []) |
| 946 |
assert lltype.typeOf(res.item0) == lltype.typeOf(res.item1) |
| 947 |
|
| 948 |
def test_r_dict(self): |
| 949 |
class FooError(Exception): |
| 950 |
pass |
| 951 |
def myeq(n, m): |
| 952 |
return n == m |
| 953 |
def myhash(n): |
| 954 |
if n < 0: |
| 955 |
raise FooError |
| 956 |
return -n |
| 957 |
def f(n): |
| 958 |
d = self.new_r_dict(myeq, myhash) |
| 959 |
for i in range(10): |
| 960 |
d[i] = i*i |
| 961 |
try: |
| 962 |
value1 = d[n] |
| 963 |
except FooError: |
| 964 |
value1 = 99 |
| 965 |
try: |
| 966 |
value2 = n in d |
| 967 |
except FooError: |
| 968 |
value2 = 99 |
| 969 |
try: |
| 970 |
value3 = d[-n] |
| 971 |
except FooError: |
| 972 |
value3 = 99 |
| 973 |
try: |
| 974 |
value4 = (-n) in d |
| 975 |
except FooError: |
| 976 |
value4 = 99 |
| 977 |
return (value1 * 1000000 + |
| 978 |
value2 * 10000 + |
| 979 |
value3 * 100 + |
| 980 |
value4) |
| 981 |
res = self.interpret(f, [5]) |
| 982 |
assert res == 25019999 |
| 983 |
|
| 984 |
def test_r_dict_popitem_hash(self): |
| 985 |
def deq(n, m): |
| 986 |
return n == m |
| 987 |
def dhash(n): |
| 988 |
return ~n |
| 989 |
def func(): |
| 990 |
d = self.new_r_dict(deq, dhash) |
| 991 |
d[5] = 2 |
| 992 |
d[6] = 3 |
| 993 |
k1, v1 = d.popitem() |
| 994 |
assert len(d) == 1 |
| 995 |
k2, v2 = d.popitem() |
| 996 |
try: |
| 997 |
d.popitem() |
| 998 |
except KeyError: |
| 999 |
pass |
| 1000 |
else: |
| 1001 |
assert 0, "should have raised KeyError" |
| 1002 |
assert len(d) == 0 |
| 1003 |
return k1*1000 + v1*100 + k2*10 + v2 |
| 1004 |
|
| 1005 |
res = self.interpret(func, []) |
| 1006 |
assert res in [5263, 6352] |
| 1007 |
|
| 1008 |
def test_prebuilt_r_dict(self): |
| 1009 |
def deq(n, m): |
| 1010 |
return (n & 3) == (m & 3) |
| 1011 |
def dhash(n): |
| 1012 |
return n & 3 |
| 1013 |
d = self.new_r_dict(deq, dhash) |
| 1014 |
d[0x123] = "abcd" |
| 1015 |
d[0x231] = "efgh" |
| 1016 |
def func(): |
| 1017 |
return d[0x348973] + d[0x12981] |
| 1018 |
|
| 1019 |
res = self.interpret(func, []) |
| 1020 |
res = self.ll_to_string(res) |
| 1021 |
assert res == "abcdefgh" |
| 1022 |
|
| 1023 |
|
| 1024 |
class TestRDict(BaseTestRDict): |
| 1025 |
@staticmethod |
| 1026 |
def newdict(): |
| 1027 |
return {} |
| 1028 |
|
| 1029 |
@staticmethod |
| 1030 |
def newdict2(): |
| 1031 |
return {} |
| 1032 |
|
| 1033 |
@staticmethod |
| 1034 |
def new_r_dict(myeq, myhash, force_non_null=False, simple_hash_eq=False): |
| 1035 |
return r_dict(myeq, myhash, force_non_null=force_non_null, simple_hash_eq=simple_hash_eq) |
| 1036 |
|
| 1037 |
def test_two_dicts_with_different_value_types(self): |
| 1038 |
def func(i): |
| 1039 |
d1 = {} |
| 1040 |
d1['hello'] = i + 1 |
| 1041 |
d2 = {} |
| 1042 |
d2['world'] = d1 |
| 1043 |
return d2['world']['hello'] |
| 1044 |
res = self.interpret(func, [5]) |
| 1045 |
assert res == 6 |
| 1046 |
|
| 1047 |
def test_type_erase(self): |
| 1048 |
class A(object): |
| 1049 |
pass |
| 1050 |
class B(object): |
| 1051 |
pass |
| 1052 |
|
| 1053 |
def f(): |
| 1054 |
d = {} |
| 1055 |
d[A()] = B() |
| 1056 |
d2 = {} |
| 1057 |
d2[B()] = A() |
| 1058 |
return d, d2 |
| 1059 |
|
| 1060 |
t = TranslationContext() |
| 1061 |
s = t.buildannotator().build_types(f, []) |
| 1062 |
rtyper = t.buildrtyper() |
| 1063 |
rtyper.specialize() |
| 1064 |
|
| 1065 |
s_AB_dic = s.items[0] |
| 1066 |
s_BA_dic = s.items[1] |
| 1067 |
|
| 1068 |
r_AB_dic = rtyper.getrepr(s_AB_dic) |
| 1069 |
r_BA_dic = rtyper.getrepr(s_BA_dic) |
| 1070 |
|
| 1071 |
assert r_AB_dic.lowleveltype == r_BA_dic.lowleveltype |
| 1072 |
|
| 1073 |
def test_type_erase_gcref(self): |
| 1074 |
class A(object): |
| 1075 |
pass |
| 1076 |
class B(object): |
| 1077 |
pass |
| 1078 |
|
| 1079 |
def f(): |
| 1080 |
d = {} |
| 1081 |
d[A()] = B() |
| 1082 |
d2 = {} |
| 1083 |
d2[B()] = "abc" |
| 1084 |
return d, d2 |
| 1085 |
|
| 1086 |
t = TranslationContext() |
| 1087 |
t.config.translation.gc = 'incminimark' |
| 1088 |
s = t.buildannotator().build_types(f, []) |
| 1089 |
rtyper = t.buildrtyper() |
| 1090 |
rtyper.specialize() |
| 1091 |
|
| 1092 |
s_AB_dic = s.items[0] |
| 1093 |
s_BA_dic = s.items[1] |
| 1094 |
|
| 1095 |
r_AB_dic = rtyper.getrepr(s_AB_dic) |
| 1096 |
r_BA_dic = rtyper.getrepr(s_BA_dic) |
| 1097 |
|
| 1098 |
assert r_AB_dic.lowleveltype == r_BA_dic.lowleveltype |
| 1099 |
|
| 1100 |
def test_opt_dummykeymarker(self): |
| 1101 |
def f(): |
| 1102 |
d = {"hello": None} |
| 1103 |
d["world"] = None |
| 1104 |
return "hello" in d, d |
| 1105 |
res = self.interpret(f, []) |
| 1106 |
assert res.item0 == True |
| 1107 |
DICT = lltype.typeOf(res.item1).TO |
| 1108 |
assert not hasattr(DICT.entries.TO.OF, 'f_valid') |
| 1109 |
|
| 1110 |
def test_opt_dummyvaluemarker(self): |
| 1111 |
def f(n): |
| 1112 |
d = {-5: "abcd"} |
| 1113 |
d[123] = "def" |
| 1114 |
return len(d[n]), d |
| 1115 |
res = self.interpret(f, [-5]) |
| 1116 |
assert res.item0 == 4 |
| 1117 |
DICT = lltype.typeOf(res.item1).TO |
| 1118 |
assert not hasattr(DICT.entries.TO.OF, 'f_valid') |
| 1119 |
|
| 1120 |
def test_opt_nonnegint_dummy(self): |
| 1121 |
def f(n): |
| 1122 |
d = {n: 12} |
| 1123 |
d[-87] = 24 |
| 1124 |
del d[n] |
| 1125 |
return len(d.copy()), d[-87], d |
| 1126 |
res = self.interpret(f, [5]) |
| 1127 |
assert res.item0 == 1 |
| 1128 |
assert res.item1 == 24 |
| 1129 |
DICT = lltype.typeOf(res.item2).TO |
| 1130 |
assert not hasattr(DICT.entries.TO.OF, 'f_valid') |
| 1131 |
|
| 1132 |
def test_opt_no_dummy(self): |
| 1133 |
def f(n): |
| 1134 |
d = {n: 12} |
| 1135 |
d[-87] = -24 |
| 1136 |
del d[n] |
| 1137 |
return len(d.copy()), d[-87], d |
| 1138 |
res = self.interpret(f, [5]) |
| 1139 |
assert res.item0 == 1 |
| 1140 |
assert res.item1 == -24 |
| 1141 |
DICT = lltype.typeOf(res.item2).TO |
| 1142 |
assert hasattr(DICT.entries.TO.OF, 'f_valid') |
| 1143 |
|
| 1144 |
def test_opt_boolean_has_no_dummy(self): |
| 1145 |
def f(n): |
| 1146 |
d = {n: True} |
| 1147 |
d[-87] = True |
| 1148 |
del d[n] |
| 1149 |
return len(d.copy()), d[-87], d |
| 1150 |
res = self.interpret(f, [5]) |
| 1151 |
assert res.item0 == 1 |
| 1152 |
assert res.item1 is True |
| 1153 |
DICT = lltype.typeOf(res.item2).TO |
| 1154 |
assert hasattr(DICT.entries.TO.OF, 'f_valid') |
| 1155 |
|
| 1156 |
def test_opt_multiple_identical_dicts(self): |
| 1157 |
def f(n): |
| 1158 |
s = "x" * n |
| 1159 |
d1 = {s: 12} |
| 1160 |
d2 = {s: 24} |
| 1161 |
d3 = {s: 36} |
| 1162 |
d1["a"] = d2[s] |
| 1163 |
d3[s] += d1["a"] |
| 1164 |
d2["bc"] = d3[s] |
| 1165 |
return d2["bc"], d1, d2, d3 |
| 1166 |
res = self.interpret(f, [5]) |
| 1167 |
assert res.item0 == 60 |
| 1168 |
|
| 1169 |
assert lltype.typeOf(res.item1) == lltype.typeOf(res.item2) |
| 1170 |
assert lltype.typeOf(res.item1) == lltype.typeOf(res.item3) |
| 1171 |
|
| 1172 |
def test_nonnull_hint(self): |
| 1173 |
def eq(a, b): |
| 1174 |
return a == b |
| 1175 |
def rhash(a): |
| 1176 |
return 3 |
| 1177 |
|
| 1178 |
def func(i): |
| 1179 |
d = r_dict(eq, rhash, force_non_null=True) |
| 1180 |
if not i: |
| 1181 |
d[None] = i |
| 1182 |
else: |
| 1183 |
d[str(i)] = i |
| 1184 |
return "12" in d, d |
| 1185 |
|
| 1186 |
llres = self.interpret(func, [12]) |
| 1187 |
assert llres.item0 == 1 |
| 1188 |
DICT = lltype.typeOf(llres.item1) |
| 1189 |
assert sorted(DICT.TO.entries.TO.OF._flds) == ['f_hash', 'key', 'value'] |
| 1190 |
|
| 1191 |
|
| 1192 |
class Action(object): |
| 1193 |
def __init__(self, method, args): |
| 1194 |
self.method = method |
| 1195 |
self.args = args |
| 1196 |
|
| 1197 |
def execute(self, space): |
| 1198 |
getattr(space, self.method)(*self.args) |
| 1199 |
|
| 1200 |
def __repr__(self): |
| 1201 |
return "space.%s(%s)" % (self.method, ', '.join(map(repr, self.args))) |
| 1202 |
|
| 1203 |
class PseudoRTyper: |
| 1204 |
cache_dummy_values = {} |
| 1205 |
class annotator: |
| 1206 |
class translator: |
| 1207 |
class config: |
| 1208 |
class translation: |
| 1209 |
gc = 'ref' |
| 1210 |
|
| 1211 |
|
| 1212 |
keytypes_s = [ |
| 1213 |
SomeString(), SomeInteger(), SomeChar(), |
| 1214 |
SomeUnicodeString(), SomeUnicodeCodePoint()] |
| 1215 |
st_keys = sampled_from(keytypes_s) |
| 1216 |
st_values = sampled_from(keytypes_s + [SomeString(can_be_None=True)]) |
| 1217 |
|
| 1218 |
class MappingSpace(object): |
| 1219 |
def __init__(self, s_key, s_value): |
| 1220 |
from rpython.rtyper.rtuple import TupleRepr |
| 1221 |
|
| 1222 |
self.s_key = s_key |
| 1223 |
self.s_value = s_value |
| 1224 |
rtyper = PseudoRTyper() |
| 1225 |
r_key = s_key.rtyper_makerepr(rtyper) |
| 1226 |
r_value = s_value.rtyper_makerepr(rtyper) |
| 1227 |
dictrepr = self.MappingRepr(rtyper, r_key, r_value, |
| 1228 |
DictKey(None, s_key), |
| 1229 |
DictValue(None, s_value)) |
| 1230 |
dictrepr.setup() |
| 1231 |
self.l_dict = self.newdict(dictrepr) |
| 1232 |
self.reference = OrderedDict() |
| 1233 |
self.ll_key = r_key.convert_const |
| 1234 |
self.ll_value = r_value.convert_const |
| 1235 |
self.removed_keys = [] |
| 1236 |
r_tuple = TupleRepr(rtyper, [r_key, r_value]) |
| 1237 |
self.TUPLE = r_tuple.lowleveltype |
| 1238 |
|
| 1239 |
def setitem(self, key, value): |
| 1240 |
ll_key = self.ll_key(key) |
| 1241 |
ll_value = self.ll_value(value) |
| 1242 |
self.ll_setitem(self.l_dict, ll_key, ll_value) |
| 1243 |
self.reference[key] = value |
| 1244 |
assert self.ll_contains(self.l_dict, ll_key) |
| 1245 |
|
| 1246 |
def delitem(self, key): |
| 1247 |
ll_key = self.ll_key(key) |
| 1248 |
self.ll_delitem(self.l_dict, ll_key) |
| 1249 |
del self.reference[key] |
| 1250 |
assert not self.ll_contains(self.l_dict, ll_key) |
| 1251 |
self.removed_keys.append(key) |
| 1252 |
|
| 1253 |
def move_to_end(self, key, last=True): |
| 1254 |
"For test_rordereddict" |
| 1255 |
|
| 1256 |
def move_to_first(self, key): |
| 1257 |
self.move_to_end(key, last=False) |
| 1258 |
|
| 1259 |
def many_move_to_first(self, keys): |
| 1260 |
for key in keys: |
| 1261 |
self.move_to_first(key) |
| 1262 |
|
| 1263 |
def many_move_to_end(self, keys): |
| 1264 |
for key in keys: |
| 1265 |
self.move_to_end(key) |
| 1266 |
|
| 1267 |
def copydict(self): |
| 1268 |
self.l_dict = self.ll_copy(self.l_dict) |
| 1269 |
assert self.ll_len(self.l_dict) == len(self.reference) |
| 1270 |
|
| 1271 |
def cleardict(self): |
| 1272 |
self.ll_clear(self.l_dict) |
| 1273 |
for key in self.reference: |
| 1274 |
self.removed_keys.append(key) |
| 1275 |
self.reference.clear() |
| 1276 |
assert self.ll_len(self.l_dict) == 0 |
| 1277 |
|
| 1278 |
def popitem(self): |
| 1279 |
try: |
| 1280 |
ll_tuple = self.ll_popitem(self.TUPLE, self.l_dict) |
| 1281 |
except KeyError: |
| 1282 |
assert len(self.reference) == 0 |
| 1283 |
else: |
| 1284 |
ll_key = ll_tuple.item0 |
| 1285 |
ll_value = ll_tuple.item1 |
| 1286 |
for key, value in self.reference.iteritems(): |
| 1287 |
if self.ll_key(key) == ll_key: |
| 1288 |
assert self.ll_value(value) == ll_value |
| 1289 |
del self.reference[key] |
| 1290 |
self.removed_keys.append(key) |
| 1291 |
break |
| 1292 |
else: |
| 1293 |
raise AssertionError("popitem() returned unexpected key") |
| 1294 |
|
| 1295 |
def removeindex(self): |
| 1296 |
pass |
| 1297 |
|
| 1298 |
def fullcheck(self): |
| 1299 |
assert self.ll_len(self.l_dict) == len(self.reference) |
| 1300 |
for key, value in self.reference.iteritems(): |
| 1301 |
assert (self.ll_getitem(self.l_dict, self.ll_key(key)) == |
| 1302 |
self.ll_value(value)) |
| 1303 |
for key in self.removed_keys: |
| 1304 |
if key not in self.reference: |
| 1305 |
try: |
| 1306 |
self.ll_getitem(self.l_dict, self.ll_key(key)) |
| 1307 |
except KeyError: |
| 1308 |
pass |
| 1309 |
else: |
| 1310 |
raise AssertionError("removed key still shows up") |
| 1311 |
|
| 1312 |
def init_many(self, l): |
| 1313 |
for key, value in l: |
| 1314 |
self.setitem(key, value) |
| 1315 |
|
| 1316 |
@composite |
| 1317 |
def st_many_elements(draw, st_keys, st_values): |
| 1318 |
keys = draw(sets(st_keys, min_size=50)) |
| 1319 |
res = [(key, draw(st_values)) for key in keys] |
| 1320 |
return (res, ) |
| 1321 |
|
| 1322 |
@composite |
| 1323 |
def st_many_moves(draw, reference): |
| 1324 |
assume(len(reference) > 3) |
| 1325 |
number = draw(integers(3, len(reference) - 1)) |
| 1326 |
remaining = set(reference) |
| 1327 |
keys = [] |
| 1328 |
for i in range(number): |
| 1329 |
key = draw(sampled_from(list(remaining))) |
| 1330 |
keys.append(key) |
| 1331 |
remaining.remove(key) |
| 1332 |
print("st_many_elements", len(keys), len(reference)) |
| 1333 |
return (keys, ) |
| 1334 |
|
| 1335 |
class MappingSM(GenericStateMachine): |
| 1336 |
def __init__(self): |
| 1337 |
self.space = None |
| 1338 |
|
| 1339 |
def st_setitem(self): |
| 1340 |
return builds(Action, |
| 1341 |
just('setitem'), tuples(self.st_keys, self.st_values)) |
| 1342 |
|
| 1343 |
def st_init_many(self): |
| 1344 |
return builds(Action, |
| 1345 |
just('init_many'), st_many_elements(self.st_keys, self.st_values)) |
| 1346 |
|
| 1347 |
def st_updateitem(self): |
| 1348 |
return builds(Action, |
| 1349 |
just('setitem'), |
| 1350 |
tuples(sampled_from(self.space.reference), self.st_values)) |
| 1351 |
|
| 1352 |
def st_delitem(self): |
| 1353 |
return builds(Action, |
| 1354 |
just('delitem'), tuples(sampled_from(self.space.reference))) |
| 1355 |
|
| 1356 |
def st_move_to_end(self): |
| 1357 |
return builds(Action, |
| 1358 |
just('move_to_end'), tuples(sampled_from(self.space.reference))) |
| 1359 |
|
| 1360 |
def st_move_to_first(self): |
| 1361 |
return builds(Action, |
| 1362 |
just('move_to_first'), |
| 1363 |
tuples(sampled_from(self.space.reference))) |
| 1364 |
|
| 1365 |
def st_many_move_to_end(self): |
| 1366 |
return builds(Action, |
| 1367 |
just('many_move_to_end'), st_many_moves(self.space.reference)) |
| 1368 |
|
| 1369 |
def st_many_move_to_first(self): |
| 1370 |
return builds(Action, |
| 1371 |
just('many_move_to_first'), st_many_moves(self.space.reference)) |
| 1372 |
|
| 1373 |
def steps(self): |
| 1374 |
if not self.space: |
| 1375 |
return builds(Action, just('setup'), tuples(st_keys, st_values)) |
| 1376 |
global_actions = [Action('copydict', ()), Action('cleardict', ()), |
| 1377 |
Action('popitem', ()), Action('removeindex', ())] |
| 1378 |
if self.space.reference: |
| 1379 |
res = ( |
| 1380 |
self.st_setitem() | sampled_from(global_actions) | |
| 1381 |
self.st_updateitem() | self.st_delitem()) |
| 1382 |
if self.HAVE_MOVE_TO_FIRST_END: |
| 1383 |
res |= (self.st_move_to_end() | self.st_move_to_first() | |
| 1384 |
self.st_many_move_to_end() | self.st_many_move_to_first()) |
| 1385 |
return res |
| 1386 |
else: |
| 1387 |
return (self.st_setitem() | sampled_from(global_actions) | self.st_init_many()) |
| 1388 |
|
| 1389 |
def execute_step(self, action): |
| 1390 |
if action.method == 'setup': |
| 1391 |
self.space = self.Space(*action.args) |
| 1392 |
self.st_keys = ann2strategy(self.space.s_key) |
| 1393 |
self.st_values = ann2strategy(self.space.s_value) |
| 1394 |
return |
| 1395 |
with signal_timeout(10): |
| 1396 |
action.execute(self.space) |
| 1397 |
|
| 1398 |
def teardown(self): |
| 1399 |
if self.space: |
| 1400 |
self.space.fullcheck() |
| 1401 |
|
| 1402 |
|
| 1403 |
class DictSpace(MappingSpace): |
| 1404 |
MappingRepr = rdict.DictRepr |
| 1405 |
ll_getitem = staticmethod(rdict.ll_dict_getitem) |
| 1406 |
ll_setitem = staticmethod(rdict.ll_dict_setitem) |
| 1407 |
ll_delitem = staticmethod(rdict.ll_dict_delitem) |
| 1408 |
ll_len = staticmethod(rdict.ll_dict_len) |
| 1409 |
ll_contains = staticmethod(rdict.ll_contains) |
| 1410 |
ll_copy = staticmethod(rdict.ll_copy) |
| 1411 |
ll_clear = staticmethod(rdict.ll_clear) |
| 1412 |
ll_popitem = staticmethod(rdict.ll_popitem) |
| 1413 |
|
| 1414 |
def newdict(self, repr): |
| 1415 |
return rdict.ll_newdict(repr.DICT) |
| 1416 |
|
| 1417 |
class DictSM(MappingSM): |
| 1418 |
HAVE_MOVE_TO_FIRST_END = False |
| 1419 |
|
| 1420 |
Space = DictSpace |
| 1421 |
|
| 1422 |
def test_hypothesis(): |
| 1423 |
run_state_machine_as_test( |
| 1424 |
DictSM, settings(max_examples=50, stateful_step_count=10)) |