Debian Codegraph @

pypy/module/micronumpy/test/test_iterators.py

raw symbols SCIP
1 from pypy.module.micronumpy import support
2 from pypy.module.micronumpy.iterators import ArrayIter
3 from pypy.module.micronumpy.strides import is_c_contiguous, is_f_contiguous
4 from pypy.module.micronumpy import constants as NPY
5
6
7 class MockArray(object):
8 flags = 0
9
10 class dtype:
11 elsize = 1
12
13 def __init__(self, shape, strides, start=0):
14 self.shape = shape
15 self.strides = strides
16 self.start = start
17 if is_c_contiguous(self):
18 self.flags |= NPY.ARRAY_C_CONTIGUOUS
19 if is_f_contiguous(self):
20 self.flags |= NPY.ARRAY_F_CONTIGUOUS
21
22 def get_shape(self):
23 return self.shape
24
25 def get_strides(self):
26 return self.strides
27
28 class TestIterDirect(object):
29 def test_iterator_basic(self):
30 #Let's get started, simple iteration in C order with
31 #contiguous layout => strides[-1] is 1
32 shape = [3, 5]
33 strides = [5, 1]
34 backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
35 assert backstrides == [10, 4]
36 i = ArrayIter(MockArray(shape, strides), support.product(shape), shape,
37 strides, backstrides)
38 assert i.contiguous
39 s = i.reset()
40 s = i.next(s)
41 s = i.next(s)
42 s = i.next(s)
43 assert s.offset == 3
44 assert not i.done(s)
45 assert s._indices == [0,0]
46 assert i.indices(s) == [0,3]
47 #cause a dimension overflow
48 s = i.next(s)
49 s = i.next(s)
50 assert s.offset == 5
51 assert s._indices == [0,3]
52 assert i.indices(s) == [1,0]
53
54 #Now what happens if the array is transposed? strides[-1] != 1
55 # therefore layout is non-contiguous
56 strides = [1, 3]
57 backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
58 assert backstrides == [2, 12]
59 i = ArrayIter(MockArray(shape, strides), support.product(shape), shape,
60 strides, backstrides)
61 assert not i.contiguous
62 s = i.reset()
63 s = i.next(s)
64 s = i.next(s)
65 s = i.next(s)
66 assert s.offset == 9
67 assert not i.done(s)
68 assert s._indices == [0,3]
69 #cause a dimension overflow
70 s = i.next(s)
71 s = i.next(s)
72 assert s.offset == 1
73 assert s._indices == [1,0]
74
75 def test_one_in_shape(self):
76 strides = [16, 4, 8]
77 shape = [3, 4, 1]
78 backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
79 assert backstrides == [32, 12, 0]
80 i = ArrayIter(MockArray(shape, strides), support.product(shape), shape,
81 strides, backstrides)
82 assert not i.contiguous
83 s = i.reset()
84 for j in range(3):
85 s = i.next(s)
86 assert s.offset == 12
87 assert not i.done(s)
88 assert s._indices == [0, 3, 0]
89 while not i.done(s):
90 old_indices = s._indices[:]
91 old_offset = s.offset
92 s = i.next(s)
93 assert s.offset == 0
94 assert s._indices == [0, 0, 0]
95 assert old_indices == [2, 3, 0]
96 assert old_offset == 44
97
98 def test_iterator_goto(self):
99 shape = [3, 5]
100 strides = [1, 3]
101 backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
102 assert backstrides == [2, 12]
103 a = MockArray(shape, strides, 42)
104 i = ArrayIter(a, support.product(shape), shape,
105 strides, backstrides)
106 assert not i.contiguous
107 s = i.reset()
108 assert s.index == 0
109 assert s._indices == [0, 0]
110 assert s.offset == a.start
111 s = i.goto(11)
112 assert s.index == 11
113 assert s._indices is None
114 assert s.offset == a.start + 5