Commit fd647f2b authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 2ad300e8
......@@ -20,25 +20,102 @@
from numpy import arange, dtype, int32
from wendelin.lib.xnumpy import restructure
from pytest import raises
# XXX text
def test_restructure():
a = arange(3*3, dtype=int32).reshape((3,3))
b = a[:2,:2]
# xbase returns original object from which arr was restructured.
def xbase(arr):
b = arr.base # arr -> typed view
b = b.base # -> DummyArray
b = b.base # -> origin
return b
dtxy = dtype([('x', int32), ('y', int32)])
# C order
a = arange(4*3, dtype=int32).reshape((4,3))
# 0 1 2
# 3 4 5
# 6 7 8
# 9 10 11
with raises(ValueError, match="minor-axis is not C-contiguous: stride \(-4\) < 0"):
restructure(a[:3,:2][:,::-1], dtxy)
with raises(ValueError, match="minor-axis is not C-contiguous: stride \(8\) != itemsize \(4\)"):
restructure(a[:,::2], dtxy)
with raises(ValueError, match="dtype.itemsize \(8\) != sizeof\(minor-axis\) \(12\)"):
restructure(a, dtxy)
b = a[:3,:2]
bxy = restructure(b, dtxy)
assert xbase(bxy) is b
assert bxy.dtype == dtxy
assert bxy.shape == (2,)
assert (0, 1) == (0, 1)
assert bxy.shape == (3,)
assert bxy[0]['x'] == 0
assert bxy[0]['y'] == 1
assert bxy[1]['x'] == 3
assert bxy[1]['y'] == 4
assert bxy[2]['x'] == 6
assert bxy[2]['y'] == 7
bxy['x'][0] = 100
assert bxy[0]['x'] == 100
assert b[0,0] == 100
assert a[0,0] == 100
bxy['y'][2] = 200
assert bxy[2]['y'] == 200
assert b[2,1] == 200
assert a[2,1] == 200
# C contigous in minor; reverse in major
a = arange(4*3, dtype=int32).reshape((4,3))
# 0 1 2
# 3 4 5
# 6 7 8
# 9 10 11
b = a[:3,:2][::-1,:]
bxy = restructure(b, dtxy)
assert xbase(bxy) is b
assert bxy.dtype == dtxy
assert bxy.shape == (3,)
assert bxy[0]['x'] == 6
assert bxy[0]['y'] == 7
assert bxy[1]['x'] == 3
assert bxy[1]['y'] == 4
assert bxy[2]['x'] == 0
assert bxy[2]['y'] == 1
bxy['x'][0] = 100
assert bxy[0]['x'] == 100
assert b[0,0] == 100
assert a[2,0] == 100
bxy['y'][2] = 200
assert bxy[2]['y'] == 200
assert b[2,1] == 200
assert a[0,1] == 200
# F order
a = arange(4*3, dtype=int32).reshape((4,3), order='F')
# 0 4 8
# 1 5 9
# 2 6 10
# 3 7 11
b = a[:2,:3]
bxy = restructure(b, dtxy)
assert xbase(bxy) is b
assert bxy.dtype == dtxy
assert bxy.shape == (3,)
assert bxy[0]['x'] == 0
assert bxy[0]['y'] == 1
assert bxy[1]['x'] == 4
assert bxy[1]['y'] == 5
assert bxy[2]['x'] == 8
assert bxy[2]['y'] == 9
bxy['x'][0] = 100
assert bxy[0]['x'] == 100
assert b[0,0] == 100
assert a[0,0] == 100
bxy['y'][1] = 200
assert bxy[1]['y'] == 200
assert b[1,1] == 200
assert a[1,1] == 200
bxy['y'][2] = 200
assert bxy[2]['y'] == 200
assert b[1,2] == 200
assert a[1,2] == 200
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