add test for --> MIME type (linked URL)
[platform/upstream/flac.git] / src / test_grabbag / picture / main.c
1 /* test_picture - Simple tester for cuesheet routines in grabbag
2  * Copyright (C) 2006  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <string.h>
24 #include "FLAC/assert.h"
25 #include "share/grabbag.h"
26
27 typedef struct {
28         const char *path;
29         const char *mime_type;
30         const char *description;
31         FLAC__uint32 width;
32         FLAC__uint32 height;
33         FLAC__uint32 depth;
34         FLAC__uint32 colors;
35 } PictureFile;
36
37 PictureFile picturefiles[] = {
38         { "0.gif", "image/gif" , "", 24, 24, 24, 2 },
39         { "1.gif", "image/gif" , "", 12,  8, 24, 256 },
40         { "2.gif", "image/gif" , "", 16, 14, 24, 128 },
41         { "0.jpg", "image/jpeg", "", 30, 20,  8, 0 },
42         { "4.jpg", "image/jpeg", "", 31, 47, 24, 0 },
43         { "0.png", "image/png" , "", 30, 20,  8, 0 },
44         { "1.png", "image/png" , "", 30, 20,  8, 0 },
45         { "2.png", "image/png" , "", 30, 20, 24, 7 },
46         { "3.png", "image/png" , "", 30, 20, 24, 7 },
47         { "4.png", "image/png" , "", 31, 47, 24, 0 },
48         { "5.png", "image/png" , "", 31, 47, 24, 0 },
49         { "6.png", "image/png" , "", 31, 47, 24, 23 },
50         { "7.png", "image/png" , "", 31, 47, 24, 23 },
51         { "8.png", "image/png" , "", 32, 32, 32, 0 }
52 };
53
54 static FLAC__bool debug_ = false;
55
56 static FLAC__bool failed_(const char *msg)
57 {
58     if(msg)
59         printf("FAILED, %s\n", msg);
60     else
61         printf("FAILED\n");
62
63     return false;
64 }
65
66 static FLAC__bool test_one_picture(const char *prefix, const PictureFile *pf, const char *res)
67 {
68         FLAC__StreamMetadata *obj;
69         const char *error;
70         char s[4096];
71         snprintf(s, sizeof(s)-1, "%s|%s|%s|%s/%s", pf->mime_type, pf->description, res, prefix, pf->path);
72
73         printf("testing grabbag__picture_parse_specification(\"%s\")... ", s);
74         if(0 == (obj = grabbag__picture_parse_specification(s, &error)))
75                 return failed_(error);
76         if(debug_) {
77                 printf("\nmime_type=%s\ndescription=%s\nwidth=%u\nheight=%u\ndepth=%u\ncolors=%u\ndata_length=%u\n",
78                         obj->data.picture.mime_type,
79                         obj->data.picture.description,
80                         obj->data.picture.width,
81                         obj->data.picture.height,
82                         obj->data.picture.depth,
83                         obj->data.picture.colors,
84                         obj->data.picture.data_length
85                 );
86         }
87         if(strcmp(obj->data.picture.mime_type, pf->mime_type))
88                 return failed_("picture MIME type mismatch");
89         if(strcmp((const char *)obj->data.picture.description, (const char *)pf->description))
90                 return failed_("picture description mismatch");
91         if(obj->data.picture.width != pf->width)
92                 return failed_("picture width mismatch");
93         if(obj->data.picture.height != pf->height)
94                 return failed_("picture height mismatch");
95         if(obj->data.picture.depth != pf->depth)
96                 return failed_("picture depth mismatch");
97         if(obj->data.picture.colors != pf->colors)
98                 return failed_("picture colors mismatch");
99         printf("OK\n");
100         FLAC__metadata_object_delete(obj);
101         return true;
102 }
103
104 static FLAC__bool do_picture(const char *prefix)
105 {
106         FLAC__StreamMetadata *obj;
107         const char *error;
108         size_t i;
109
110     printf("\n+++ grabbag unit test: picture\n\n");
111
112         /* invalid spec: no filename */
113         printf("testing grabbag__picture_parse_specification(\"\")... ");
114         if(0 != (obj = grabbag__picture_parse_specification("", &error)))
115                 return failed_("expected error, got object");
116         printf("OK (failed as expected, error: %s)\n", error);
117
118         /* invalid spec: no filename */
119         printf("testing grabbag__picture_parse_specification(\"|||\")... ");
120         if(0 != (obj = grabbag__picture_parse_specification("|||", &error)))
121                 return failed_("expected error, got object");
122         printf("OK (failed as expected: %s)\n", error);
123
124         /* invalid spec: no filename */
125         printf("testing grabbag__picture_parse_specification(\"image/gif|||\")... ");
126         if(0 != (obj = grabbag__picture_parse_specification("image/gif|||", &error)))
127                 return failed_("expected error, got object");
128         printf("OK (failed as expected: %s)\n", error);
129
130         /* invalid spec: bad resolution */
131         printf("testing grabbag__picture_parse_specification(\"image/gif|desc|320|0.gif\")... ");
132         if(0 != (obj = grabbag__picture_parse_specification("image/gif|desc|320|0.gif", &error)))
133                 return failed_("expected error, got object");
134         printf("OK (failed as expected: %s)\n", error);
135
136         /* invalid spec: bad resolution */
137         printf("testing grabbag__picture_parse_specification(\"image/gif|desc|320x240|0.gif\")... ");
138         if(0 != (obj = grabbag__picture_parse_specification("image/gif|desc|320x240|0.gif", &error)))
139                 return failed_("expected error, got object");
140         printf("OK (failed as expected: %s)\n", error);
141
142         /* invalid spec: no filename */
143         printf("testing grabbag__picture_parse_specification(\"image/gif|desc|320x240x9|\")... ");
144         if(0 != (obj = grabbag__picture_parse_specification("image/gif|desc|320x240x9|", &error)))
145                 return failed_("expected error, got object");
146         printf("OK (failed as expected: %s)\n", error);
147
148         /* invalid spec: #colors exceeds color depth */
149         printf("testing grabbag__picture_parse_specification(\"image/gif|desc|320x240x9/2345|0.gif\")... ");
150         if(0 != (obj = grabbag__picture_parse_specification("image/gif|desc|320x240x9/2345|0.gif", &error)))
151                 return failed_("expected error, got object");
152         printf("OK (failed as expected: %s)\n", error);
153
154         /* invalid spec: need resolution for linked URL */
155         printf("testing grabbag__picture_parse_specification(\"-->|desc||http://blah.blah.blah/z.gif\")... ");
156         if(0 != (obj = grabbag__picture_parse_specification("-->|desc||http://blah.blah.blah/z.gif", &error)))
157                 return failed_("expected error, got object");
158         printf("OK (failed as expected: %s)\n", error);
159
160         printf("testing grabbag__picture_parse_specification(\"-->|desc|320x240x9|http://blah.blah.blah/z.gif\")... ");
161         if(0 == (obj = grabbag__picture_parse_specification("-->|desc|320x240x9|http://blah.blah.blah/z.gif", &error)))
162                 return failed_(error);
163         printf("OK\n");
164
165         /* test automatic parsing of picture files to get resolution/color info */
166         for(i = 0; i < sizeof(picturefiles)/sizeof(picturefiles[0]); i++)
167                 if(!test_one_picture(prefix, picturefiles+i, ""))
168                         return false;
169
170         picturefiles[0].width = 320;
171         picturefiles[0].height = 240;
172         picturefiles[0].depth = 3;
173         picturefiles[0].colors = 2;
174         if(!test_one_picture(prefix, picturefiles+0, "320x240x3/2"))
175                 return false;
176
177         return true;
178 }
179
180 int main(int argc, char *argv[])
181 {
182         const char *usage = "usage: test_pictures path_prefix\n";
183
184         if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
185                 printf(usage);
186                 return 0;
187         }
188
189         if(argc != 2) {
190                 fprintf(stderr, usage);
191                 return 255;
192         }
193
194         return do_picture(argv[1])? 0 : 1;
195 }