Rename GP_Context -> GP_Pixmap
[gfxprim.git] / tests / loaders / SaveLoad.gen.c.t
blob12d24780e3a0fe2addf41be5299ef23dd53a538e
1 @ include source.t
2 @ include savers.t
3 /*
4  * Iterate over all pixel types, try to save and load back pixmap.
5  *
6  * Copyright (C) 2009-2014 Cyril Hrubis <metan@ucw.cz>
7  */
9 #include <string.h>
10 #include <errno.h>
11 #include <stdlib.h>
13 #include <core/GP_Pixmap.h>
14 #include <core/GP_GetPutPixel.h>
15 #include <loaders/GP_Loaders.h>
17 #include "tst_test.h"
19 typedef int (*Save)(const GP_Pixmap *src, const char *path, GP_ProgressCallback *callback);
20 typedef GP_Pixmap *(*Load)(const char *path, GP_ProgressCallback *callback);
22 static int test(Save Saver, Load Loader, GP_PixelType pixel_type)
24         GP_Pixmap *src;
25         GP_Pixmap *res;
26         unsigned int x, y;
27         int ret = TST_SUCCESS;
29         src = GP_PixmapAlloc(100, 100, pixel_type);
31         if (!src) {
32                 tst_msg("Malloc failed");
33                 return TST_UNTESTED;
34         }
36         for (x = 0; x < src->w; x++)
37                 for (y = 0; y < src->w; y++)
38                         GP_PutPixel(src, x, y, 0);
40         if (Saver(src, "testfile", NULL)) {
41                 if (errno == ENOSYS) {
42                         tst_msg("Unimplemented pixel value");
43                         ret = TST_SKIPPED;
44                         goto err;
45                 }
47                 if (errno == EINVAL) {
48                         tst_msg("Invalid pixel value for the format");
49                         ret = TST_SKIPPED;
50                         goto err;
51                 }
53                 tst_msg("Saver failed with %s", strerror(errno));
54                 ret = TST_FAILED;
55                 goto err;
56         }
58         res = Loader("testfile", NULL);
60         if (!res) {
61                 tst_msg("Failed to load saved image");
62                 ret = TST_FAILED;
63                 goto err;
64         }
66         tst_msg("Loaded back as %s", GP_PixelTypeName(res->pixel_type));
68         if (res->w != src->w || res->h != src->h) {
69                 tst_msg("Invalid loaded image size %ux%u", res->w, res->h);
70                 ret = TST_FAILED;
71         }
73         if (GP_GetPixel(res, 0, 0) != 0) {
74                 tst_msg("Pixel value is wrong %x", GP_GetPixel(res, 0, 0));
75                 ret = TST_FAILED;
76         }
78         GP_PixmapFree(res);
79 err:
80         GP_PixmapFree(src);
81         return ret;
84 @ for fmt in fmts:
85 @     for pt in pixeltypes:
86 @         if not pt.is_unknown():
87 static int test_{{ fmt }}_{{ pt.name }}(void)
89         return test(GP_Save{{ fmt }}, GP_Load{{ fmt }}, GP_PIXEL_{{ pt.name }});
92 @ end
94 const struct tst_suite tst_suite = {
95         .suite_name = "SaveLoad",
96         .tests = {
97 @ for fmt in fmts:
98 @     for pt in pixeltypes:
99 @         if not pt.is_unknown():
100                 {.name = "{{ fmt }} {{ pt.name }}",
101                  .tst_fn = test_{{ fmt }}_{{ pt.name }},
102                  .flags = TST_TMPDIR | TST_MALLOC_CANARIES},
103 @ end
104                 {.name = NULL},
105         }