File: rect_test.py

package info (click to toggle)
pysdl2 0.9.9%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,276 kB
  • sloc: python: 18,592; makefile: 148; sh: 40
file content (467 lines) | stat: -rw-r--r-- 16,352 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import sys
import copy
import pytest
import random
from ctypes import byref, c_int
import sdl2
from sdl2.stdinc import SDL_FALSE, SDL_TRUE
from sdl2 import rect

to_ctypes = lambda seq, dtype: (dtype * len(seq))(*seq)


class TestSDLPoint(object):
    __tags__ = ["sdl"]

    def test_SDL_Point(self):
        pt = rect.SDL_Point()
        assert (pt.x, pt.y) == (0, 0)
        for i in range(0, 100):
            x = random.randint(-1000, 1000)
            y = random.randint(-1000, 1000)
            pt = rect.SDL_Point(x, y)
            assert (pt.x, pt.y) == (x, y)

    def test_SDL_Point_xy(self):
        pt = rect.SDL_Point()
        for i in range(0, 50):
            x = random.randint(-1000, 1000)
            y = random.randint(-1000, 1000)
            pt.x = x
            pt.y = y
            assert (pt.x, pt.y) == (x, y)
        with pytest.raises(TypeError):
            pt.x = 10.4
        with pytest.raises(TypeError):
            pt.y = 10.4
        with pytest.raises(TypeError):
            pt.x = "point"
        with pytest.raises(TypeError):
            pt.y = "point"
        with pytest.raises(TypeError):
            pt.x = None
        with pytest.raises(TypeError):
            pt.y = None

    def test_SDL_Point__repr__(self):
        pt = rect.SDL_Point(10, 12)
        pt2 = eval("rect.%s" % repr(pt))
        assert pt == pt2
        assert (pt.x, pt.y) == (pt2.x, pt2.y)

    def test_SDL_Point__copy__(self):
        pt = rect.SDL_Point()
        pt2 = copy.copy(pt)
        assert pt == pt2
        assert (pt.x, pt.y) == (pt2.x, pt2.y)
        pt2.x = 7
        pt2.y = 9
        pt3 = copy.copy(pt2)
        assert pt != pt2
        assert pt3 == pt2

    def test_SDL_Point__eq__(self):
        assert rect.SDL_Point() == rect.SDL_Point()
        coords = [(0, 0), (10, 0), (0, 10), (12, 10), (7, 10)]
        for x1, y1 in coords:
            for x2, y2 in coords:
                equal = rect.SDL_FPoint(x1, y1) == rect.SDL_FPoint(x2, y2)
                assert equal if (x1 == x2 and y1 == y2) else not equal

    def test_SDL_Point__ne__(self):
        assert not rect.SDL_Point() != rect.SDL_Point()
        coords = [(0, 0), (10, 0), (0, 10), (12, 10), (7, 10)]
        for x1, y1 in coords:
            for x2, y2 in coords:
                notequal = rect.SDL_Point(x1, y1) != rect.SDL_Point(x2, y2)
                assert notequal if (x1 != x2 or y1 != y2) else not notequal



