Rename GP_Context -> GP_Pixmap
[gfxprim.git] / tests / core / Pixmap.c
blob417b0f3319e38ddc5ad3d9e8f310f8fec72812d0
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-2013 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Very basic GP_Pixmap tests.
28 #include <errno.h>
30 #include <core/GP_Pixmap.h>
32 #include "tst_test.h"
35 * Check that Pixmap is correctly allocated and freed
37 static int Pixmap_Alloc_Free(void)
39 GP_Pixmap *c;
41 c = GP_PixmapAlloc(100, 200, GP_PIXEL_RGB888);
43 if (c == NULL) {
44 tst_msg("GP_PixmapAlloc() failed");
45 return TST_FAILED;
48 /* Assert pixmap properties */
49 if (c->bpp != 24) {
50 tst_msg("Pixmap->bpp != 24 (== %i)", c->bpp);
51 return TST_FAILED;
54 if (c->bytes_per_row != 3 * c->w) {
55 tst_msg("Pixmap->bytes_per_row != %i (== %i)",
56 3 * c->w, c->bytes_per_row);
57 return TST_FAILED;
60 if (c->w != 100) {
61 tst_msg("Pixmap->w != 100 (== %i)", c->w);
62 return TST_FAILED;
65 if (c->h != 200) {
66 tst_msg("Pixmap->h != 200 (== %i)", c->h);
67 return TST_FAILED;
70 if (c->offset != 0) {
71 tst_msg("Pixmap->offset != 0");
72 return TST_FAILED;
75 if (c->pixel_type != GP_PIXEL_RGB888) {
76 tst_msg("Pixmap->pixel_type != GP_PIXEL_RGB888");
77 return TST_FAILED;
80 if (c->gamma != NULL) {
81 tst_msg("Pixmap->gamma != NULL");
82 return TST_FAILED;
85 if (c->axes_swap != 0 || c->x_swap != 0 || c->y_swap != 0) {
86 tst_msg("Wrong default orientation %i %i %i",
87 c->axes_swap, c->x_swap, c->y_swap);
88 return TST_FAILED;
91 /* access the pixel buffer */
92 *(char*)GP_PIXEL_ADDR(c, 0, 0) = 0;
93 *(char*)GP_PIXEL_ADDR(c, 100, 100) = 0;
95 GP_PixmapFree(c);
97 return TST_SUCCESS;
101 * Asserts that subpixmap structure is initialized correctly
103 static int subpixmap_assert(const GP_Pixmap *c, const GP_Pixmap *sc,
104 GP_Size w, GP_Size h)
106 if (c->bpp != sc->bpp) {
107 tst_msg("Pixmap->bpp != SubPixmap->bpp");
108 return TST_FAILED;
111 if (c->bytes_per_row != sc->bytes_per_row) {
112 tst_msg("Pixmap->bytes_per_row != SubPixmap->bytes_per_row");
113 return TST_FAILED;
116 if (sc->w != w) {
117 tst_msg("SubPixmap->w != %u (== %i)", w, sc->w);
118 return TST_FAILED;
121 if (sc->h != h) {
122 tst_msg("SubPixmap->h != %u (== %i)", h, sc->h);
123 return TST_FAILED;
126 if (sc->offset != 0) {
127 tst_msg("SubPixmap->offset != 0");
128 return TST_FAILED;
131 if (sc->pixel_type != GP_PIXEL_RGB888) {
132 tst_msg("SubPixmap->pixel_type != GP_PIXEL_RGB888");
133 return TST_FAILED;
136 if (sc->gamma != NULL) {
137 tst_msg("SubPixmap->gamma != NULL");
138 return TST_FAILED;
141 if (sc->axes_swap != 0 || sc->x_swap != 0 || sc->y_swap != 0) {
142 tst_msg("Wrong default orientation %i %i %i",
143 sc->axes_swap, sc->x_swap, sc->y_swap);
144 return TST_FAILED;
147 /* access the start and end of the pixel buffer */
148 *(char*)GP_PIXEL_ADDR(sc, 0, 0) = 0;
149 *(char*)GP_PIXEL_ADDR(sc, sc->w - 1, sc->h - 1) = 0;
151 return 0;
154 static int SubPixmap_Alloc_Free(void)
156 GP_Pixmap *c, *sc;
157 int ret;
159 c = GP_PixmapAlloc(300, 300, GP_PIXEL_RGB888);
161 if (c == NULL) {
162 tst_msg("GP_PixmapAlloc() failed");
163 return TST_UNTESTED;
166 sc = GP_SubPixmapAlloc(c, 100, 100, 100, 100);
168 if (sc == NULL) {
169 GP_PixmapFree(c);
170 return TST_FAILED;
173 ret = subpixmap_assert(c, sc, 100, 100);
175 if (ret)
176 return ret;
178 GP_PixmapFree(c);
179 GP_PixmapFree(sc);
181 return TST_SUCCESS;
184 static int SubPixmap_Create(void)
186 GP_Pixmap *c, sc;
187 int ret;
189 c = GP_PixmapAlloc(300, 300, GP_PIXEL_RGB888);
191 if (c == NULL) {
192 tst_msg("GP_PixmapAlloc() failed");
193 return TST_UNTESTED;
196 GP_SubPixmap(c, &sc, 100, 100, 100, 100);
198 ret = subpixmap_assert(c, &sc, 100, 100);
200 if (ret)
201 return ret;
203 GP_PixmapFree(c);
205 return TST_SUCCESS;
208 static int pixmap_zero_w(void)
210 GP_Pixmap *c;
212 c = GP_PixmapAlloc(0, 200, GP_PIXEL_G8);
214 if (c != NULL) {
215 tst_msg("Pixmap with zero width successfuly allocated");
216 return TST_FAILED;
219 if (errno != EINVAL) {
220 tst_msg("Expected errno set to EINVAL");
221 return TST_FAILED;
224 return TST_SUCCESS;
227 static int pixmap_zero_h(void)
229 GP_Pixmap *c;
231 c = GP_PixmapAlloc(200, 0, GP_PIXEL_G8);
233 if (c != NULL) {
234 tst_msg("Pixmap with zero height successfuly allocated");
235 return TST_FAILED;
238 if (errno != EINVAL) {
239 tst_msg("Expected errno set to EINVAL");
240 return TST_FAILED;
243 return TST_SUCCESS;
246 static int pixmap_invalid_pixeltype1(void)
248 GP_Pixmap *c;
250 c = GP_PixmapAlloc(100, 100, -1);
252 if (c != NULL) {
253 tst_msg("Pixmap with invalid pixel type (-1) succesfully allocated");
254 return TST_FAILED;
257 if (errno != EINVAL) {
258 tst_msg("Expected errno set to EINVAL");
259 return TST_FAILED;
262 return TST_SUCCESS;
265 static int pixmap_invalid_pixeltype2(void)
267 GP_Pixmap *c;
269 c = GP_PixmapAlloc(100, 100, GP_PIXEL_MAX + 1000);
271 if (c != NULL) {
272 tst_msg("Pixmap with invalid pixel type (-1) succesfully allocated");
273 return TST_FAILED;
276 if (errno != EINVAL) {
277 tst_msg("Expected errno set to EINVAL");
278 return TST_FAILED;
281 return TST_SUCCESS;
284 const struct tst_suite tst_suite = {
285 .suite_name = "Pixmap Testsuite",
286 .tests = {
287 {.name = "Pixmap Alloc Free", .tst_fn = Pixmap_Alloc_Free,
288 .flags = TST_CHECK_MALLOC},
289 {.name = "SubPixmap Alloc Free",
290 .tst_fn = SubPixmap_Alloc_Free,
291 .flags = TST_CHECK_MALLOC},
292 {.name = "SubPixmap Create",
293 .tst_fn = SubPixmap_Create},
294 {.name = "Pixmap Create w = 0",
295 .tst_fn = pixmap_zero_w},
296 {.name = "Pixmap Create h = 0",
297 .tst_fn = pixmap_zero_h},
298 {.name = "Pixmap Create pixel_type = -1",
299 .tst_fn = pixmap_invalid_pixeltype1},
300 {.name = "Pixmap Create invalid pixel_type",
301 .tst_fn = pixmap_invalid_pixeltype2},
302 {.name = NULL},