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

Function test_iter_broadcasting_errors

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

Source from the content-addressed store, hash-verified

636 i = np.nditer([np.ones(2), None, None], itershape=(2,))
637
638def test_iter_broadcasting_errors():
639 # Check that errors are thrown for bad broadcasting shapes
640
641 # 1D with 1D
642 assert_raises(ValueError, nditer, [arange(2), arange(3)],
643 [], [['readonly']]*2)
644 # 2D with 1D
645 assert_raises(ValueError, nditer,
646 [arange(6).reshape(2, 3), arange(2)],
647 [], [['readonly']]*2)
648 # 2D with 2D
649 assert_raises(ValueError, nditer,
650 [arange(6).reshape(2, 3), arange(9).reshape(3, 3)],
651 [], [['readonly']]*2)
652 assert_raises(ValueError, nditer,
653 [arange(6).reshape(2, 3), arange(4).reshape(2, 2)],
654 [], [['readonly']]*2)
655 # 3D with 3D
656 assert_raises(ValueError, nditer,
657 [arange(36).reshape(3, 3, 4), arange(24).reshape(2, 3, 4)],
658 [], [['readonly']]*2)
659 assert_raises(ValueError, nditer,
660 [arange(8).reshape(2, 4, 1), arange(24).reshape(2, 3, 4)],
661 [], [['readonly']]*2)
662
663 # Verify that the error message mentions the right shapes
664 try:
665 nditer([arange(2).reshape(1, 2, 1),
666 arange(3).reshape(1, 3),
667 arange(6).reshape(2, 3)],
668 [],
669 [['readonly'], ['readonly'], ['writeonly', 'no_broadcast']])
670 raise AssertionError('Should have raised a broadcast error')
671 except ValueError as e:
672 msg = str(e)
673 # The message should contain the shape of the 3rd operand
674 assert_(msg.find('(2,3)') >= 0,
675 'Message "%s" doesn\'t contain operand shape (2,3)' % msg)
676 # The message should contain the broadcast shape
677 assert_(msg.find('(1,2,3)') >= 0,
678 'Message "%s" doesn\'t contain broadcast shape (1,2,3)' % msg)
679
680 try:
681 nditer([arange(6).reshape(2, 3), arange(2)],
682 [],
683 [['readonly'], ['readonly']],
684 op_axes=[[0, 1], [0, np.newaxis]],
685 itershape=(4, 3))
686 raise AssertionError('Should have raised a broadcast error')
687 except ValueError as e:
688 msg = str(e)
689 # The message should contain "shape->remappedshape" for each operand
690 assert_(msg.find('(2,3)->(2,3)') >= 0,
691 'Message "%s" doesn\'t contain operand shape (2,3)->(2,3)' % msg)
692 assert_(msg.find('(2,)->(2,newaxis)') >= 0,
693 ('Message "%s" doesn\'t contain remapped operand shape' +
694 '(2,)->(2,newaxis)') % msg)
695 # The message should contain the itershape parameter

Callers

nothing calls this directly

Calls 5

assert_raisesFunction · 0.90
arangeFunction · 0.90
assert_Function · 0.90
reshapeMethod · 0.80
findMethod · 0.80

Tested by

no test coverage detected