From 9d59d56c0a840ecc55825c84d9ee2be4e914b0a5 Mon Sep 17 00:00:00 2001 From: Du Toit Spies Date: Fri, 7 Jun 2024 15:09:59 +0200 Subject: [PATCH] Added test for HPySlice_New --- test/test_hpyslice.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/test_hpyslice.py b/test/test_hpyslice.py index 8f8126828..48d474ffa 100644 --- a/test/test_hpyslice.py +++ b/test/test_hpyslice.py @@ -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) \ No newline at end of file