1/* test_picture - Simple tester for picture routines in grabbag 2 * Copyright (C) 2006,2007 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 27typedef 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 FLAC__StreamMetadata_Picture_Type type; 36} PictureFile; 37 38PictureFile 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 } 53}; 54 55static FLAC__bool debug_ = false; 56 57static FLAC__bool failed_(const char *msg) 58{ 59 if(msg) 60 printf("FAILED, %s\n", msg); 61 else 62 printf("FAILED\n"); 63 64 return false; 65} 66 67static FLAC__bool test_one_picture(const char *prefix, const PictureFile *pf, const char *res, FLAC__bool fn_only) 68{ 69 FLAC__StreamMetadata *obj; 70 const char *error; 71 char s[4096]; 72 if(fn_only) 73#if defined _MSC_VER || defined __MINGW32__ 74 _snprintf(s, sizeof(s)-1, "%s/%s", prefix, pf->path); 75#else 76 snprintf(s, sizeof(s)-1, "%s/%s", prefix, pf->path); 77#endif 78 else 79#if defined _MSC_VER || defined __MINGW32__ 80 _snprintf(s, sizeof(s)-1, "%u|%s|%s|%s|%s/%s", (unsigned)pf->type, pf->mime_type, pf->description, res, prefix, pf->path); 81#else 82 snprintf(s, sizeof(s)-1, "%u|%s|%s|%s|%s/%s", (unsigned)pf->type, pf->mime_type, pf->description, res, prefix, pf->path); 83#endif 84 85 printf("testing grabbag__picture_parse_specification(\"%s\")... ", s); 86 if(0 == (obj = grabbag__picture_parse_specification(s, &error))) 87 return failed_(error); 88 if(debug_) { 89 printf("\ntype=%u (%s)\nmime_type=%s\ndescription=%s\nwidth=%u\nheight=%u\ndepth=%u\ncolors=%u\ndata_length=%u\n", 90 obj->data.picture.type, 91 obj->data.picture.type < FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED? 92 FLAC__StreamMetadata_Picture_TypeString[obj->data.picture.type] : "UNDEFINED", 93 obj->data.picture.mime_type, 94 obj->data.picture.description, 95 obj->data.picture.width, 96 obj->data.picture.height, 97 obj->data.picture.depth, 98 obj->data.picture.colors, 99 obj->data.picture.data_length 100 ); 101 } 102 if(obj->data.picture.type != (fn_only? FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER : pf->type)) 103 return failed_("picture type mismatch"); 104 if(strcmp(obj->data.picture.mime_type, pf->mime_type)) 105 return failed_("picture MIME type mismatch"); 106 if(strcmp((const char *)obj->data.picture.description, (const char *)pf->description)) 107 return failed_("picture description mismatch"); 108 if(obj->data.picture.width != pf->width) 109 return failed_("picture width mismatch"); 110 if(obj->data.picture.height != pf->height) 111 return failed_("picture height mismatch"); 112 if(obj->data.picture.depth != pf->depth) 113 return failed_("picture depth mismatch"); 114 if(obj->data.picture.colors != pf->colors) 115 return failed_("picture colors mismatch"); 116 printf("OK\n"); 117 FLAC__metadata_object_delete(obj); 118 return true; 119} 120 121static FLAC__bool do_picture(const char *prefix) 122{ 123 FLAC__StreamMetadata *obj; 124 const char *error; 125 size_t i; 126 127 printf("\n+++ grabbag unit test: picture\n\n"); 128 129 /* invalid spec: no filename */ 130 printf("testing grabbag__picture_parse_specification(\"\")... "); 131 if(0 != (obj = grabbag__picture_parse_specification("", &error))) 132 return failed_("expected error, got object"); 133 printf("OK (failed as expected, error: %s)\n", error); 134 135 /* invalid spec: no filename */ 136 printf("testing grabbag__picture_parse_specification(\"||||\")... "); 137 if(0 != (obj = grabbag__picture_parse_specification("||||", &error))) 138 return failed_("expected error, got object"); 139 printf("OK (failed as expected: %s)\n", error); 140 141 /* invalid spec: no filename */ 142 printf("testing grabbag__picture_parse_specification(\"|image/gif|||\")... "); 143 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|||", &error))) 144 return failed_("expected error, got object"); 145 printf("OK (failed as expected: %s)\n", error); 146 147 /* invalid spec: bad resolution */ 148 printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320|0.gif\")... "); 149 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320|0.gif", &error))) 150 return failed_("expected error, got object"); 151 printf("OK (failed as expected: %s)\n", error); 152 153 /* invalid spec: bad resolution */ 154 printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320x240|0.gif\")... "); 155 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320x240|0.gif", &error))) 156 return failed_("expected error, got object"); 157 printf("OK (failed as expected: %s)\n", error); 158 159 /* invalid spec: no filename */ 160 printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320x240x9|\")... "); 161 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320x240x9|", &error))) 162 return failed_("expected error, got object"); 163 printf("OK (failed as expected: %s)\n", error); 164 165 /* invalid spec: #colors exceeds color depth */ 166 printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320x240x9/2345|0.gif\")... "); 167 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320x240x9/2345|0.gif", &error))) 168 return failed_("expected error, got object"); 169 printf("OK (failed as expected: %s)\n", error); 170 171 /* invalid spec: standard icon has to be 32x32 PNG */ 172 printf("testing grabbag__picture_parse_specification(\"1|-->|desc|32x24x9|0.gif\")... "); 173 if(0 != (obj = grabbag__picture_parse_specification("1|-->|desc|32x24x9|0.gif", &error))) 174 return failed_("expected error, got object"); 175 printf("OK (failed as expected: %s)\n", error); 176 177 /* invalid spec: need resolution for linked URL */ 178 printf("testing grabbag__picture_parse_specification(\"|-->|desc||http://blah.blah.blah/z.gif\")... "); 179 if(0 != (obj = grabbag__picture_parse_specification("|-->|desc||http://blah.blah.blah/z.gif", &error))) 180 return failed_("expected error, got object"); 181 printf("OK (failed as expected: %s)\n", error); 182 183 printf("testing grabbag__picture_parse_specification(\"|-->|desc|320x240x9|http://blah.blah.blah/z.gif\")... "); 184 if(0 == (obj = grabbag__picture_parse_specification("|-->|desc|320x240x9|http://blah.blah.blah/z.gif", &error))) 185 return failed_(error); 186 printf("OK\n"); 187 FLAC__metadata_object_delete(obj); 188 189 /* test automatic parsing of picture files from only the file name */ 190 for(i = 0; i < sizeof(picturefiles)/sizeof(picturefiles[0]); i++) 191 if(!test_one_picture(prefix, picturefiles+i, "", /*fn_only=*/true)) 192 return false; 193 194 /* test automatic parsing of picture files to get resolution/color info */ 195 for(i = 0; i < sizeof(picturefiles)/sizeof(picturefiles[0]); i++) 196 if(!test_one_picture(prefix, picturefiles+i, "", /*fn_only=*/false)) 197 return false; 198 199 picturefiles[0].width = 320; 200 picturefiles[0].height = 240; 201 picturefiles[0].depth = 3; 202 picturefiles[0].colors = 2; 203 if(!test_one_picture(prefix, picturefiles+0, "320x240x3/2", /*fn_only=*/false)) 204 return false; 205 206 return true; 207} 208 209int main(int argc, char *argv[]) 210{ 211 const char *usage = "usage: test_pictures path_prefix\n"; 212 213 if(argc > 1 && 0 == strcmp(argv[1], "-h")) { 214 printf(usage); 215 return 0; 216 } 217 218 if(argc != 2) { 219 fprintf(stderr, usage); 220 return 255; 221 } 222 223 return do_picture(argv[1])? 0 : 1; 224} 225