Rename GP_Context -> GP_Pixmap
[gfxprim.git] / tests / gfx / Line.c
blob25524acbce80ba006147ac23a4daacfc15c149b7
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include <string.h>
24 #include <errno.h>
25 #include <sys/stat.h>
27 #include <core/GP_Pixmap.h>
28 #include <gfx/GP_Line.h>
30 #include "tst_test.h"
32 #include "common.h"
34 struct testcase {
35 /* line description */
36 GP_Coord x0;
37 GP_Coord y0;
38 GP_Coord x1;
39 GP_Coord y1;
41 /* expected result */
42 GP_Size w, h;
43 const char pixmap[];
46 static int test_line(const struct testcase *t)
48 GP_Pixmap *c;
49 int err;
51 c = GP_PixmapAlloc(t->w, t->h, GP_PIXEL_G8);
53 if (c == NULL) {
54 tst_err("Failed to allocate pixmap");
55 return TST_UNTESTED;
58 /* zero the pixels buffer */
59 memset(c->pixels, 0, c->w * c->h);
61 GP_Line(c, t->x0, t->y0, t->x1, t->y1, 1);
63 err = compare_buffers(t->pixmap, c);
65 if (err)
66 return TST_FAILED;
68 return TST_SUCCESS;
71 static struct testcase testcase_line_1px = {
72 .x0 = 1,
73 .y0 = 1,
74 .x1 = 1,
75 .y1 = 1,
77 .w = 3,
78 .h = 3,
80 .pixmap = {
81 0, 0, 0,
82 0, 1, 0,
83 0, 0, 0,
87 static struct testcase testcase_line_horiz = {
88 .x0 = 2,
89 .y0 = 2,
90 .x1 = 8,
91 .y1 = 2,
93 .w = 11,
94 .h = 5,
96 .pixmap = {
97 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
100 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105 static struct testcase testcase_line_vert = {
106 .x0 = 2,
107 .y0 = 2,
108 .x1 = 2,
109 .y1 = 8,
111 .w = 5,
112 .h = 11,
114 .pixmap = {
115 0, 0, 0, 0, 0,
116 0, 0, 0, 0, 0,
117 0, 0, 1, 0, 0,
118 0, 0, 1, 0, 0,
119 0, 0, 1, 0, 0,
120 0, 0, 1, 0, 0,
121 0, 0, 1, 0, 0,
122 0, 0, 1, 0, 0,
123 0, 0, 1, 0, 0,
124 0, 0, 0, 0, 0,
125 0, 0, 0, 0, 0,
129 static struct testcase testcase_line_45 = {
130 .x0 = 0,
131 .y0 = 0,
132 .x1 = 10,
133 .y1 = 10,
135 .w = 11,
136 .h = 11,
138 .pixmap = {
139 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
140 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
141 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
142 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
143 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
144 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
145 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
146 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
147 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
148 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
153 static struct testcase testcase_line_15 = {
154 .x0 = 0,
155 .y0 = 1,
156 .x1 = 11,
157 .y1 = 6,
159 .w = 11,
160 .h = 8,
162 .pixmap = {
163 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0,
166 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
167 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
168 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
169 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174 static struct testcase testcase_line_clip = {
175 .x0 = -1000,
176 .y0 = -1000,
177 .x1 = 1000,
178 .y1 = 1000,
180 .w = 4,
181 .h = 4,
183 .pixmap = {
184 1, 0, 0, 0,
185 0, 1, 0, 0,
186 0, 0, 1, 0,
187 0, 0, 0, 1,
191 static struct testcase testcase_line_large_xy = {
192 .x0 = -1000000000,
193 .y0 = -1000000000,
194 .x1 = 1000000000,
195 .y1 = 1000000000,
197 .w = 4,
198 .h = 4,
200 .pixmap = {
201 1, 0, 0, 0,
202 0, 1, 0, 0,
203 0, 0, 1, 0,
204 0, 0, 0, 1,
208 const struct tst_suite tst_suite = {
209 .suite_name = "Line Testsuite",
210 .tests = {
211 {.name = "Line 1px",
212 .tst_fn = test_line,
213 .data = &testcase_line_1px},
215 {.name = "Line Horizontal",
216 .tst_fn = test_line,
217 .data = &testcase_line_horiz},
219 {.name = "Line Vertical",
220 .tst_fn = test_line,
221 .data = &testcase_line_vert},
223 {.name = "Line 45 degrees",
224 .tst_fn = test_line,
225 .data = &testcase_line_45},
227 {.name = "Line 15 degrees",
228 .tst_fn = test_line,
229 .data = &testcase_line_15},
231 {.name = "Line clipping",
232 .tst_fn = test_line,
233 .data = &testcase_line_clip},
235 {.name = "Line large coordinates",
236 .tst_fn = test_line,
237 .data = &testcase_line_large_xy,
238 .timeout = 1},
240 {.name = NULL}