(self)
| 8 | |
| 9 | class TestTake: |
| 10 | def test_simple(self): |
| 11 | a = [[1, 2], [3, 4]] |
| 12 | a_str = [[b'1', b'2'], [b'3', b'4']] |
| 13 | modes = ['raise', 'wrap', 'clip'] |
| 14 | indices = [-1, 4] |
| 15 | index_arrays = [np.empty(0, dtype=np.intp), |
| 16 | np.empty((), dtype=np.intp), |
| 17 | np.empty((1, 1), dtype=np.intp)] |
| 18 | real_indices = {'raise': {-1: 1, 4: IndexError}, |
| 19 | 'wrap': {-1: 1, 4: 0}, |
| 20 | 'clip': {-1: 0, 4: 1}} |
| 21 | # Currently all types but object, use the same function generation. |
| 22 | # So it should not be necessary to test all. However test also a non |
| 23 | # refcounted struct on top of object, which has a size that hits the |
| 24 | # default (non-specialized) path. |
| 25 | types = int, object, np.dtype([('', 'i2', 3)]) |
| 26 | for t in types: |
| 27 | # ta works, even if the array may be odd if buffer interface is used |
| 28 | ta = np.array(a if np.issubdtype(t, np.number) else a_str, dtype=t) |
| 29 | tresult = list(ta.T.copy()) |
| 30 | for index_array in index_arrays: |
| 31 | if index_array.size != 0: |
| 32 | tresult[0] = tresult[0].reshape((2,) + index_array.shape) |
| 33 | tresult[1] = tresult[1].reshape((2,) + index_array.shape) |
| 34 | for mode in modes: |
| 35 | for index in indices: |
| 36 | real_index = real_indices[mode][index] |
| 37 | if real_index is IndexError and index_array.size != 0: |
| 38 | index_array.put(0, index) |
| 39 | assert_raises(IndexError, ta.take, index_array, |
| 40 | mode=mode, axis=1) |
| 41 | elif index_array.size != 0: |
| 42 | index_array.put(0, index) |
| 43 | res = ta.take(index_array, mode=mode, axis=1) |
| 44 | assert_array_equal(res, tresult[real_index]) |
| 45 | else: |
| 46 | res = ta.take(index_array, mode=mode, axis=1) |
| 47 | assert_(res.shape == (2,) + index_array.shape) |
| 48 | |
| 49 | def test_refcounting(self): |
| 50 | objects = [object() for i in range(10)] |
nothing calls this directly
no test coverage detected