1
from __future__ import annotations
6
from collections.abc import Sequence
10
from PIL import Image, ImagePath
13
def test_path() -> None:
14
p = ImagePath.Path(list(range(10)))
18
assert p[0] == (0.0, 1.0)
19
assert p[-1] == (8.0, 9.0)
20
assert list(p[:1]) == [(0.0, 1.0)]
21
with pytest.raises(TypeError) as cm:
23
assert str(cm.value) == "Path indices must be integers, not str"
24
assert list(p) == [(0.0, 1.0), (2.0, 3.0), (4.0, 5.0), (6.0, 7.0), (8.0, 9.0)]
27
assert p.tolist() == [
34
assert p.tolist(True) == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
36
assert p.getbbox() == (0.0, 1.0, 8.0, 9.0)
38
assert p.compact(5) == 2
39
assert list(p) == [(0.0, 1.0), (4.0, 5.0), (8.0, 9.0)]
41
p.transform((1, 0, 1, 0, 1, 1))
42
assert list(p) == [(1.0, 2.0), (5.0, 6.0), (9.0, 10.0)]
45
@pytest.mark.parametrize(
56
array.array("f", [0, 1]),
57
array.array("f", [0, 1]).tobytes(),
58
ImagePath.Path((0, 1)),
61
def test_path_constructors(
62
coords: Sequence[float] | array.array[float] | ImagePath.Path,
65
p = ImagePath.Path(coords)
68
assert list(p) == [(0.0, 1.0)]
71
@pytest.mark.parametrize(
81
def test_invalid_path_constructors(
82
coords: tuple[str, str] | Sequence[Sequence[int]]
85
with pytest.raises(ValueError) as e:
86
ImagePath.Path(coords)
89
assert str(e.value) == "incorrect coordinate type"
92
@pytest.mark.parametrize(
101
def test_path_odd_number_of_coordinates(coords: Sequence[int]) -> None:
103
with pytest.raises(ValueError) as e:
104
ImagePath.Path(coords)
107
assert str(e.value) == "wrong number of coordinates"
110
@pytest.mark.parametrize(
113
([0, 1, 2, 3], (0.0, 1.0, 2.0, 3.0)),
114
([3, 2, 1, 0], (1.0, 0.0, 3.0, 2.0)),
115
(0, (0.0, 0.0, 0.0, 0.0)),
116
(1, (0.0, 0.0, 0.0, 0.0)),
120
coords: int | list[int], expected: tuple[float, float, float, float]
123
p = ImagePath.Path(coords)
126
assert p.getbbox() == expected
129
def test_getbbox_no_args() -> None:
131
p = ImagePath.Path([0, 1, 2, 3])
134
with pytest.raises(TypeError):
138
@pytest.mark.parametrize(
142
(list(range(6)), [(0.0, 3.0), (4.0, 9.0), (8.0, 15.0)]),
145
def test_map(coords: int | list[int], expected: list[tuple[float, float]]) -> None:
147
p = ImagePath.Path(coords)
151
p.map(lambda x, y: (x * 2, y * 3))
154
assert list(p) == expected
157
def test_transform() -> None:
159
p = ImagePath.Path([0, 1, 2, 3])
165
(math.cos(theta), math.sin(theta), 20, -math.sin(theta), math.cos(theta), 20),
169
assert p.tolist() == [
170
(20.20791169081776, 20.978147600733806),
171
(22.58003027392089, 22.518619420565898),
175
def test_transform_with_wrap() -> None:
177
p = ImagePath.Path([0, 1, 2, 3])
183
(math.cos(theta), math.sin(theta), 20, -math.sin(theta), math.cos(theta), 20),
188
assert p.tolist() == [
189
(0.20791169081775962, 20.978147600733806),
190
(0.5800302739208902, 22.518619420565898),
194
def test_overflow_segfault() -> None:
197
with pytest.raises((TypeError, MemoryError)):
203
for i in range(200000):
208
def __init__(self) -> None:
209
self.corrupt = Image.core.path(0x4000000000000000)
211
def __getitem__(self, i: int) -> bytes:
213
return struct.pack("dd", x[0], x[1])
215
def __setitem__(self, i: int, x: bytes) -> None:
216
self.corrupt[i] = struct.unpack("dd", x)