Mercurial > sdl-ios-xcode
comparison test/automated/rect/rect.c @ 3541:0c429a5fda8a
Added an automated test for rectangle routines, currently only testing line clipping.
Use the Cohen-Sutherland algorithm for line clipping which uses integer math and preserves ordering of clipped points.
Removed getopt() support in testsdl.c, replaced with simple argv scanning.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Fri, 11 Dec 2009 09:22:34 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3540:3ad09fdbfcb0 | 3541:0c429a5fda8a |
---|---|
1 /** | |
2 * Automated SDL rect test. | |
3 * | |
4 * Written by Edgar Simo "bobbens" | |
5 * | |
6 * Released under Public Domain. | |
7 */ | |
8 | |
9 | |
10 | |
11 | |
12 #include "SDL_rect.h" | |
13 #include "../SDL_at.h" | |
14 | |
15 | |
16 /* | |
17 * Prototypes. | |
18 */ | |
19 static void rect_testIntersectRectAndLine (void); | |
20 | |
21 | |
22 /** | |
23 * @brief Tests SDL_IntersectRectAndLine() | |
24 */ | |
25 static void rect_testIntersectRectAndLine (void) | |
26 { | |
27 SDL_Rect rect = { 0, 0, 32, 32 }; | |
28 int x1, y1; | |
29 int x2, y2; | |
30 SDL_bool clipped; | |
31 | |
32 SDL_ATbegin( "IntersectRectAndLine" ); | |
33 | |
34 x1 = -10; | |
35 y1 = 0; | |
36 x2 = -10; | |
37 y2 = 31; | |
38 clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2); | |
39 SDL_ATvassert( !clipped && | |
40 x1 == -10 && y1 == 0 && x2 == -10 && y2 == 31, | |
41 "line outside to the left was incorrectly clipped: %d,%d - %d,%d", | |
42 x1, y1, x2, y2); | |
43 | |
44 x1 = 40; | |
45 y1 = 0; | |
46 x2 = 40; | |
47 y2 = 31; | |
48 clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2); | |
49 SDL_ATvassert( !clipped && | |
50 x1 == 40 && y1 == 0 && x2 == 40 && y2 == 31, | |
51 "line outside to the right was incorrectly clipped: %d,%d - %d,%d", | |
52 x1, y1, x2, y2); | |
53 | |
54 x1 = 0; | |
55 y1 = -10; | |
56 x2 = 31; | |
57 y2 = -10; | |
58 clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2); | |
59 SDL_ATvassert( !clipped && | |
60 x1 == 0 && y1 == -10 && x2 == 31 && y2 == -10, | |
61 "line outside above was incorrectly clipped: %d,%d - %d,%d", | |
62 x1, y1, x2, y2); | |
63 | |
64 x1 = 0; | |
65 y1 = 40; | |
66 x2 = 31; | |
67 y2 = 40; | |
68 clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2); | |
69 SDL_ATvassert( !clipped && | |
70 x1 == 0 && y1 == 40 && x2 == 31 && y2 == 40, | |
71 "line outside below was incorrectly clipped: %d,%d - %d,%d", | |
72 x1, y1, x2, y2); | |
73 | |
74 x1 = 0; | |
75 y1 = 0; | |
76 x2 = 31; | |
77 y2 = 31; | |
78 clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2); | |
79 SDL_ATvassert( clipped && | |
80 x1 == 0 && y1 == 0 && x2 == 31 && y2 == 31, | |
81 "line fully inside rect was clipped: %d,%d - %d,%d", | |
82 x1, y1, x2, y2); | |
83 | |
84 x1 = -10; | |
85 y1 = 15; | |
86 x2 = 40; | |
87 y2 = 15; | |
88 clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2); | |
89 SDL_ATvassert( clipped && | |
90 x1 == 0 && y1 == 15 && x2 == 31 && y2 == 15, | |
91 "horizontal line rect was incorrectly clipped: %d,%d - %d,%d", | |
92 x1, y1, x2, y2); | |
93 | |
94 x1 = -32; | |
95 y1 = -32; | |
96 x2 = 63; | |
97 y2 = 63; | |
98 clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2); | |
99 SDL_ATvassert( clipped && | |
100 x1 == 0 && y1 == 0 && x2 == 31 && y2 == 31, | |
101 "diagonal line to lower right was incorrectly clipped: %d,%d - %d,%d", | |
102 x1, y1, x2, y2); | |
103 | |
104 x1 = 63; | |
105 y1 = 63; | |
106 x2 = -32; | |
107 y2 = -32; | |
108 clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2); | |
109 SDL_ATvassert( clipped && | |
110 x1 == 31 && y1 == 31 && x2 == 0 && y2 == 0, | |
111 "diagonal line to upper left was incorrectly clipped: %d,%d - %d,%d", | |
112 x1, y1, x2, y2); | |
113 | |
114 x1 = 63; | |
115 y1 = -32; | |
116 x2 = -32; | |
117 y2 = 63; | |
118 clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2); | |
119 SDL_ATvassert( clipped && | |
120 x1 == 31 && y1 == 0 && x2 == 0 && y2 == 31, | |
121 "diagonal line to lower left was incorrectly clipped: %d,%d - %d,%d", | |
122 x1, y1, x2, y2); | |
123 | |
124 x1 = -32; | |
125 y1 = 63; | |
126 x2 = 63; | |
127 y2 = -32; | |
128 clipped = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2); | |
129 SDL_ATvassert( clipped && | |
130 x1 == 0 && y1 == 31 && x2 == 31 && y2 == 0, | |
131 "diagonal line to upper right was incorrectly clipped: %d,%d - %d,%d", | |
132 x1, y1, x2, y2); | |
133 | |
134 SDL_ATend(); | |
135 } | |
136 | |
137 | |
138 /** | |
139 * @brief Rect test entrypoint. | |
140 */ | |
141 #ifdef TEST_STANDALONE | |
142 int main( int argc, const char *argv[] ) | |
143 { | |
144 (void) argc; | |
145 (void) argv; | |
146 #else /* TEST_STANDALONE */ | |
147 int test_rect (void) | |
148 { | |
149 #endif /* TEST_STANDALONE */ | |
150 | |
151 SDL_ATinit( "Rect" ); | |
152 | |
153 rect_testIntersectRectAndLine(); | |
154 | |
155 return SDL_ATfinish(); | |
156 } |