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
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
ed913db0
Commit
ed913db0
authored
Sep 09, 2010
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tests for pointer slicing.
parent
15e5567f
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
0 deletions
+67
-0
tests/run/slice_ptr.pyx
tests/run/slice_ptr.pyx
+67
-0
No files found.
tests/run/slice_ptr.pyx
0 → 100644
View file @
ed913db0
from
libc.stdlib
cimport
malloc
,
free
from
cpython.object
cimport
Py_EQ
,
Py_NE
def
double_ptr_slice
(
x
,
L
,
int
a
,
int
b
):
"""
>>> L = range(10)
>>> double_ptr_slice(5, L, 0, 10)
>>> double_ptr_slice(6, L, 0, 10)
>>> double_ptr_slice(None, L, 0, 10)
>>> double_ptr_slice(0, L, 3, 7)
>>> double_ptr_slice(5, L, 3, 7)
>>> double_ptr_slice(9, L, 3, 7)
>>> double_ptr_slice(EqualsEvens(), L, 0, 10)
>>> double_ptr_slice(EqualsEvens(), L, 1, 10)
"""
cdef
double
*
L_c
=
NULL
try
:
L_c
=
<
double
*>
malloc
(
len
(
L
)
*
sizeof
(
double
))
for
i
,
a
in
enumerate
(
L
):
L_c
[
i
]
=
L
[
i
]
assert
(
x
in
L_c
[:
b
])
==
(
x
in
L
[:
b
])
assert
(
x
in
L_c
[
a
:
b
])
==
(
x
in
L
[
a
:
b
])
assert
(
x
in
L_c
[
a
:
b
:
2
])
==
(
x
in
L
[
a
:
b
:
2
])
finally
:
free
(
L_c
)
def
void_ptr_slice
(
py_x
,
L
,
int
a
,
int
b
):
"""
>>> L = range(10)
>>> void_ptr_slice(5, L, 0, 10)
>>> void_ptr_slice(6, L, 0, 10)
>>> void_ptr_slice(None, L, 0, 10)
>>> void_ptr_slice(0, L, 3, 7)
>>> void_ptr_slice(5, L, 3, 7)
>>> void_ptr_slice(9, L, 3, 7)
"""
# I'm using the fact that small Python ints are cached.
cdef
void
**
L_c
=
NULL
cdef
void
*
x
=
<
void
*>
py_x
try
:
L_c
=
<
void
**>
malloc
(
len
(
L
)
*
sizeof
(
void
*
))
for
i
,
a
in
enumerate
(
L
):
L_c
[
i
]
=
<
void
*>
L
[
i
]
assert
(
x
in
L_c
[:
b
])
==
(
py_x
in
L
[:
b
])
assert
(
x
in
L_c
[
a
:
b
])
==
(
py_x
in
L
[
a
:
b
])
# assert (x in L_c[a:b:2]) == (py_x in L[a:b:2])
finally
:
free
(
L_c
)
cdef
class
EqualsEvens
:
"""
>>> e = EqualsEvens()
>>> e == 2
True
>>> e == 5
False
>>> [e == k for k in range(4)]
[True, False, True, False]
"""
def
__richcmp__
(
self
,
other
,
int
op
):
if
op
==
Py_EQ
:
return
other
%
2
==
0
elif
op
==
Py_NE
:
return
other
%
2
==
1
else
:
return
False
\ No newline at end of file
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