@pytest.mark.skipif(sdl2.dll.version < 2010, reason="not available")
class TestSDLFPoint(object):
    __tags__ = ["sdl"]

    def test_SDL_FPoint(self):
        pt = rect.SDL_FPoint()
        assert (pt.x, pt.y) == (0, 0)
        for i in range(0, 100):
            x = random.uniform(-1000, 1000)
            y = random.uniform(-1000, 1000)
            pt = rect.SDL_FPoint(x, y)
            assert (pt.x, pt.y) == pytest.approx((x, y))

    def test_SDL_FPoint_xy(self):
        pt = rect.SDL_FPoint()
        for i in range(0, 50):
            x = random.uniform(-1000, 1000)
            y = random.uniform(-1000, 1000)
            pt.x = x
            pt.y = y
            assert (pt.x, pt.y) == pytest.approx((x, y))
        with pytest.raises(TypeError):
            pt.x = "point"
        with pytest.raises(TypeError):
            pt.y = "point"
        with pytest.raises(TypeError):
            pt.x = None
        with pytest.raises(TypeError):
            pt.y = None

    def test_SDL_FPoint__repr__(self):
        pt = rect.SDL_FPoint(3.24, 12.8)
        pt2 = eval("rect.%s" % repr(pt))
        assert pt == pt2
        assert (pt.x, pt.y) == (pt2.x, pt2.y)

    def test_SDL_FPoint__copy__(self):
        pt = rect.SDL_FPoint()
        pt2 = copy.copy(pt)
        assert pt == pt2
        assert (pt.x, pt.y) == (pt2.x, pt2.y)
        pt2.x = 7
        pt2.y = 9
        pt3 = copy.copy(pt2)
        assert pt != pt2
        assert pt3 == pt2

    def test_SDL_FPoint__eq__(self):
        assert rect.SDL_FPoint() == rect.SDL_FPoint()
        coords = [(0, 0.5), (10, 0.5), (0, 10.5), (12, 10.5), (7, 10.5)]
        for x1, y1 in coords:
            for x2, y2 in coords:
                equal = rect.SDL_FPoint(x1, y1) == rect.SDL_FPoint(x2, y2)
                assert equal if (x1 == x2 and y1 == y2) else not equal

    def test_SDL_FPoint__ne__(self):
        assert not rect.SDL_FPoint() != rect.SDL_FPoint()
        coords = [(0, 0.5), (10, 0.5), (0, 10.5), (12, 10.5), (7, 10.5)]
        for x1, y1 in coords:
            for x2, y2 in coords:
                notequal = rect.SDL_FPoint(x1, y1) != rect.SDL_FPoint(x2, y2)
                assert notequal if (x1 != x2 or y1 != y2) else not notequal


class TestSDLRect(object):
    __tags__ = ["sdl"]

    def test_SDL_Rect(self):
        rt = rect.SDL_Rect()
        assert (rt.x, rt.y, rt.w, rt.h) == (0, 0, 0, 0)
        for i in range(0, 50):
            x = random.randint(-1000, 1000)
            y = random.randint(-1000, 1000)
            w = random.randint(-1000, 1000)
            h = random.randint(-1000, 1000)
            rt = rect.SDL_Rect(x, y, w, h)
            assert (rt.x, rt.y, rt.w, rt.h) == (x, y, w, h)

    def test_SDL_Rect_xywh(self):
        rt = rect.SDL_Rect()
        for i in range(0, 50):
            x = random.randint(-1000, 1000)
            y = random.randint(-1000, 1000)
            w = random.randint(-1000, 1000)
            h = random.randint(-1000, 1000)
            rt.x = x
            rt.y = y
            rt.w = w
            rt.h = h
            assert (rt.x, rt.y, rt.w, rt.h) == (x, y, w, h)
        with pytest.raises(TypeError):
            rt.x = 10.4
        with pytest.raises(TypeError):
            rt.y = 10.4
        with pytest.raises(TypeError):
            rt.w = 10.4
        with pytest.raises(TypeError):
            rt.h = 10.4
        with pytest.raises(TypeError):
            rt.x = "point"
        with pytest.raises(TypeError):
            rt.y = "point"
        with pytest.raises(TypeError):
            rt.w = "point"
        with pytest.raises(TypeError):
            rt.h = "point"
        with pytest.raises(TypeError):
            rt.x = None
        with pytest.raises(TypeError):
            rt.y = None
        with pytest.raises(TypeError):
            rt.w = None
        with pytest.raises(TypeError):
            rt.h = None

    def test_SDL_Rect__repr__(self):
        rt = rect.SDL_Rect(1, 2, 3, 4)
        rt2 = eval("rect.%s" % repr(rt))
        assert (rt.x, rt.y, rt.w, rt.h) == (rt2.x, rt2.y, rt2.w, rt2.h)
        assert rt == rt2

    def test_SDL_Rect__copy__(self):
        rt = rect.SDL_Rect()
        rt2 = copy.copy(rt)
        assert rt == rt2
        assert (rt.x, rt.y, rt.w, rt.h) == (rt2.x, rt2.y, rt2.w, rt2.h)
        rt2.x = 5
        rt2.y = 33
        rt2.w = 17
        rt2.w = 212
        rt3 = copy.copy(rt2)
        assert rt != rt2
        assert rt3 == rt2

    def test_SDL_Rect__eq__(self):
        sdlr = rect.SDL_Rect
        assert sdlr() == sdlr()
        rects = [
            (0, 0, 0, 0), (0, 0, 0, 1), (10, 0, 1, 1), (0, 10, 1, 1),
            (1, 2, 3, 4)
        ]
        for x1, y1, w1, h1 in rects:
            for x2, y2, w2, h2 in rects:
                same = x1 == x2 and y1 == y2 and w1 == w2 and h1 == h2
                equal = sdlr(x1, y1, w1, h1) == sdlr(x2, y2, w2, h2)
                assert equal if same else not equal

    def test_SDL_Rect__ne__(self):
        sdlr = rect.SDL_Rect
        assert sdlr() == sdlr()
        rects = [
            (0, 0, 0, 0), (0, 0, 0, 1), (10, 0, 1, 1), (0, 10, 1, 1),
            (1, 2, 3, 4)
        ]
        for x1, y1, w1, h1 in rects:
            for x2, y2, w2, h2 in rects:
                same = x1 == x2 and y1 == y2 and w1 == w2 and h1 == h2
                notequal = sdlr(x1, y1, w1, h1) != sdlr(x2, y2, w2, h2)
                assert notequal if same == False else not notequal


