1from fontTools.varLib.builder import buildVarData
2import pytest
3
4
5@pytest.mark.parametrize("region_indices, items, expected_num_shorts", [
6    ([], [], 0),
7    ([0], [[1]], 0),
8    ([0], [[128]], 1),
9    ([0, 1, 2], [[128, 1, 2], [3, -129, 5], [6, 7, 8]], 2),
10    ([0, 1, 2], [[0, 128, 2], [3, 4, 5], [6, 7, -129]], 3),
11], ids=[
12    "0_regions_0_deltas",
13    "1_region_1_uint8",
14    "1_region_1_short",
15    "3_regions_2_shorts_ordered",
16    "3_regions_2_shorts_unordered",
17])
18def test_buildVarData_no_optimize(region_indices, items, expected_num_shorts):
19    data = buildVarData(region_indices, items, optimize=False)
20
21    assert data.ItemCount == len(items)
22    assert data.NumShorts == expected_num_shorts
23    assert data.VarRegionCount == len(region_indices)
24    assert data.VarRegionIndex == region_indices
25    assert data.Item == items
26
27
28@pytest.mark.parametrize([
29    "region_indices", "items", "expected_num_shorts",
30    "expected_regions", "expected_items"
31], [
32    ([0, 1, 2], [[0, 1, 2], [3, 4, 5], [6, 7, 8]], 0,
33     [0, 1, 2], [[0, 1, 2], [3, 4, 5], [6, 7, 8]]),
34    ([0, 1, 2], [[0, 128, 2], [3, 4, 5], [6, 7, 8]], 1,
35     [1, 0, 2], [[128, 0, 2], [4, 3, 5], [7, 6, 8]]),
36    ([0, 1, 2], [[0, 1, 128], [3, 4, 5], [6, -129, 8]], 2,
37     [1, 2, 0], [[1, 128, 0], [4, 5, 3], [-129, 8, 6]]),
38    ([0, 1, 2], [[128, 1, -129], [3, 4, 5], [6, 7, 8]], 2,
39     [0, 2, 1], [[128, -129, 1], [3, 5, 4], [6, 8, 7]]),
40    ([0, 1, 2], [[0, 1, 128], [3, -129, 5], [256, 7, 8]], 3,
41     [0, 1, 2], [[0, 1, 128], [3, -129, 5], [256, 7, 8]]),
42    ([0, 1, 2], [[0, 128, 2], [0, 4, 5], [0, 7, 8]], 1,
43     [1, 2], [[128, 2], [4, 5], [7, 8]]),
44], ids=[
45    "0/3_shorts_no_reorder",
46    "1/3_shorts_reorder",
47    "2/3_shorts_reorder",
48    "2/3_shorts_same_row_reorder",
49    "3/3_shorts_no_reorder",
50    "1/3_shorts_1/3_zeroes",
51])
52def test_buildVarData_optimize(
53        region_indices, items, expected_num_shorts, expected_regions,
54        expected_items):
55    data = buildVarData(region_indices, items, optimize=True)
56
57    assert data.ItemCount == len(items)
58    assert data.NumShorts == expected_num_shorts
59    assert data.VarRegionCount == len(expected_regions)
60    assert data.VarRegionIndex == expected_regions
61    assert data.Item == expected_items
62
63
64if __name__ == "__main__":
65    import sys
66    sys.exit(pytest.main(sys.argv))
67