File: test_tracker.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 (120 lines) | stat: -rw-r--r-- 4,131 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
NOTE: this tests are also meant to be run as PyPy "applevel" tests.

This means that global imports will NOT be visible inside the test
functions. In particular, you have to "import pytest" inside the test in order
to be able to use e.g. pytest.raises (which on PyPy will be implemented by a
"fake pytest module")
"""
from .support import HPyTest


class TestHPyTracker(HPyTest):
    def hpytracker_module(self, ops, size=0):
        return self.make_module("""
            HPyDef_METH(f, "f", HPyFunc_VARARGS)
            static HPy f_impl(HPyContext *ctx, HPy self,
                              const HPy *args, size_t nargs)
            {{
                HPyTracker ht;
                HPy result = HPy_NULL;
                ht = HPyTracker_New(ctx, {size});
                if HPy_IsNull(ht) {{
                    return HPy_NULL;
                }}
                {ops}
                HPyTracker_Close(ctx, ht);
                if (HPy_IsNull(result))
                    result = HPy_Dup(ctx, ctx->h_None);
                return result;
            }}
            @EXPORT(f)
            @INIT
        """.format(ops=ops, size=size))

    def test_new_and_free(self):
        mod = self.hpytracker_module(ops="")
        mod.f()

    def test_new_with_size_and_free(self):
        mod = self.hpytracker_module(ops="", size=10)
        mod.f()

    def test_add_and_free(self):
        mod = self.hpytracker_module(ops="""
            HPyTracker_Add(ctx, ht, HPy_Dup(ctx, args[0]));
        """)
        mod.f(5)

    def test_add_and_remove_all(self):
        mod = self.hpytracker_module(ops="""
            HPyTracker_Add(ctx, ht, args[0]);
            HPyTracker_ForgetAll(ctx, ht);
        """)
        assert mod.f(5) is None

    def test_remove_all_on_nothing(self):
        mod = self.hpytracker_module(ops="""
            HPyTracker_ForgetAll(ctx, ht);
        """)
        assert mod.f() is None

    def test_squares_example(self):
        import pytest
        mod = self.make_module("""
            HPyDef_METH(squares, "squares", HPyFunc_VARARGS)
            static HPy squares_impl(HPyContext *ctx, HPy self,
                              const HPy *args, size_t nargs)
            {
                long i, n;
                long n_err = -1; // simulate an error at the given index
                int result;
                HPy key, value;
                HPyTracker ht;

                if (!HPyArg_Parse(ctx, NULL, args, nargs, "l|l", &n, &n_err))
                    return HPy_NULL;

                ht = HPyTracker_New(ctx, 0);  // track key-value pairs
                if HPy_IsNull(ht)
                    return HPy_NULL;

                HPy dict = HPyDict_New(ctx);
                if (HPy_IsNull(dict))
                    goto error;

                for (i=1; i<=n; i++) {
                    if (i==n_err)
                        goto error;
                    key = HPyLong_FromLong(ctx, i);
                    if (HPy_IsNull(key))
                        goto error;
                    if (HPyTracker_Add(ctx, ht, key) < 0)
                        goto error;
                    value = HPyLong_FromLong(ctx, i * i);
                    if (HPy_IsNull(value))
                        goto error;
                    if (HPyTracker_Add(ctx, ht, value) < 0)
                        goto error;
                    result = HPy_SetItem(ctx, dict, key, value);
                    if (result < 0)
                        goto error;
                }

                HPyTracker_Close(ctx, ht);
                return dict;

                error:
                    HPyTracker_Close(ctx, ht);
                    HPy_Close(ctx, dict);
                    HPyErr_SetString(ctx, ctx->h_ValueError, "Failed!");
                    return HPy_NULL;
            }
            @EXPORT(squares)
            @INIT
        """)
        assert mod.squares(5) == {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
        assert mod.squares(100) == dict((i, i**2) for i in range(1, 101))
        with pytest.raises(ValueError) as err:
            mod.squares(5, 3)
        assert str(err.value) == "Failed!"