Commit a90dae5c authored by gsamain's avatar gsamain

Copy cypclass tests to cython test suite

parent dbe290c4
# mode: run
# distutils: language=c++
# distutils: extra_compile_args=-std=c++11
# tag: cpp
# This is extremely hacky
cdef extern from "Python.h":
struct CyObject "CyObject"
void Cy_INCREF(CyObject* o) nogil
cdef cypclass SomeMemory:
int a
SomeMemory __new__(f):
o = f()
o.a = -2
return o
void __init__(self):
this.a += 1
void __init__(self, int a):
pass
void __init__(self, int a, int b, int c, int d = 21, int e = 31):
self.a = e
cdef cypclass SomeArgUnpacking:
int a
SomeArgUnpacking __new__(f, int a, int b, int c, int d = 21, int e = 31):
o = f()
return o
void __init__(self, int a, int b, int c=2, int d=4, int e = 32, int g = 65):
self.a = e
cdef cypclass SomeNewTrick:
int a
void* __new__(f, int a):
o = f()
o.a = a
# As we are returning a non-reference counted variable (void*),
# Cython will decref o (as it is reference-counted).
# To avoid premature memory deallocation, we have to keep a reference here
Cy_INCREF(<CyObject*>o)
return <void*> o
void __init__(self, int a, int b, int c, int d = 21, int e = 31):
self.a = e
cdef cypclass SomeNotInstanciable:
double __new__(unused, int a, double b):
return a*b
cdef int foo():
o = SomeMemory()
return o.a
cdef int bar():
o = <SomeNewTrick> SomeNewTrick(3)
return o.a
cdef int baz():
o = SomeArgUnpacking(3, 2, 1, 0)
return o.a
cdef double baa():
o = SomeNotInstanciable.__new__(SomeNotInstanciable.__alloc__, 2, 3.4)
o = SomeNotInstanciable.__new__(NULL, 2, 3.4)
return o
cpdef bag():
"""
>>> bag()
-1\n3\n32\n6.8
"""
return str(foo()) + '\n' + str(bar()) + '\n' + str(baz()) + '\n' + str(baa())
# mode: run
# distutils: language=c++
# distutils: extra_compile_args=-std=c++11
# tag: cpp
cdef cypclass SomeMemory:
int a
cpdef foo():
cdef SomeMemory o = SomeMemory()
o.a = 3
return o.a
def bag():
"""
>>> bag()
3
"""
return str(foo())
\ No newline at end of file
# mode: run
# distutils: language=c++
# distutils: extra_compile_args=-std=c++11
# tag: cpp
cdef cypclass SomeMemory:
int a
void __init__(self, int a):
this.a = a
void __init__(self, int a, int b):
this.a = a*b
cpdef foo():
cdef SomeMemory o1 = SomeMemory(3)
return o1.a
cpdef bar():
cdef SomeMemory o2 = SomeMemory(2, 7)
return o2.a
def bag():
"""
>>> bag()
3\n14
"""
return str(foo()) + '\n' + str(bar())
# mode: run
# distutils: language=c++
# distutils: extra_compile_args=-std=c++11
# tag: cpp
cdef cypclass SomeMemory:
int a
void __init__(self, int a, int b=1):
this.a = a*b
cpdef foo():
cdef SomeMemory o1 = SomeMemory(3)
return o1.a
cpdef bar():
cdef SomeMemory o2 = SomeMemory(2, 7)
return o2.a
def bag():
"""
>>> bag()
3\n14
"""
return str(foo()) + '\n' + str(bar())
# mode: run
# distutils: language=c++
# distutils: extra_compile_args=-std=c++11
# tag: cpp
cdef cypclass SomeMemory:
int a
void __init__(self, int a):
this.a = a
void __init__(self, int a, int b):
this.a = a*b
cdef cypclass SomeSubMemory(SomeMemory):
void __init__(self, int a, int b):
this.a = a+b
int getter(self):
return this.a
cpdef foo():
cdef SomeMemory o1 = SomeMemory(2, 7)
return o1.a
cpdef bar():
cdef SomeSubMemory o2 = SomeSubMemory(2, 7)
return o2.getter()
cpdef baz():
cdef SomeSubMemory o3 = SomeSubMemory(2)
return o3.getter()
def bag():
"""
>>> bag()
14\n9\n2
"""
return str(foo()) + '\n' + str(bar()) + '\n' + str(baz())
# mode: run
# distutils: language=c++
# distutils: extra_compile_args=-std=c++11
# tag: cpp
cdef cypclass SomeMemory:
int a
cpdef foo():
cdef SomeMemory o = new SomeMemory()
o.a = 3
return o.a
def bag():
"""
>>> bag()
3
"""
return str(foo())
\ No newline at end of file
# mode: run
# distutils: language=c++
# distutils: extra_compile_args=-std=c++11
# tag: cpp
cdef cypclass A:
int a
void __init__(self, int arg=0):
self.a = arg
A __iadd__(self, A other):
self.a += other.a
int getter(self):
return self.a
cdef cypclass B:
int b
void __init__(self, int arg=0):
self.b = arg
B __isub__(self, B other):
self.b -= other.b
int getter(self):
return self.b
cdef cypclass C(A,B):
void __init__(self, int arg=0):
self.a = self.b = arg
cpdef bag():
"""
>>> bag()
3\n4\n8\n1\n8\n1
"""
a = A(3)
b = B(4)
c = C(5)
c += a
c -= b
return str(a.a) + '\n' + str(b.b) + '\n' + str(c.a) + '\n' + str(c.b)\
+ '\n' + str(A.getter(c)) + '\n' + str(B.getter(c))
# mode: run
# distutils: language=c++
# distutils: extra_compile_args=-std=c++11
# tag: cpp
cdef cypclass SomeMemory[T, U]:
T a
U b
void __init__(self, T a, U b):
self.a = a
self.b = b
T first(self):
return self.a
U second(self):
return self.b
cpdef bag():
"""
>>> bag()
1\n2.3\n1\n2.3
"""
cdef SomeMemory[int, double] o = SomeMemory[int, double](1, 2.3)
cdef SomeMemory[int, int] b = new SomeMemory[int, int]()
del b
return str(o.a) + '\n' + str(o.b) + '\n' + str(o.first()) + '\n' + str(o.second())
# mode: run
# distutils: language=c++
# distutils: extra_compile_args=-std=c++11
# tag: cpp
cdef cypclass SomeMemory:
int a
void __init__(self, int a=0):
self.a = a
SomeMemory __add__(self, SomeMemory other):
return SomeMemory(self.a + other.a)
SomeMemory __iadd__(self, SomeMemory other):
self.a += other.a
SomeMemory __mul__(self, SomeMemory other):
return SomeMemory(self.a * other.a)
cdef cypclass SomeSubMemory(SomeMemory):
int b
cpdef bag():
"""
>>> bag()
28\n4\n7
"""
o1 = SomeMemory()
o2 = SomeMemory(4)
o3 = SomeSubMemory(3)
o3 += o2
o1 = o2 * o3
return str(o1.a) + '\n' + str(o2.a) + '\n' + str(o3.a)
# mode: run
# distutils: language=c++
# distutils: extra_compile_args=-std=c++11
# tag: cpp
cdef cypclass SomeMemory:
int a
unsigned int __unsigned_int__(self):
return <unsigned int> self.a
int __int__(self):
return self.a
bint __bool__(self):
return self.a < 0
cdef cypclass SomeWrapper:
SomeMemory m
void __init__(self, int a=0):
self.m = SomeMemory()
self.m.a = a
SomeMemory __SomeMemory__(self):
return self.m
cpdef bag():
"""
>>> bag()
-1\n4294967295\nTrue\n3\nFalse
"""
cdef SomeMemory o = SomeMemory()
o.a = -1
cdef SomeWrapper w = SomeWrapper(3)
return str(<int> o) + '\n' + str(<unsigned int> o) + '\n' + str(<bint> o)\
+ '\n' + str(<int> <SomeMemory> w) + '\n' + str(<bint> <SomeMemory> w)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment