Commit c4ff9e93 authored by gsamain's avatar gsamain Committed by Xavier Thompson

Unit testing

parent 91c8bc06
# mode: error
# tag: cpp, cpp11
# cython: experimental_cpp_class_def=True, language_level=2
cdef cypclass UnknownNewOptionalArgs:
"""
This will fail because the wrapper knows b (in __new__) is optional,
but doesn't know its default value, so it cannot pass it to __init__
"""
__init__(self, int a, double b, int c = 42):
pass
UnknownNewOptionalArgs __new__(alloc, int a, double b = 4.2, int c = 0):
return alloc()
def test_new_unknown_optional_args():
cdef UnknownNewOptionalArgs o = UnknownNewOptionalArgs(3, 2.1)
cdef cypclass VarArgsConstructor:
__init__(self, int a, ...)
def test_varargs_constructor():
cdef VarArgsConstructor o = VarArgsConstructor(1)
_ERRORS = u'''
10:4: Could not call this __init__ function because the corresponding __new__ wrapper isn't aware of default values
12:4: Wrapped __new__ is here (some args passed to __init__ could be at their default values)
19:13: Cypclass cannot handle variable arguments constructors, but you can use optional arguments (arg=some_value)
'''
# mode: error
# tag: cpp, cpp11
# cython: experimental_cpp_class_def=True, language_level=2
def GILHoldingFunction():
pass
cdef class GILHoldingClass:
pass
cdef GILHoldingClass gil_holding_object
cdef cypclass NoGILClass:
return_type(self):
pass
void call(self):
GILHoldingFunction()
void access(self):
o = gil_holding_object
_ERRORS = u'''
21:8: Assignment of Python object not allowed without gil
14:4: Function with Python return type cannot be declared nogil
18:26: Discarding owned Python object not allowed without gil
18:26: Calling gil-requiring function not allowed without gil
18:8: Accessing Python global or builtin not allowed without gil
18:26: Constructing Python tuple not allowed without gil
20:4: Function declared nogil has Python locals or temporaries
'''
\ No newline at end of file
# mode: error
# tag: cpp, cpp11
# cython: experimental_cpp_class_def=True, language_level=2
cdef cypclass MethodsWithoutSelf:
void declared()
void defined():
pass
_ERRORS = u'''
6:4: Cypclass methods must have a self argument
7:4: Cypclass methods must have a self argument
'''
\ No newline at end of file
# mode: run
# tag: cpp, cpp11
# cython: experimental_cpp_class_def=True, language_level=2
cdef cypclass SomeMemory:
void print_class(self) with gil:
print "SomeMemory"
cdef cypclass SomeSubMemory(SomeMemory):
void print_class(self) with gil:
print "SomeSubMemory"
def test_constructor_type_inference():
"""
>>> test_constructor_type_inference()
SomeMemory
SomeSubMemory
"""
foo = SomeMemory()
foo.print_class()
bar = SomeSubMemory()
bar.print_class()
# mode: run
# tag: cpp, cpp11
# cython: experimental_cpp_class_def=True, language_level=2
cdef cypclass Base:
int base
__init__(self, int arg):
self.base = arg
cdef cypclass InplaceAddition(Base):
__init__(self, int arg):
Base.__init__(self, arg)
InplaceAddition __iadd__(self, InplaceAddition other):
self.base += other.base
void print_IA_base(self) with gil:
print self.base
cdef cypclass InplaceSubstraction(Base):
__init__(self, int arg):
Base.__init__(self, arg)
InplaceSubstraction __isub__(self, InplaceSubstraction other):
self.base -= other.base
void print_IS_base(self) with gil:
print self.base
cdef cypclass Diamond(InplaceAddition, InplaceSubstraction):
__init__(self, int a, int b):
InplaceAddition.__init__(self, a)
InplaceSubstraction.__init__(self, b)
def test_non_virtual_inheritance():
"""
>>> test_non_virtual_inheritance()
1
2
3
0
"""
cdef Diamond diamond = Diamond(1, 2)
diamond.print_IA_base()
diamond.print_IS_base()
cdef InplaceAddition iadd_obj = InplaceAddition(2)
cdef InplaceSubstraction isub_obj = InplaceSubstraction(2)
diamond += iadd_obj
diamond -= isub_obj
diamond.print_IA_base()
diamond.print_IS_base()
# mode: run
# tag: cpp, cpp11
# cython: experimental_cpp_class_def=True, language_level=2
cdef cypclass Singleton
cdef int allocated = 0
cdef Singleton ptr
cdef cypclass Singleton:
Singleton __new__(alloc):
global allocated
global ptr
if not allocated:
ptr = alloc()
allocated = 1
return ptr
def test_singleton():
"""
>>> test_singleton()
True
"""
cdef Singleton s1 = Singleton()
cdef Singleton s2 = Singleton()
print s1 is s2
cdef cypclass Base:
double value
__init__(self, int a):
self.value = (<double> a)*1.2
__init__(self, double b):
self.value = b
Base __new__(alloc, int a):
return alloc()
cdef cypclass Derived(Base):
Derived __new__(alloc, double b):
return alloc()
def test_changing_init_choice():
"""
>>> test_changing_init_choice()
6.0
5.0
"""
cdef Base base = Base(5)
cdef Derived derived = Derived(5)
print base.value
print derived.value
cdef cypclass NoisyConstruction:
__init__(self) with gil:
print "I'm a noisy constructor"
NoisyConstruction __new__(alloc):
return alloc()
def test_direct_new_call():
"""
>>> test_direct_new_call()
Noisy construction
I'm a noisy constructor
Silent direct __new__ call
"""
print "Noisy construction"
cdef NoisyConstruction obj1 = NoisyConstruction()
print "Silent direct __new__ call"
cdef NoisyConstruction obj2 = NoisyConstruction.__new__(NoisyConstruction.__alloc__)
cdef cypclass Multiply:
int __new__(unused, int a, int b):
return a*b
def test_non_class_return_new():
"""
>>> test_non_class_return_new()
6
"""
cdef int obj = Multiply(2, 3)
print obj
cdef cypclass SomeArgUnpacking:
int a
int b
SomeArgUnpacking __new__(alloc, int a, int b = 31):
return alloc()
void __init__(self, int a = 0, int b = 32):
self.a = a
self.b = b
def test_new_args_unpacking():
"""
>>> test_new_args_unpacking()
1
2
1
32
"""
cdef SomeArgUnpacking obj1 = SomeArgUnpacking(1, 2)
cdef SomeArgUnpacking obj2 = SomeArgUnpacking(1)
print obj1.a
print obj1.b
print obj2.a
print obj2.b
# mode: run
# tag: cpp, cpp11
# cython: experimental_cpp_class_def=True, language_level=2
cdef cypclass Nothing:
pass
cdef cypclass Init:
__init__(self) with gil:
print "__init__ called"
cdef cypclass New:
New __new__(alloc):
obj = alloc()
with gil:
print "__new__ called"
def test_new_on_Nothing():
"""
>>> test_new_on_Nothing()
Nothing shouldn't shout with new
"""
cdef Nothing o = new Nothing()
print "Nothing shouldn't shout with new"
def test_normal_Nothing_allocation():
"""
>>> test_normal_Nothing_allocation()
Nothing shouldn't shout with constructor wrapper
"""
cdef Nothing o = Nothing()
print "Nothing shouldn't shout with constructor wrapper"
def test_new_on_Init():
"""
>>> test_new_on_Init()
Init shouldn't shout with new
"""
cdef Init o = new Init()
print "Init shouldn't shout with new"
def test_normal_Init_allocation():
"""
>>> test_normal_Init_allocation()
__init__ called
Init should shout with constructor wrapper
"""
cdef Init o = Init()
print "Init should shout with constructor wrapper"
def test_new_on_New():
"""
>>> test_new_on_New()
New shouldn't shout with new
"""
cdef New o = new New()
print "New shouldn't shout with new"
def test_normal_New_allocation():
"""
>>> test_normal_New_allocation()
__new__ called
New should shout with constructor wrapper
"""
cdef New o = New()
print "New should shout with constructor wrapper"
# mode: run
# tag: cpp, cpp11
# cython: experimental_cpp_class_def=True, language_level=2
cdef cypclass OverloadedOperators:
int a
__init__(self, int a):
self.a = a
OverloadedOperators __add__(self, OverloadedOperators other):
return OverloadedOperators(self.a + other.a)
OverloadedOperators __iadd__(self, OverloadedOperators other):
self.a += other.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 Wrapper:
OverloadedOperators m
void __init__(self, int a=0):
self.m = OverloadedOperators(a)
OverloadedOperators __OverloadedOperators__(self):
return self.m
def test_overloaded_casts():
"""
>>> test_overloaded_casts()
-1
4294967295
True
3
False
"""
cdef OverloadedOperators o = OverloadedOperators(-1)
cdef Wrapper w = Wrapper(3)
print str(<int> o) + '\n' + str(<unsigned int> o) + '\n' + str(<bint> o)\
+ '\n' + str(<int> <OverloadedOperators> w) + '\n' + str(<bint> <OverloadedOperators> w)
def test_overloaded_addition():
"""
>>> test_overloaded_addition()
True
True
True
True
True
"""
cdef OverloadedOperators o1 = OverloadedOperators(2)
cdef OverloadedOperators o2 = OverloadedOperators(3)
cdef OverloadedOperators o1_second_ref = o1
cdef OverloadedOperators o3 = o1 + o2
o1 += o2
print o3.a == o1.a
print o3 is not o1
print o3 is not o2
print o1 is not o2
print o1 is o1_second_ref
\ No newline at end of file
# mode: run
# tag: cpp, cpp11
# cython: experimental_cpp_class_def=True, language_level=2
cdef cypclass TemplatedBase[T, U]:
T a
U b
__init__(self, T a, U b):
self.a = a
self.b = b
T first(self):
return self.a
U second(self):
return self.b
cdef cypclass TemplatedDerived[T](TemplatedBase[T, T]):
T c
__init__(self, T a, T b, T c):
TemplatedBase[T, T].__init__(self, a, b)
self.c = c
T third(self):
return self.c
def test_base_same_type_construction():
"""
>>> test_base_same_type_construction()
1
2
"""
cdef TemplatedBase[int, int] o = TemplatedBase[int, int](1, 2)
print o.first()
print o.second()
def test_base_different_type_construction():
"""
>>> test_base_different_type_construction()
1
2.3
"""
cdef TemplatedBase[int, double] o = TemplatedBase[int, double](1, 2.3)
print o.first()
print o.second()
def test_base_new_keyword():
"""
>>> test_base_new_keyword()
1
2.3
"""
cdef TemplatedBase[int, double] o = new TemplatedBase[int, double]()
o.__init__(1, 2.3)
print o.first()
print o.second()
def test_derived_twoargs_construction():
"""
>>> test_derived_twoargs_construction()
42
4
"""
cdef TemplatedDerived[int] o = TemplatedDerived[int](42, 4)
print o.first()
print o.second()
def test_derived_threeargs_construction():
"""
>>> test_derived_threeargs_construction()
1
2
3
"""
cdef TemplatedDerived[int] o = TemplatedDerived[int](1, 2, 3)
print o.first()
print o.second()
print o.third()
# mode: run
# tag: cpp, cpp11
# cython: experimental_cpp_class_def=True, language_level=2
cdef cypclass Default:
int a
def test_default():
"""
>>> test_default()
3
"""
cdef Default o = Default()
o.a = 3
print o.a
cdef cypclass OverloadedConstructor:
int a
void __init__(self, int a):
self.a = a
void __init__(self, int a, int b):
self.a = a*b
cdef cypclass Derived(OverloadedConstructor):
__init__(self, int a, int b):
self.a = a+b
def test_overloaded_constructor():
"""
>>> test_overloaded_constructor()
3
14
9
"""
cdef OverloadedConstructor o1 = OverloadedConstructor(3)
print o1.a
cdef OverloadedConstructor o2 = OverloadedConstructor(2, 7)
print o2.a
cdef Derived o3 = Derived(2, 7)
print o3.a
cdef cypclass OptionalArgsConstructor:
int a
void __init__(self, int a, int b=1, int c=0):
this.a = a*b + c
def test_mandatory_only_arg_constructor():
"""
>>> test_mandatory_only_arg_constructor()
3
"""
cdef OptionalArgsConstructor o = OptionalArgsConstructor(3)
print o.a
def test_some_optional_arguments():
"""
>>> test_some_optional_arguments()
14
"""
cdef OptionalArgsConstructor o = OptionalArgsConstructor(2, 7)
print o.a
def test_all_optional_arguments():
"""
>>> test_all_optional_arguments()
15
"""
cdef OptionalArgsConstructor o = OptionalArgsConstructor(2, 7, 1)
print o.a
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