|
5 | 5 | assert_allclose, |
6 | 6 | assert_array_equal, |
7 | 7 | assert_equal, |
| 8 | + assert_raises, |
8 | 9 | assert_raises_regex, |
9 | 10 | ) |
10 | 11 |
|
@@ -530,43 +531,71 @@ def test_print_dpnp_zero_shape(): |
530 | 531 | assert result == expected |
531 | 532 |
|
532 | 533 |
|
533 | | -# Numpy will raise an error when converting a.ndim > 0 to a scalar |
534 | | -# TODO: Discuss dpnp behavior according to these future changes |
535 | | -@pytest.mark.skip("until dpctl-2223") |
536 | | -@pytest.mark.filterwarnings("ignore::DeprecationWarning") |
537 | | -@pytest.mark.parametrize("func", [bool, float, int, complex]) |
| 534 | +@testing.with_requires("numpy>=2.4") |
| 535 | +@pytest.mark.parametrize("xp", [dpnp, numpy]) |
538 | 536 | @pytest.mark.parametrize("shape", [tuple(), (1,), (1, 1), (1, 1, 1)]) |
539 | | -@pytest.mark.parametrize( |
540 | | - "dtype", get_all_dtypes(no_float16=False, no_complex=True) |
541 | | -) |
542 | | -def test_scalar_type_casting(func, shape, dtype): |
543 | | - a = numpy.full(shape, 5, dtype=dtype) |
544 | | - ia = dpnp.full(shape, 5, dtype=dtype) |
545 | | - assert func(a) == func(ia) |
546 | | - |
| 537 | +class TestPythonScalarConversion: |
| 538 | + @pytest.mark.parametrize( |
| 539 | + "dtype", get_all_dtypes(no_none=True, no_float16=False, no_complex=True) |
| 540 | + ) |
| 541 | + def test_bool_conversion(self, xp, shape, dtype): |
| 542 | + a = xp.full(shape, 5, dtype=dtype) |
| 543 | + if xp == dpnp and len(shape) > 0: |
| 544 | + # dpnp behavior differs from NumPy: |
| 545 | + # non-0D singe-element arrays are not convertible to |
| 546 | + # Python bool |
| 547 | + assert_raises(TypeError, bool, a) |
| 548 | + else: |
| 549 | + # NumPy allows conversion to Python bool for |
| 550 | + # non-0D singe-element arrays |
| 551 | + assert bool(a) is True |
547 | 552 |
|
548 | | -# Numpy will raise an error when converting a.ndim > 0 to a scalar |
549 | | -# TODO: Discuss dpnp behavior according to these future changes |
550 | | -@pytest.mark.filterwarnings("ignore::DeprecationWarning") |
551 | | -@pytest.mark.skip("until dpctl-2223") |
552 | | -@pytest.mark.parametrize( |
553 | | - "method", ["__bool__", "__float__", "__int__", "__complex__"] |
554 | | -) |
555 | | -@pytest.mark.parametrize("shape", [tuple(), (1,), (1, 1), (1, 1, 1)]) |
556 | | -@pytest.mark.parametrize( |
557 | | - "dtype", get_all_dtypes(no_float16=False, no_complex=True) |
558 | | -) |
559 | | -def test_scalar_type_casting_by_method(method, shape, dtype): |
560 | | - a = numpy.full(shape, 4.7, dtype=dtype) |
561 | | - ia = dpnp.full(shape, 4.7, dtype=dtype) |
562 | | - assert_allclose(getattr(a, method)(), getattr(ia, method)(), rtol=1e-06) |
| 553 | + @pytest.mark.parametrize( |
| 554 | + "dtype", get_all_dtypes(no_none=True, no_float16=False, no_complex=True) |
| 555 | + ) |
| 556 | + def test_bool_method_conversion(self, xp, shape, dtype): |
| 557 | + a = xp.full(shape, 5, dtype=dtype) |
| 558 | + if xp == dpnp and len(shape) > 0: |
| 559 | + assert_raises(TypeError, getattr(a, "__bool__")) |
| 560 | + else: |
| 561 | + assert a.__bool__() is True |
| 562 | + |
| 563 | + @pytest.mark.parametrize("func", [float, int, complex]) |
| 564 | + @pytest.mark.parametrize( |
| 565 | + "dtype", get_all_dtypes(no_none=True, no_float16=False, no_complex=True) |
| 566 | + ) |
| 567 | + def test_non_bool_conversion(self, xp, func, shape, dtype): |
| 568 | + a = xp.full(shape, 5, dtype=dtype) |
| 569 | + if len(shape) > 0: |
| 570 | + # Non-0D arrays are not allowed to be converted to |
| 571 | + # Python numeric scalars |
| 572 | + assert_raises(TypeError, func, a) |
| 573 | + else: |
| 574 | + # 0D arrays are allowed to be converted to |
| 575 | + # Python numeric scalars |
| 576 | + expected = 1 if dtype == xp.bool else 5 |
| 577 | + assert func(a) == func(expected) |
| 578 | + |
| 579 | + @pytest.mark.parametrize("method", ["__float__", "__int__", "__complex__"]) |
| 580 | + @pytest.mark.parametrize( |
| 581 | + "dtype", get_all_dtypes(no_none=True, no_float16=False, no_complex=True) |
| 582 | + ) |
| 583 | + def test_non_bool_method_conversion(self, xp, method, shape, dtype): |
| 584 | + a = xp.full(shape, 5, dtype=dtype) |
| 585 | + if len(shape) > 0: |
| 586 | + assert_raises(TypeError, getattr(a, method)) |
| 587 | + else: |
| 588 | + expected = 1 if dtype == xp.bool else 5 |
| 589 | + func = {"__float__": float, "__int__": int, "__complex__": complex}[ |
| 590 | + method |
| 591 | + ] |
| 592 | + assert getattr(a, method)() == func(expected) |
563 | 593 |
|
564 | 594 |
|
565 | | -@pytest.mark.parametrize("shape", [(1,), (1, 1), (1, 1, 1)]) |
566 | 595 | @pytest.mark.parametrize("index_dtype", [dpnp.int32, dpnp.int64]) |
567 | | -def test_array_as_index(shape, index_dtype): |
568 | | - ind_arr = dpnp.ones(shape, dtype=index_dtype) |
569 | | - a = numpy.arange(ind_arr.size + 1) |
| 596 | +def test_array_as_index(index_dtype): |
| 597 | + ind_arr = dpnp.ones((1,), dtype=index_dtype) |
| 598 | + a = dpnp.arange(ind_arr.size + 1) |
570 | 599 | assert a[tuple(ind_arr)] == a[1] |
571 | 600 |
|
572 | 601 |
|
|
0 commit comments