Commit 78df7dc4 authored by Stefan Behnel's avatar Stefan Behnel

loads of doctests

parent b488c7c5
__doc__ = """
>>> s = Swallow()
>>> s.spam(1)
1 42 'grail' True
>>> s.spam(1, 2)
1 2 'grail' True
>>> s.spam(1, z = 2)
1 42 'grail' 2
>>> s.spam(1, y = 2)
1 42 2 True
>>> s.spam(1, x = 2, y = 'test')
1 2 'test' True
"""
swallow = True
class Swallow:
def spam(w, int x = 42, y = "grail", z = swallow):
pass
print w, x, y, z
__doc__ = """
>>> fiches_CP
[]
"""
fiches_CP = [1,2,3]
fiches_CP[:] = []
__doc__ = """
>>>
"""
def f():
cdef int int1, int2, int3
cdef char *ptr1, *ptr2, *ptr3
int2 = 10
int3 = 20
obj1 = 1
obj2 = 2
obj3 = 3
......@@ -8,4 +14,4 @@ def f():
ptr1 = ptr2 + int3
ptr1 = int2 + ptr3
obj1 = obj2 + int3
\ No newline at end of file
return int1, obj1
__doc__ = """
>>> f()
42
"""
def f():
a = 42
return a
__doc__ = """
>>> m = fmatrix()
>>> m[1] = True
>>> m.getfoo()
1
>>> m[0] = True
>>> m.getfoo()
0
"""
cdef class fmatrix:
cdef int foo
......@@ -6,3 +16,6 @@ cdef class fmatrix:
self.foo = value
return
self.foo = not value
def getfoo(self):
return self.foo
cdef void foo(obj1, obj2, obj3, obj4, obj5):
__doc__ = """
>>> foo(True, False, 23, 'test', 1)
(0, 1, False, False)
"""
def foo(obj1, obj2, obj3, obj4, obj5):
cdef int bool1, bool2, bool3, bool4
cdef char *ptr
cdef float f
bool1 = 1
bool2 = 0
ptr = NULL
f = 0.0
bool3 = bool1 and bool2
bool3 = bool1 or bool2
bool3 = obj1 and obj2
......@@ -11,3 +21,4 @@ cdef void foo(obj1, obj2, obj3, obj4, obj5):
bool4 = bool1 or bool2 and bool3
obj4 = obj1 and obj2 and obj3
obj5 = (obj1 + obj2 + obj3) and obj4
return bool3, bool4, obj4, obj5
__doc__ = """
>>> s = Spam()
>>> print s.__class__.__name__
Spam
>>> s = SpamT()
>>> print type(s).__name__
SpamT
"""
class Spam: pass
class SpamT(object): pass
__doc__ = """
>>> spam == "C string 1" + "C string 2"
True
"""
spam = "C string 1" + "C string 2"
spam = "eggs" * 42
grail = 17 * "tomato"
__doc__ = """
>>> print spam
eggseggseggseggs
>>> print grail
tomatotomatotomatotomatotomatotomatotomato
"""
spam = "eggs" * 4
grail = 7 * "tomato"
__doc__ = """
>>> f()
"""
def f():
cdef char a_char
cdef short a_short
......
__doc__ = """
>>> p = Point(1,2,3)
>>> p.gettuple()
(1.0, 2.0, 3.0)
>>> q = p + Point(2,3,4)
>>> q.gettuple()
(3.0, 5.0, 7.0)
>>> p.gettuple()
(1.0, 2.0, 3.0)
"""
cdef class Point:
cdef double x, y, z
def __init__(self, double x, double y, double z):
......@@ -5,6 +16,9 @@ cdef class Point:
self.y = y
self.z = z
# XXX
def __add__(self, other):
# XXX: originally, this said "def __add__(self, other)"
def __add__(Point self, Point other):
return Point(self.x + other.x, self.y + other.y, self.z + other.z)
def gettuple(self):
return (self.x, self.y, self.z)
cdef class Eggs: pass
__doc__ = """
>>> e = Eggs()
>>> print type(e).__name__
Eggs
"""
cdef class Eggs: pass
__doc__ = """
>>> print type(f()).__name__
Spam
"""
cdef class Spam:
pass
def f():
s = Spam()
return s
__doc__ = """
>>> f(0,0)
0
>>> f(1,2)
2
>>> f(1,-1)
1
>>> g(1,2)
1
>>> g(0,2)
2
>>> g(0,0)
0
>>> h(1,2)
1
>>> h(0,2)
2
>>> h(0,0)
3
"""
def f(a, b):
x = 0
if a:
x = 1
if a+b:
x = 1
x = 2
return x
def g(a, b):
x = 0
if a:
x = 1
elif b:
x = 2
return x
def h(a, b):
x = 0
if a:
x = 1
elif b:
x = 2
else:
x = 3
\ No newline at end of file
return x
__doc__ = """
>>> c1 = C1()
>>> c2 = C2(c1)
>>> c1 is c2.getc1()
True
"""
cdef class C1:
pass
......@@ -6,3 +13,6 @@ cdef class C2:
def __init__(self, arg):
self.c1 = arg
def getc1(self):
return self.c1
__doc__ = """
>>> t = TEST()
>>> 1 in t
True
"""
cdef class TEST:
def __contains__(self, x):
return 42
__doc__ = """
>>> x = X()
>>> x.slots
['']
"""
class X:
slots = ["", ]
__doc__ = """
>>> a = A(1,2,3)
>>> a[0]
1.0
>>> a[1]
2.0
>>> a[2]
3.0
"""
cdef class A:
cdef double x[3]
def __init__(self, *args):
cdef int i, max
max = len(args)
if max > 3:
max = 3
for i from 0 <= i < max:
self.x[i] = args[i]
def __getitem__(self,i):
return self.x[i]
__doc__ = """
>>> f(1, 2, 3, 4, 5)
[]
>>> g(1, 2, 3, 4, 5)
[2]
>>> h(1, 2, 3, 4, 5)
[2, 3]
>>> j(1, 2, 3, 4, 5)
[2, 3, 4]
>>> k(1, 2, 3, 4, 5)
[17, 42, 88]
"""
def f(obj1, obj2, obj3, obj4, obj5):
obj1 = []
return obj1
def g(obj1, obj2, obj3, obj4, obj5):
obj1 = [obj2]
return obj1
def h(obj1, obj2, obj3, obj4, obj5):
obj1 = [obj2, obj3]
return obj1
def j(obj1, obj2, obj3, obj4, obj5):
obj1 = [obj2, obj3, obj4]
return obj1
def k(obj1, obj2, obj3, obj4, obj5):
obj1 = [17, 42, 88]
\ No newline at end of file
return obj1
__doc__ = """
>>> foo()
"""
def foo():
a = 42
a1 = 0123
......
cdef void f():
__doc__ = """
>>> f()
(1, 2, 1, 2)
>>> g()
(1, 1, 2, 2, 3, 3)
>>> h()
(1, 'test', 3, 1, 'test', 3)
>>> j()
(2, 1, 4, 2, 6, 3)
"""
def f():
cdef object obj1a, obj2a, obj3a, obj1b, obj2b, obj3b
cdef int int1, int2
cdef char *ptr1, *ptr2
obj1b, obj2b, obj3b = 1, 2, 3
obj1a, obj2a = obj1b, obj2b
return obj1a, obj2a, obj1b, obj2b
def g():
cdef object obj1a, obj2a, obj3a, obj1b, obj2b, obj3b
obj1b, obj2b, obj3b = 1, 2, 3
obj1a, [obj2a, obj3a] = [obj1b, (obj2b, obj3b)]
return obj1a, obj1b, obj2a, obj2b, obj3a, obj3b
def h():
cdef object obj1a, obj2a, obj3a, obj1b, obj2b, obj3b
cdef int int1, int2
cdef char *ptr1, *ptr2
int2, ptr2, obj1b = 1, "test", 3
int1, ptr1, obj1a = int2, ptr2, obj1b
return int1, ptr1, obj1a, int2, ptr2, obj1b
def j():
cdef object obj1a, obj2a, obj3a, obj1b, obj2b, obj3b
obj1b, obj2b, obj3b = 1, 2, 3
obj1a, obj2a, obj3a = obj1b + 1, obj2b + 2, obj3b + 3
return obj1a, obj1b, obj2a, obj2b, obj3a, obj3b
__doc__ = """
>>> f()
"""
def f():
pass
__doc__ = """
>>> x
(1, 2)
"""
x = 1,
x = 1, 2,
__doc__ = """
>>> f = Fiche()
>>> f[0] = 1
>>> f.geti()
1
>>> f[1] = None
>>> f.geti()
0
>>> f[0] = 1
>>> f.geti()
1
"""
cdef class Fiche:
cdef int i
def __setitem__(self, element, valeur):
self.i = 0
if valeur is None:
return
self.i = 1
def geti(self):
return self.i
__doc__ = """
>>> f(1, 'test')
<BLANKLINE>
1
1 test
1 test 42 spam
"""
def f(a, b):
print
print a
print a, b
print a, b,
print 42, "spam"
\ No newline at end of file
def f():
obj1 = 1
obj2 = 2
obj3 = 3
__doc__ = """
>>> f(1,2,3)
3
>>> g(1,2,3)
1
>>> h(1,2,3)
2
>>> j(1,2,3)
16
>>> k(1,2,3)
0
>>> l(1,2,3)
16
"""
def f(obj1, obj2, obj3):
obj1 = obj2 | obj3
return obj1
def g(obj1, obj2, obj3):
obj1 = obj2 ^ obj3
return obj1
def h(obj1, obj2, obj3):
obj1 = obj2 & obj3
return obj1
def j(obj1, obj2, obj3):
obj1 = obj2 << obj3
return obj1
def k(obj1, obj2, obj3):
obj1 = obj2 >> obj3
return obj1
def l(obj1, obj2, obj3):
obj1 = obj2 << obj3 | obj2 >> obj3
\ No newline at end of file
return obj1
__doc__ = """
>>> test()
This parrot is resting.
Lovely plumage!
"""
cdef class Parrot:
cdef void describe(self):
......@@ -11,8 +18,9 @@ cdef class Norwegian(Parrot):
cdef void describe(self):
print "Lovely plumage!"
cdef Parrot p1, p2
p1 = Parrot()
p2 = Norwegian()
p1.describe()
p2.describe()
def test():
cdef Parrot p1, p2
p1 = Parrot()
p2 = Norwegian()
p1.describe()
p2.describe()
__doc__ = """
>>> b = Bicycle()
>>> b.fall_off()
Falling off extremely hard
>>> b.fall_off("somewhat")
Falling off somewhat hard
"""
class Bicycle:
def fall_off(self, how_hard = "extremely"):
......
__doc__ = """
>>> boolExpressionsFail()
'Not 2b'
"""
def boolExpressionsFail():
dict = {1: 1}
if not dict.has_key("2b"):
......
__doc__ = """
>>> test(0)
0L
>>> test(1)
1L
>>> 2**36
68719476736L
>>> test(2**36)
0L
>>> test(2L**36)
0L
"""
def test(k):
cdef unsigned long m
m = k
......
__doc__ = """
>>> b = B()
>>> b.t
{1: ((1, 2, 3),), 2: (1, 2, 3)}
"""
class B:
def __init__(self):
self.t = {
......
__doc__ = """
>>> z(1,9.2,'test')
>>> failtype()
Traceback (most recent call last):
TypeError: an integer is required
>>> fail0(1,2)
Traceback (most recent call last):
TypeError: function takes exactly 2 arguments (0 given)
>>> fail1(1,2)
Traceback (most recent call last):
TypeError: function takes exactly 2 arguments (1 given)
"""
def f(x, y):
x = y
......@@ -8,11 +23,16 @@ cdef h(int i, obj):
i = obj
def z(a, b, c):
f()
f(a)
f(a, b)
f(a, b,)
g(1, 2.0, "spam")
g(a, b, c)
def fail0(a, b):
f()
def fail1(a, b):
f(a)
def failtype():
h(42, "eggs")
\ No newline at end of file
__doc__ = """
>>> l = [1,2,3,4]
>>> f(1, l, 2, 3)
[1, 2, 3, 4]
>>> l == f(1, l, 2, 3)
True
>>> l is f(1, l, 2, 3)
False
>>> g(1, [1,2,3,4], 2, 3)
[3, 4]
>>> h(1, [1,2,3,4], 2, 3)
[1, 2, 3]
>>> j(1, [1,2,3,4], 2, 3)
[3]
"""
def f(obj1, obj2, obj3, obj4):
obj1 = obj2[:]
return obj1
def g(obj1, obj2, obj3, obj4):
obj1 = obj2[obj3:]
return obj1
def h(obj1, obj2, obj3, obj4):
obj1 = obj2[:obj4]
return obj1
def j(obj1, obj2, obj3, obj4):
obj1 = obj2[obj3:obj4]
return obj1
__doc__ = """
>>> c = C()
>>> print c.x
foo
"""
class C:
x = "foo"
__doc__ = """
>>> f(1,2,3,4,5)
()
>>> g(1,2,3,4,5)
(2,)
>>> h(1,2,3,4,5)
(2, 3)
>>> j(1,2,3,4,5)
(2, 3, 4)
>>> k(1,2,3,4,5)
(2, 3, 4)
>>> l(1,2,3,4,5)
(17, 42, 88)
"""
def f(obj1, obj2, obj3, obj4, obj5):
obj1 = ()
return obj1
def g(obj1, obj2, obj3, obj4, obj5):
obj1 = ()
obj1 = (obj2,)
return obj1
def h(obj1, obj2, obj3, obj4, obj5):
obj1 = ()
obj1 = (obj2,)
obj1 = obj2, obj3
return obj1
def j(obj1, obj2, obj3, obj4, obj5):
obj1 = ()
obj1 = (obj2,)
obj1 = obj2, obj3
obj1 = (obj2, obj3, obj4)
return obj1
def k(obj1, obj2, obj3, obj4, obj5):
obj1 = ()
obj1 = (obj2,)
obj1 = obj2, obj3
obj1 = (obj2, obj3, obj4)
obj1 = (obj2, obj3, obj4,)
return obj1
def l(obj1, obj2, obj3, obj4, obj5):
obj1 = ()
obj1 = (obj2,)
obj1 = obj2, obj3
obj1 = (obj2, obj3, obj4)
obj1 = (obj2, obj3, obj4,)
obj1 = 17, 42, 88
return obj1
__doc__ = """
>>> f(1, 2, 3)
(-3, -4, 1)
"""
def f(obj1, obj2, obj3):
cdef int bool1, bool2
cdef int int1, int2
cdef char *str1
int2 = obj3
str1 = NULL
bool2 = 0
bool1 = not bool2
obj1 = not obj2
bool1 = not str1
......@@ -11,4 +21,4 @@ def f(obj1, obj2, obj3):
obj1 = -obj2
int1 = ~int2
obj1 = ~obj2
\ No newline at end of file
return obj1, int1, bool1
__doc__ = """
>>> f(1, (2,), (3,4,5), (6,(7,(8,9))), 0)
(8, 9, (8, 9), (6, (7, (8, 9))), 0)
"""
def f(obj1, obj2, obj3, obj4, obj5):
obj1, = obj2
obj1, = obj2 + obj3
obj1, obj2 = obj2 + obj2
obj1, obj2, obj3 = obj3
obj1, (obj2, obj3) = obj4
[obj1, obj2] = obj3
\ No newline at end of file
return obj1, obj2, obj3, obj4, obj5
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