Commit 7d9fa927 authored by Stefan Behnel's avatar Stefan Behnel

Add dedicated test file for read-only memory views.

parent 486421f6
# mode: run
# tag: readonly, const, numpy
import numpy as np
def new_array():
return np.arange(10).astype('float')
ARRAY = new_array()
cdef getmax(double[:] x):
"""Example code, should work with both
ro and rw memoryviews"""
cdef double max_val = -float('inf')
for val in x:
if val > max_val:
max_val = val
return max_val
cdef update_array(double [:] x):
"""Modifying a ro memoryview should raise an error"""
x[0] = 23.
cdef getconst(const double [:] x):
"""Should only accept ro memoryviews"""
return x[0]
def test_mmview_rw(x):
"""
>>> test_mmview_rw(ARRAY)
9.0
"""
return getmax(x)
def test_mmview_ro(x):
"""
>>> test_mmview_ro(new_array())
9.0
"""
x.setflags(write=False)
assert x.flags.writeable is False
return getmax(x)
def test_update_mmview_rw(x):
"""
>>> test_update_mmview_rw(new_array())
23.0
"""
update_array(x)
return x[0]
def test_update_mmview_ro(x):
"""
>>> test_update_mmview_ro(new_array())
0.0
"""
x.setflags(write=False)
assert x.flags.writeable is False
try:
update_array(x)
except ValueError: pass
else:
assert False, "RO error not raised!"
return getconst(x)
def test_rw_call_getmax(double[:] x):
"""
>>> test_rw_call_getmax(new_array())
23.0
"""
update_array(x)
assert getconst(x) == 23
return getmax(x)
def test_const_mmview_ro(x):
"""
>>> test_const_mmview_ro(new_array())
0.0
"""
x.setflags(write=False)
assert x.flags.writeable is False
return getconst(x)
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