Repository navigation
Expand file tree
/
Copy pathtest_dummyarray.py
More file actions
154 lines (132 loc) · 5.44 KB
/
Copy pathtest_dummyarray.py
File metadata and controls
154 lines (132 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from __future__ import print_function
import numba.unittest_support as unittest
import itertools
import numpy as np
from numba.dummyarray import Array
class TestSlicing(unittest.TestCase):
def assertSameContig(self, arr, nparr):
attrs = 'C_CONTIGUOUS', 'F_CONTIGUOUS'
for attr in attrs:
if arr.flags[attr] != nparr.flags[attr]:
if arr.size == 0 and nparr.size == 0:
# numpy <=1.7 bug that some empty array are contiguous and
# some are not
pass
else:
self.fail("contiguous flag mismatch:\ngot=%s\nexpect=%s" %
(arr.flags, nparr.flags))
#### 1D
def test_slice0_1d(self):
nparr = np.empty(4)
arr = Array.from_desc(0, nparr.shape, nparr.strides,
nparr.dtype.itemsize)
self.assertSameContig(arr, nparr)
xx = -2, -1, 0, 1, 2
for x in xx:
expect = nparr[x:]
got = arr[x:]
self.assertSameContig(got, expect)
self.assertEqual(got.shape, expect.shape)
self.assertEqual(got.strides, expect.strides)
def test_slice1_1d(self):
nparr = np.empty(4)
arr = Array.from_desc(0, nparr.shape, nparr.strides,
nparr.dtype.itemsize)
xx = -2, -1, 0, 1, 2
for x in xx:
expect = nparr[:x]
got = arr[:x]
self.assertSameContig(got, expect)
self.assertEqual(got.shape, expect.shape)
self.assertEqual(got.strides, expect.strides)
def test_slice2_1d(self):
nparr = np.empty(4)
arr = Array.from_desc(0, nparr.shape, nparr.strides,
nparr.dtype.itemsize)
xx = -2, -1, 0, 1, 2
for x, y in itertools.product(xx, xx):
expect = nparr[x:y]
got = arr[x:y]
self.assertSameContig(got, expect)
self.assertEqual(got.shape, expect.shape)
self.assertEqual(got.strides, expect.strides)
#### 2D
def test_slice0_2d(self):
nparr = np.empty((4, 5))
arr = Array.from_desc(0, nparr.shape, nparr.strides,
nparr.dtype.itemsize)
xx = -2, -1, 0, 1, 2
for x in xx:
expect = nparr[x:]
got = arr[x:]
self.assertSameContig(got, expect)
self.assertEqual(got.shape, expect.shape)
self.assertEqual(got.strides, expect.strides)
for x, y in itertools.product(xx, xx):
expect = nparr[x:, y:]
got = arr[x:, y:]
self.assertSameContig(got, expect)
self.assertEqual(got.shape, expect.shape)
self.assertEqual(got.strides, expect.strides)
def test_slice1_2d(self):
nparr = np.empty((4, 5))
arr = Array.from_desc(0, nparr.shape, nparr.strides,
nparr.dtype.itemsize)
xx = -2, -1, 0, 1, 2
for x in xx:
expect = nparr[:x]
got = arr[:x]
self.assertEqual(got.shape, expect.shape)
self.assertEqual(got.strides, expect.strides)
self.assertSameContig(got, expect)
for x, y in itertools.product(xx, xx):
expect = nparr[:x, :y]
got = arr[:x, :y]
self.assertEqual(got.shape, expect.shape)
self.assertEqual(got.strides, expect.strides)
self.assertSameContig(got, expect)
def test_slice2_2d(self):
nparr = np.empty((4, 5))
arr = Array.from_desc(0, nparr.shape, nparr.strides,
nparr.dtype.itemsize)
xx = -2, -1, 0, 1, 2
for s, t, u, v in itertools.product(xx, xx, xx, xx):
expect = nparr[s:t, u:v]
got = arr[s:t, u:v]
self.assertSameContig(got, expect)
self.assertEqual(got.shape, expect.shape)
self.assertEqual(got.strides, expect.strides)
for x, y in itertools.product(xx, xx):
expect = nparr[s:t, u:v]
got = arr[s:t, u:v]
self.assertSameContig(got, expect)
self.assertEqual(got.shape, expect.shape)
self.assertEqual(got.strides, expect.strides)
class TestExtent(unittest.TestCase):
def test_extent_1d(self):
nparr = np.empty(4)
arr = Array.from_desc(0, nparr.shape, nparr.strides,
nparr.dtype.itemsize)
s, e = arr.extent
self.assertEqual(e - s, nparr.size * nparr.dtype.itemsize)
def test_extent_2d(self):
nparr = np.empty((4, 5))
arr = Array.from_desc(0, nparr.shape, nparr.strides,
nparr.dtype.itemsize)
s, e = arr.extent
self.assertEqual(e - s, nparr.size * nparr.dtype.itemsize)
def test_extent_iter_1d(self):
nparr = np.empty(4)
arr = Array.from_desc(0, nparr.shape, nparr.strides,
nparr.dtype.itemsize)
[ext] = list(arr.iter_contiguous_extent())
self.assertEqual(ext, arr.extent)
def test_extent_iter_2d(self):
nparr = np.empty((4, 5))
arr = Array.from_desc(0, nparr.shape, nparr.strides,
nparr.dtype.itemsize)
[ext] = list(arr.iter_contiguous_extent())
self.assertEqual(ext, arr.extent)
self.assertEqual(len(list(arr[::2].iter_contiguous_extent())), 2)
if __name__ == '__main__':
unittest.main()