This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
testrect.c
167 lines (143 loc) · 4.61 KB
/
testrect.c
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
/**
* Original code: automated SDL rect test written by Edgar Simo "bobbens"
*/
#include <stdio.h>
#include <SDL/SDL.h>
#include "../../include/SDL_test.h"
/* Test cases */
static const TestCaseReference test1 =
(TestCaseReference){ "rect_testIntersectRectAndLine", "description", TEST_ENABLED, 0, 0 };
static const TestCaseReference test2 =
(TestCaseReference){ "rect_testIntersectRectAndLineFuzzed", "Tests rect to line intersection with fuzzed values", TEST_ENABLED, 0, 0 };
/* Test suite */
extern const TestCaseReference *testSuite[] = {
&test1, &test2, NULL
};
TestCaseReference **QueryTestSuite() {
return (TestCaseReference **)testSuite;
}
/*!
* \brief Tests SDL_IntersectRectAndLine()
*
* \sa
* http://wiki.libsdl.org/moin.cgi/SDL_IntersectRectAndLine
*/
int rect_testIntersectRectAndLine (void *arg)
{
SDL_Rect rect = { 0, 0, 32, 32 };
int x1, y1;
int x2, y2;
SDL_bool clipped;
x1 = -10;
y1 = 0;
x2 = -10;
y2 = 31;
clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
AssertTrue( !clipped &&
x1 == -10 && y1 == 0 && x2 == -10 && y2 == 31,
"line outside to the left was incorrectly clipped: %d,%d - %d,%d",
x1, y1, x2, y2);
x1 = 40;
y1 = 0;
x2 = 40;
y2 = 31;
clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
AssertTrue( !clipped &&
x1 == 40 && y1 == 0 && x2 == 40 && y2 == 31,
"line outside to the right was incorrectly clipped: %d,%d - %d,%d",
x1, y1, x2, y2);
x1 = 0;
y1 = -10;
x2 = 31;
y2 = -10;
clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
AssertTrue( !clipped &&
x1 == 0 && y1 == -10 && x2 == 31 && y2 == -10,
"line outside above was incorrectly clipped: %d,%d - %d,%d",
x1, y1, x2, y2);
x1 = 0;
y1 = 40;
x2 = 31;
y2 = 40;
clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
AssertTrue( !clipped &&
x1 == 0 && y1 == 40 && x2 == 31 && y2 == 40,
"line outside below was incorrectly clipped: %d,%d - %d,%d",
x1, y1, x2, y2);
x1 = 0;
y1 = 0;
x2 = 31;
y2 = 31;
clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
AssertTrue( clipped &&
x1 == 0 && y1 == 0 && x2 == 31 && y2 == 31,
"line fully inside rect was clipped: %d,%d - %d,%d",
x1, y1, x2, y2);
x1 = -10;
y1 = 15;
x2 = 40;
y2 = 15;
clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
AssertTrue( clipped &&
x1 == 0 && y1 == 15 && x2 == 31 && y2 == 15,
"horizontal line rect was incorrectly clipped: %d,%d - %d,%d",
x1, y1, x2, y2);
x1 = -32;
y1 = -32;
x2 = 63;
y2 = 63;
clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
AssertTrue( clipped &&
x1 == 0 && y1 == 0 && x2 == 31 && y2 == 31,
"diagonal line to lower right was incorrectly clipped: %d,%d - %d,%d",
x1, y1, x2, y2);
x1 = 63;
y1 = 63;
x2 = -32;
y2 = -32;
clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
AssertTrue( clipped &&
x1 == 31 && y1 == 31 && x2 == 0 && y2 == 0,
"diagonal line to upper left was incorrectly clipped: %d,%d - %d,%d",
x1, y1, x2, y2);
x1 = 63;
y1 = -32;
x2 = -32;
y2 = 63;
clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
AssertTrue( clipped &&
x1 == 31 && y1 == 0 && x2 == 0 && y2 == 31,
"diagonal line to lower left was incorrectly clipped: %d,%d - %d,%d",
x1, y1, x2, y2);
x1 = -32;
y1 = 63;
x2 = 63;
y2 = -32;
clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
AssertTrue( clipped &&
x1 == 0 && y1 == 31 && x2 == 31 && y2 == 0,
"diagonal line to upper right was incorrectly clipped: %d,%d - %d,%d",
x1, y1, x2, y2);
}
/*!
* \brief Tests SDL_IntersectRectAndLine()
*
* \sa
* http://wiki.libsdl.org/moin.cgi/SDL_IntersectRectAndLine
*/
int rect_testIntersectRectAndLineFuzzed(void *arg)
{
SDL_Rect rect = { 0, 0, RandomInteger(), RandomInteger() };
int x1, y1;
int x2, y2;
SDL_bool clipped;
x1 = -RandomInteger();
y1 = RandomInteger();
x2 = -RandomInteger();
y2 = RandomInteger();
clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
AssertTrue( !clipped,
/*&& x1 == -10 && y1 == 0 && x2 == -10 && y2 == 31, */
"line outside to the left was incorrectly clipped: %d,%d - %d,%d",
x1, y1, x2, y2);
}