File: test_mapping.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (45 lines) | stat: -rw-r--r-- 1,711 bytes parent folder | download | duplicates (4)
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
from pypy.module.cpyext.test.test_api import BaseApiTest
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from rpython.rtyper.lltypesystem import lltype, rffi
from pypy.module.cpyext.pyobject import get_w_obj_and_decref


class TestMapping(BaseApiTest):
    def test_check(self, space, api):
        assert api.PyMapping_Check(space.newdict())
        assert not api.PyMapping_Check(space.newlist([]))
        assert not api.PyMapping_Check(space.newtuple([]))

    def test_size(self, space, api):
        w_d = space.newdict()
        space.setitem(w_d, space.wrap("a"), space.wrap("b"))

        assert api.PyMapping_Size(w_d) == 1
        assert api.PyMapping_Length(w_d) == 1

    def test_keys(self, space, api):
        w_d = space.newdict()
        space.setitem(w_d, space.wrap("a"), space.wrap("b"))

        assert space.eq_w(api.PyMapping_Keys(w_d), space.wrap(["a"]))
        assert space.eq_w(api.PyMapping_Values(w_d), space.wrap(["b"]))
        assert space.eq_w(api.PyMapping_Items(w_d), space.wrap([("a", "b")]))

    def test_setitemstring(self, space, api):
        w_d = space.newdict()
        key = rffi.str2charp("key")
        api.PyMapping_SetItemString(w_d, key, space.wrap(42))
        assert 42 == space.unwrap(get_w_obj_and_decref(space,
            api.PyMapping_GetItemString(w_d, key)))
        rffi.free_charp(key)

    def test_haskey(self, space, api):
        w_d = space.newdict()
        space.setitem(w_d, space.wrap("a"), space.wrap("b"))

        assert api.PyMapping_HasKey(w_d, space.wrap("a"))
        assert not api.PyMapping_HasKey(w_d, space.wrap("b"))

        assert api.PyMapping_HasKey(w_d, w_d) == 0
        # and no error is set