xref: /aosp_15_r20/external/fonttools/Tests/misc/arrayTools_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1from fontTools.misc.arrayTools import (
2    calcBounds,
3    calcIntBounds,
4    updateBounds,
5    pointInRect,
6    pointsInRect,
7    vectorLength,
8    asInt16,
9    normRect,
10    scaleRect,
11    offsetRect,
12    insetRect,
13    sectRect,
14    unionRect,
15    rectCenter,
16    intRect,
17)
18import math
19
20
21def test_calcBounds():
22    assert calcBounds([]) == (0, 0, 0, 0)
23    assert calcBounds([(0, 40), (0, 100), (50, 50), (80, 10)]) == (0, 10, 80, 100)
24
25
26def test_calcIntBounds():
27    assert calcIntBounds([(0.1, 40.1), (0.1, 100.1), (49.9, 49.9), (78.5, 9.5)]) == (
28        0,
29        10,
30        79,
31        100,
32    )
33
34    assert calcIntBounds(
35        [(0.1, 40.1), (0.1, 100.1), (49.9, 49.9), (78.5, 9.5)], round=round
36    ) == (0, 10, 78, 100)
37
38
39def test_updateBounds():
40    assert updateBounds((0, 0, 0, 0), (100, 100)) == (0, 0, 100, 100)
41
42
43def test_pointInRect():
44    assert pointInRect((50, 50), (0, 0, 100, 100))
45    assert pointInRect((0, 0), (0, 0, 100, 100))
46    assert pointInRect((100, 100), (0, 0, 100, 100))
47    assert not pointInRect((101, 100), (0, 0, 100, 100))
48
49
50def test_pointsInRect():
51    assert pointsInRect([], (0, 0, 100, 100)) == []
52    assert pointsInRect(
53        [(50, 50), (0, 0), (100, 100), (101, 100)], (0, 0, 100, 100)
54    ) == [True, True, True, False]
55
56
57def test_vectorLength():
58    assert vectorLength((1, 1)) == math.sqrt(2)
59
60
61def test_asInt16():
62    assert asInt16([0, 0.1, 0.5, 0.9]) == [0, 0, 1, 1]
63
64
65def test_normRect():
66    assert normRect((0, 10, 100, 200)) == (0, 10, 100, 200)
67    assert normRect((100, 200, 0, 10)) == (0, 10, 100, 200)
68
69
70def test_scaleRect():
71    assert scaleRect((10, 20, 50, 150), 1.5, 2) == (15.0, 40, 75.0, 300)
72
73
74def test_offsetRect():
75    assert offsetRect((10, 20, 30, 40), 5, 6) == (15, 26, 35, 46)
76
77
78def test_insetRect():
79    assert insetRect((10, 20, 50, 60), 5, 10) == (15, 30, 45, 50)
80    assert insetRect((10, 20, 50, 60), -5, -10) == (5, 10, 55, 70)
81
82
83def test_sectRect():
84    intersects, rect = sectRect((0, 10, 20, 30), (0, 40, 20, 50))
85    assert not intersects
86
87    intersects, rect = sectRect((0, 10, 20, 30), (5, 20, 35, 50))
88    assert intersects
89    assert rect == (5, 20, 20, 30)
90
91
92def test_unionRect():
93    assert unionRect((0, 10, 20, 30), (0, 40, 20, 50)) == (0, 10, 20, 50)
94
95
96def test_rectCenter():
97    assert rectCenter((0, 0, 100, 200)) == (50.0, 100.0)
98    assert rectCenter((0, 0, 100, 199.0)) == (50.0, 99.5)
99
100
101def test_intRect():
102    assert intRect((0.9, 2.9, 3.1, 4.1)) == (0, 2, 4, 5)
103