MCPcopy Index your code
hub / github.com/numpy/numpy / test_as_strided

Function test_as_strided

numpy/lib/tests/test_stride_tricks.py:362–412  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

360
361
362def test_as_strided():
363 a = np.array([None])
364 a_view = as_strided(a)
365 expected = np.array([None])
366 assert_array_equal(a_view, np.array([None]))
367
368 a = np.array([1, 2, 3, 4])
369 a_view = as_strided(a, shape=(2,), strides=(2 * a.itemsize,))
370 expected = np.array([1, 3])
371 assert_array_equal(a_view, expected)
372
373 a = np.array([1, 2, 3, 4])
374 a_view = as_strided(a, shape=(3, 4), strides=(0, 1 * a.itemsize))
375 expected = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])
376 assert_array_equal(a_view, expected)
377
378 # Regression test for gh-5081
379 dt = np.dtype([('num', 'i4'), ('obj', 'O')])
380 a = np.empty((4,), dtype=dt)
381 a['num'] = np.arange(1, 5)
382 a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize))
383 expected_num = [[1, 2, 3, 4]] * 3
384 expected_obj = [[None] * 4] * 3
385 assert_equal(a_view.dtype, dt)
386 assert_array_equal(expected_num, a_view['num'])
387 assert_array_equal(expected_obj, a_view['obj'])
388
389 # Make sure that void types without fields are kept unchanged
390 a = np.empty((4,), dtype='V4')
391 a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize))
392 assert_equal(a.dtype, a_view.dtype)
393
394 # Make sure that the only type that could fail is properly handled
395 dt = np.dtype({'names': [''], 'formats': ['V4']})
396 a = np.empty((4,), dtype=dt)
397 a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize))
398 assert_equal(a.dtype, a_view.dtype)
399
400 # Custom dtypes should not be lost (gh-9161)
401 r = [rational(i) for i in range(4)]
402 a = np.array(r, dtype=rational)
403 a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize))
404 assert_equal(a.dtype, a_view.dtype)
405 assert_array_equal([r] * 3, a_view)
406
407 # Also exercise the new-DType-API rational variant.
408 r = [rational2(i) for i in range(4)]
409 a = np.array(r, dtype=rational2)
410 a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize))
411 assert_equal(a.dtype, a_view.dtype)
412 assert_array_equal([r] * 3, a_view)
413
414
415class TestSlidingWindowView:

Callers

nothing calls this directly

Calls 4

as_stridedFunction · 0.90
assert_array_equalFunction · 0.90
assert_equalFunction · 0.90
dtypeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…