| 1 | """ |
| 2 | NOTE: this tests are also meant to be run as PyPy "applevel" tests. |
| 3 | |
| 4 | This means that global imports will NOT be visible inside the test |
| 5 | functions. In particular, you have to "import pytest" inside the test in order |
| 6 | to be able to use e.g. pytest.raises (which on PyPy will be implemented by a |
| 7 | "fake pytest module") |
| 8 | """ |
| 9 | from .support import HPyTest |
| 10 | |
| 11 | |
| 12 | class TestHPyTracker(HPyTest): |
| 13 | def hpytracker_module(self, ops, size=0): |
| 14 | return self.make_module(""" |
| 15 | HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 16 | static HPy f_impl(HPyContext *ctx, HPy self, |
| 17 | const HPy *args, size_t nargs) |
| 18 | {{ |
| 19 | HPyTracker ht; |
| 20 | HPy result = HPy_NULL; |
| 21 | ht = HPyTracker_New(ctx, {size}); |
| 22 | if HPy_IsNull(ht) {{ |
| 23 | return HPy_NULL; |
| 24 | }} |
| 25 | {ops} |
| 26 | HPyTracker_Close(ctx, ht); |
| 27 | if (HPy_IsNull(result)) |
| 28 | result = HPy_Dup(ctx, ctx->h_None); |
| 29 | return result; |
| 30 | }} |
| 31 | @EXPORT(f) |
| 32 | @INIT |
| 33 | """.format(ops=ops, size=size)) |
| 34 | |
| 35 | def test_new_and_free(self): |
| 36 | mod = self.hpytracker_module(ops="") |
| 37 | mod.f() |
| 38 | |
| 39 | def test_new_with_size_and_free(self): |
| 40 | mod = self.hpytracker_module(ops="", size=10) |
| 41 | mod.f() |
| 42 | |
| 43 | def test_add_and_free(self): |
| 44 | mod = self.hpytracker_module(ops=""" |
| 45 | HPyTracker_Add(ctx, ht, HPy_Dup(ctx, args[0])); |
| 46 | """) |
| 47 | mod.f(5) |
| 48 | |
| 49 | def test_add_and_remove_all(self): |
| 50 | mod = self.hpytracker_module(ops=""" |
| 51 | HPyTracker_Add(ctx, ht, args[0]); |
| 52 | HPyTracker_ForgetAll(ctx, ht); |
| 53 | """) |
| 54 | assert mod.f(5) is None |
| 55 | |
| 56 | def test_remove_all_on_nothing(self): |
| 57 | mod = self.hpytracker_module(ops=""" |
| 58 | HPyTracker_ForgetAll(ctx, ht); |
| 59 | """) |
| 60 | assert mod.f() is None |
| 61 | |
| 62 | def test_squares_example(self): |
| 63 | import pytest |
| 64 | mod = self.make_module(""" |
| 65 | HPyDef_METH(squares, "squares", HPyFunc_VARARGS) |
| 66 | static HPy squares_impl(HPyContext *ctx, HPy self, |
| 67 | const HPy *args, size_t nargs) |
| 68 | { |
| 69 | long i, n; |
| 70 | long n_err = -1; // simulate an error at the given index |
| 71 | int result; |
| 72 | HPy key, value; |
| 73 | HPyTracker ht; |
| 74 | |
| 75 | if (!HPyArg_Parse(ctx, NULL, args, nargs, "l|l", &n, &n_err)) |
| 76 | return HPy_NULL; |
| 77 | |
| 78 | ht = HPyTracker_New(ctx, 0); // track key-value pairs |
| 79 | if HPy_IsNull(ht) |
| 80 | return HPy_NULL; |
| 81 | |
| 82 | HPy dict = HPyDict_New(ctx); |
| 83 | if (HPy_IsNull(dict)) |
| 84 | goto error; |
| 85 | |
| 86 | for (i=1; i<=n; i++) { |
| 87 | if (i==n_err) |
| 88 | goto error; |
| 89 | key = HPyLong_FromLong(ctx, i); |
| 90 | if (HPy_IsNull(key)) |
| 91 | goto error; |
| 92 | if (HPyTracker_Add(ctx, ht, key) < 0) |
| 93 | goto error; |
| 94 | value = HPyLong_FromLong(ctx, i * i); |
| 95 | if (HPy_IsNull(value)) |
| 96 | goto error; |
| 97 | if (HPyTracker_Add(ctx, ht, value) < 0) |
| 98 | goto error; |
| 99 | result = HPy_SetItem(ctx, dict, key, value); |
| 100 | if (result < 0) |
| 101 | goto error; |
| 102 | } |
| 103 | |
| 104 | HPyTracker_Close(ctx, ht); |
| 105 | return dict; |
| 106 | |
| 107 | error: |
| 108 | HPyTracker_Close(ctx, ht); |
| 109 | HPy_Close(ctx, dict); |
| 110 | HPyErr_SetString(ctx, ctx->h_ValueError, "Failed!"); |
| 111 | return HPy_NULL; |
| 112 | } |
| 113 | @EXPORT(squares) |
| 114 | @INIT |
| 115 | """) |
| 116 | assert mod.squares(5) == {1: 1, 2: 4, 3: 9, 4: 16, 5: 25} |
| 117 | assert mod.squares(100) == dict((i, i**2) for i in range(1, 101)) |
| 118 | with pytest.raises(ValueError) as err: |
| 119 | mod.squares(5, 3) |
| 120 | assert str(err.value) == "Failed!" |