From 1dea06f66f2622de88bb3a7f4895ee97e64d3552 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 13 Oct 2022 18:35:45 +0100 Subject: [PATCH] add tests for normalizeLocation(extrapolate=True) These fails at the moment with various division-by-zero errors, fix in follow-up commit. --- Tests/varLib/models_test.py | 97 ++++++++++++++++++++++++++++++++++--- 1 file changed, 91 insertions(+), 6 deletions(-) diff --git a/Tests/varLib/models_test.py b/Tests/varLib/models_test.py index 58a047bb1..11ec1a1e8 100644 --- a/Tests/varLib/models_test.py +++ b/Tests/varLib/models_test.py @@ -31,19 +31,104 @@ def test_normalizeLocation(): assert normalizeLocation({"wght": 1001}, axes) == {"wght": 0.0} +@pytest.mark.parametrize( + "axes, location, expected", + [ + # lower != default != upper + ({"wght": (100, 400, 900)}, {"wght": 1000}, {"wght": 1.2}), + ({"wght": (100, 400, 900)}, {"wght": 900}, {"wght": 1.0}), + ({"wght": (100, 400, 900)}, {"wght": 650}, {"wght": 0.5}), + ({"wght": (100, 400, 900)}, {"wght": 400}, {"wght": 0.0}), + ({"wght": (100, 400, 900)}, {"wght": 250}, {"wght": -0.5}), + ({"wght": (100, 400, 900)}, {"wght": 100}, {"wght": -1.0}), + ({"wght": (100, 400, 900)}, {"wght": 25}, {"wght": -1.25}), + # lower == default != upper + ( + {"wght": (400, 400, 900), "wdth": (100, 100, 150)}, + {"wght": 1000, "wdth": 200}, + {"wght": 1.2, "wdth": 2.0}, + ), + ( + {"wght": (400, 400, 900), "wdth": (100, 100, 150)}, + {"wght": 25, "wdth": 25}, + {"wght": -0.75, "wdth": -1.5}, + ), + # lower != default == upper + ( + {"wght": (100, 400, 400), "wdth": (50, 100, 100)}, + {"wght": 700, "wdth": 150}, + {"wght": 1.0, "wdth": 1.0}, + ), + ( + {"wght": (100, 400, 400), "wdth": (50, 100, 100)}, + {"wght": -50, "wdth": 25}, + {"wght": -1.5, "wdth": -1.5}, + ), + # degenerate case with lower == default == upper, normalized location always 0 + ({"wght": (400, 400, 400)}, {"wght": 100}, {"wght": 0.0}), + ({"wght": (400, 400, 400)}, {"wght": 400}, {"wght": 0.0}), + ({"wght": (400, 400, 400)}, {"wght": 700}, {"wght": 0.0}), + ], +) +def test_normalizeLocation_extrapolate(axes, location, expected): + assert normalizeLocation(location, axes, extrapolate=True) == expected + + def test_supportScalar(): assert supportScalar({}, {}) == 1.0 assert supportScalar({"wght": 0.2}, {}) == 1.0 assert supportScalar({"wght": 0.2}, {"wght": (0, 2, 3)}) == 0.1 assert supportScalar({"wght": 2.5}, {"wght": (0, 2, 4)}) == 0.75 assert supportScalar({"wght": 3}, {"wght": (0, 2, 2)}) == 0.0 - assert supportScalar({"wght": 3}, {"wght": (0, 2, 2)}, extrapolate=True, axisRanges={"wght": (0, 2)}) == 1.5 - assert supportScalar({"wght": -1}, {"wght": (0, 2, 2)}, extrapolate=True, axisRanges={"wght": (0, 2)}) == -0.5 - assert supportScalar({"wght": 3}, {"wght": (0, 1, 2)}, extrapolate=True, axisRanges={"wght": (0, 2)}) == -1.0 - assert supportScalar({"wght": -1}, {"wght": (0, 1, 2)}, extrapolate=True, axisRanges={"wght": (0, 2)}) == -1.0 - assert supportScalar({"wght": 2}, {"wght": (0, 0.75, 1)}, extrapolate=True, axisRanges={"wght": (0, 1)}) == -4.0 + assert ( + supportScalar( + {"wght": 3}, + {"wght": (0, 2, 2)}, + extrapolate=True, + axisRanges={"wght": (0, 2)}, + ) + == 1.5 + ) + assert ( + supportScalar( + {"wght": -1}, + {"wght": (0, 2, 2)}, + extrapolate=True, + axisRanges={"wght": (0, 2)}, + ) + == -0.5 + ) + assert ( + supportScalar( + {"wght": 3}, + {"wght": (0, 1, 2)}, + extrapolate=True, + axisRanges={"wght": (0, 2)}, + ) + == -1.0 + ) + assert ( + supportScalar( + {"wght": -1}, + {"wght": (0, 1, 2)}, + extrapolate=True, + axisRanges={"wght": (0, 2)}, + ) + == -1.0 + ) + assert ( + supportScalar( + {"wght": 2}, + {"wght": (0, 0.75, 1)}, + extrapolate=True, + axisRanges={"wght": (0, 1)}, + ) + == -4.0 + ) with pytest.raises(TypeError): - supportScalar({"wght": 2}, {"wght": (0, 0.75, 1)}, extrapolate=True, axisRanges=None) + supportScalar( + {"wght": 2}, {"wght": (0, 0.75, 1)}, extrapolate=True, axisRanges=None + ) def test_model_extrapolate():