Debian Codegraph @

rpython/rtyper/test/test_rconstantdict.py

raw symbols SCIP
1 import py
2 from rpython.rlib.objectmodel import r_dict
3 from rpython.rtyper.test.tool import BaseRtypingTest
4
5 class TestRconstantdict(BaseRtypingTest):
6
7 def test_constant_int_dict(self):
8 d = {1: 2, 2: 3, 3: 4}
9 def func(i):
10 return d[i]
11 res = self.interpret(func, [3])
12 assert res == 4
13
14 def test_constantdict_contains(self):
15 d = {1: True, 4: True, 16: True}
16 def func(i):
17 return i in d
18 res = self.interpret(func, [15])
19 assert res is False
20 res = self.interpret(func, [4])
21 assert res is True
22
23 def test_constantdict_get(self):
24 d = {1: -11, 4: -44, 16: -66}
25 def func(i, j):
26 return d.get(i, j)
27 res = self.interpret(func, [15, 62])
28 assert res == 62
29 res = self.interpret(func, [4, 25])
30 assert res == -44
31
32 def test_unichar_dict(self):
33 d = {u'a': 5, u'b': 123, u'?': 321}
34 def func(i):
35 return d[unichr(i)]
36 res = self.interpret(func, [97])
37 assert res == 5
38 res = self.interpret(func, [98])
39 assert res == 123
40 res = self.interpret(func, [63])
41 assert res == 321
42
43 def test_constant_r_dict(self):
44 def strange_key_eq(key1, key2):
45 return key1[0] == key2[0] # only the 1st character is relevant
46 def strange_key_hash(key):
47 return ord(key[0])
48
49 d = r_dict(strange_key_eq, strange_key_hash)
50 d['hello'] = 42
51 d['world'] = 43
52 for x in range(65, 91):
53 d[chr(x)] = x*x
54 def func(i):
55 return d[chr(i)]
56 res = self.interpret(func, [ord('h')])
57 assert res == 42
58 res = self.interpret(func, [ord('w')])
59 assert res == 43
60 for x in range(65, 91):
61 res = self.interpret(func, [x])
62 assert res == x*x