Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
5beb1f90
Commit
5beb1f90
authored
May 13, 2011
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tests for reversed() iteration
parent
f27a5ba2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
185 additions
and
0 deletions
+185
-0
tests/run/reversed_iteration.pyx
tests/run/reversed_iteration.pyx
+185
-0
No files found.
tests/run/reversed_iteration.pyx
0 → 100644
View file @
5beb1f90
cimport
cython
def
_reversed
(
it
):
return
list
(
it
)[::
-
1
]
def
reversed_list
(
list
l
):
"""
>>> [ i for i in _reversed([1,2,3,4]) ]
[4, 3, 2, 1]
>>> reversed_list([1,2,3,4])
[4, 3, 2, 1]
"""
result
=
[]
for
item
in
reversed
(
l
):
result
.
append
(
item
)
return
result
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
def
reversed_range
(
int
N
):
"""
>>> i = 99
>>> [ i for i in _reversed(range(5)) ], i
([4, 3, 2, 1, 0], 0)
>>> reversed_range(5)
([4, 3, 2, 1, 0], 0)
>>> i = 99
>>> ([ i for i in _reversed(range(0)) ], i)
([], 99)
>>> reversed_range(0)
([], 99)
"""
cdef
int
i
=
99
result
=
[]
for
i
in
reversed
(
range
(
N
)):
result
.
append
(
i
)
return
result
,
i
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
def
reversed_range_step_pos
(
int
a
,
int
b
):
"""
>>> [ i for i in _reversed(range(0, 5, 1)) ]
[4, 3, 2, 1, 0]
>>> reversed_range_step_pos(0, 5)
([4, 3, 2, 1, 0], 0)
>>> [ i for i in _reversed(range(5, 0, 1)) ]
[]
>>> reversed_range_step_pos(5, 0)
([], 99)
"""
cdef
int
i
=
99
result
=
[]
for
i
in
reversed
(
range
(
a
,
b
,
1
)):
result
.
append
(
i
)
return
result
,
i
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
def
reversed_range_step_neg
(
int
a
,
int
b
):
"""
>>> [ i for i in _reversed(range(5, -1, -1)) ]
[0, 1, 2, 3, 4, 5]
>>> reversed_range_step_neg(5, -1)
([0, 1, 2, 3, 4, 5], 5)
>>> [ i for i in _reversed(range(0, 5, -1)) ]
[]
>>> reversed_range_step_neg(0, 5)
([], 99)
"""
cdef
int
i
=
99
result
=
[]
for
i
in
reversed
(
range
(
a
,
b
,
-
1
)):
result
.
append
(
i
)
return
result
,
i
unicode_string
=
u"abcDEF"
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
def
reversed_unicode
(
unicode
u
):
"""
>>> print(''.join(_reversed(unicode_string)))
FEDcba
>>> print(''.join(reversed_unicode(unicode_string)))
FEDcba
"""
result
=
[]
for
c
in
reversed
(
u
):
result
.
append
(
c
)
return
result
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
def
reversed_unicode_slice
(
unicode
u
):
"""
>>> print(''.join(_reversed(unicode_string[1:-2])))
Dcb
>>> print(''.join(reversed_unicode_slice(unicode_string)))
Dcb
"""
result
=
[]
for
c
in
reversed
(
u
[
1
:
-
2
]):
result
.
append
(
c
)
return
result
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
def
reversed_unicode_slice_step
(
unicode
u
):
"""
>>> print(''.join(_reversed(unicode_string[-2:1:-1])))
cDE
>>> print(''.join(reversed_unicode_slice_step(unicode_string)))
cDE
"""
result
=
[]
for
c
in
reversed
(
u
[
-
2
:
1
:
-
1
]):
result
.
append
(
c
)
return
result
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
def
reversed_unicode_slice_step_only
(
unicode
u
):
"""
>>> print(''.join(_reversed(unicode_string[::-1])))
abcDEF
>>> print(''.join(reversed_unicode_slice_step_only(unicode_string)))
abcDEF
"""
result
=
[]
for
c
in
reversed
(
u
[::
-
1
]):
result
.
append
(
c
)
return
result
bytes_string
=
b'abcDEF'
join_bytes
=
b''
.
join
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
def
reversed_bytes
(
bytes
s
):
"""
>>> print(join_bytes(_reversed(bytes_string)).decode('ASCII'))
FEDcba
>>> print(join_bytes(reversed_bytes(bytes_string)).decode('ASCII'))
FEDcba
"""
result
=
[]
for
c
in
reversed
(
s
):
result
.
append
(
c
)
return
result
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
def
reversed_bytes_slice
(
bytes
s
):
"""
>>> print(join_bytes(_reversed(bytes_string[1:-2])).decode('ASCII'))
Dcb
>>> print(join_bytes(reversed_bytes_slice(bytes_string)).decode('ASCII'))
Dcb
"""
result
=
[]
for
c
in
reversed
(
s
[
1
:
-
2
]):
result
.
append
(
c
)
return
result
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
def
reversed_bytes_slice_step
(
bytes
s
):
"""
>>> print(join_bytes(_reversed(bytes_string[-2:1:-1])).decode('ASCII'))
cDE
>>> print(join_bytes(reversed_bytes_slice_step(bytes_string)).decode('ASCII'))
cDE
"""
result
=
[]
for
c
in
reversed
(
s
[
-
2
:
1
:
-
1
]):
result
.
append
(
c
)
return
result
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
def
reversed_bytes_slice_step_only
(
bytes
s
):
"""
>>> print(join_bytes(_reversed(bytes_string[::-1])).decode('ASCII'))
abcDEF
>>> print(join_bytes(reversed_bytes_slice_step_only(bytes_string)).decode('ASCII'))
abcDEF
"""
result
=
[]
for
c
in
reversed
(
s
[::
-
1
]):
result
.
append
(
c
)
return
result
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment