Add in an example/test for Python __pow__
- A test for the ternaryfunc builtin slot - Example of how to wrap C++ class for Python's pow
This commit is contained in:
parent
4d34d419b6
commit
bf85b6f7a9
2 changed files with 34 additions and 0 deletions
|
|
@ -83,6 +83,7 @@ if is_python_builtin():
|
||||||
if MyClass.less_than_counts != 6:
|
if MyClass.less_than_counts != 6:
|
||||||
raise RuntimeError("python:compare feature not working")
|
raise RuntimeError("python:compare feature not working")
|
||||||
|
|
||||||
|
# Test 6
|
||||||
sa = SimpleArray(5)
|
sa = SimpleArray(5)
|
||||||
elements = [x for x in sa]
|
elements = [x for x in sa]
|
||||||
if elements != [0, 10, 20, 30, 40]:
|
if elements != [0, 10, 20, 30, 40]:
|
||||||
|
|
@ -96,3 +97,17 @@ subslice = sa[1:3]
|
||||||
elements = [x for x in subslice]
|
elements = [x for x in subslice]
|
||||||
if elements != [10, 20]:
|
if elements != [10, 20]:
|
||||||
raise RuntimeError("slice not working")
|
raise RuntimeError("slice not working")
|
||||||
|
|
||||||
|
# Test 7 mapping to Python's pow
|
||||||
|
x = ANumber(2)
|
||||||
|
y = ANumber(4)
|
||||||
|
z = x ** y
|
||||||
|
if z.Value() != 16:
|
||||||
|
raise RuntimeError("x ** y wrong")
|
||||||
|
z = pow(x, y)
|
||||||
|
if z.Value() != 16:
|
||||||
|
raise RuntimeError("pow(x, y) wrong")
|
||||||
|
z = ANumber(9)
|
||||||
|
z = pow(x, y, z)
|
||||||
|
if z.Value() != 7:
|
||||||
|
raise RuntimeError("pow(x, y, z) wrong")
|
||||||
|
|
|
||||||
|
|
@ -226,3 +226,22 @@ void Dealloc2Destroyer(PyObject *v) {
|
||||||
};
|
};
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
// Test 7 mapping to Python's pow
|
||||||
|
%pybinoperator(__pow__, ANumber::power, ternaryfunc, nb_power);
|
||||||
|
|
||||||
|
%inline %{
|
||||||
|
class ANumber {
|
||||||
|
int num;
|
||||||
|
public:
|
||||||
|
ANumber(int d = 0) : num(d) {}
|
||||||
|
ANumber __pow__(const ANumber &other, const ANumber *x) const {
|
||||||
|
int val = (int)pow(num, other.num);
|
||||||
|
val = x ? val % x->num : val;
|
||||||
|
return ANumber(val);
|
||||||
|
}
|
||||||
|
int Value() const {
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
%}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue