add test for FLAC__metadata_object_seektable_is_legal()
[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         printf("testing FLAC__metadata_object_seektable_is_legal()...");
401         if(!FLAC__metadata_object_seektable_is_legal(block)) {
402                 printf("FAILED, returned false\n");
403                 return 1;
404         }
405         printf("OK\n");
406
407         seekpoints = 0;
408         printf("testing FLAC__metadata_object_seektable_resize_points(shrink to %u)...", seekpoints);
409         if(!FLAC__metadata_object_seektable_resize_points(block, seekpoints)) {
410                 printf("FAILED, returned false\n");
411                 return 1;
412         }
413         if(!check_seektable_(block, seekpoints, 0))
414                 return 1;
415
416         seekpoints++;
417         printf("testing FLAC__metadata_object_seektable_insert_point() on empty array...");
418         if(!FLAC__metadata_object_seektable_insert_point(block, 0, seekpoint_array[0])) {
419                 printf("FAILED, returned false\n");
420                 return 1;
421         }
422         if(!check_seektable_(block, seekpoints, seekpoint_array))
423                 return 1;
424
425         seekpoint_array[0].sample_number = 1;
426         seekpoints++;
427         printf("testing FLAC__metadata_object_seektable_insert_point() on beginning of non-empty array...");
428         if(!FLAC__metadata_object_seektable_insert_point(block, 0, seekpoint_array[0])) {
429                 printf("FAILED, returned false\n");
430                 return 1;
431         }
432         if(!check_seektable_(block, seekpoints, seekpoint_array))
433                 return 1;
434
435         seekpoint_array[1].sample_number = 2;
436         seekpoints++;
437         printf("testing FLAC__metadata_object_seektable_insert_point() on middle of non-empty array...");
438         if(!FLAC__metadata_object_seektable_insert_point(block, 1, seekpoint_array[1])) {
439                 printf("FAILED, returned false\n");
440                 return 1;
441         }
442         if(!check_seektable_(block, seekpoints, seekpoint_array))
443                 return 1;
444
445         seekpoint_array[3].sample_number = 3;
446         seekpoints++;
447         printf("testing FLAC__metadata_object_seektable_insert_point() on end of non-empty array...");
448         if(!FLAC__metadata_object_seektable_insert_point(block, 3, seekpoint_array[3])) {
449                 printf("FAILED, returned false\n");
450                 return 1;
451         }
452         if(!check_seektable_(block, seekpoints, seekpoint_array))
453                 return 1;
454
455         printf("testing FLAC__metadata_object_copy()... ");
456         blockcopy = FLAC__metadata_object_copy(block);
457         if(0 == blockcopy) {
458                 printf("FAILED, returned NULL\n");
459                 return 1;
460         }
461         if(!compare_block_(block, blockcopy))
462                 return 1;
463         printf("OK\n");
464
465         printf("testing FLAC__metadata_object_delete()... ");
466         FLAC__metadata_object_delete(blockcopy);
467         printf("OK\n");
468
469         seekpoint_array[2].sample_number = seekpoint_array[3].sample_number;
470         seekpoints--;
471         printf("testing FLAC__metadata_object_seektable_delete_point() on middle of array...");
472         if(!FLAC__metadata_object_seektable_delete_point(block, 2)) {
473                 printf("FAILED, returned false\n");
474                 return 1;
475         }
476         if(!check_seektable_(block, seekpoints, seekpoint_array))
477                 return 1;
478
479         seekpoints--;
480         printf("testing FLAC__metadata_object_seektable_delete_point() on end of array...");
481         if(!FLAC__metadata_object_seektable_delete_point(block, 2)) {
482                 printf("FAILED, returned false\n");
483                 return 1;
484         }
485         if(!check_seektable_(block, seekpoints, seekpoint_array))
486                 return 1;
487
488         seekpoints--;
489         printf("testing FLAC__metadata_object_seektable_delete_point() on beginning of array...");
490         if(!FLAC__metadata_object_seektable_delete_point(block, 0)) {
491                 printf("FAILED, returned false\n");
492                 return 1;
493         }
494         if(!check_seektable_(block, seekpoints, seekpoint_array+1))
495                 return 1;
496
497         printf("testing FLAC__metadata_object_seektable_set_point()...");
498         FLAC__metadata_object_seektable_set_point(block, 0, seekpoint_array[0]);
499         if(!check_seektable_(block, seekpoints, seekpoint_array))
500                 return 1;
501
502
503
504         printf("testing VORBIS_COMMENT\n");
505
506         printf("testing FLAC__metadata_object_new()... ");
507         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
508         if(0 == block) {
509                 printf("FAILED, returned NULL\n");
510                 return 1;
511         }
512         expected_length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN + FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
513         if(block->length != expected_length) {
514                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
515                 return 1;
516     }
517         printf("OK\n");
518
519         printf("testing FLAC__metadata_object_copy()... ");
520         vorbiscomment = FLAC__metadata_object_copy(block);
521         if(0 == vorbiscomment) {
522                 printf("FAILED, returned NULL\n");
523                 return 1;
524         }
525         if(!compare_block_(vorbiscomment, block))
526                 return 1;
527         printf("OK\n");
528
529         vc_resize_(vorbiscomment, 2);
530         printf("testing FLAC__metadata_object_vorbiscomment_resize_comments(grow to %u)...", vorbiscomment->data.vorbis_comment.num_comments);
531         if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, vorbiscomment->data.vorbis_comment.num_comments)) {
532                 printf("FAILED, returned false\n");
533                 return 1;
534         }
535         if(!compare_block_(vorbiscomment, block))
536                 return 1;
537         printf("OK\n");
538
539         vc_resize_(vorbiscomment, 1);
540         printf("testing FLAC__metadata_object_vorbiscomment_resize_comments(shrink to %u)...", vorbiscomment->data.vorbis_comment.num_comments);
541         if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, vorbiscomment->data.vorbis_comment.num_comments)) {
542                 printf("FAILED, returned false\n");
543                 return 1;
544         }
545         if(!compare_block_(vorbiscomment, block))
546                 return 1;
547         printf("OK\n");
548
549         vc_resize_(vorbiscomment, 0);
550         printf("testing FLAC__metadata_object_vorbiscomment_resize_comments(shrink to %u)...", vorbiscomment->data.vorbis_comment.num_comments);
551         if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, vorbiscomment->data.vorbis_comment.num_comments)) {
552                 printf("FAILED, returned false\n");
553                 return 1;
554         }
555         if(!compare_block_(vorbiscomment, block))
556                 return 1;
557         printf("OK\n");
558
559         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on empty array...");
560         vc_insert_new_(&entry, vorbiscomment, 0, "name1=field1");
561         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/true)) {
562                 printf("FAILED, returned false\n");
563                 return 1;
564         }
565         if(!compare_block_(vorbiscomment, block))
566                 return 1;
567         printf("OK\n");
568
569         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on beginning of non-empty array...");
570         vc_insert_new_(&entry, vorbiscomment, 0, "name2=field2");
571         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/true)) {
572                 printf("FAILED, returned false\n");
573                 return 1;
574         }
575         if(!compare_block_(vorbiscomment, block))
576                 return 1;
577         printf("OK\n");
578
579         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on middle of non-empty array...");
580         vc_insert_new_(&entry, vorbiscomment, 1, "name3=field3");
581         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 1, entry, /*copy=*/true)) {
582                 printf("FAILED, returned false\n");
583                 return 1;
584         }
585         if(!compare_block_(vorbiscomment, block))
586                 return 1;
587         printf("OK\n");
588
589         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on end of non-empty array...");
590         vc_insert_new_(&entry, vorbiscomment, 3, "name4=field4");
591         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 3, entry, /*copy=*/true)) {
592                 printf("FAILED, returned false\n");
593                 return 1;
594         }
595         if(!compare_block_(vorbiscomment, block))
596                 return 1;
597         printf("OK\n");
598
599         printf("testing FLAC__metadata_object_copy()... ");
600         blockcopy = FLAC__metadata_object_copy(block);
601         if(0 == blockcopy) {
602                 printf("FAILED, returned NULL\n");
603                 return 1;
604         }
605         if(!compare_block_(block, blockcopy))
606                 return 1;
607         printf("OK\n");
608
609         printf("testing FLAC__metadata_object_delete()... ");
610         FLAC__metadata_object_delete(blockcopy);
611         printf("OK\n");
612
613         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on middle of array...");
614         vc_delete_(vorbiscomment, 2);
615         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) {
616                 printf("FAILED, returned false\n");
617                 return 1;
618         }
619         if(!compare_block_(vorbiscomment, block))
620                 return 1;
621         printf("OK\n");
622
623         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on end of array...");
624         vc_delete_(vorbiscomment, 2);
625         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) {
626                 printf("FAILED, returned false\n");
627                 return 1;
628         }
629         if(!compare_block_(vorbiscomment, block))
630                 return 1;
631         printf("OK\n");
632
633         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on beginning of array...");
634         vc_delete_(vorbiscomment, 0);
635         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 0)) {
636                 printf("FAILED, returned false\n");
637                 return 1;
638         }
639         if(!compare_block_(vorbiscomment, block))
640                 return 1;
641         printf("OK\n");
642
643         printf("testing FLAC__metadata_object_vorbiscomment_set_comment(copy)...");
644         vc_set_new_(&entry, vorbiscomment, 0, "name5=field5");
645         FLAC__metadata_object_vorbiscomment_set_comment(block, 0, entry, /*copy=*/true);
646         if(!compare_block_(vorbiscomment, block))
647                 return 1;
648         printf("OK\n");
649
650         printf("testing FLAC__metadata_object_vorbiscomment_set_vendor_string(copy)...");
651         vc_set_vs_new_(&entry, vorbiscomment, "name6=field6");
652         FLAC__metadata_object_vorbiscomment_set_vendor_string(block, entry, /*copy=*/true);
653         if(!compare_block_(vorbiscomment, block))
654                 return 1;
655         printf("OK\n");
656
657         printf("testing FLAC__metadata_object_delete()... ");
658         FLAC__metadata_object_delete(vorbiscomment);
659         FLAC__metadata_object_delete(block);
660         printf("OK\n");
661
662
663         printf("testing FLAC__metadata_object_new()... ");
664         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
665         if(0 == block) {
666                 printf("FAILED, returned NULL\n");
667                 return 1;
668         }
669         printf("OK\n");
670
671         printf("testing FLAC__metadata_object_copy()... ");
672         vorbiscomment = FLAC__metadata_object_copy(block);
673         if(0 == vorbiscomment) {
674                 printf("FAILED, returned NULL\n");
675                 return 1;
676         }
677         if(!compare_block_(vorbiscomment, block))
678                 return 1;
679         printf("OK\n");
680
681         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on empty array...");
682         vc_insert_new_(&entry, vorbiscomment, 0, "name1=field1");
683         entry_clone_(&entry);
684         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/false)) {
685                 printf("FAILED, returned false\n");
686                 return 1;
687         }
688         if(!compare_block_(vorbiscomment, block))
689                 return 1;
690         printf("OK\n");
691
692         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on beginning of non-empty array...");
693         vc_insert_new_(&entry, vorbiscomment, 0, "name2=field2");
694         entry_clone_(&entry);
695         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/false)) {
696                 printf("FAILED, returned false\n");
697                 return 1;
698         }
699         if(!compare_block_(vorbiscomment, block))
700                 return 1;
701         printf("OK\n");
702
703         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on middle of non-empty array...");
704         vc_insert_new_(&entry, vorbiscomment, 1, "name3=field3");
705         entry_clone_(&entry);
706         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 1, entry, /*copy=*/false)) {
707                 printf("FAILED, returned false\n");
708                 return 1;
709         }
710         if(!compare_block_(vorbiscomment, block))
711                 return 1;
712         printf("OK\n");
713
714         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on end of non-empty array...");
715         vc_insert_new_(&entry, vorbiscomment, 3, "name4=field4");
716         entry_clone_(&entry);
717         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 3, entry, /*copy=*/false)) {
718                 printf("FAILED, returned false\n");
719                 return 1;
720         }
721         if(!compare_block_(vorbiscomment, block))
722                 return 1;
723         printf("OK\n");
724
725         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on middle of array...");
726         vc_delete_(vorbiscomment, 2);
727         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) {
728                 printf("FAILED, returned false\n");
729                 return 1;
730         }
731         if(!compare_block_(vorbiscomment, block))
732                 return 1;
733         printf("OK\n");
734
735         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on end of array...");
736         vc_delete_(vorbiscomment, 2);
737         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) {
738                 printf("FAILED, returned false\n");
739                 return 1;
740         }
741         if(!compare_block_(vorbiscomment, block))
742                 return 1;
743         printf("OK\n");
744
745         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on beginning of array...");
746         vc_delete_(vorbiscomment, 0);
747         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 0)) {
748                 printf("FAILED, returned false\n");
749                 return 1;
750         }
751         if(!compare_block_(vorbiscomment, block))
752                 return 1;
753         printf("OK\n");
754
755         printf("testing FLAC__metadata_object_vorbiscomment_set_comment(own)...");
756         vc_set_new_(&entry, vorbiscomment, 0, "name5=field5");
757         entry_clone_(&entry);
758         FLAC__metadata_object_vorbiscomment_set_comment(block, 0, entry, /*copy=*/false);
759         if(!compare_block_(vorbiscomment, block))
760                 return 1;
761         printf("OK\n");
762
763         printf("testing FLAC__metadata_object_vorbiscomment_set_vendor_string(own)...");
764         vc_set_vs_new_(&entry, vorbiscomment, "name6=field6");
765         entry_clone_(&entry);
766         FLAC__metadata_object_vorbiscomment_set_vendor_string(block, entry, /*copy=*/false);
767         if(!compare_block_(vorbiscomment, block))
768                 return 1;
769         printf("OK\n");
770
771         printf("testing FLAC__metadata_object_delete()... ");
772         FLAC__metadata_object_delete(vorbiscomment);
773         FLAC__metadata_object_delete(block);
774         printf("OK\n");
775
776
777         return 0;
778 }
779
780 #if 0
781 int test_metadata_object_pp()
782 {
783         FLAC::Metadata::Prototype *block, *blockcopy;
784         FLAC__StreamMetaData_SeekPoint *seekpoint_array, *seekpoint_array_copy;
785         FLAC__StreamMetaData_VorbisComment_Entry *vorbiscomment_entry_array, *vorbiscomment_entry_array_copy;
786         unsigned expected_length;
787         static FLAC__byte dummydata[4] = { 'a', 'b', 'c', 'd' };
788
789         printf("\n+++ unit test: metadata objects (libFLAC++)\n\n");
790
791
792         printf("testing STREAMINFO\n");
793
794         printf("testing FLAC::Metadata::StreamInfo::StreamInfo()... ");
795         block = new FLAC::Metadata::StreamInfo();
796         if(0 == block) {
797                 printf("FAILED, new returned NULL\n");
798                 return 1;
799         }
800         if(!block->is_valid()) {
801                 printf("FAILED, !block->is_valid()\n");
802                 return 1;
803         }
804         if(block->is_valid() != *block) {
805                 printf("FAILED, FLAC::Metadata::Prototype::operator bool() is broken\n");
806                 return 1;
807         }
808         expected_length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
809         if(block->length() != expected_length) {
810                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length());
811                 return 1;
812     }
813         printf("OK\n");
814
815         printf("testing FLAC__metadata_object_copy()... ");
816         blockcopy = new FLAC::Metadata::StreamInfo(block);
817         if(0 == blockcopy) {
818                 printf("FAILED, new returned NULL\n");
819                 return 1;
820         }
821         if(!compare_block_(block, blockcopy))
822                 return 1;
823         printf("OK\n");
824
825         printf("testing FLAC__metadata_object_delete()... ");
826         FLAC__metadata_object_delete(blockcopy);
827         FLAC__metadata_object_delete(block);
828         printf("OK\n");
829
830
831         return 0;
832 }
833 #endif