| 1 |
from .support import HPyTest |
| 2 |
|
| 3 |
|
| 4 |
class TestNumber(HPyTest): |
| 5 |
|
| 6 |
def test_bool_from_bool_and_long(self): |
| 7 |
import pytest |
| 8 |
mod = self.make_module(""" |
| 9 |
HPyDef_METH(from_bool, "from_bool", HPyFunc_O) |
| 10 |
static HPy from_bool_impl(HPyContext *ctx, HPy self, HPy arg) |
| 11 |
{ |
| 12 |
int32_t x = HPyLong_AsInt32_t(ctx, arg); |
| 13 |
if (x == -1 && HPyErr_Occurred(ctx)) |
| 14 |
return HPy_NULL; |
| 15 |
if (x != 0 && x != 1) { |
| 16 |
HPyErr_SetString(ctx, ctx->h_ValueError, |
| 17 |
"value must be 0 or 1"); |
| 18 |
return HPy_NULL; |
| 19 |
} |
| 20 |
return HPyBool_FromBool(ctx, (x ? true : false)); |
| 21 |
} |
| 22 |
|
| 23 |
HPyDef_METH(from_long, "from_long", HPyFunc_O) |
| 24 |
static HPy from_long_impl(HPyContext *ctx, HPy self, HPy arg) |
| 25 |
{ |
| 26 |
long x = HPyLong_AsLong(ctx, arg); |
| 27 |
if (x == -1 && HPyErr_Occurred(ctx)) |
| 28 |
return HPy_NULL; |
| 29 |
return HPyBool_FromLong(ctx, x); |
| 30 |
} |
| 31 |
@EXPORT(from_bool) |
| 32 |
@EXPORT(from_long) |
| 33 |
@INIT |
| 34 |
""") |
| 35 |
assert mod.from_bool(0) is False |
| 36 |
assert mod.from_bool(1) is True |
| 37 |
with pytest.raises(ValueError): |
| 38 |
mod.from_bool(2) |
| 39 |
assert mod.from_long(0) is False |
| 40 |
assert mod.from_long(42) is True |
| 41 |
|
| 42 |
def test_unary(self): |
| 43 |
import pytest |
| 44 |
import operator |
| 45 |
for c_name, op in [ |
| 46 |
('Negative', operator.neg), |
| 47 |
('Positive', operator.pos), |
| 48 |
('Absolute', abs), |
| 49 |
('Invert', operator.invert), |
| 50 |
('Index', operator.index), |
| 51 |
('Long', int), |
| 52 |
('Float', float), |
| 53 |
]: |
| 54 |
mod = self.make_module(""" |
| 55 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 56 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 57 |
{ |
| 58 |
return HPy_%s(ctx, arg); |
| 59 |
} |
| 60 |
@EXPORT(f) |
| 61 |
@INIT |
| 62 |
""" % (c_name,), name='number_'+c_name) |
| 63 |
assert mod.f(-5) == op(-5) |
| 64 |
assert mod.f(6) == op(6) |
| 65 |
try: |
| 66 |
res = op(4.75) |
| 67 |
except Exception as e: |
| 68 |
with pytest.raises(e.__class__): |
| 69 |
mod.f(4.75) |
| 70 |
else: |
| 71 |
assert mod.f(4.75) == res |
| 72 |
|
| 73 |
def test_binary(self): |
| 74 |
import operator |
| 75 |
for c_name, op in [ |
| 76 |
('Add', operator.add), |
| 77 |
('Subtract', operator.sub), |
| 78 |
('Multiply', operator.mul), |
| 79 |
('FloorDivide', operator.floordiv), |
| 80 |
('TrueDivide', operator.truediv), |
| 81 |
('Remainder', operator.mod), |
| 82 |
('Divmod', divmod), |
| 83 |
('Lshift', operator.lshift), |
| 84 |
('Rshift', operator.rshift), |
| 85 |
('And', operator.and_), |
| 86 |
('Xor', operator.xor), |
| 87 |
('Or', operator.or_), |
| 88 |
]: |
| 89 |
mod = self.make_module(""" |
| 90 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 91 |
static HPy f_impl(HPyContext *ctx, HPy self, |
| 92 |
const HPy *args, size_t nargs) |
| 93 |
{ |
| 94 |
HPy a, b; |
| 95 |
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO", &a, &b)) |
| 96 |
return HPy_NULL; |
| 97 |
return HPy_%s(ctx, a, b); |
| 98 |
} |
| 99 |
@EXPORT(f) |
| 100 |
@INIT |
| 101 |
""" % (c_name,), name='number_'+c_name) |
| 102 |
assert mod.f(5, 4) == op(5, 4) |
| 103 |
assert mod.f(6, 3) == op(6, 3) |
| 104 |
|
| 105 |
def test_power(self): |
| 106 |
mod = self.make_module(""" |
| 107 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 108 |
static HPy f_impl(HPyContext *ctx, HPy self, |
| 109 |
const HPy *args, size_t nargs) |
| 110 |
{ |
| 111 |
HPy a, b, c; |
| 112 |
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OOO", &a, &b, &c)) |
| 113 |
return HPy_NULL; |
| 114 |
return HPy_Power(ctx, a, b, c); |
| 115 |
} |
| 116 |
@EXPORT(f) |
| 117 |
@INIT |
| 118 |
""") |
| 119 |
assert mod.f(4, 5, None) == 4 ** 5 |
| 120 |
assert mod.f(4, 5, 7) == pow(4, 5, 7) |
| 121 |
|
| 122 |
def test_matmul(self): |
| 123 |
class Mat: |
| 124 |
def __matmul__(self, other): |
| 125 |
return ('matmul', self, other) |
| 126 |
m1 = Mat() |
| 127 |
m2 = Mat() |
| 128 |
mod = self.make_module(""" |
| 129 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 130 |
static HPy f_impl(HPyContext *ctx, HPy self, |
| 131 |
const HPy *args, size_t nargs) |
| 132 |
{ |
| 133 |
HPy a, b; |
| 134 |
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO", &a, &b)) |
| 135 |
return HPy_NULL; |
| 136 |
return HPy_MatrixMultiply(ctx, a, b); |
| 137 |
} |
| 138 |
@EXPORT(f) |
| 139 |
@INIT |
| 140 |
""") |
| 141 |
assert mod.f(m1, m2) == m1.__matmul__(m2) |
| 142 |
|
| 143 |
def test_inplace_binary(self): |
| 144 |
import operator |
| 145 |
for c_name, py_name in [ |
| 146 |
('Add', '__iadd__'), |
| 147 |
('Subtract', '__isub__'), |
| 148 |
('Multiply', '__imul__'), |
| 149 |
('FloorDivide', '__ifloordiv__'), |
| 150 |
('TrueDivide', '__itruediv__'), |
| 151 |
('Remainder', '__imod__'), |
| 152 |
('Lshift', '__ilshift__'), |
| 153 |
('Rshift', '__irshift__'), |
| 154 |
('And', '__iand__'), |
| 155 |
('Xor', '__ixor__'), |
| 156 |
('Or', '__ior__'), |
| 157 |
]: |
| 158 |
mod = self.make_module(""" |
| 159 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 160 |
static HPy f_impl(HPyContext *ctx, HPy self, |
| 161 |
const HPy *args, size_t nargs) |
| 162 |
{ |
| 163 |
HPy a, b; |
| 164 |
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO", &a, &b)) |
| 165 |
return HPy_NULL; |
| 166 |
return HPy_InPlace%s(ctx, a, b); |
| 167 |
} |
| 168 |
@EXPORT(f) |
| 169 |
@INIT |
| 170 |
""" % (c_name,), name='number_'+c_name) |
| 171 |
class A: |
| 172 |
def mymethod(self, b): |
| 173 |
return (py_name, b) |
| 174 |
setattr(A, py_name, A.mymethod) |
| 175 |
assert mod.f(A(), 12.34) == A().mymethod(12.34) |
| 176 |
|
| 177 |
def test_inplace_power(self): |
| 178 |
mod = self.make_module(""" |
| 179 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 180 |
static HPy f_impl(HPyContext *ctx, HPy self, |
| 181 |
const HPy *args, size_t nargs) |
| 182 |
{ |
| 183 |
HPy a, b, c; |
| 184 |
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OOO", &a, &b, &c)) |
| 185 |
return HPy_NULL; |
| 186 |
return HPy_InPlacePower(ctx, a, b, c); |
| 187 |
} |
| 188 |
@EXPORT(f) |
| 189 |
@INIT |
| 190 |
""") |
| 191 |
class A: |
| 192 |
def __ipow__(self, b): |
| 193 |
return ('ipow', b) |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
assert mod.f(A(), 5, None) == A().__ipow__(5) |
| 198 |
assert mod.f(A(), 7, 'hello') == A().__ipow__(7) |
| 199 |
assert mod.f(4, 5, 7) == pow(4, 5, 7) |
| 200 |
|
| 201 |
def test_inplace_matmul(self): |
| 202 |
class Mat: |
| 203 |
def __imatmul__(self, other): |
| 204 |
return ('imatmul', self, other) |
| 205 |
m1 = Mat() |
| 206 |
m2 = Mat() |
| 207 |
mod = self.make_module(""" |
| 208 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 209 |
static HPy f_impl(HPyContext *ctx, HPy self, |
| 210 |
const HPy *args, size_t nargs) |
| 211 |
{ |
| 212 |
HPy a, b; |
| 213 |
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO", &a, &b)) |
| 214 |
return HPy_NULL; |
| 215 |
return HPy_InPlaceMatrixMultiply(ctx, a, b); |
| 216 |
} |
| 217 |
@EXPORT(f) |
| 218 |
@INIT |
| 219 |
""") |
| 220 |
assert mod.f(m1, m2) == m1.__imatmul__(m2) |
| 221 |
|
| 222 |
def test_number_check(self): |
| 223 |
mod = self.make_module(""" |
| 224 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 225 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 226 |
{ |
| 227 |
int cond = HPyNumber_Check(ctx, arg); |
| 228 |
return HPyLong_FromLong(ctx, cond); |
| 229 |
} |
| 230 |
@EXPORT(f) |
| 231 |
@INIT |
| 232 |
""") |
| 233 |
assert mod.f("foo") == 0 |
| 234 |
assert mod.f(42) == 1 |
| 235 |
assert mod.f(42.1) == 1 |