diff options
author | Max Sperlich <max.sperlich@livingsocial.com> | 2013-12-08 20:57:23 -0500 |
---|---|---|
committer | Max Sperlich <max.sperlich@livingsocial.com> | 2013-12-08 20:57:23 -0500 |
commit | aa2443af6e6de74edff19757ba1c827b69f7640f (patch) | |
tree | 164edf04c50c9990594ed653a68832506403032d /numpy/lib/tests/test_format.py | |
parent | faf4131f6b264477e4047257440eebdf27c2f767 (diff) | |
download | numpy-aa2443af6e6de74edff19757ba1c827b69f7640f.tar.gz |
TST: Adds test case for Issue 4093
Diffstat (limited to 'numpy/lib/tests/test_format.py')
-rw-r--r-- | numpy/lib/tests/test_format.py | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py index deec2e4eb..f7df8b11b 100644 --- a/numpy/lib/tests/test_format.py +++ b/numpy/lib/tests/test_format.py @@ -325,7 +325,7 @@ basic_arrays = [] for scalar in scalars: for endian in '<>': dtype = np.dtype(scalar).newbyteorder(endian) - basic = np.arange(15).astype(dtype) + basic = np.arange(1500).astype(dtype) basic_arrays.extend([ # Empty np.array([], dtype=dtype), @@ -334,11 +334,11 @@ for scalar in scalars: # 1-D basic, # 2-D C-contiguous - basic.reshape((3, 5)), + basic.reshape((30, 50)), # 2-D F-contiguous - basic.reshape((3, 5)).T, + basic.reshape((30, 50)).T, # 2-D non-contiguous - basic.reshape((3, 5))[::-1, ::2], + basic.reshape((30, 50))[::-1, ::2], ]) # More complicated record arrays. @@ -410,6 +410,14 @@ record_arrays = [ np.array(NbufferT, dtype=np.dtype(Ndescr).newbyteorder('>')), ] +#BytesIO that returns fewer bytes than requested +class BytesIORandomSize(BytesIO): + def read(self, size=None): + from random import randint + size = randint(min(512,size), size) + data = super(BytesIORandomSize, self).read(size) + assert len(data) <= size + return data def roundtrip(arr): f = BytesIO() @@ -418,6 +426,21 @@ def roundtrip(arr): arr2 = format.read_array(f2) return arr2 +def roundtrip_randsize(arr): + f = BytesIO() + format.write_array(f, arr) + f2 = BytesIORandomSize(f.getvalue()) + arr2 = format.read_array(f2) + return arr2 + +def roundtrip_truncated(arr): + f = BytesIO() + format.write_array(f, arr) + #BytesIO is one byte short + f2 = BytesIO(f.getvalue()[0:-1]) + arr2 = format.read_array(f2) + return arr2 + def assert_equal(o1, o2): assert_(o1 == o2) @@ -428,6 +451,16 @@ def test_roundtrip(): arr2 = roundtrip(arr) yield assert_array_equal, arr, arr2 +def test_roundtrip_randsize(): + for arr in basic_arrays[2:] + record_arrays: + arr2 = roundtrip_randsize(arr) + yield assert_array_equal, arr, arr2 + +def test_roundtrip_truncated(): + for arr in basic_arrays: + if arr.dtype != object: + assert_raises(ValueError, roundtrip_truncated, arr) + def test_long_str(): # check items larger than internal buffer size, gh-4027 long_str_arr = np.ones(1, dtype=np.dtype((str, format.BUFFER_SIZE + 1))) |