|
1 | | -"""Engine lowering must never silently discard index-array constraints.""" |
| 1 | +"""Index-array bounds are validated against raw coordinates during loading.""" |
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -from typing import TYPE_CHECKING |
| 5 | +from typing import TYPE_CHECKING, Any |
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 | import pytest |
9 | 9 |
|
10 | | -from zarr_indexing import IndexTransform, normalize_ndsel |
| 10 | +from zarr_indexing import ConstantMap, IndexTransform, normalize_ndsel |
11 | 11 | from zarr_indexing.messages import NdselError |
12 | 12 | from zarr_indexing.output_map import output_index_map_from_json |
13 | 13 |
|
14 | 14 | if TYPE_CHECKING: |
15 | 15 | from zarr_indexing.json import IndexTransformJSON, OutputIndexMapJSON |
16 | 16 |
|
17 | 17 |
|
18 | | -@pytest.mark.parametrize("bounds", [[0, 3], ["-inf", 3], [0, "+inf"], [0, 0]]) |
19 | | -@pytest.mark.parametrize("values", [[], [1], [-1, 1, 3]]) |
20 | | -@pytest.mark.parametrize(("offset", "stride"), [(0, 1), (10, -2), (7, 0)]) |
| 18 | +def load(values: Any, bounds: Any, entry: str, offset: int, stride: int) -> Any: |
| 19 | + output: OutputIndexMapJSON = {"index_array": values, "offset": offset, "stride": stride} |
| 20 | + if bounds is not None: |
| 21 | + output["index_array_bounds"] = bounds |
| 22 | + if entry == "output_map": |
| 23 | + return output_index_map_from_json(output) |
| 24 | + body: IndexTransformJSON = {"input_shape": list(np.asarray(values).shape), "output": [output]} |
| 25 | + return IndexTransform.from_json(body) |
| 26 | + |
| 27 | + |
21 | 28 | @pytest.mark.parametrize("entry", ["transform", "output_map"]) |
22 | | -def test_lowering_rejects_unsupported_index_array_bounds( |
23 | | - bounds: list[int | str], values: list[int], offset: int, stride: int, entry: str |
| 29 | +@pytest.mark.parametrize(("offset", "stride"), [(0, 1), (10, -2), (7, 0)]) |
| 30 | +@pytest.mark.parametrize( |
| 31 | + ("values", "bounds"), |
| 32 | + [ |
| 33 | + ([-3, 0, 4], [-3, 4]), |
| 34 | + ([-3, 0, 4], ["-inf", 4]), |
| 35 | + ([-3, 0, 4], [-3, "+inf"]), |
| 36 | + ([-3, 0, 4], ["-inf", "+inf"]), |
| 37 | + ([-3, 0, 4], None), |
| 38 | + ([2], [2, 2]), |
| 39 | + ([], [2, 2]), |
| 40 | + ([], ["+inf", "+inf"]), |
| 41 | + ([], ["-inf", "-inf"]), |
| 42 | + ([[0, 2], [2, 0]], [0, 2]), |
| 43 | + ], |
| 44 | +) |
| 45 | +def test_valid_bounds_preserve_values_and_roundtrip( |
| 46 | + values: Any, bounds: Any, entry: str, offset: int, stride: int |
24 | 47 | ) -> None: |
25 | | - output: OutputIndexMapJSON = { |
26 | | - "index_array": values, |
27 | | - "index_array_bounds": bounds, |
28 | | - "offset": offset, |
29 | | - "stride": stride, |
30 | | - } |
31 | | - body: IndexTransformJSON = { |
32 | | - "input_inclusive_min": [0], |
33 | | - "input_shape": [len(values)], |
34 | | - "output": [output], |
35 | | - } |
36 | | - # JSON-only processing preserves constraints for capable consumers. |
37 | | - canonical = normalize_ndsel({"kind": "transform", **body}) |
38 | | - assert canonical["output"][0]["index_array_bounds"] == bounds |
| 48 | + model = np.asarray(values, dtype=np.intp) |
| 49 | + loaded = load(values, bounds, entry, offset, stride) |
| 50 | + expected = offset + stride * model |
39 | 51 | if entry == "transform": |
40 | | - with pytest.raises(NdselError, match="index_array_bounds.*unsupported") as exc: |
41 | | - IndexTransform.from_json(canonical) |
42 | | - assert exc.value.reason == "invalid_json" |
| 52 | + points = np.array(list(np.ndindex(model.shape)), dtype=np.intp).reshape(-1, model.ndim) |
| 53 | + restored = IndexTransform.from_json(loaded.to_json()) |
| 54 | + np.testing.assert_array_equal(loaded.apply_many(points).reshape(model.shape), expected) |
| 55 | + np.testing.assert_array_equal(restored.apply_many(points).reshape(model.shape), expected) |
| 56 | + # Further selection and affine adjustment operate on validated coordinates. |
| 57 | + if model.ndim == 1 and model.size > 1: |
| 58 | + selected = loaded.oindex[[model.size - 1, 0]].translate((3,)) |
| 59 | + np.testing.assert_array_equal( |
| 60 | + selected.apply_many([[0], [1]]).ravel(), expected[[-1, 0]] + 3 |
| 61 | + ) |
43 | 62 | else: |
44 | | - with pytest.raises(NdselError, match="index_array_bounds.*unsupported") as exc: |
45 | | - output_index_map_from_json(output) |
46 | | - assert exc.value.reason == "invalid_json" |
| 63 | + restored = output_index_map_from_json(loaded.to_json()) |
| 64 | + if isinstance(restored, ConstantMap): |
| 65 | + np.testing.assert_array_equal(np.full(model.shape, restored.offset), expected) |
| 66 | + else: |
| 67 | + assert restored == loaded |
| 68 | + np.testing.assert_array_equal(loaded.offset + loaded.stride * loaded.index_array, expected) |
| 69 | + if bounds is not None: |
| 70 | + canonical = normalize_ndsel( |
| 71 | + { |
| 72 | + "kind": "transform", |
| 73 | + "input_shape": list(model.shape), |
| 74 | + "output": [{"index_array": values, "index_array_bounds": bounds}], |
| 75 | + } |
| 76 | + ) |
| 77 | + assert canonical["output"][0]["index_array_bounds"] == bounds |
47 | 78 |
|
48 | 79 |
|
49 | | -@pytest.mark.parametrize("explicit_bounds", [False, True]) |
50 | | -@pytest.mark.parametrize("values", [[], [1], [-1, 1, 3]]) |
| 80 | +@pytest.mark.parametrize("entry", ["transform", "output_map"]) |
51 | 81 | @pytest.mark.parametrize(("offset", "stride"), [(0, 1), (10, -2), (7, 0)]) |
52 | | -def test_unconstrained_index_array_roundtrip_evaluation( |
53 | | - explicit_bounds: bool, values: list[int], offset: int, stride: int |
| 82 | +@pytest.mark.parametrize( |
| 83 | + ("values", "bounds"), |
| 84 | + [ |
| 85 | + ([-1, 0], [0, 2]), |
| 86 | + ([0, 3], [0, 2]), |
| 87 | + ([3], [2, 2]), |
| 88 | + ([-1], [0, "+inf"]), |
| 89 | + ([3], ["-inf", 2]), |
| 90 | + ([0], ["+inf", "+inf"]), |
| 91 | + ([0], ["-inf", "-inf"]), |
| 92 | + ([[0, 1], [1, 3]], [0, 2]), |
| 93 | + ], |
| 94 | +) |
| 95 | +def test_out_of_bounds_raw_values_are_rejected( |
| 96 | + values: Any, bounds: Any, entry: str, offset: int, stride: int |
54 | 97 | ) -> None: |
55 | | - output: OutputIndexMapJSON = {"index_array": values, "offset": offset, "stride": stride} |
56 | | - if explicit_bounds: |
57 | | - output["index_array_bounds"] = ["-inf", "+inf"] |
58 | | - body: IndexTransformJSON = { |
59 | | - "input_inclusive_min": [0], |
60 | | - "input_shape": [len(values)], |
61 | | - "output": [output], |
62 | | - } |
63 | | - transform = IndexTransform.from_json(body) |
64 | | - restored = IndexTransform.from_json(transform.to_json()) |
65 | | - points = np.arange(len(values)).reshape(-1, 1) |
66 | | - expected = np.array([offset + stride * value for value in values]).reshape(-1, 1) |
67 | | - np.testing.assert_array_equal(transform.apply_many(points), expected) |
68 | | - np.testing.assert_array_equal(restored.apply_many(points), expected) |
69 | | - # The standalone decoder follows the same omission/default policy. |
70 | | - assert output_index_map_from_json(output) == transform.output[0] |
| 98 | + with pytest.raises(NdselError, match="index_array_bounds") as exc: |
| 99 | + load(values, bounds, entry, offset, stride) |
| 100 | + assert exc.value.reason == "invalid_json" |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.parametrize("entry", ["transform", "output_map"]) |
| 104 | +@pytest.mark.parametrize("values", [[], [1]]) |
| 105 | +@pytest.mark.parametrize("bounds", [[], [0], [0, 1, 2], [False, 2], [0.5, 2], ["bad", 2]]) |
| 106 | +def test_malformed_bounds_are_rejected(values: Any, bounds: Any, entry: str) -> None: |
| 107 | + with pytest.raises(NdselError) as exc: |
| 108 | + load(values, bounds, entry, 0, 1) |
| 109 | + assert exc.value.reason == "invalid_json" |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.parametrize("entry", ["transform", "output_map"]) |
| 113 | +@pytest.mark.parametrize("values", [[], [1]]) |
| 114 | +def test_reversed_bounds_are_rejected(values: Any, entry: str) -> None: |
| 115 | + with pytest.raises(NdselError) as exc: |
| 116 | + load(values, [2, 0], entry, 0, 1) |
| 117 | + assert exc.value.reason == "bounds_out_of_order" |
| 118 | + |
| 119 | + |
| 120 | +@pytest.mark.parametrize("entry", ["transform", "output_map"]) |
| 121 | +@pytest.mark.parametrize("value", [int(np.iinfo(np.intp).min), int(np.iinfo(np.intp).max)]) |
| 122 | +def test_integer_extreme_bounds_are_exact(entry: str, value: int) -> None: |
| 123 | + result = load([value], [value, value], entry, 0, 1) |
| 124 | + if entry == "transform": |
| 125 | + assert result.apply((0,)) == (value,) |
| 126 | + else: |
| 127 | + assert int(result.index_array[0]) == value |
| 128 | + bounds = [value + 1, "+inf"] if value < 0 else ["-inf", value - 1] |
| 129 | + with pytest.raises(NdselError, match="index_array_bounds"): |
| 130 | + load([value], bounds, entry, 0, 1) |
0 commit comments