Skip to content

Commit

Permalink
edits to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nfarabullini committed Nov 14, 2023
1 parent dc88138 commit 12ccc42
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions tests/next_tests/unit_tests/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand All @@ -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():
Expand All @@ -70,46 +71,43 @@ 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():
with pytest.raises(
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(
"allocator, device",
[[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(
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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):
Expand All @@ -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,
Expand Down

0 comments on commit 12ccc42

Please sign in to comment.