moved doctests to individual functions.

parent 8a4faf57
# tag: array # tag: array
__doc__ = u"""
>>> len(a)
3
>>> test_len(a)
3L
>>> test_copy(a)
array('f', [1.0, 2.0, 3.0])
>>> a[2]
3.5
>>> test_fast_access(a)
>>> test_new_zero(a)
array('f', [0.0, 0.0, 0.0])
>>> test_set_zero(a)
array('f', [0.0, 0.0, 0.0])
>>> test_resize(a)
>> test_likes(a)
array('f', [0.0, 0.0, 0.0])
>>> test_view()
>>> test_extend()
>>> test_extend_buffer()
array('c', 'abcdefghij')
"""
import array # Python builtin module import array # Python builtin module
from cpython cimport array # array.pxd / arrayarray.h from cpython cimport array # array.pxd / arrayarray.h
...@@ -42,10 +6,24 @@ from cpython cimport array # array.pxd / arrayarray.h ...@@ -42,10 +6,24 @@ from cpython cimport array # array.pxd / arrayarray.h
a = array.array('f', [1.0, 2.0, 3.0]) a = array.array('f', [1.0, 2.0, 3.0])
def test_len(a): def test_len(a):
"""
>>> a = array.array('f', [1.0, 2.0, 3.0])
>>> len(a)
3
>>> test_len(a)
3L
>>> assert len(a) == test_len(a)
"""
cdef array.array ca = a # for C-fast array usage cdef array.array ca = a # for C-fast array usage
return ca.length return ca.length
def test_copy(a): def test_copy(a):
"""
>>> a = array.array('f', [1.0, 2.0, 3.0])
>>> test_copy(a)
array('f', [1.0, 2.0, 3.0])
>>> assert a == test_copy(a)
"""
cdef array.array ca = a cdef array.array ca = a
cdef array.array b cdef array.array b
b = array.copy(a) b = array.copy(a)
...@@ -55,6 +33,14 @@ def test_copy(a): ...@@ -55,6 +33,14 @@ def test_copy(a):
def test_fast_access(a): def test_fast_access(a):
"""
>>> a = array.array('f', [1.0, 2.0, 3.0])
>>> a[2]
3.5
>>> test_fast_access(a)
"""
cdef array.array ca = a cdef array.array ca = a
assert ca._f[1] == 2.0, ca._f[1] assert ca._f[1] == 2.0, ca._f[1]
...@@ -65,20 +51,33 @@ def test_fast_access(a): ...@@ -65,20 +51,33 @@ def test_fast_access(a):
def test_new_zero(a): def test_new_zero(a):
"""
>>> a = array.array('f', [1.0, 2.0, 3.0])
>>> test_new_zero(a)
array('f', [0.0, 0.0, 0.0])
"""
cdef array.array cb = array.zeros_like(a) cdef array.array cb = array.zeros_like(a)
assert cb.length == len(a) assert cb.length == len(a)
return cb return cb
def test_set_zero(a): def test_set_zero(a):
"""
>>> a = array.array('f', [1.0, 2.0, 3.0])
>>> test_set_zero(a)
array('f', [0.0, 0.0, 0.0])
"""
cdef array.array cb = array.copy(a) cdef array.array cb = array.copy(a)
array.zero(cb) array.zero(cb)
assert a[1] != 0.0, a assert a[1] != 0.0, a
assert cb[1] == 0.0, cb assert cb[1] == 0.0, cb
return cb return cb
def test_resize(a): def test_resize(a):
"""
>>> a = array.array('f', [1.0, 2.0, 3.0])
>>> test_resize(a)
"""
cdef array.array cb = array.copy(a) cdef array.array cb = array.copy(a)
array.resize(cb, 10) array.resize(cb, 10)
for i in range(10): for i in range(10):
...@@ -86,27 +85,43 @@ def test_resize(a): ...@@ -86,27 +85,43 @@ def test_resize(a):
assert cb.length == 10 assert cb.length == 10
assert cb[9] == cb[-1] == cb._f[9] == 9 assert cb[9] == cb[-1] == cb._f[9] == 9
def test_view(): def test_view():
"""
>>> a = array.array('f', [1.0, 2.0, 3.0])
>>> test_view()
"""
a = array.array('i', [1, 2, 3]) a = array.array('i', [1, 2, 3])
cdef array.array[int] ca = a cdef object[int] ca = a
assert ca._i[0] == 1 assert ca[0] == 1
assert ca._i[2] == 3 assert ca[2] == 3
def test_extend(): def test_extend():
"""
>>> a = array.array('f', [1.0, 2.0, 3.0])
>>> test_extend()
"""
cdef array.array ca = array.array('i', [1, 2, 3]) cdef array.array ca = array.array('i', [1, 2, 3])
cdef array.array cb = array.array('i', range(4, 6)) cdef array.array cb = array.array('i', range(4, 6))
array.extend(ca, cb) array.extend(ca, cb)
assert list(ca) == range(1, 6), list(ca) assert list(ca) == range(1, 6), list(ca)
def test_likes(a): def test_likes(a):
"""
>>> a = array.array('f', [1.0, 2.0, 3.0])
>>> test_likes(a)
array('f', [0.0, 0.0, 0.0])
"""
cdef array.array z = array.zeros_like(a) cdef array.array z = array.zeros_like(a)
cdef array.array e = array.empty_like(a) cdef array.array e = array.empty_like(a)
assert e.length == len(a) assert e.length == len(a)
return z return z
def test_extend_buffer(): def test_extend_buffer():
"""
>>> a = array.array('f', [1.0, 2.0, 3.0])
>>> test_extend_buffer()
array('c', 'abcdefghij')
"""
cdef array.array ca = array.array('c', "abcdef") cdef array.array ca = array.array('c', "abcdef")
cdef char* s = "ghij" cdef char* s = "ghij"
array.extend_buffer(ca, s, len(s)) # or use stdlib.strlen array.extend_buffer(ca, s, len(s)) # or use stdlib.strlen
......
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