1 /* test_picture - Simple tester for picture routines in grabbag
2 * Copyright (C) 2006,2007,2008,2009 Josh Coalson
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.
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.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "FLAC/assert.h"
25 #include "share/grabbag.h"
29 const char *mime_type;
30 const char *description;
35 FLAC__StreamMetadata_Picture_Type type;
38 PictureFile picturefiles[] = {
39 { "0.gif", "image/gif" , "", 24, 24, 24, 2, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
40 { "1.gif", "image/gif" , "", 12, 8, 24, 256, FLAC__STREAM_METADATA_PICTURE_TYPE_BACK_COVER },
41 { "2.gif", "image/gif" , "", 16, 14, 24, 128, FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER },
42 { "0.jpg", "image/jpeg", "", 30, 20, 8, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
43 { "4.jpg", "image/jpeg", "", 31, 47, 24, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
44 { "0.png", "image/png" , "", 30, 20, 8, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
45 { "1.png", "image/png" , "", 30, 20, 8, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
46 { "2.png", "image/png" , "", 30, 20, 24, 7, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
47 { "3.png", "image/png" , "", 30, 20, 24, 7, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
48 { "4.png", "image/png" , "", 31, 47, 24, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
49 { "5.png", "image/png" , "", 31, 47, 24, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
50 { "6.png", "image/png" , "", 31, 47, 24, 23, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
51 { "7.png", "image/png" , "", 31, 47, 24, 23, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
52 { "8.png", "image/png" , "", 32, 32, 32, 0, 999 }
55 static FLAC__bool debug_ = false;
57 static FLAC__bool failed_(const char *msg)
60 printf("FAILED, %s\n", msg);
67 static FLAC__bool test_one_picture(const char *prefix, const PictureFile *pf, const char *res, FLAC__bool fn_only)
69 FLAC__StreamMetadata *obj;
73 flac_snprintf(s, sizeof(s), "%s/%s", prefix, pf->path);
75 flac_snprintf(s, sizeof(s), "%u|%s|%s|%s|%s/%s", (unsigned)pf->type, pf->mime_type, pf->description, res, prefix, pf->path);
77 printf("testing grabbag__picture_parse_specification(\"%s\")... ", s);
78 if(0 == (obj = grabbag__picture_parse_specification(s, &error)))
79 return failed_(error);
81 printf("\ntype=%u (%s)\nmime_type=%s\ndescription=%s\nwidth=%u\nheight=%u\ndepth=%u\ncolors=%u\ndata_length=%u\n",
82 obj->data.picture.type,
83 obj->data.picture.type < FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED?
84 FLAC__StreamMetadata_Picture_TypeString[obj->data.picture.type] : "UNDEFINED",
85 obj->data.picture.mime_type,
86 obj->data.picture.description,
87 obj->data.picture.width,
88 obj->data.picture.height,
89 obj->data.picture.depth,
90 obj->data.picture.colors,
91 obj->data.picture.data_length
94 if(obj->data.picture.type != (fn_only? FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER : pf->type))
95 return failed_("picture type mismatch");
96 if(strcmp(obj->data.picture.mime_type, pf->mime_type))
97 return failed_("picture MIME type mismatch");
98 if(strcmp((const char *)obj->data.picture.description, (const char *)pf->description))
99 return failed_("picture description mismatch");
100 if(obj->data.picture.width != pf->width)
101 return failed_("picture width mismatch");
102 if(obj->data.picture.height != pf->height)
103 return failed_("picture height mismatch");
104 if(obj->data.picture.depth != pf->depth)
105 return failed_("picture depth mismatch");
106 if(obj->data.picture.colors != pf->colors)
107 return failed_("picture colors mismatch");
109 FLAC__metadata_object_delete(obj);
113 static FLAC__bool do_picture(const char *prefix)
115 FLAC__StreamMetadata *obj;
119 printf("\n+++ grabbag unit test: picture\n\n");
121 /* invalid spec: no filename */
122 printf("testing grabbag__picture_parse_specification(\"\")... ");
123 if(0 != (obj = grabbag__picture_parse_specification("", &error)))
124 return failed_("expected error, got object");
125 printf("OK (failed as expected, error: %s)\n", error);
127 /* invalid spec: no filename */
128 printf("testing grabbag__picture_parse_specification(\"||||\")... ");
129 if(0 != (obj = grabbag__picture_parse_specification("||||", &error)))
130 return failed_("expected error, got object");
131 printf("OK (failed as expected: %s)\n", error);
133 /* invalid spec: no filename */
134 printf("testing grabbag__picture_parse_specification(\"|image/gif|||\")... ");
135 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|||", &error)))
136 return failed_("expected error, got object");
137 printf("OK (failed as expected: %s)\n", error);
139 /* invalid spec: bad resolution */
140 printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320|0.gif\")... ");
141 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320|0.gif", &error)))
142 return failed_("expected error, got object");
143 printf("OK (failed as expected: %s)\n", error);
145 /* invalid spec: bad resolution */
146 printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320x240|0.gif\")... ");
147 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320x240|0.gif", &error)))
148 return failed_("expected error, got object");
149 printf("OK (failed as expected: %s)\n", error);
151 /* invalid spec: no filename */
152 printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320x240x9|\")... ");
153 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320x240x9|", &error)))
154 return failed_("expected error, got object");
155 printf("OK (failed as expected: %s)\n", error);
157 /* invalid spec: #colors exceeds color depth */
158 printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320x240x9/2345|0.gif\")... ");
159 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320x240x9/2345|0.gif", &error)))
160 return failed_("expected error, got object");
161 printf("OK (failed as expected: %s)\n", error);
163 /* invalid spec: standard icon has to be 32x32 PNG */
164 printf("testing grabbag__picture_parse_specification(\"1|-->|desc|32x24x9|0.gif\")... ");
165 if(0 != (obj = grabbag__picture_parse_specification("1|-->|desc|32x24x9|0.gif", &error)))
166 return failed_("expected error, got object");
167 printf("OK (failed as expected: %s)\n", error);
169 /* invalid spec: need resolution for linked URL */
170 printf("testing grabbag__picture_parse_specification(\"|-->|desc||http://blah.blah.blah/z.gif\")... ");
171 if(0 != (obj = grabbag__picture_parse_specification("|-->|desc||http://blah.blah.blah/z.gif", &error)))
172 return failed_("expected error, got object");
173 printf("OK (failed as expected: %s)\n", error);
175 printf("testing grabbag__picture_parse_specification(\"|-->|desc|320x240x9|http://blah.blah.blah/z.gif\")... ");
176 if(0 == (obj = grabbag__picture_parse_specification("|-->|desc|320x240x9|http://blah.blah.blah/z.gif", &error)))
177 return failed_(error);
179 FLAC__metadata_object_delete(obj);
181 /* test automatic parsing of picture files from only the file name */
182 for(i = 0; i < sizeof(picturefiles)/sizeof(picturefiles[0]); i++)
183 if(!test_one_picture(prefix, picturefiles+i, "", /*fn_only=*/true))
186 /* test automatic parsing of picture files to get resolution/color info */
187 for(i = 0; i < sizeof(picturefiles)/sizeof(picturefiles[0]); i++)
188 if(!test_one_picture(prefix, picturefiles+i, "", /*fn_only=*/false))
191 picturefiles[0].width = 320;
192 picturefiles[0].height = 240;
193 picturefiles[0].depth = 3;
194 picturefiles[0].colors = 2;
195 if(!test_one_picture(prefix, picturefiles+0, "320x240x3/2", /*fn_only=*/false))
201 int main(int argc, char *argv[])
203 const char *usage = "usage: test_pictures path_prefix\n";
205 if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
211 fputs(usage, stderr);
215 return do_picture(argv[1])? 0 : 1;