@pytest.mark.skipif(sdl2.dll.version < 2010, reason="not available")
class TestSDLFRect(object):
    __tags__ = ["sdl"]

    def test_SDL_FRect(self):
        rt = rect.SDL_FRect()
        assert (rt.x, rt.y, rt.w, rt.h) == (0, 0, 0, 0)
        for i in range(0, 50):
            x = random.uniform(-1000, 1000)
            y = random.uniform(-1000, 1000)
            w = random.uniform(-1000, 1000)
            h = random.uniform(-1000, 1000)
            rt = rect.SDL_FRect(x, y, w, h)
            assert (rt.x, rt.y, rt.w, rt.h) == pytest.approx((x, y, w, h))

    def test_SDL_FRect_xywh(self):
        rt = rect.SDL_FRect()
        for i in range(0, 50):
            x = random.uniform(-1000, 1000)
            y = random.uniform(-1000, 1000)
            w = random.uniform(-1000, 1000)
            h = random.uniform(-1000, 1000)
            rt.x = x
            rt.y = y
            rt.w = w
            rt.h = h
            assert (rt.x, rt.y, rt.w, rt.h) == pytest.approx((x, y, w, h))
        with pytest.raises(TypeError):
            rt.x = "point"
        with pytest.raises(TypeError):
            rt.y = "point"
        with pytest.raises(TypeError):
            rt.w = "point"
        with pytest.raises(TypeError):
            rt.h = "point"
        with pytest.raises(TypeError):
            rt.x = None
        with pytest.raises(TypeError):
            rt.y = None
        with pytest.raises(TypeError):
            rt.w = None
        with pytest.raises(TypeError):
            rt.h = None

    def test_SDL_FRect__repr__(self):
        rt = rect.SDL_FRect(1.5, 2.2, 3.8, 4.9)
        rt2 = eval("rect.%s" % repr(rt))
        assert (rt.x, rt.y, rt.w, rt.h) == (rt2.x, rt2.y, rt2.w, rt2.h)
        assert rt == rt2

    def test_SDL_FRect__copy__(self):
        rt = rect.SDL_FRect()
        rt2 = copy.copy(rt)
        assert rt == rt2
        assert (rt.x, rt.y, rt.w, rt.h) == (rt2.x, rt2.y, rt2.w, rt2.h)
        rt2.x = 5.5
        rt2.y = 33
        rt2.w = 17.2
        rt2.w = 212
        rt3 = copy.copy(rt2)
        assert rt != rt2
        assert rt3 == rt2

    def test_SDL_FRect__eq__(self):
        sdlr = rect.SDL_FRect
        assert sdlr() == sdlr()
        rects = [
            (0, 0.5, 1, 1), (10, 0.5, 99.9, 1.9), (0, 0.5, 99.9, 96),
            (-2.4, 0.5, 1, 1), (0, 0, 99.9, 1.9)
        ]
        for x1, y1, w1, h1 in rects:
            for x2, y2, w2, h2 in rects:
                same = x1 == x2 and y1 == y2 and w1 == w2 and h1 == h2
                equal = sdlr(x1, y1, w1, h1) == sdlr(x2, y2, w2, h2)
                assert equal if same else not equal

    def test_SDL_FRect__ne__(self):
        sdlr = rect.SDL_FRect
        assert sdlr() == sdlr()
        rects = [
            (0, 0.5, 1, 1), (10, 0.5, 99.9, 1.9), (0, 0.5, 99.9, 96),
            (-2.4, 0.5, 1, 1), (0, 0, 99.9, 1.9)
        ]
        for x1, y1, w1, h1 in rects:
            for x2, y2, w2, h2 in rects:
                same = x1 == x2 and y1 == y2 and w1 == w2 and h1 == h2
                notequal = sdlr(x1, y1, w1, h1) != sdlr(x2, y2, w2, h2)
                assert notequal if same == False else not notequal


