|
| 1 | +import itertools |
| 2 | + |
1 | 3 | import dpctl.tensor as dpt
|
2 | 4 | import numpy
|
3 | 5 | import pytest
|
@@ -665,6 +667,73 @@ def test_minimum_signed_integers(self, data, dtype):
|
665 | 667 | assert_array_equal(result, expected)
|
666 | 668 |
|
667 | 669 |
|
| 670 | +class TestRequire: |
| 671 | + flag_names = ["C", "C_CONTIGUOUS", "F", "F_CONTIGUOUS", "W"] |
| 672 | + |
| 673 | + def generate_all_false(self, dtype): |
| 674 | + a_np = numpy.zeros((10, 10), dtype=dtype) |
| 675 | + a_dp = dpnp.zeros((10, 10), dtype=dtype) |
| 676 | + a_np = a_np[::2, ::2] |
| 677 | + a_dp = a_dp[::2, ::2] |
| 678 | + a_np.flags["W"] = False |
| 679 | + a_dp.flags["W"] = False |
| 680 | + assert not a_dp.flags["C"] |
| 681 | + assert not a_dp.flags["F"] |
| 682 | + assert not a_dp.flags["W"] |
| 683 | + return a_np, a_dp |
| 684 | + |
| 685 | + def set_and_check_flag(self, flag, dtype, arr): |
| 686 | + if dtype is None: |
| 687 | + dtype = arr[1].dtype |
| 688 | + result = numpy.require(arr[0], dtype, [flag]) |
| 689 | + expected = dpnp.require(arr[1], dtype, [flag]) |
| 690 | + assert result.flags[flag] == expected.flags[flag] |
| 691 | + assert result.dtype == expected.dtype |
| 692 | + |
| 693 | + # a further call to dpnp.require ought to return the same array |
| 694 | + c = dpnp.require(expected, None, [flag]) |
| 695 | + assert c is expected |
| 696 | + |
| 697 | + def test_require_each(self): |
| 698 | + id = ["f4", "i4"] |
| 699 | + fd = [None, "f4", "c8"] |
| 700 | + for idtype, fdtype, flag in itertools.product(id, fd, self.flag_names): |
| 701 | + a = self.generate_all_false(idtype) |
| 702 | + self.set_and_check_flag(flag, fdtype, a) |
| 703 | + |
| 704 | + def test_unknown_requirement(self): |
| 705 | + a = self.generate_all_false("f4") |
| 706 | + assert_raises(KeyError, numpy.require, a[0], None, "Q") |
| 707 | + assert_raises(ValueError, dpnp.require, a[1], None, "Q") |
| 708 | + |
| 709 | + def test_non_array_input(self): |
| 710 | + a_np = numpy.array([1, 2, 3, 4]) |
| 711 | + a_dp = dpnp.array(a_np) |
| 712 | + expected = numpy.require(a_np, "i4", ["C", "W"]) |
| 713 | + result = dpnp.require(a_dp, "i4", ["C", "W"]) |
| 714 | + assert expected.flags["C"] == result.flags["C"] |
| 715 | + assert expected.flags["F"] == result.flags["F"] |
| 716 | + assert expected.flags["W"] == result.flags["W"] |
| 717 | + assert expected.dtype == result.dtype |
| 718 | + assert_array_equal(expected, result) |
| 719 | + |
| 720 | + def test_C_and_F_simul(self): |
| 721 | + a = self.generate_all_false("f4") |
| 722 | + assert_raises(ValueError, numpy.require, a[0], None, ["C", "F"]) |
| 723 | + assert_raises(ValueError, dpnp.require, a[1], None, ["C", "F"]) |
| 724 | + |
| 725 | + def test_copy(self): |
| 726 | + a_np = numpy.arange(6).reshape(2, 3) |
| 727 | + a_dp = dpnp.arange(6).reshape(2, 3) |
| 728 | + a_np.flags["W"] = False |
| 729 | + a_dp.flags["W"] = False |
| 730 | + expected = numpy.require(a_np, requirements=["W", "C"]) |
| 731 | + result = dpnp.require(a_dp, requirements=["W", "C"]) |
| 732 | + # copy is done |
| 733 | + assert result is not a_dp |
| 734 | + assert_array_equal(expected, result) |
| 735 | + |
| 736 | + |
668 | 737 | class TestResize:
|
669 | 738 | @pytest.mark.parametrize(
|
670 | 739 | "data, shape",
|
|
0 commit comments