cf4df7b7e2f73f639623bc82a557543175b885d4
[platform/upstream/flac.git] / src / test_unit / metadata_object.c
1 /* test_unit - Simple FLAC unit tester
2  * Copyright (C) 2002  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 #include "FLAC/assert.h"
20 #include "FLAC/metadata.h"
21 #include "metadata_utils.h"
22 #include <stdio.h>
23 #include <stdlib.h> /* for malloc() */
24 #include <string.h> /* for memcmp() */
25
26 static FLAC__byte *make_dummydata_(FLAC__byte *dummydata, unsigned len)
27 {
28         FLAC__byte *ret;
29
30         if(0 == (ret = malloc(len))) {
31                 printf("FAILED, malloc error\n");
32                 exit(1);
33         }
34         else
35                 memcpy(ret, dummydata, len);
36
37         return ret;
38 }
39
40 static FLAC__bool compare_seekpoint_array_(const FLAC__StreamMetaData_SeekPoint *from, const FLAC__StreamMetaData_SeekPoint *to, unsigned n)
41 {
42         unsigned i;
43
44         FLAC__ASSERT(0 != from);
45         FLAC__ASSERT(0 != to);
46
47         for(i = 0; i < n; i++) {
48                 if(from[i].sample_number != to[i].sample_number) {
49                         printf("FAILED, point[%u].sample_number mismatch, expected %llu, got %llu\n", i, to[i].sample_number, from[i].sample_number);
50                         return false;
51                 }
52                 if(from[i].stream_offset != to[i].stream_offset) {
53                         printf("FAILED, point[%u].stream_offset mismatch, expected %llu, got %llu\n", i, to[i].stream_offset, from[i].stream_offset);
54                         return false;
55                 }
56                 if(from[i].frame_samples != to[i].frame_samples) {
57                         printf("FAILED, point[%u].frame_samples mismatch, expected %u, got %u\n", i, to[i].frame_samples, from[i].frame_samples);
58                         return false;
59                 }
60         }
61
62         return true;
63 }
64
65 static FLAC__bool check_seektable_(const FLAC__StreamMetaData *block, unsigned num_points, const FLAC__StreamMetaData_SeekPoint *array)
66 {
67         const unsigned expected_length = num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
68
69         if(block->length != expected_length) {
70                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
71                 return false;
72     }
73         if(block->data.seek_table.num_points != num_points) {
74                 printf("FAILED, expected %u point, got %u\n", num_points, block->data.seek_table.num_points);
75                 return false;
76         }
77         if(0 == array) {
78                 if(0 != block->data.seek_table.points) {
79                         printf("FAILED, 'points' pointer is not null\n");
80                         return false;
81                 }
82         }
83         else {
84                 if(!compare_seekpoint_array_(block->data.seek_table.points, array, num_points))
85                         return false;
86         }
87         printf("OK\n");
88
89         return true;
90 }
91
92 static void entry_new_(FLAC__StreamMetaData_VorbisComment_Entry *entry, const char *field)
93 {
94         entry->length = strlen(field);
95         entry->entry = malloc(entry->length);
96         FLAC__ASSERT(0 != entry->entry);        
97         memcpy(entry->entry, field, entry->length);
98 }
99
100 static void entry_clone_(FLAC__StreamMetaData_VorbisComment_Entry *entry)
101 {
102         FLAC__byte *x = malloc(entry->length);
103         FLAC__ASSERT(0 != x);   
104         memcpy(x, entry->entry, entry->length);
105         entry->entry = x;
106 }
107
108 static void vc_calc_len_(FLAC__StreamMetaData *block)
109 {
110         const FLAC__StreamMetaData_VorbisComment *vc = &block->data.vorbis_comment;
111         unsigned i;
112
113         block->length = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
114         block->length += vc->vendor_string.length;
115         block->length += FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
116         for(i = 0; i < vc->num_comments; i++) {
117                 block->length += FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
118                 block->length += vc->comments[i].length;
119         }
120 }
121
122 static void vc_resize_(FLAC__StreamMetaData *block, unsigned num)
123 {
124         FLAC__StreamMetaData_VorbisComment *vc = &block->data.vorbis_comment;
125
126         if(vc->num_comments != 0) {
127                 FLAC__ASSERT(0 != vc->comments);
128                 if(num < vc->num_comments) {
129                         unsigned i;
130                         for(i = num; i < vc->num_comments; i++) {
131                                 if(0 != vc->comments[i].entry)
132                                         free(vc->comments[i].entry);
133                         }
134                 }
135         }
136         if(num == 0) {
137                 if(0 != vc->comments) {
138                         free(vc->comments);
139                         vc->comments = 0;
140                 }
141         }
142         else {
143                 vc->comments = realloc(vc->comments, sizeof(FLAC__StreamMetaData_VorbisComment_Entry)*num);
144                 FLAC__ASSERT(0 != vc->comments);
145                 if(num > vc->num_comments)
146                         memset(vc->comments+vc->num_comments, 0, sizeof(FLAC__StreamMetaData_VorbisComment_Entry)*(num-vc->num_comments));
147         }
148
149         vc->num_comments = num;
150         vc_calc_len_(block);
151 }
152
153 static void vc_set_vs_new_(FLAC__StreamMetaData_VorbisComment_Entry *entry, FLAC__StreamMetaData *block, const char *field)
154 {
155         entry_new_(entry, field);
156         block->data.vorbis_comment.vendor_string = *entry;
157         vc_calc_len_(block);
158 }
159
160 static void vc_set_new_(FLAC__StreamMetaData_VorbisComment_Entry *entry, FLAC__StreamMetaData *block, unsigned pos, const char *field)
161 {
162         entry_new_(entry, field);
163         block->data.vorbis_comment.comments[pos] = *entry;
164         vc_calc_len_(block);
165 }
166
167 static void vc_insert_new_(FLAC__StreamMetaData_VorbisComment_Entry *entry, FLAC__StreamMetaData *block, unsigned pos, const char *field)
168 {
169         vc_resize_(block, block->data.vorbis_comment.num_comments+1);
170         memmove(&block->data.vorbis_comment.comments[pos+1], &block->data.vorbis_comment.comments[pos], sizeof(FLAC__StreamMetaData_VorbisComment_Entry)*(block->data.vorbis_comment.num_comments-1-pos));
171         vc_set_new_(entry, block, pos, field);
172         vc_calc_len_(block);
173 }
174
175 static void vc_delete_(FLAC__StreamMetaData *block, unsigned pos)
176 {
177         if(0 != block->data.vorbis_comment.comments[pos].entry)
178                 free(block->data.vorbis_comment.comments[pos].entry);
179         memmove(&block->data.vorbis_comment.comments[pos], &block->data.vorbis_comment.comments[pos+1], sizeof(FLAC__StreamMetaData_VorbisComment_Entry)*(block->data.vorbis_comment.num_comments-pos-1));
180         block->data.vorbis_comment.comments[block->data.vorbis_comment.num_comments-1].entry = 0;
181         block->data.vorbis_comment.comments[block->data.vorbis_comment.num_comments-1].length = 0;
182         vc_resize_(block, block->data.vorbis_comment.num_comments-1);
183         vc_calc_len_(block);
184 }
185
186 int test_metadata_object()
187 {
188         FLAC__StreamMetaData *block, *blockcopy, *vorbiscomment;
189         FLAC__StreamMetaData_SeekPoint seekpoint_array[4];
190         FLAC__StreamMetaData_VorbisComment_Entry entry;
191         unsigned i, expected_length, seekpoints;
192         static FLAC__byte dummydata[4] = { 'a', 'b', 'c', 'd' };
193
194         printf("\n+++ unit test: metadata objects (libFLAC)\n\n");
195
196
197         printf("testing STREAMINFO\n");
198
199         printf("testing FLAC__metadata_object_new()... ");
200         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_STREAMINFO);
201         if(0 == block) {
202                 printf("FAILED, returned NULL\n");
203                 return 1;
204         }
205         expected_length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
206         if(block->length != expected_length) {
207                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
208                 return 1;
209     }
210         printf("OK\n");
211
212         printf("testing FLAC__metadata_object_copy()... ");
213         blockcopy = FLAC__metadata_object_copy(block);
214         if(0 == blockcopy) {
215                 printf("FAILED, returned NULL\n");
216                 return 1;
217         }
218         if(!compare_block_(block, blockcopy))
219                 return 1;
220         printf("OK\n");
221
222         printf("testing FLAC__metadata_object_delete()... ");
223         FLAC__metadata_object_delete(blockcopy);
224         FLAC__metadata_object_delete(block);
225         printf("OK\n");
226
227
228         printf("testing PADDING\n");
229
230         printf("testing FLAC__metadata_object_new()... ");
231         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
232         if(0 == block) {
233                 printf("FAILED, returned NULL\n");
234                 return 1;
235         }
236         expected_length = 0;
237         if(block->length != expected_length) {
238                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
239                 return 1;
240     }
241         printf("OK\n");
242
243         printf("testing FLAC__metadata_object_copy()... ");
244         blockcopy = FLAC__metadata_object_copy(block);
245         if(0 == blockcopy) {
246                 printf("FAILED, returned NULL\n");
247                 return 1;
248         }
249         if(!compare_block_(block, blockcopy))
250                 return 1;
251         printf("OK\n");
252
253         printf("testing FLAC__metadata_object_delete()... ");
254         FLAC__metadata_object_delete(blockcopy);
255         FLAC__metadata_object_delete(block);
256         printf("OK\n");
257
258
259         printf("testing APPLICATION\n");
260
261         printf("testing FLAC__metadata_object_new()... ");
262         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION);
263         if(0 == block) {
264                 printf("FAILED, returned NULL\n");
265                 return 1;
266         }
267         expected_length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
268         if(block->length != expected_length) {
269                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
270                 return 1;
271     }
272         printf("OK\n");
273
274         printf("testing FLAC__metadata_object_copy()... ");
275         blockcopy = FLAC__metadata_object_copy(block);
276         if(0 == blockcopy) {
277                 printf("FAILED, returned NULL\n");
278                 return 1;
279         }
280         if(!compare_block_(block, blockcopy))
281                 return 1;
282         printf("OK\n");
283
284         printf("testing FLAC__metadata_object_delete()... ");
285         FLAC__metadata_object_delete(blockcopy);
286         printf("OK\n");
287
288         printf("testing FLAC__metadata_object_application_set_data(copy)... ");
289         if(!FLAC__metadata_object_application_set_data(block, dummydata, sizeof(dummydata), true/*copy*/)) {
290                 printf("FAILED, returned false\n");
291                 return 1;
292         }
293         expected_length = (FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) + sizeof(dummydata);
294         if(block->length != expected_length) {
295                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
296                 return 1;
297         }
298         if(0 != memcmp(block->data.application.data, dummydata, sizeof(dummydata))) {
299                 printf("FAILED, data mismatch\n");
300                 return 1;
301         }
302         printf("OK\n");
303
304         printf("testing FLAC__metadata_object_copy()... ");
305         blockcopy = FLAC__metadata_object_copy(block);
306         if(0 == blockcopy) {
307                 printf("FAILED, returned NULL\n");
308                 return 1;
309         }
310         if(!compare_block_(block, blockcopy))
311                 return 1;
312         printf("OK\n");
313
314         printf("testing FLAC__metadata_object_delete()... ");
315         FLAC__metadata_object_delete(blockcopy);
316         printf("OK\n");
317
318         printf("testing FLAC__metadata_object_application_set_data(own)... ");
319         if(!FLAC__metadata_object_application_set_data(block, make_dummydata_(dummydata, sizeof(dummydata)), sizeof(dummydata), false/*own*/)) {
320                 printf("FAILED, returned false\n");
321                 return 1;
322         }
323         expected_length = (FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) + sizeof(dummydata);
324         if(block->length != expected_length) {
325                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
326                 return 1;
327         }
328         if(0 != memcmp(block->data.application.data, dummydata, sizeof(dummydata))) {
329                 printf("FAILED, data mismatch\n");
330                 return 1;
331         }
332         printf("OK\n");
333
334         printf("testing FLAC__metadata_object_copy()... ");
335         blockcopy = FLAC__metadata_object_copy(block);
336         if(0 == blockcopy) {
337                 printf("FAILED, returned NULL\n");
338                 return 1;
339         }
340         if(!compare_block_(block, blockcopy))
341                 return 1;
342         printf("OK\n");
343
344         printf("testing FLAC__metadata_object_delete()... ");
345         FLAC__metadata_object_delete(blockcopy);
346         FLAC__metadata_object_delete(block);
347         printf("OK\n");
348
349
350         printf("testing SEEKTABLE\n");
351
352         for(i = 0; i < 4; i++) {
353                 seekpoint_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
354                 seekpoint_array[i].stream_offset = 0;
355                 seekpoint_array[i].frame_samples = 0;
356         }
357
358         seekpoints = 0;
359         printf("testing FLAC__metadata_object_new()... ");
360         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE);
361         if(0 == block) {
362                 printf("FAILED, returned NULL\n");
363                 return 1;
364         }
365         if(!check_seektable_(block, seekpoints, 0))
366                 return 1;
367
368         printf("testing FLAC__metadata_object_copy()... ");
369         blockcopy = FLAC__metadata_object_copy(block);
370         if(0 == blockcopy) {
371                 printf("FAILED, returned NULL\n");
372                 return 1;
373         }
374         if(!compare_block_(block, blockcopy))
375                 return 1;
376         printf("OK\n");
377
378         printf("testing FLAC__metadata_object_delete()... ");
379         FLAC__metadata_object_delete(blockcopy);
380         printf("OK\n");
381
382         seekpoints = 2;
383         printf("testing FLAC__metadata_object_seektable_resize_points(grow to %u)...", seekpoints);
384         if(!FLAC__metadata_object_seektable_resize_points(block, seekpoints)) {
385                 printf("FAILED, returned false\n");
386                 return 1;
387         }
388         if(!check_seektable_(block, seekpoints, seekpoint_array))
389                 return 1;
390
391         seekpoints = 1;
392         printf("testing FLAC__metadata_object_seektable_resize_points(shrink to %u)...", seekpoints);
393         if(!FLAC__metadata_object_seektable_resize_points(block, seekpoints)) {
394                 printf("FAILED, returned false\n");
395                 return 1;
396         }
397         if(!check_seektable_(block, seekpoints, seekpoint_array))
398                 return 1;
399
400         seekpoints = 0;
401         printf("testing FLAC__metadata_object_seektable_resize_points(shrink to %u)...", seekpoints);
402         if(!FLAC__metadata_object_seektable_resize_points(block, seekpoints)) {
403                 printf("FAILED, returned false\n");
404                 return 1;
405         }
406         if(!check_seektable_(block, seekpoints, 0))
407                 return 1;
408
409         seekpoints++;
410         printf("testing FLAC__metadata_object_seektable_insert_point() on empty array...");
411         if(!FLAC__metadata_object_seektable_insert_point(block, 0, seekpoint_array[0])) {
412                 printf("FAILED, returned false\n");
413                 return 1;
414         }
415         if(!check_seektable_(block, seekpoints, seekpoint_array))
416                 return 1;
417
418         seekpoint_array[0].sample_number = 1;
419         seekpoints++;
420         printf("testing FLAC__metadata_object_seektable_insert_point() on beginning of non-empty array...");
421         if(!FLAC__metadata_object_seektable_insert_point(block, 0, seekpoint_array[0])) {
422                 printf("FAILED, returned false\n");
423                 return 1;
424         }
425         if(!check_seektable_(block, seekpoints, seekpoint_array))
426                 return 1;
427
428         seekpoint_array[1].sample_number = 2;
429         seekpoints++;
430         printf("testing FLAC__metadata_object_seektable_insert_point() on middle of non-empty array...");
431         if(!FLAC__metadata_object_seektable_insert_point(block, 1, seekpoint_array[1])) {
432                 printf("FAILED, returned false\n");
433                 return 1;
434         }
435         if(!check_seektable_(block, seekpoints, seekpoint_array))
436                 return 1;
437
438         seekpoint_array[3].sample_number = 3;
439         seekpoints++;
440         printf("testing FLAC__metadata_object_seektable_insert_point() on end of non-empty array...");
441         if(!FLAC__metadata_object_seektable_insert_point(block, 3, seekpoint_array[3])) {
442                 printf("FAILED, returned false\n");
443                 return 1;
444         }
445         if(!check_seektable_(block, seekpoints, seekpoint_array))
446                 return 1;
447
448         printf("testing FLAC__metadata_object_copy()... ");
449         blockcopy = FLAC__metadata_object_copy(block);
450         if(0 == blockcopy) {
451                 printf("FAILED, returned NULL\n");
452                 return 1;
453         }
454         if(!compare_block_(block, blockcopy))
455                 return 1;
456         printf("OK\n");
457
458         printf("testing FLAC__metadata_object_delete()... ");
459         FLAC__metadata_object_delete(blockcopy);
460         printf("OK\n");
461
462         seekpoint_array[2].sample_number = seekpoint_array[3].sample_number;
463         seekpoints--;
464         printf("testing FLAC__metadata_object_seektable_delete_point() on middle of array...");
465         if(!FLAC__metadata_object_seektable_delete_point(block, 2)) {
466                 printf("FAILED, returned false\n");
467                 return 1;
468         }
469         if(!check_seektable_(block, seekpoints, seekpoint_array))
470                 return 1;
471
472         seekpoints--;
473         printf("testing FLAC__metadata_object_seektable_delete_point() on end of array...");
474         if(!FLAC__metadata_object_seektable_delete_point(block, 2)) {
475                 printf("FAILED, returned false\n");
476                 return 1;
477         }
478         if(!check_seektable_(block, seekpoints, seekpoint_array))
479                 return 1;
480
481         seekpoints--;
482         printf("testing FLAC__metadata_object_seektable_delete_point() on beginning of array...");
483         if(!FLAC__metadata_object_seektable_delete_point(block, 0)) {
484                 printf("FAILED, returned false\n");
485                 return 1;
486         }
487         if(!check_seektable_(block, seekpoints, seekpoint_array+1))
488                 return 1;
489
490         printf("testing FLAC__metadata_object_seektable_set_point()...");
491         FLAC__metadata_object_seektable_set_point(block, 0, seekpoint_array[0]);
492         if(!check_seektable_(block, seekpoints, seekpoint_array))
493                 return 1;
494
495
496
497         printf("testing VORBIS_COMMENT\n");
498
499         printf("testing FLAC__metadata_object_new()... ");
500         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
501         if(0 == block) {
502                 printf("FAILED, returned NULL\n");
503                 return 1;
504         }
505         expected_length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN + FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
506         if(block->length != expected_length) {
507                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
508                 return 1;
509     }
510         printf("OK\n");
511
512         printf("testing FLAC__metadata_object_copy()... ");
513         vorbiscomment = FLAC__metadata_object_copy(block);
514         if(0 == vorbiscomment) {
515                 printf("FAILED, returned NULL\n");
516                 return 1;
517         }
518         if(!compare_block_(vorbiscomment, block))
519                 return 1;
520         printf("OK\n");
521
522         vc_resize_(vorbiscomment, 2);
523         printf("testing FLAC__metadata_object_vorbiscomment_resize_comments(grow to %u)...", vorbiscomment->data.vorbis_comment.num_comments);
524         if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, vorbiscomment->data.vorbis_comment.num_comments)) {
525                 printf("FAILED, returned false\n");
526                 return 1;
527         }
528         if(!compare_block_(vorbiscomment, block))
529                 return 1;
530         printf("OK\n");
531
532         vc_resize_(vorbiscomment, 1);
533         printf("testing FLAC__metadata_object_vorbiscomment_resize_comments(shrink to %u)...", vorbiscomment->data.vorbis_comment.num_comments);
534         if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, vorbiscomment->data.vorbis_comment.num_comments)) {
535                 printf("FAILED, returned false\n");
536                 return 1;
537         }
538         if(!compare_block_(vorbiscomment, block))
539                 return 1;
540         printf("OK\n");
541
542         vc_resize_(vorbiscomment, 0);
543         printf("testing FLAC__metadata_object_vorbiscomment_resize_comments(shrink to %u)...", vorbiscomment->data.vorbis_comment.num_comments);
544         if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, vorbiscomment->data.vorbis_comment.num_comments)) {
545                 printf("FAILED, returned false\n");
546                 return 1;
547         }
548         if(!compare_block_(vorbiscomment, block))
549                 return 1;
550         printf("OK\n");
551
552         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on empty array...");
553         vc_insert_new_(&entry, vorbiscomment, 0, "name1=field1");
554         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/true)) {
555                 printf("FAILED, returned false\n");
556                 return 1;
557         }
558         if(!compare_block_(vorbiscomment, block))
559                 return 1;
560         printf("OK\n");
561
562         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on beginning of non-empty array...");
563         vc_insert_new_(&entry, vorbiscomment, 0, "name2=field2");
564         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/true)) {
565                 printf("FAILED, returned false\n");
566                 return 1;
567         }
568         if(!compare_block_(vorbiscomment, block))
569                 return 1;
570         printf("OK\n");
571
572         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on middle of non-empty array...");
573         vc_insert_new_(&entry, vorbiscomment, 1, "name3=field3");
574         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 1, entry, /*copy=*/true)) {
575                 printf("FAILED, returned false\n");
576                 return 1;
577         }
578         if(!compare_block_(vorbiscomment, block))
579                 return 1;
580         printf("OK\n");
581
582         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on end of non-empty array...");
583         vc_insert_new_(&entry, vorbiscomment, 3, "name4=field4");
584         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 3, entry, /*copy=*/true)) {
585                 printf("FAILED, returned false\n");
586                 return 1;
587         }
588         if(!compare_block_(vorbiscomment, block))
589                 return 1;
590         printf("OK\n");
591
592         printf("testing FLAC__metadata_object_copy()... ");
593         blockcopy = FLAC__metadata_object_copy(block);
594         if(0 == blockcopy) {
595                 printf("FAILED, returned NULL\n");
596                 return 1;
597         }
598         if(!compare_block_(block, blockcopy))
599                 return 1;
600         printf("OK\n");
601
602         printf("testing FLAC__metadata_object_delete()... ");
603         FLAC__metadata_object_delete(blockcopy);
604         printf("OK\n");
605
606         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on middle of array...");
607         vc_delete_(vorbiscomment, 2);
608         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) {
609                 printf("FAILED, returned false\n");
610                 return 1;
611         }
612         if(!compare_block_(vorbiscomment, block))
613                 return 1;
614         printf("OK\n");
615
616         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on end of array...");
617         vc_delete_(vorbiscomment, 2);
618         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) {
619                 printf("FAILED, returned false\n");
620                 return 1;
621         }
622         if(!compare_block_(vorbiscomment, block))
623                 return 1;
624         printf("OK\n");
625
626         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on beginning of array...");
627         vc_delete_(vorbiscomment, 0);
628         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 0)) {
629                 printf("FAILED, returned false\n");
630                 return 1;
631         }
632         if(!compare_block_(vorbiscomment, block))
633                 return 1;
634         printf("OK\n");
635
636         printf("testing FLAC__metadata_object_vorbiscomment_set_comment(copy)...");
637         vc_set_new_(&entry, vorbiscomment, 0, "name5=field5");
638         FLAC__metadata_object_vorbiscomment_set_comment(block, 0, entry, /*copy=*/true);
639         if(!compare_block_(vorbiscomment, block))
640                 return 1;
641         printf("OK\n");
642
643         printf("testing FLAC__metadata_object_vorbiscomment_set_vendor_string(copy)...");
644         vc_set_vs_new_(&entry, vorbiscomment, "name6=field6");
645         FLAC__metadata_object_vorbiscomment_set_vendor_string(block, entry, /*copy=*/true);
646         if(!compare_block_(vorbiscomment, block))
647                 return 1;
648         printf("OK\n");
649
650         printf("testing FLAC__metadata_object_delete()... ");
651         FLAC__metadata_object_delete(vorbiscomment);
652         FLAC__metadata_object_delete(block);
653         printf("OK\n");
654
655
656         printf("testing FLAC__metadata_object_new()... ");
657         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
658         if(0 == block) {
659                 printf("FAILED, returned NULL\n");
660                 return 1;
661         }
662         printf("OK\n");
663
664         printf("testing FLAC__metadata_object_copy()... ");
665         vorbiscomment = FLAC__metadata_object_copy(block);
666         if(0 == vorbiscomment) {
667                 printf("FAILED, returned NULL\n");
668                 return 1;
669         }
670         if(!compare_block_(vorbiscomment, block))
671                 return 1;
672         printf("OK\n");
673
674         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on empty array...");
675         vc_insert_new_(&entry, vorbiscomment, 0, "name1=field1");
676         entry_clone_(&entry);
677         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/false)) {
678                 printf("FAILED, returned false\n");
679                 return 1;
680         }
681         if(!compare_block_(vorbiscomment, block))
682                 return 1;
683         printf("OK\n");
684
685         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on beginning of non-empty array...");
686         vc_insert_new_(&entry, vorbiscomment, 0, "name2=field2");
687         entry_clone_(&entry);
688         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/false)) {
689                 printf("FAILED, returned false\n");
690                 return 1;
691         }
692         if(!compare_block_(vorbiscomment, block))
693                 return 1;
694         printf("OK\n");
695
696         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on middle of non-empty array...");
697         vc_insert_new_(&entry, vorbiscomment, 1, "name3=field3");
698         entry_clone_(&entry);
699         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 1, entry, /*copy=*/false)) {
700                 printf("FAILED, returned false\n");
701                 return 1;
702         }
703         if(!compare_block_(vorbiscomment, block))
704                 return 1;
705         printf("OK\n");
706
707         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on end of non-empty array...");
708         vc_insert_new_(&entry, vorbiscomment, 3, "name4=field4");
709         entry_clone_(&entry);
710         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 3, entry, /*copy=*/false)) {
711                 printf("FAILED, returned false\n");
712                 return 1;
713         }
714         if(!compare_block_(vorbiscomment, block))
715                 return 1;
716         printf("OK\n");
717
718         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on middle of array...");
719         vc_delete_(vorbiscomment, 2);
720         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) {
721                 printf("FAILED, returned false\n");
722                 return 1;
723         }
724         if(!compare_block_(vorbiscomment, block))
725                 return 1;
726         printf("OK\n");
727
728         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on end of array...");
729         vc_delete_(vorbiscomment, 2);
730         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) {
731                 printf("FAILED, returned false\n");
732                 return 1;
733         }
734         if(!compare_block_(vorbiscomment, block))
735                 return 1;
736         printf("OK\n");
737
738         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on beginning of array...");
739         vc_delete_(vorbiscomment, 0);
740         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 0)) {
741                 printf("FAILED, returned false\n");
742                 return 1;
743         }
744         if(!compare_block_(vorbiscomment, block))
745                 return 1;
746         printf("OK\n");
747
748         printf("testing FLAC__metadata_object_vorbiscomment_set_comment(own)...");
749         vc_set_new_(&entry, vorbiscomment, 0, "name5=field5");
750         entry_clone_(&entry);
751         FLAC__metadata_object_vorbiscomment_set_comment(block, 0, entry, /*copy=*/false);
752         if(!compare_block_(vorbiscomment, block))
753                 return 1;
754         printf("OK\n");
755
756         printf("testing FLAC__metadata_object_vorbiscomment_set_vendor_string(own)...");
757         vc_set_vs_new_(&entry, vorbiscomment, "name6=field6");
758         entry_clone_(&entry);
759         FLAC__metadata_object_vorbiscomment_set_vendor_string(block, entry, /*copy=*/false);
760         if(!compare_block_(vorbiscomment, block))
761                 return 1;
762         printf("OK\n");
763
764         printf("testing FLAC__metadata_object_delete()... ");
765         FLAC__metadata_object_delete(vorbiscomment);
766         FLAC__metadata_object_delete(block);
767         printf("OK\n");
768
769
770         return 0;
771 }
772
773 #if 0
774 int test_metadata_object_pp()
775 {
776         FLAC::Metadata::Prototype *block, *blockcopy;
777         FLAC__StreamMetaData_SeekPoint *seekpoint_array, *seekpoint_array_copy;
778         FLAC__StreamMetaData_VorbisComment_Entry *vorbiscomment_entry_array, *vorbiscomment_entry_array_copy;
779         unsigned expected_length;
780         static FLAC__byte dummydata[4] = { 'a', 'b', 'c', 'd' };
781
782         printf("\n+++ unit test: metadata objects (libFLAC++)\n\n");
783
784
785         printf("testing STREAMINFO\n");
786
787         printf("testing FLAC::Metadata::StreamInfo::StreamInfo()... ");
788         block = new FLAC::Metadata::StreamInfo();
789         if(0 == block) {
790                 printf("FAILED, new returned NULL\n");
791                 return 1;
792         }
793         if(!block->is_valid()) {
794                 printf("FAILED, !block->is_valid()\n");
795                 return 1;
796         }
797         if(block->is_valid() != *block) {
798                 printf("FAILED, FLAC::Metadata::Prototype::operator bool() is broken\n");
799                 return 1;
800         }
801         expected_length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
802         if(block->length() != expected_length) {
803                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length());
804                 return 1;
805     }
806         printf("OK\n");
807
808         printf("testing FLAC__metadata_object_copy()... ");
809         blockcopy = new FLAC::Metadata::StreamInfo(block);
810         if(0 == blockcopy) {
811                 printf("FAILED, new returned NULL\n");
812                 return 1;
813         }
814         if(!compare_block_(block, blockcopy))
815                 return 1;
816         printf("OK\n");
817
818         printf("testing FLAC__metadata_object_delete()... ");
819         FLAC__metadata_object_delete(blockcopy);
820         FLAC__metadata_object_delete(block);
821         printf("OK\n");
822
823
824         return 0;
825 }
826 #endif