def test_SDL_RectEmpty():
    for i in range(0, 50):
        w = random.randint(-100, 100)
        h = random.randint(-100, 100)
        r = rect.SDL_Rect(0, 0, w, h)
        empty = rect.SDL_RectEmpty(r)
        assert empty if not (w > 0 and h > 0) else not empty
    with pytest.raises(AttributeError):
        rect.SDL_RectEmpty("Test")

def test_SDL_RectEquals():
    r1 = rect.SDL_Rect(0, 0, 1, 1)
    r2 = rect.SDL_Rect(0, 0, 1, 1)
    assert rect.SDL_RectEquals(r1, r2)
    r2 = rect.SDL_Rect(-1, 2, 1, 1)
    assert not rect.SDL_RectEquals(r1, r2)
    r2 = rect.SDL_Rect(0, 0, 1, 2)
    assert not rect.SDL_RectEquals(r1, r2)
    # Test exceptions
    with pytest.raises(AttributeError):
        rect.SDL_RectEquals("Test", r2)
    with pytest.raises(AttributeError):
        rect.SDL_RectEquals(r1, None)

def test_SDL_UnionRect():
    tests = [
        [(0, 0, 10, 10), (20, 20, 10, 10), (0, 0, 30, 30)],
        [(0, 0, 0, 0), (20, 20, 10, 10), (20, 20, 10, 10)],
        [(-200, -4, 450, 33), (20, 20, 10, 10), (-200, -4, 450, 34)],
        [(0, 0, 15, 16), (20, 20, 0, 0), (0, 0, 15, 16)]
    ]
    out = rect.SDL_Rect()
    for rect1, rect2, expected in tests:
        r1 = rect.SDL_Rect(*rect1)
        r2 = rect.SDL_Rect(*rect2)
        rect.SDL_UnionRect(r1, r2, byref(out))
        assert (out.x, out.y, out.w, out.h) == expected
    # Test exceptions
    with pytest.raises((AttributeError, TypeError)):
        rect.SDL_UnionRect(None, None)
    with pytest.raises((AttributeError, TypeError)):
        rect.SDL_UnionRect("Test", r2)
    with pytest.raises((AttributeError, TypeError)):
        rect.SDL_UnionRect(r1, None)
    with pytest.raises((AttributeError, TypeError)):
        rect.SDL_UnionRect(r1, "Test")

def test_SDL_IntersectRectAndLine():
    tests = [
        [(0, 0, 0, 0), (-5, -5, 5, 5), SDL_FALSE, None],
        [(0, 0, 2, 2), (-1, -1, 3, 3), SDL_TRUE, (0, 0, 1, 1)],
        [(-4, -4, 14, 14), (8, 22, 8, 33), SDL_FALSE, None]
    ]
    for rect1, line, expected_ret, expected_coords in tests:
        r = rect.SDL_Rect(*rect1)
        x1, y1, x2, y2 = line
        x1, y1, x2, y2 = c_int(x1), c_int(y1), c_int(x2), c_int(y2)
        ret = rect.SDL_IntersectRectAndLine(
            r, byref(x1), byref(y1), byref(x2), byref(y2))
        assert ret == expected_ret
        if ret == SDL_TRUE:
            assert (x1.value, y1.value, x2.value, y2.value) == expected_coords

