Skip to content

Commit

Permalink
Added test for HPySlice_New
Browse files Browse the repository at this point in the history
  • Loading branch information
Du Toit Spies committed Jun 7, 2024
1 parent 11e634d commit 9d59d56
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/test_hpyslice.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,32 @@ def test_adjust_indices(self):
assert mod.f(10, 9, 0, -3) == (3, 9, 0, -3)
assert mod.f(10, -1, -10, -3) == (3, 9, 0, -3)
assert mod.f(10, 5, 5, -3) == (0, 5, 5, -3)

def test_new(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
{
HPy start, stop, step;
if (nargs != 3) {
HPyErr_SetString(ctx, ctx->h_TypeError,
"expected exactly 3 arguments");
return HPy_NULL;
}
if (HPyArg_Parse(ctx, NULL, args, nargs, "OOO",
&start, &stop, &step) < 0) {
return HPy_NULL;
}
HPy length = HPySlice_New(ctx, start, stop, step);
return length;
}
@EXPORT(f)
@INIT
""")

assert mod.f(0, 10, 1) == slice(0, 10, 1)
assert mod.f(None, 10, 1) == slice(None, 10, 1)
assert mod.f(1, None, 1) == slice(1, None, 1)
assert mod.f(0, 10, None) == slice(0, 10, None)

0 comments on commit 9d59d56

Please sign in to comment.