File: test_rconstantdict.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (62 lines) | stat: -rw-r--r-- 1,879 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import py
from rpython.rlib.objectmodel import r_dict
from rpython.rtyper.test.tool import BaseRtypingTest

class TestRconstantdict(BaseRtypingTest):

    def test_constant_int_dict(self):
        d = {1: 2, 2: 3, 3: 4}
        def func(i):
            return d[i]
        res = self.interpret(func, [3])
        assert res == 4

    def test_constantdict_contains(self):
        d = {1: True, 4: True, 16: True}
        def func(i):
            return i in d
        res = self.interpret(func, [15])
        assert res is False
        res = self.interpret(func, [4])
        assert res is True

    def test_constantdict_get(self):
        d = {1: -11, 4: -44, 16: -66}
        def func(i, j):
            return d.get(i, j)
        res = self.interpret(func, [15, 62])
        assert res == 62
        res = self.interpret(func, [4, 25])
        assert res == -44

    def test_unichar_dict(self):
        d = {u'a': 5, u'b': 123, u'?': 321}
        def func(i):
            return d[unichr(i)]
        res = self.interpret(func, [97])
        assert res == 5
        res = self.interpret(func, [98])
        assert res == 123
        res = self.interpret(func, [63])
        assert res == 321

    def test_constant_r_dict(self):
        def strange_key_eq(key1, key2):
            return key1[0] == key2[0]   # only the 1st character is relevant
        def strange_key_hash(key):
            return ord(key[0])

        d = r_dict(strange_key_eq, strange_key_hash)
        d['hello'] = 42
        d['world'] = 43
        for x in range(65, 91):
            d[chr(x)] = x*x
        def func(i):
            return d[chr(i)]
        res = self.interpret(func, [ord('h')])
        assert res == 42
        res = self.interpret(func, [ord('w')])
        assert res == 43
        for x in range(65, 91):
            res = self.interpret(func, [x])
            assert res == x*x