Pillow

Форк
0
/
test_imagepath.py 
216 строк · 4.9 Кб
1
from __future__ import annotations
2

3
import array
4
import math
5
import struct
6
from collections.abc import Sequence
7

8
import pytest
9

10
from PIL import Image, ImagePath
11

12

13
def test_path() -> None:
14
    p = ImagePath.Path(list(range(10)))
15

16
    # sequence interface
17
    assert len(p) == 5
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:
22
        p["foo"]
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)]
25

26
    # method sanity check
27
    assert p.tolist() == [
28
        (0.0, 1.0),
29
        (2.0, 3.0),
30
        (4.0, 5.0),
31
        (6.0, 7.0),
32
        (8.0, 9.0),
33
    ]
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]
35

36
    assert p.getbbox() == (0.0, 1.0, 8.0, 9.0)
37

38
    assert p.compact(5) == 2
39
    assert list(p) == [(0.0, 1.0), (4.0, 5.0), (8.0, 9.0)]
40

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)]
43

44

45
@pytest.mark.parametrize(
46
    "coords",
47
    (
48
        (0, 1),
49
        [0, 1],
50
        (0.0, 1.0),
51
        [0.0, 1.0],
52
        ((0, 1),),
53
        [(0, 1)],
54
        ((0.0, 1.0),),
55
        [(0.0, 1.0)],
56
        array.array("f", [0, 1]),
57
        array.array("f", [0, 1]).tobytes(),
58
        ImagePath.Path((0, 1)),
59
    ),
60
)
61
def test_path_constructors(
62
    coords: Sequence[float] | array.array[float] | ImagePath.Path,
63
) -> None:
64
    # Arrange / Act
65
    p = ImagePath.Path(coords)
66

67
    # Assert
68
    assert list(p) == [(0.0, 1.0)]
69

70

71
@pytest.mark.parametrize(
72
    "coords",
73
    (
74
        ("a", "b"),
75
        ([0, 1],),
76
        [[0, 1]],
77
        ([0.0, 1.0],),
78
        [[0.0, 1.0]],
79
    ),
80
)
81
def test_invalid_path_constructors(
82
    coords: tuple[str, str] | Sequence[Sequence[int]]
83
) -> None:
84
    # Act
85
    with pytest.raises(ValueError) as e:
86
        ImagePath.Path(coords)
87

88
    # Assert
89
    assert str(e.value) == "incorrect coordinate type"
90

91

92
@pytest.mark.parametrize(
93
    "coords",
94
    (
95
        (0,),
96
        [0],
97
        (0, 1, 2),
98
        [0, 1, 2],
99
    ),
100
)
101
def test_path_odd_number_of_coordinates(coords: Sequence[int]) -> None:
102
    # Act
103
    with pytest.raises(ValueError) as e:
104
        ImagePath.Path(coords)
105

106
    # Assert
107
    assert str(e.value) == "wrong number of coordinates"
108

109

110
@pytest.mark.parametrize(
111
    "coords, expected",
112
    [
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)),
117
    ],
118
)
119
def test_getbbox(
120
    coords: int | list[int], expected: tuple[float, float, float, float]
121
) -> None:
122
    # Arrange
123
    p = ImagePath.Path(coords)
124

125
    # Act / Assert
126
    assert p.getbbox() == expected
127

128

129
def test_getbbox_no_args() -> None:
130
    # Arrange
131
    p = ImagePath.Path([0, 1, 2, 3])
132

133
    # Act / Assert
134
    with pytest.raises(TypeError):
135
        p.getbbox(1)
136

137

138
@pytest.mark.parametrize(
139
    "coords, expected",
140
    [
141
        (0, []),
142
        (list(range(6)), [(0.0, 3.0), (4.0, 9.0), (8.0, 15.0)]),
143
    ],
144
)
145
def test_map(coords: int | list[int], expected: list[tuple[float, float]]) -> None:
146
    # Arrange
147
    p = ImagePath.Path(coords)
148

149
    # Act
150
    # Modifies the path in-place
151
    p.map(lambda x, y: (x * 2, y * 3))
152

153
    # Assert
154
    assert list(p) == expected
155

156

157
def test_transform() -> None:
158
    # Arrange
159
    p = ImagePath.Path([0, 1, 2, 3])
160
    theta = math.pi / 15
161

162
    # Act
163
    # Affine transform, in-place
164
    p.transform(
165
        (math.cos(theta), math.sin(theta), 20, -math.sin(theta), math.cos(theta), 20),
166
    )
167

168
    # Assert
169
    assert p.tolist() == [
170
        (20.20791169081776, 20.978147600733806),
171
        (22.58003027392089, 22.518619420565898),
172
    ]
173

174

175
def test_transform_with_wrap() -> None:
176
    # Arrange
177
    p = ImagePath.Path([0, 1, 2, 3])
178
    theta = math.pi / 15
179

180
    # Act
181
    # Affine transform, in-place, with wrap parameter
182
    p.transform(
183
        (math.cos(theta), math.sin(theta), 20, -math.sin(theta), math.cos(theta), 20),
184
        1.0,
185
    )
186

187
    # Assert
188
    assert p.tolist() == [
189
        (0.20791169081775962, 20.978147600733806),
190
        (0.5800302739208902, 22.518619420565898),
191
    ]
192

193

194
def test_overflow_segfault() -> None:
195
    # Some Pythons fail getting the argument as an integer, and it falls
196
    # through to the sequence. Seeing this on 32-bit Windows.
197
    with pytest.raises((TypeError, MemoryError)):
198
        # post patch, this fails with a memory error
199
        x = Evil()
200

201
        # This fails due to the invalid malloc above,
202
        # and segfaults
203
        for i in range(200000):
204
            x[i] = b"0" * 16
205

206

207
class Evil:
208
    def __init__(self) -> None:
209
        self.corrupt = Image.core.path(0x4000000000000000)
210

211
    def __getitem__(self, i: int) -> bytes:
212
        x = self.corrupt[i]
213
        return struct.pack("dd", x[0], x[1])
214

215
    def __setitem__(self, i: int, x: bytes) -> None:
216
        self.corrupt[i] = struct.unpack("dd", x)
217

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.