xref: /aosp_15_r20/external/fonttools/Tests/misc/vector_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1import math
2import pytest
3from fontTools.misc.arrayTools import Vector as ArrayVector
4from fontTools.misc.vector import Vector
5
6
7def test_Vector():
8    v = Vector((100, 200))
9    assert repr(v) == "Vector((100, 200))"
10    assert v == Vector((100, 200))
11    assert v == Vector([100, 200])
12    assert v == (100, 200)
13    assert (100, 200) == v
14    assert v == [100, 200]
15    assert [100, 200] == v
16    assert v is Vector(v)
17    assert v + 10 == (110, 210)
18    assert 10 + v == (110, 210)
19    assert v + Vector((1, 2)) == (101, 202)
20    assert v - Vector((1, 2)) == (99, 198)
21    assert v * 2 == (200, 400)
22    assert 2 * v == (200, 400)
23    assert v * 0.5 == (50, 100)
24    assert v / 2 == (50, 100)
25    assert 2 / v == (0.02, 0.01)
26    v = Vector((3, 4))
27    assert abs(v) == 5  # length
28    assert v.length() == 5
29    assert v.normalized() == Vector((0.6, 0.8))
30    assert abs(Vector((1, 1, 1))) == math.sqrt(3)
31    assert bool(Vector((0, 0, 1)))
32    assert not bool(Vector((0, 0, 0)))
33    v1 = Vector((2, 3))
34    v2 = Vector((3, 4))
35    assert v1.dot(v2) == 18
36    v = Vector((2, 4))
37    assert round(v / 3) == (1, 1)
38    with pytest.raises(
39        AttributeError,
40        match="'Vector' object has no attribute 'newAttr'",
41    ):
42        v.newAttr = 12
43
44
45def test_deprecated():
46    with pytest.warns(
47        DeprecationWarning,
48        match="fontTools.misc.arrayTools.Vector has been deprecated",
49    ):
50        ArrayVector((1, 2))
51    with pytest.warns(
52        DeprecationWarning,
53        match="the 'keep' argument has been deprecated",
54    ):
55        Vector((1, 2), keep=True)
56    v = Vector((1, 2))
57    with pytest.warns(
58        DeprecationWarning,
59        match="the 'toInt' method has been deprecated",
60    ):
61        v.toInt()
62    with pytest.warns(
63        DeprecationWarning,
64        match="the 'values' attribute has been deprecated",
65    ):
66        v.values
67    with pytest.raises(
68        AttributeError,
69        match="the 'values' attribute has been deprecated",
70    ):
71        v.values = [12, 23]
72