MCPcopy Create free account
hub / github.com/numpy/numpy / test_0d_iter

Function test_0d_iter

numpy/core/tests/test_nditer.py:2963–2999  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2961
2962
2963def test_0d_iter():
2964 # Basic test for iteration of 0-d arrays:
2965 i = nditer([2, 3], ['multi_index'], [['readonly']]*2)
2966 assert_equal(i.ndim, 0)
2967 assert_equal(next(i), (2, 3))
2968 assert_equal(i.multi_index, ())
2969 assert_equal(i.iterindex, 0)
2970 assert_raises(StopIteration, next, i)
2971 # test reset:
2972 i.reset()
2973 assert_equal(next(i), (2, 3))
2974 assert_raises(StopIteration, next, i)
2975
2976 # test forcing to 0-d
2977 i = nditer(np.arange(5), ['multi_index'], [['readonly']], op_axes=[()])
2978 assert_equal(i.ndim, 0)
2979 assert_equal(len(i), 1)
2980
2981 i = nditer(np.arange(5), ['multi_index'], [['readonly']],
2982 op_axes=[()], itershape=())
2983 assert_equal(i.ndim, 0)
2984 assert_equal(len(i), 1)
2985
2986 # passing an itershape alone is not enough, the op_axes are also needed
2987 with assert_raises(ValueError):
2988 nditer(np.arange(5), ['multi_index'], [['readonly']], itershape=())
2989
2990 # Test a more complex buffered casting case (same as another test above)
2991 sdt = [('a', 'f4'), ('b', 'i8'), ('c', 'c8', (2, 3)), ('d', 'O')]
2992 a = np.array(0.5, dtype='f4')
2993 i = nditer(a, ['buffered', 'refs_ok'], ['readonly'],
2994 casting='unsafe', op_dtypes=sdt)
2995 vals = next(i)
2996 assert_equal(vals['a'], 0.5)
2997 assert_equal(vals['b'], 0)
2998 assert_equal(vals['c'], [[(0.5)]*3]*2)
2999 assert_equal(vals['d'], 0.5)
3000
3001def test_object_iter_cleanup():
3002 # see gh-18450

Callers

nothing calls this directly

Calls 3

assert_equalFunction · 0.90
assert_raisesFunction · 0.90
nextFunction · 0.50

Tested by

no test coverage detected