Rename GP_Context -> GP_Pixmap
[gfxprim.git] / tests / loaders / Loader.h
blob475af5ec80732fd9a12df5b91e380e7c7002da91
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-2014 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #ifndef TESTS_LOADER_H
24 #define TESTS_LOADER_H
26 #include "loaders/GP_IO.h"
28 struct testcase {
29 GP_Size w;
30 GP_Size h;
31 GP_Pixel pix;
32 char *path;
35 static int test_check(struct testcase *test, GP_Pixmap *img)
37 unsigned int x, y, err = 0;
39 if (img->w != test->w || img->h != test->h) {
40 tst_msg("Invalid image size have %ux%u expected %ux%u",
41 img->w, img->h, test->w, test->h);
42 GP_PixmapFree(img);
43 return TST_FAILED;
46 for (x = 0; x < img->w; x++) {
47 for (y = 0; y < img->h; y++) {
49 GP_Pixel pix = GP_GetPixel(img, x, y);
51 if (pix != test->pix) {
52 if (err < 5)
53 tst_msg("%08x instead of %08x (%ux%u)",
54 pix, test->pix, x, y);
55 err++;
60 if (err > 5)
61 tst_msg("And %u errors...", err);
63 if (err)
64 return TST_FAILED;
66 return TST_SUCCESS;
69 static int test_read(struct testcase *test)
71 GP_Pixmap *img;
72 GP_IO *io;
73 int err;
75 io = GP_IOMem(test->path, strlen(test->path), NULL);
77 if (!io) {
78 tst_msg("Failed to initialize memory IO: %s", strerror(errno));
79 return TST_UNTESTED;
82 img = READ(io, NULL);
84 if (!img) {
85 switch (errno) {
86 case ENOSYS:
87 tst_msg("Not Implemented");
88 err = TST_SKIPPED;
89 goto out;
90 default:
91 tst_msg("Got %s", strerror(errno));
92 err = TST_FAILED;
93 goto out;
97 err = test_check(test, img);
99 GP_PixmapFree(img);
100 out:
101 GP_IOClose(io);
102 return err;
105 # ifdef LOAD
107 static int test_load(struct testcase *test)
109 GP_Pixmap *img;
110 int err;
112 errno = 0;
114 img = LOAD(test->path, NULL);
116 if (!img) {
117 switch (errno) {
118 case ENOSYS:
119 tst_msg("Not Implemented");
120 return TST_SKIPPED;
121 default:
122 tst_msg("Got %s", strerror(errno));
123 return TST_FAILED;
127 err = test_check(test, img);
129 GP_PixmapFree(img);
131 return err;
135 static int test_load_fail(const char *path)
137 GP_Pixmap *img;
139 errno = 0;
141 img = LOAD(path, NULL);
143 if (img != NULL) {
144 tst_msg("Succeeded unexpectedly");
145 GP_PixmapFree(img);
146 return TST_FAILED;
149 switch (errno) {
150 case ENOSYS:
151 tst_msg("Not Implemented");
152 return TST_SKIPPED;
153 case 0:
154 tst_msg("Load failed and errno == 0");
155 return TST_FAILED;
156 default:
157 tst_msg("Got %s", strerror(errno));
158 return TST_SUCCESS;
162 # endif /* LOAD */
165 # if defined(SAVE) && defined(LOAD)
168 * Saves and loads image using the SAVE and LOAD functions
169 * and compares the results.
171 struct testcase_save_load {
172 GP_PixelType pixel_type;
173 GP_Size w, h;
176 static int test_save_load(struct testcase_save_load *test)
178 GP_Pixmap *img, *img2;
179 unsigned int x, y;
181 img = GP_PixmapAlloc(test->w, test->h, test->pixel_type);
183 if (img == NULL) {
184 tst_msg("Failed to allocate pixmap %ux%u %s",
185 test->w, test->h, GP_PixelTypeName(test->pixel_type));
186 return TST_FAILED;
189 for (x = 0; x < img->w; x++)
190 for (y = 0; y < img->w; y++)
191 GP_PutPixel(img, x, y, 0);
193 errno = 0;
195 if (SAVE(img, "testfile", NULL)) {
196 if (errno == ENOSYS) {
197 tst_msg("Not implemented");
198 return TST_SKIPPED;
201 tst_msg("Failed to save pixmap %ux%u %s: %s",
202 test->w, test->h, GP_PixelTypeName(test->pixel_type),
203 strerror(errno));
204 return TST_FAILED;
208 img2 = LOAD("testfile", NULL);
210 if (img2 == NULL) {
211 switch (errno) {
212 case ENOSYS:
213 tst_msg("Not Implemented");
214 GP_PixmapFree(img);
215 return TST_SKIPPED;
216 default:
217 tst_msg("Got %s", strerror(errno));
218 GP_PixmapFree(img);
219 return TST_FAILED;
223 if (img->w != img2->w || img->h != img2->h) {
224 tst_msg("Source size %ux%u and loaded size %ux%u differs",
225 img->w, img->h, img2->w, img2->h);
226 GP_PixmapFree(img);
227 GP_PixmapFree(img2);
228 return TST_FAILED;
231 if (img->pixel_type != img2->pixel_type) {
232 GP_PixmapFree(img);
233 GP_PixmapFree(img2);
234 tst_msg("Source pixel type %s and loaded type %s differs",
235 GP_PixelTypeName(img->pixel_type),
236 GP_PixelTypeName(img2->pixel_type));
237 return TST_FAILED;
240 if (GP_GetPixel(img2, 0, 0) != 0) {
241 tst_msg("Pixel value is wrong %x", GP_GetPixel(img2, 0, 0));
242 GP_PixmapFree(img);
243 GP_PixmapFree(img2);
244 return TST_FAILED;
247 GP_PixmapFree(img);
248 GP_PixmapFree(img2);
250 return TST_SUCCESS;
253 # endif /* SAVE && LOAD */
255 #endif /* TESTS_LOADER_H */