diff --git a/tests/next_tests/unit_tests/test_constructors.py b/tests/next_tests/unit_tests/test_constructors.py index 9e010ee23c..c9b6e9f86f 100644 --- a/tests/next_tests/unit_tests/test_constructors.py +++ b/tests/next_tests/unit_tests/test_constructors.py @@ -25,28 +25,29 @@ I = gtx.Dimension("I") J = gtx.Dimension("J") +K = gtx.Dimension("K") -sizes = {"I": 10, "J": 10, "K": 10} +sizes = {I: 10, J: 10, K: 10} def test_as_field(): - a = np.random.rand(sizes["I"]).astype(gtx.float32) + a = np.random.rand(sizes[I]).astype(gtx.float32) ref = gtx.as_field([I], a) assert np.allclose(ref.ndarray, a) def test_as_field_domain(): - a = np.random.rand(sizes["I"] - 1, sizes["J"] - 1).astype(gtx.float32) + a = np.random.rand(sizes[I] - 1, sizes[J] - 1).astype(gtx.float32) domain = common.Domain( dims=(I, J), - ranges=(common.UnitRange(0, sizes["I"] - 1), common.UnitRange(0, sizes["J"] - 1)), + ranges=(common.UnitRange(0, sizes[I] - 1), common.UnitRange(0, sizes[J] - 1)), ) ref = gtx.as_field(domain, a) assert np.allclose(ref.ndarray, a) def test_as_field_origin(): - a = np.random.rand(sizes["I"], sizes["J"]).astype(gtx.float32) + a = np.random.rand(sizes[I], sizes[J]).astype(gtx.float32) ref = gtx.as_field([I, J], a, origin={I: 1, J: 2}) domain_range = [(val.start, val.stop) for val in ref.domain.ranges] assert np.allclose(domain_range, [(-1, 9), (-2, 8)]) @@ -60,7 +61,7 @@ def test_field_wrong_dims(): ValueError, match=(r"Cannot construct `Field` from array of shape"), ): - gtx.as_field([I, J], np.random.rand(sizes["I"]).astype(gtx.float32)) + gtx.as_field([I, J], np.random.rand(sizes[I]).astype(gtx.float32)) def test_field_wrong_domain(): @@ -70,9 +71,9 @@ def test_field_wrong_domain(): ): domain = common.Domain( dims=(I, J), - ranges=(common.UnitRange(0, sizes["I"] - 1), common.UnitRange(0, sizes["J"] - 1)), + ranges=(common.UnitRange(0, sizes[I] - 1), common.UnitRange(0, sizes[J] - 1)), ) - gtx.as_field(domain, np.random.rand(sizes["I"], sizes["J"]).astype(gtx.float32)) + gtx.as_field(domain, np.random.rand(sizes[I], sizes[J]).astype(gtx.float32)) def test_field_wrong_origin(): @@ -80,20 +81,18 @@ def test_field_wrong_origin(): ValueError, match=(r"Origin keys {'J'} not in domain"), ): - gtx.as_field([I], np.random.rand(sizes["I"]).astype(gtx.float32), origin={"J": 0}) + gtx.as_field([I], np.random.rand(sizes[I]).astype(gtx.float32), origin={"J": 0}) with pytest.raises( ValueError, match=(r"Cannot specify origin for domain I"), ): - gtx.as_field("I", np.random.rand(sizes["J"]).astype(gtx.float32), origin={"J": 0}) + gtx.as_field("I", np.random.rand(sizes[J]).astype(gtx.float32), origin={"J": 0}) +@pytest.mark.xfail(reason="aligned_index not supported yet") def test_aligned_index(): - with pytest.raises( - AssertionError, - ): - gtx.as_field([I], np.random.rand(sizes["I"]).astype(gtx.float32), aligned_index=[I, 0]) + gtx.as_field([I], np.random.rand(sizes[I]).astype(gtx.float32), aligned_index=[I, 0]) @pytest.mark.parametrize( @@ -101,15 +100,14 @@ def test_aligned_index(): [[roundtrip.backend, None], [None, core_defs.Device(core_defs.DeviceType.CPU, 0)]], ) def test_empty(allocator, device): - a = np.empty([sizes["I"], sizes["J"]]).astype(gtx.float32) + a = np.empty([sizes[I], sizes[J]]).astype(gtx.float32) ref = gtx.constructors.empty( - domain={I: range(sizes["I"]), J: range(sizes["J"])}, + domain={I: range(sizes[I]), J: range(sizes[J])}, dtype=core_defs.dtype(np.float32), allocator=allocator, device=device, ) assert ref.shape, a.shape - assert np.allclose(ref.ndarray, a) @pytest.mark.parametrize( @@ -119,15 +117,15 @@ def test_empty(allocator, device): def test_zeros(allocator, device): ref = gtx.constructors.zeros( common.Domain( - dims=(I, J), ranges=(common.UnitRange(0, sizes["I"]), common.UnitRange(0, sizes["J"])) + dims=(I, J), ranges=(common.UnitRange(0, sizes[I]), common.UnitRange(0, sizes[J])) ), dtype=core_defs.dtype(np.float32), allocator=allocator, device=device, ) - a = np.zeros((sizes["I"], sizes["J"])).astype(gtx.float32) + a = np.zeros((sizes[I], sizes[J])).astype(gtx.float32) - assert np.allclose(ref.ndarray, a) + assert np.array_equal(ref.ndarray, a) # parametrize with gpu backend and compare with cupy array @@ -144,9 +142,9 @@ def test_ones(allocator, device): allocator=allocator, device=device, ) - a = np.ones((sizes["I"], sizes["J"])).astype(gtx.float32) + a = np.ones((sizes[I], sizes[J])).astype(gtx.float32) - assert np.allclose(ref.ndarray, a) + assert np.array_equal(ref.ndarray, a) @pytest.mark.parametrize( @@ -155,15 +153,15 @@ def test_ones(allocator, device): ) def test_full(allocator, device): ref = gtx.constructors.full( - domain={I: range(sizes["I"] - 2), J: (sizes["J"] - 2)}, + domain={I: range(sizes[I] - 2), J: (sizes[J] - 2)}, fill_value=42.0, dtype=core_defs.dtype(np.float32), allocator=allocator, device=device, ) - a = np.full((sizes["I"] - 2, sizes["J"] - 2), 42.0).astype(gtx.float32) + a = np.full((sizes[I] - 2, sizes[J] - 2), 42.0).astype(gtx.float32) - assert np.allclose(ref.ndarray, a) + assert np.array_equal(ref.ndarray, a) def test_as_field_with(cartesian_case): @@ -182,12 +180,12 @@ def as_field_with_prog( ): field_fo(a, out=out) - a = gtx.as_field([I, J], np.random.rand(sizes["I"], sizes["J"]).astype(gtx.float32)) + a = gtx.as_field([I, J], np.random.rand(sizes[I], sizes[J]).astype(gtx.float32)) ref = gtx.constructors.as_field_with( common.Domain(dims=(I, J), ranges=(common.UnitRange(0, 10), common.UnitRange(0, 10))), ) - out = gtx.as_field([I, J], np.zeros((sizes["I"], sizes["J"])).astype(gtx.float32)) + out = gtx.as_field([I, J], np.zeros((sizes[I], sizes[J])).astype(gtx.float32)) cases.verify( cartesian_case, as_field_with_fo,