def test_SDL_EnclosePoints():
    tests = [
        [rect.SDL_Rect(0, 0, 10, 10), SDL_TRUE, (0, 0, 6, 8)],
        [rect.SDL_Rect(-10, -10, 3, 3), SDL_FALSE, (0, 0, 0, 0)],
        [None, SDL_TRUE, (0, 0, 6, 8)],
    ]
    pt1, pt2 = [rect.SDL_Point(0, 0), rect.SDL_Point(5, 7)]
    points = to_ctypes([pt1, pt2], rect.SDL_Point)
    res = rect.SDL_Rect()
    for clip, expected_ret, expected_rect in tests:
        clip_p = byref(clip) if isinstance(clip, rect.SDL_Rect) else None
        ret = rect.SDL_EnclosePoints(points, 2, clip_p, byref(res))
        assert ret == expected_ret
        r = rect.SDL_Rect(*expected_rect)
        assert res == r if ret == SDL_TRUE else res != r
    # Test with no points
    ret = rect.SDL_EnclosePoints(None, 0, None, byref(res))
    assert not ret
    assert res != rect.SDL_Rect()
    # Test expceptions
    with pytest.raises(TypeError):
        rect.SDL_EnclosePoints(None, None)
    with pytest.raises(TypeError):
        rect.SDL_EnclosePoints("Test", None)
    with pytest.raises(TypeError):
        rect.SDL_EnclosePoints((1, 2, 3), None)
    with pytest.raises(TypeError):
        rect.SDL_EnclosePoints((None,), None)

def test_SDL_HasIntersection():
    tests = [
        [(0, 0, 0, 0), (0, 0, 0, 0), SDL_FALSE],
        [(0, 0, -200, 200), (0, 0, -200, 200), SDL_FALSE],
        [(0, 0, 10, 10), (-5, 5, 10, 2), SDL_TRUE],
        [(0, 0, 10, 10), (-5, -5, 10, 2), SDL_FALSE],
        [(0, 0, 10, 10), (-5, -5, 2, 10), SDL_FALSE],
        [(0, 0, 10, 10), (-5, -5, 5, 5), SDL_FALSE],
        [(0, 0, 10, 10), (-5, -5, 6, 6), SDL_TRUE]
    ]
    for rect1, rect2, expected in tests:
        r1 = rect.SDL_Rect(*rect1)
        r2 = rect.SDL_Rect(*rect2)
        assert rect.SDL_HasIntersection(r1, r2) == expected

def test_SDL_IntersectRect():
    tests = [
        [(0, 0, 0, 0), (0, 0, 0, 0), SDL_FALSE, None],
        [(0, 0, -200, 200), (0, 0, -200, 200), SDL_FALSE, None],
        [(0, 0, 10, 10), (-5, 5, 10, 2), SDL_TRUE, (0, 5, 5, 2)],
        [(0, 0, 10, 10), (-5, -5, 10, 2), SDL_FALSE, None],
        [(0, 0, 10, 10), (-5, -5, 2, 10), SDL_FALSE, None],
        [(0, 0, 10, 10), (-5, -5, 5, 5), SDL_FALSE, None],
        [(0, 0, 10, 10), (-5, -5, 6, 6), SDL_TRUE, (0, 0, 1, 1)]
    ]
    res = rect.SDL_Rect()
    for rect1, rect2, expected_ret, expected_rect in tests:
        r1 = rect.SDL_Rect(*rect1)
        r2 = rect.SDL_Rect(*rect2)
        ret = rect.SDL_IntersectRect(r1, r2, byref(res))
        assert ret == expected_ret
        if ret == SDL_TRUE:
            res == rect.SDL_Rect(*expected_rect)

def test_SDL_PointInRect():
    r = rect.SDL_Rect(0, 0, 10, 10)
    inside = [(0, 0), (4, 2)]
    outside = [(10, 10), (10, 3), (3, 10), (-1, -3)]
    for x, y in inside:
        p = rect.SDL_Point(x, y)
        assert rect.SDL_PointInRect(p, r)
    for x, y in outside:
        p = rect.SDL_Point(x, y)
        assert not rect.SDL_PointInRect(p, r)