minor fixes
[flac.git] / src / test_libFLAC / metadata_object.c
1 /* test_libFLAC - Unit tester for libFLAC
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 = (FLAC__byte*)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_track_(const FLAC__StreamMetadata_CueSheet_Track *from, const FLAC__StreamMetadata_CueSheet_Track *to)
41 {
42         unsigned i;
43
44         if(from->offset != to->offset) {
45                 printf("FAILED, track offset mismatch, expected %llu, got %llu\n", to->offset, from->offset);
46                 return false;
47         }
48         if(from->number != to->number) {
49                 printf("FAILED, track number mismatch, expected %u, got %u\n", (unsigned)to->number, (unsigned)from->number);
50                 return false;
51         }
52         if(0 != strcmp(from->isrc, to->isrc)) {
53                 printf("FAILED, track number mismatch, expected %s, got %s\n", to->isrc, from->isrc);
54                 return false;
55         }
56         if(from->type != to->type) {
57                 printf("FAILED, track type mismatch, expected %u, got %u\n", (unsigned)to->type, (unsigned)from->type);
58                 return false;
59         }
60         if(from->pre_emphasis != to->pre_emphasis) {
61                 printf("FAILED, track pre_emphasis mismatch, expected %u, got %u\n", (unsigned)to->pre_emphasis, (unsigned)from->pre_emphasis);
62                 return false;
63         }
64         if(from->num_indices != to->num_indices) {
65                 printf("FAILED, track num_indices mismatch, expected %u, got %u\n", (unsigned)to->num_indices, (unsigned)from->num_indices);
66                 return false;
67         }
68         if(0 == to->indices || 0 == from->indices) {
69                 if(to->indices != from->indices) {
70                         printf("FAILED, track indices mismatch\n");
71                         return false;
72                 }
73         }
74         else {
75                 for(i = 0; i < to->num_indices; i++) {
76                         if(from->indices[i].offset != to->indices[i].offset) {
77                                 printf("FAILED, track indices[%u].offset mismatch, expected %llu, got %llu\n", i, to->indices[i].offset, from->indices[i].offset);
78                                 return false;
79                         }
80                         if(from->indices[i].number != to->indices[i].number) {
81                                 printf("FAILED, track indices[%u].number mismatch, expected %u, got %u\n", i, (unsigned)to->indices[i].number, (unsigned)from->indices[i].number);
82                                 return false;
83                         }
84                 }
85         }
86
87         return true;
88 }
89
90 static FLAC__bool compare_seekpoint_array_(const FLAC__StreamMetadata_SeekPoint *from, const FLAC__StreamMetadata_SeekPoint *to, unsigned n)
91 {
92         unsigned i;
93
94         FLAC__ASSERT(0 != from);
95         FLAC__ASSERT(0 != to);
96
97         for(i = 0; i < n; i++) {
98                 if(from[i].sample_number != to[i].sample_number) {
99                         printf("FAILED, point[%u].sample_number mismatch, expected %llu, got %llu\n", i, to[i].sample_number, from[i].sample_number);
100                         return false;
101                 }
102                 if(from[i].stream_offset != to[i].stream_offset) {
103                         printf("FAILED, point[%u].stream_offset mismatch, expected %llu, got %llu\n", i, to[i].stream_offset, from[i].stream_offset);
104                         return false;
105                 }
106                 if(from[i].frame_samples != to[i].frame_samples) {
107                         printf("FAILED, point[%u].frame_samples mismatch, expected %u, got %u\n", i, to[i].frame_samples, from[i].frame_samples);
108                         return false;
109                 }
110         }
111
112         return true;
113 }
114
115 static FLAC__bool check_seektable_(const FLAC__StreamMetadata *block, unsigned num_points, const FLAC__StreamMetadata_SeekPoint *array)
116 {
117         const unsigned expected_length = num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
118
119         if(block->length != expected_length) {
120                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
121                 return false;
122         }
123         if(block->data.seek_table.num_points != num_points) {
124                 printf("FAILED, expected %u point, got %u\n", num_points, block->data.seek_table.num_points);
125                 return false;
126         }
127         if(0 == array) {
128                 if(0 != block->data.seek_table.points) {
129                         printf("FAILED, 'points' pointer is not null\n");
130                         return false;
131                 }
132         }
133         else {
134                 if(!compare_seekpoint_array_(block->data.seek_table.points, array, num_points))
135                         return false;
136         }
137         printf("OK\n");
138
139         return true;
140 }
141
142 static void entry_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, const char *field)
143 {
144         entry->length = strlen(field);
145         entry->entry = (FLAC__byte*)malloc(entry->length);
146         FLAC__ASSERT(0 != entry->entry);
147         memcpy(entry->entry, field, entry->length);
148 }
149
150 static void entry_clone_(FLAC__StreamMetadata_VorbisComment_Entry *entry)
151 {
152         FLAC__byte *x = (FLAC__byte*)malloc(entry->length);
153         FLAC__ASSERT(0 != x);
154         memcpy(x, entry->entry, entry->length);
155         entry->entry = x;
156 }
157
158 static void vc_calc_len_(FLAC__StreamMetadata *block)
159 {
160         const FLAC__StreamMetadata_VorbisComment *vc = &block->data.vorbis_comment;
161         unsigned i;
162
163         block->length = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
164         block->length += vc->vendor_string.length;
165         block->length += FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
166         for(i = 0; i < vc->num_comments; i++) {
167                 block->length += FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
168                 block->length += vc->comments[i].length;
169         }
170 }
171
172 static void vc_resize_(FLAC__StreamMetadata *block, unsigned num)
173 {
174         FLAC__StreamMetadata_VorbisComment *vc = &block->data.vorbis_comment;
175
176         if(vc->num_comments != 0) {
177                 FLAC__ASSERT(0 != vc->comments);
178                 if(num < vc->num_comments) {
179                         unsigned i;
180                         for(i = num; i < vc->num_comments; i++) {
181                                 if(0 != vc->comments[i].entry)
182                                         free(vc->comments[i].entry);
183                         }
184                 }
185         }
186         if(num == 0) {
187                 if(0 != vc->comments) {
188                         free(vc->comments);
189                         vc->comments = 0;
190                 }
191         }
192         else {
193                 vc->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)realloc(vc->comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*num);
194                 FLAC__ASSERT(0 != vc->comments);
195                 if(num > vc->num_comments)
196                         memset(vc->comments+vc->num_comments, 0, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(num-vc->num_comments));
197         }
198
199         vc->num_comments = num;
200         vc_calc_len_(block);
201 }
202
203 static void vc_set_vs_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__StreamMetadata *block, const char *field)
204 {
205         entry_new_(entry, field);
206         block->data.vorbis_comment.vendor_string = *entry;
207         vc_calc_len_(block);
208 }
209
210 static void vc_set_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__StreamMetadata *block, unsigned pos, const char *field)
211 {
212         entry_new_(entry, field);
213         block->data.vorbis_comment.comments[pos] = *entry;
214         vc_calc_len_(block);
215 }
216
217 static void vc_insert_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__StreamMetadata *block, unsigned pos, const char *field)
218 {
219         vc_resize_(block, block->data.vorbis_comment.num_comments+1);
220         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));
221         vc_set_new_(entry, block, pos, field);
222         vc_calc_len_(block);
223 }
224
225 static void vc_delete_(FLAC__StreamMetadata *block, unsigned pos)
226 {
227         if(0 != block->data.vorbis_comment.comments[pos].entry)
228                 free(block->data.vorbis_comment.comments[pos].entry);
229         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));
230         block->data.vorbis_comment.comments[block->data.vorbis_comment.num_comments-1].entry = 0;
231         block->data.vorbis_comment.comments[block->data.vorbis_comment.num_comments-1].length = 0;
232         vc_resize_(block, block->data.vorbis_comment.num_comments-1);
233         vc_calc_len_(block);
234 }
235
236 static void track_new_(FLAC__StreamMetadata_CueSheet_Track *track, FLAC__uint64 offset, FLAC__byte number, const char *isrc, FLAC__bool data, FLAC__bool pre_em)
237 {
238         track->offset = offset;
239         track->number = number;
240         memcpy(track->isrc, isrc, sizeof(track->isrc));
241         track->type = data;
242         track->pre_emphasis = pre_em;
243         track->num_indices = 0;
244         track->indices = 0;
245 }
246
247 static void track_clone_(FLAC__StreamMetadata_CueSheet_Track *track)
248 {
249         if(track->num_indices > 0) {
250                 size_t bytes = sizeof(FLAC__StreamMetadata_CueSheet_Index) * track->num_indices;
251                 FLAC__StreamMetadata_CueSheet_Index *x = (FLAC__StreamMetadata_CueSheet_Index*)malloc(bytes);
252                 FLAC__ASSERT(0 != x);
253                 memcpy(x, track->indices, bytes);
254                 track->indices = x;
255         }
256 }
257
258 static void cs_calc_len_(FLAC__StreamMetadata *block)
259 {
260         const FLAC__StreamMetadata_CueSheet *cs = &block->data.cue_sheet;
261         unsigned i;
262
263         block->length = (
264                 FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
265                 FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
266                 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
267                 FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
268         ) / 8;
269         block->length += cs->num_tracks * (
270                 FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
271                 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
272                 FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
273                 FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
274                 FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
275                 FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
276                 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
277         ) / 8;
278         for(i = 0; i < cs->num_tracks; i++) {
279                 block->length += cs->tracks[i].num_indices * (
280                         FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
281                         FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
282                         FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
283                 ) / 8;
284         }
285 }
286
287 static void tr_resize_(FLAC__StreamMetadata *block, unsigned track_num, unsigned num)
288 {
289         FLAC__StreamMetadata_CueSheet_Track *tr;
290
291         FLAC__ASSERT(track_num < block->data.cue_sheet.num_tracks);
292
293         tr = &block->data.cue_sheet.tracks[track_num];
294
295         if(tr->num_indices != 0) {
296                 FLAC__ASSERT(0 != tr->indices);
297         }
298         if(num == 0) {
299                 if(0 != tr->indices) {
300                         free(tr->indices);
301                         tr->indices = 0;
302                 }
303         }
304         else {
305                 tr->indices = (FLAC__StreamMetadata_CueSheet_Index*)realloc(tr->indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)*num);
306                 FLAC__ASSERT(0 != tr->indices);
307                 if(num > tr->num_indices)
308                         memset(tr->indices+tr->num_indices, 0, sizeof(FLAC__StreamMetadata_CueSheet_Index)*(num-tr->num_indices));
309         }
310
311         tr->num_indices = num;
312         cs_calc_len_(block);
313 }
314
315 static void tr_set_new_(FLAC__StreamMetadata *block, unsigned track_num, unsigned pos, FLAC__StreamMetadata_CueSheet_Index index)
316 {
317         FLAC__StreamMetadata_CueSheet_Track *tr;
318
319         FLAC__ASSERT(track_num < block->data.cue_sheet.num_tracks);
320
321         tr = &block->data.cue_sheet.tracks[track_num];
322
323         FLAC__ASSERT(pos < tr->num_indices);
324
325         tr->indices[pos] = index;
326
327         cs_calc_len_(block);
328 }
329
330 static void tr_insert_new_(FLAC__StreamMetadata *block, unsigned track_num, unsigned pos, FLAC__StreamMetadata_CueSheet_Index index)
331 {
332         FLAC__StreamMetadata_CueSheet_Track *tr;
333
334         FLAC__ASSERT(track_num < block->data.cue_sheet.num_tracks);
335
336         tr = &block->data.cue_sheet.tracks[track_num];
337
338         FLAC__ASSERT(pos <= tr->num_indices);
339
340         tr_resize_(block, track_num, tr->num_indices+1);
341         memmove(&tr->indices[pos+1], &tr->indices[pos], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(tr->num_indices-1-pos));
342         tr_set_new_(block, track_num, pos, index);
343         cs_calc_len_(block);
344 }
345
346 static void tr_delete_(FLAC__StreamMetadata *block, unsigned track_num, unsigned pos)
347 {
348         FLAC__StreamMetadata_CueSheet_Track *tr;
349
350         FLAC__ASSERT(track_num < block->data.cue_sheet.num_tracks);
351
352         tr = &block->data.cue_sheet.tracks[track_num];
353
354         FLAC__ASSERT(pos <= tr->num_indices);
355
356         memmove(&tr->indices[pos], &tr->indices[pos+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(tr->num_indices-pos-1));
357         tr_resize_(block, track_num, tr->num_indices-1);
358         cs_calc_len_(block);
359 }
360
361 static void cs_resize_(FLAC__StreamMetadata *block, unsigned num)
362 {
363         FLAC__StreamMetadata_CueSheet *cs = &block->data.cue_sheet;
364
365         if(cs->num_tracks != 0) {
366                 FLAC__ASSERT(0 != cs->tracks);
367                 if(num < cs->num_tracks) {
368                         unsigned i;
369                         for(i = num; i < cs->num_tracks; i++) {
370                                 if(0 != cs->tracks[i].indices)
371                                         free(cs->tracks[i].indices);
372                         }
373                 }
374         }
375         if(num == 0) {
376                 if(0 != cs->tracks) {
377                         free(cs->tracks);
378                         cs->tracks = 0;
379                 }
380         }
381         else {
382                 cs->tracks = (FLAC__StreamMetadata_CueSheet_Track*)realloc(cs->tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)*num);
383                 FLAC__ASSERT(0 != cs->tracks);
384                 if(num > cs->num_tracks)
385                         memset(cs->tracks+cs->num_tracks, 0, sizeof(FLAC__StreamMetadata_CueSheet_Track)*(num-cs->num_tracks));
386         }
387
388         cs->num_tracks = num;
389         cs_calc_len_(block);
390 }
391
392 static void cs_set_new_(FLAC__StreamMetadata_CueSheet_Track *track, FLAC__StreamMetadata *block, unsigned pos, FLAC__uint64 offset, FLAC__byte number, const char *isrc, FLAC__bool data, FLAC__bool pre_em)
393 {
394         track_new_(track, offset, number, isrc, data, pre_em);
395         block->data.cue_sheet.tracks[pos] = *track;
396         cs_calc_len_(block);
397 }
398
399 static void cs_insert_new_(FLAC__StreamMetadata_CueSheet_Track *track, FLAC__StreamMetadata *block, unsigned pos, FLAC__uint64 offset, FLAC__byte number, const char *isrc, FLAC__bool data, FLAC__bool pre_em)
400 {
401         cs_resize_(block, block->data.cue_sheet.num_tracks+1);
402         memmove(&block->data.cue_sheet.tracks[pos+1], &block->data.cue_sheet.tracks[pos], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(block->data.cue_sheet.num_tracks-1-pos));
403         cs_set_new_(track, block, pos, offset, number, isrc, data, pre_em);
404         cs_calc_len_(block);
405 }
406
407 static void cs_delete_(FLAC__StreamMetadata *block, unsigned pos)
408 {
409         if(0 != block->data.cue_sheet.tracks[pos].indices)
410                 free(block->data.cue_sheet.tracks[pos].indices);
411         memmove(&block->data.cue_sheet.tracks[pos], &block->data.cue_sheet.tracks[pos+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(block->data.cue_sheet.num_tracks-pos-1));
412         block->data.cue_sheet.tracks[block->data.cue_sheet.num_tracks-1].indices = 0;
413         block->data.cue_sheet.tracks[block->data.cue_sheet.num_tracks-1].num_indices = 0;
414         cs_resize_(block, block->data.cue_sheet.num_tracks-1);
415         cs_calc_len_(block);
416 }
417
418
419 FLAC__bool test_metadata_object()
420 {
421         FLAC__StreamMetadata *block, *blockcopy, *vorbiscomment, *cuesheet;
422         FLAC__StreamMetadata_SeekPoint seekpoint_array[8];
423         FLAC__StreamMetadata_VorbisComment_Entry entry;
424         FLAC__StreamMetadata_CueSheet_Index index;
425         FLAC__StreamMetadata_CueSheet_Track track;
426         unsigned i, expected_length, seekpoints;
427         static FLAC__byte dummydata[4] = { 'a', 'b', 'c', 'd' };
428
429         printf("\n+++ libFLAC unit test: metadata objects\n\n");
430
431
432         printf("testing STREAMINFO\n");
433
434         printf("testing FLAC__metadata_object_new()... ");
435         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_STREAMINFO);
436         if(0 == block) {
437                 printf("FAILED, returned NULL\n");
438                 return false;
439         }
440         expected_length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
441         if(block->length != expected_length) {
442                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
443                 return false;
444         }
445         printf("OK\n");
446
447         printf("testing FLAC__metadata_object_clone()... ");
448         blockcopy = FLAC__metadata_object_clone(block);
449         if(0 == blockcopy) {
450                 printf("FAILED, returned NULL\n");
451                 return false;
452         }
453         if(!compare_block_(block, blockcopy))
454                 return false;
455         printf("OK\n");
456
457         printf("testing FLAC__metadata_object_delete()... ");
458         FLAC__metadata_object_delete(blockcopy);
459         FLAC__metadata_object_delete(block);
460         printf("OK\n");
461
462
463         printf("testing PADDING\n");
464
465         printf("testing FLAC__metadata_object_new()... ");
466         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
467         if(0 == block) {
468                 printf("FAILED, returned NULL\n");
469                 return false;
470         }
471         expected_length = 0;
472         if(block->length != expected_length) {
473                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
474                 return false;
475         }
476         printf("OK\n");
477
478         printf("testing FLAC__metadata_object_clone()... ");
479         blockcopy = FLAC__metadata_object_clone(block);
480         if(0 == blockcopy) {
481                 printf("FAILED, returned NULL\n");
482                 return false;
483         }
484         if(!compare_block_(block, blockcopy))
485                 return false;
486         printf("OK\n");
487
488         printf("testing FLAC__metadata_object_delete()... ");
489         FLAC__metadata_object_delete(blockcopy);
490         FLAC__metadata_object_delete(block);
491         printf("OK\n");
492
493
494         printf("testing APPLICATION\n");
495
496         printf("testing FLAC__metadata_object_new()... ");
497         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION);
498         if(0 == block) {
499                 printf("FAILED, returned NULL\n");
500                 return false;
501         }
502         expected_length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
503         if(block->length != expected_length) {
504                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
505                 return false;
506         }
507         printf("OK\n");
508
509         printf("testing FLAC__metadata_object_clone()... ");
510         blockcopy = FLAC__metadata_object_clone(block);
511         if(0 == blockcopy) {
512                 printf("FAILED, returned NULL\n");
513                 return false;
514         }
515         if(!compare_block_(block, blockcopy))
516                 return false;
517         printf("OK\n");
518
519         printf("testing FLAC__metadata_object_delete()... ");
520         FLAC__metadata_object_delete(blockcopy);
521         printf("OK\n");
522
523         printf("testing FLAC__metadata_object_application_set_data(copy)... ");
524         if(!FLAC__metadata_object_application_set_data(block, dummydata, sizeof(dummydata), true/*copy*/)) {
525                 printf("FAILED, returned false\n");
526                 return false;
527         }
528         expected_length = (FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) + sizeof(dummydata);
529         if(block->length != expected_length) {
530                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
531                 return false;
532         }
533         if(0 != memcmp(block->data.application.data, dummydata, sizeof(dummydata))) {
534                 printf("FAILED, data mismatch\n");
535                 return false;
536         }
537         printf("OK\n");
538
539         printf("testing FLAC__metadata_object_clone()... ");
540         blockcopy = FLAC__metadata_object_clone(block);
541         if(0 == blockcopy) {
542                 printf("FAILED, returned NULL\n");
543                 return false;
544         }
545         if(!compare_block_(block, blockcopy))
546                 return false;
547         printf("OK\n");
548
549         printf("testing FLAC__metadata_object_delete()... ");
550         FLAC__metadata_object_delete(blockcopy);
551         printf("OK\n");
552
553         printf("testing FLAC__metadata_object_application_set_data(own)... ");
554         if(!FLAC__metadata_object_application_set_data(block, make_dummydata_(dummydata, sizeof(dummydata)), sizeof(dummydata), false/*own*/)) {
555                 printf("FAILED, returned false\n");
556                 return false;
557         }
558         expected_length = (FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) + sizeof(dummydata);
559         if(block->length != expected_length) {
560                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
561                 return false;
562         }
563         if(0 != memcmp(block->data.application.data, dummydata, sizeof(dummydata))) {
564                 printf("FAILED, data mismatch\n");
565                 return false;
566         }
567         printf("OK\n");
568
569         printf("testing FLAC__metadata_object_clone()... ");
570         blockcopy = FLAC__metadata_object_clone(block);
571         if(0 == blockcopy) {
572                 printf("FAILED, returned NULL\n");
573                 return false;
574         }
575         if(!compare_block_(block, blockcopy))
576                 return false;
577         printf("OK\n");
578
579         printf("testing FLAC__metadata_object_delete()... ");
580         FLAC__metadata_object_delete(blockcopy);
581         FLAC__metadata_object_delete(block);
582         printf("OK\n");
583
584
585         printf("testing SEEKTABLE\n");
586
587         for(i = 0; i < sizeof(seekpoint_array) / sizeof(FLAC__StreamMetadata_SeekPoint); i++) {
588                 seekpoint_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
589                 seekpoint_array[i].stream_offset = 0;
590                 seekpoint_array[i].frame_samples = 0;
591         }
592
593         seekpoints = 0;
594         printf("testing FLAC__metadata_object_new()... ");
595         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE);
596         if(0 == block) {
597                 printf("FAILED, returned NULL\n");
598                 return false;
599         }
600         if(!check_seektable_(block, seekpoints, 0))
601                 return false;
602
603         printf("testing FLAC__metadata_object_clone()... ");
604         blockcopy = FLAC__metadata_object_clone(block);
605         if(0 == blockcopy) {
606                 printf("FAILED, returned NULL\n");
607                 return false;
608         }
609         if(!compare_block_(block, blockcopy))
610                 return false;
611         printf("OK\n");
612
613         printf("testing FLAC__metadata_object_delete()... ");
614         FLAC__metadata_object_delete(blockcopy);
615         printf("OK\n");
616
617         seekpoints = 2;
618         printf("testing FLAC__metadata_object_seektable_resize_points(grow to %u)...", seekpoints);
619         if(!FLAC__metadata_object_seektable_resize_points(block, seekpoints)) {
620                 printf("FAILED, returned false\n");
621                 return false;
622         }
623         if(!check_seektable_(block, seekpoints, seekpoint_array))
624                 return false;
625
626         seekpoints = 1;
627         printf("testing FLAC__metadata_object_seektable_resize_points(shrink to %u)...", seekpoints);
628         if(!FLAC__metadata_object_seektable_resize_points(block, seekpoints)) {
629                 printf("FAILED, returned false\n");
630                 return false;
631         }
632         if(!check_seektable_(block, seekpoints, seekpoint_array))
633                 return false;
634
635         printf("testing FLAC__metadata_object_seektable_is_legal()...");
636         if(!FLAC__metadata_object_seektable_is_legal(block)) {
637                 printf("FAILED, returned false\n");
638                 return false;
639         }
640         printf("OK\n");
641
642         seekpoints = 0;
643         printf("testing FLAC__metadata_object_seektable_resize_points(shrink to %u)...", seekpoints);
644         if(!FLAC__metadata_object_seektable_resize_points(block, seekpoints)) {
645                 printf("FAILED, returned false\n");
646                 return false;
647         }
648         if(!check_seektable_(block, seekpoints, 0))
649                 return false;
650
651         seekpoints++;
652         printf("testing FLAC__metadata_object_seektable_insert_point() on empty array...");
653         if(!FLAC__metadata_object_seektable_insert_point(block, 0, seekpoint_array[0])) {
654                 printf("FAILED, returned false\n");
655                 return false;
656         }
657         if(!check_seektable_(block, seekpoints, seekpoint_array))
658                 return false;
659
660         seekpoint_array[0].sample_number = 1;
661         seekpoints++;
662         printf("testing FLAC__metadata_object_seektable_insert_point() on beginning of non-empty array...");
663         if(!FLAC__metadata_object_seektable_insert_point(block, 0, seekpoint_array[0])) {
664                 printf("FAILED, returned false\n");
665                 return false;
666         }
667         if(!check_seektable_(block, seekpoints, seekpoint_array))
668                 return false;
669
670         seekpoint_array[1].sample_number = 2;
671         seekpoints++;
672         printf("testing FLAC__metadata_object_seektable_insert_point() on middle of non-empty array...");
673         if(!FLAC__metadata_object_seektable_insert_point(block, 1, seekpoint_array[1])) {
674                 printf("FAILED, returned false\n");
675                 return false;
676         }
677         if(!check_seektable_(block, seekpoints, seekpoint_array))
678                 return false;
679
680         seekpoint_array[3].sample_number = 3;
681         seekpoints++;
682         printf("testing FLAC__metadata_object_seektable_insert_point() on end of non-empty array...");
683         if(!FLAC__metadata_object_seektable_insert_point(block, 3, seekpoint_array[3])) {
684                 printf("FAILED, returned false\n");
685                 return false;
686         }
687         if(!check_seektable_(block, seekpoints, seekpoint_array))
688                 return false;
689
690         printf("testing FLAC__metadata_object_clone()... ");
691         blockcopy = FLAC__metadata_object_clone(block);
692         if(0 == blockcopy) {
693                 printf("FAILED, returned NULL\n");
694                 return false;
695         }
696         if(!compare_block_(block, blockcopy))
697                 return false;
698         printf("OK\n");
699
700         printf("testing FLAC__metadata_object_delete()... ");
701         FLAC__metadata_object_delete(blockcopy);
702         printf("OK\n");
703
704         seekpoint_array[2].sample_number = seekpoint_array[3].sample_number;
705         seekpoints--;
706         printf("testing FLAC__metadata_object_seektable_delete_point() on middle of array...");
707         if(!FLAC__metadata_object_seektable_delete_point(block, 2)) {
708                 printf("FAILED, returned false\n");
709                 return false;
710         }
711         if(!check_seektable_(block, seekpoints, seekpoint_array))
712                 return false;
713
714         seekpoints--;
715         printf("testing FLAC__metadata_object_seektable_delete_point() on end of array...");
716         if(!FLAC__metadata_object_seektable_delete_point(block, 2)) {
717                 printf("FAILED, returned false\n");
718                 return false;
719         }
720         if(!check_seektable_(block, seekpoints, seekpoint_array))
721                 return false;
722
723         seekpoints--;
724         printf("testing FLAC__metadata_object_seektable_delete_point() on beginning of array...");
725         if(!FLAC__metadata_object_seektable_delete_point(block, 0)) {
726                 printf("FAILED, returned false\n");
727                 return false;
728         }
729         if(!check_seektable_(block, seekpoints, seekpoint_array+1))
730                 return false;
731
732         printf("testing FLAC__metadata_object_seektable_set_point()...");
733         FLAC__metadata_object_seektable_set_point(block, 0, seekpoint_array[0]);
734         if(!check_seektable_(block, seekpoints, seekpoint_array))
735                 return false;
736
737         printf("testing FLAC__metadata_object_delete()... ");
738         FLAC__metadata_object_delete(block);
739         printf("OK\n");
740
741         /* seektable template functions */
742
743         for(i = 0; i < sizeof(seekpoint_array) / sizeof(FLAC__StreamMetadata_SeekPoint); i++) {
744                 seekpoint_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
745                 seekpoint_array[i].stream_offset = 0;
746                 seekpoint_array[i].frame_samples = 0;
747         }
748
749         seekpoints = 0;
750         printf("testing FLAC__metadata_object_new()... ");
751         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE);
752         if(0 == block) {
753                 printf("FAILED, returned NULL\n");
754                 return false;
755         }
756         if(!check_seektable_(block, seekpoints, 0))
757                 return false;
758
759         seekpoints += 2;
760         printf("testing FLAC__metadata_object_seekpoint_template_append_placeholders()... ");
761         if(!FLAC__metadata_object_seektable_template_append_placeholders(block, 2)) {
762                 printf("FAILED, returned false\n");
763                 return false;
764         }
765         if(!check_seektable_(block, seekpoints, seekpoint_array))
766                 return false;
767
768         seekpoint_array[seekpoints++].sample_number = 7;
769         printf("testing FLAC__metadata_object_seekpoint_template_append_point()... ");
770         if(!FLAC__metadata_object_seektable_template_append_point(block, 7)) {
771                 printf("FAILED, returned false\n");
772                 return false;
773         }
774         if(!check_seektable_(block, seekpoints, seekpoint_array))
775                 return false;
776
777         {
778                 FLAC__uint64 nums[2] = { 3, 7 };
779                 seekpoint_array[seekpoints++].sample_number = nums[0];
780                 seekpoint_array[seekpoints++].sample_number = nums[1];
781                 printf("testing FLAC__metadata_object_seekpoint_template_append_points()... ");
782                 if(!FLAC__metadata_object_seektable_template_append_points(block, nums, sizeof(nums)/sizeof(FLAC__uint64))) {
783                         printf("FAILED, returned false\n");
784                         return false;
785                 }
786                 if(!check_seektable_(block, seekpoints, seekpoint_array))
787                         return false;
788         }
789
790         seekpoint_array[seekpoints++].sample_number = 0;
791         seekpoint_array[seekpoints++].sample_number = 10;
792         seekpoint_array[seekpoints++].sample_number = 20;
793         printf("testing FLAC__metadata_object_seekpoint_template_append_spaced_points()... ");
794         if(!FLAC__metadata_object_seektable_template_append_spaced_points(block, 3, 30)) {
795                 printf("FAILED, returned false\n");
796                 return false;
797         }
798         if(!check_seektable_(block, seekpoints, seekpoint_array))
799                 return false;
800
801         seekpoints--;
802         seekpoint_array[0].sample_number = 0;
803         seekpoint_array[1].sample_number = 3;
804         seekpoint_array[2].sample_number = 7;
805         seekpoint_array[3].sample_number = 10;
806         seekpoint_array[4].sample_number = 20;
807         seekpoint_array[5].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
808         seekpoint_array[6].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
809         printf("testing FLAC__metadata_object_seekpoint_template_sort(compact=true)... ");
810         if(!FLAC__metadata_object_seektable_template_sort(block, /*compact=*/true)) {
811                 printf("FAILED, returned false\n");
812                 return false;
813         }
814         if(!FLAC__metadata_object_seektable_is_legal(block)) {
815                 printf("FAILED, seek table is illegal\n");
816                 return false;
817         }
818         if(!check_seektable_(block, seekpoints, seekpoint_array))
819                 return false;
820
821         printf("testing FLAC__metadata_object_seekpoint_template_sort(compact=false)... ");
822         if(!FLAC__metadata_object_seektable_template_sort(block, /*compact=*/false)) {
823                 printf("FAILED, returned false\n");
824                 return false;
825         }
826         if(!FLAC__metadata_object_seektable_is_legal(block)) {
827                 printf("FAILED, seek table is illegal\n");
828                 return false;
829         }
830         if(!check_seektable_(block, seekpoints, seekpoint_array))
831                 return false;
832
833         printf("testing FLAC__metadata_object_delete()... ");
834         FLAC__metadata_object_delete(block);
835         printf("OK\n");
836
837
838         printf("testing VORBIS_COMMENT\n");
839
840         printf("testing FLAC__metadata_object_new()... ");
841         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
842         if(0 == block) {
843                 printf("FAILED, returned NULL\n");
844                 return false;
845         }
846         expected_length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + strlen(FLAC__VENDOR_STRING) + FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN/8);
847         if(block->length != expected_length) {
848                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
849                 return false;
850         }
851         printf("OK\n");
852
853         printf("testing FLAC__metadata_object_clone()... ");
854         vorbiscomment = FLAC__metadata_object_clone(block);
855         if(0 == vorbiscomment) {
856                 printf("FAILED, returned NULL\n");
857                 return false;
858         }
859         if(!compare_block_(vorbiscomment, block))
860                 return false;
861         printf("OK\n");
862
863         vc_resize_(vorbiscomment, 2);
864         printf("testing FLAC__metadata_object_vorbiscomment_resize_comments(grow to %u)...", vorbiscomment->data.vorbis_comment.num_comments);
865         if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, vorbiscomment->data.vorbis_comment.num_comments)) {
866                 printf("FAILED, returned false\n");
867                 return false;
868         }
869         if(!compare_block_(vorbiscomment, block))
870                 return false;
871         printf("OK\n");
872
873         vc_resize_(vorbiscomment, 1);
874         printf("testing FLAC__metadata_object_vorbiscomment_resize_comments(shrink to %u)...", vorbiscomment->data.vorbis_comment.num_comments);
875         if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, vorbiscomment->data.vorbis_comment.num_comments)) {
876                 printf("FAILED, returned false\n");
877                 return false;
878         }
879         if(!compare_block_(vorbiscomment, block))
880                 return false;
881         printf("OK\n");
882
883         vc_resize_(vorbiscomment, 0);
884         printf("testing FLAC__metadata_object_vorbiscomment_resize_comments(shrink to %u)...", vorbiscomment->data.vorbis_comment.num_comments);
885         if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, vorbiscomment->data.vorbis_comment.num_comments)) {
886                 printf("FAILED, returned false\n");
887                 return false;
888         }
889         if(!compare_block_(vorbiscomment, block))
890                 return false;
891         printf("OK\n");
892
893         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on empty array...");
894         vc_insert_new_(&entry, vorbiscomment, 0, "name1=field1");
895         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/true)) {
896                 printf("FAILED, returned false\n");
897                 return false;
898         }
899         if(!compare_block_(vorbiscomment, block))
900                 return false;
901         printf("OK\n");
902
903         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on beginning of non-empty array...");
904         vc_insert_new_(&entry, vorbiscomment, 0, "name2=field2");
905         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/true)) {
906                 printf("FAILED, returned false\n");
907                 return false;
908         }
909         if(!compare_block_(vorbiscomment, block))
910                 return false;
911         printf("OK\n");
912
913         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on middle of non-empty array...");
914         vc_insert_new_(&entry, vorbiscomment, 1, "name3=field3");
915         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 1, entry, /*copy=*/true)) {
916                 printf("FAILED, returned false\n");
917                 return false;
918         }
919         if(!compare_block_(vorbiscomment, block))
920                 return false;
921         printf("OK\n");
922
923         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on end of non-empty array...");
924         vc_insert_new_(&entry, vorbiscomment, 3, "name4=field4");
925         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 3, entry, /*copy=*/true)) {
926                 printf("FAILED, returned false\n");
927                 return false;
928         }
929         if(!compare_block_(vorbiscomment, block))
930                 return false;
931         printf("OK\n");
932
933         printf("testing FLAC__metadata_object_clone()... ");
934         blockcopy = FLAC__metadata_object_clone(block);
935         if(0 == blockcopy) {
936                 printf("FAILED, returned NULL\n");
937                 return false;
938         }
939         if(!compare_block_(block, blockcopy))
940                 return false;
941         printf("OK\n");
942
943         printf("testing FLAC__metadata_object_delete()... ");
944         FLAC__metadata_object_delete(blockcopy);
945         printf("OK\n");
946
947         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on middle of array...");
948         vc_delete_(vorbiscomment, 2);
949         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) {
950                 printf("FAILED, returned false\n");
951                 return false;
952         }
953         if(!compare_block_(vorbiscomment, block))
954                 return false;
955         printf("OK\n");
956
957         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on end of array...");
958         vc_delete_(vorbiscomment, 2);
959         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) {
960                 printf("FAILED, returned false\n");
961                 return false;
962         }
963         if(!compare_block_(vorbiscomment, block))
964                 return false;
965         printf("OK\n");
966
967         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on beginning of array...");
968         vc_delete_(vorbiscomment, 0);
969         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 0)) {
970                 printf("FAILED, returned false\n");
971                 return false;
972         }
973         if(!compare_block_(vorbiscomment, block))
974                 return false;
975         printf("OK\n");
976
977         printf("testing FLAC__metadata_object_vorbiscomment_set_comment(copy)...");
978         vc_set_new_(&entry, vorbiscomment, 0, "name5=field5");
979         FLAC__metadata_object_vorbiscomment_set_comment(block, 0, entry, /*copy=*/true);
980         if(!compare_block_(vorbiscomment, block))
981                 return false;
982         printf("OK\n");
983
984         printf("testing FLAC__metadata_object_vorbiscomment_set_vendor_string(copy)...");
985         vc_set_vs_new_(&entry, vorbiscomment, "name6=field6");
986         FLAC__metadata_object_vorbiscomment_set_vendor_string(block, entry, /*copy=*/true);
987         if(!compare_block_(vorbiscomment, block))
988                 return false;
989         printf("OK\n");
990
991         printf("testing FLAC__metadata_object_delete()... ");
992         FLAC__metadata_object_delete(vorbiscomment);
993         FLAC__metadata_object_delete(block);
994         printf("OK\n");
995
996
997         printf("testing FLAC__metadata_object_new()... ");
998         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
999         if(0 == block) {
1000                 printf("FAILED, returned NULL\n");
1001                 return false;
1002         }
1003         printf("OK\n");
1004
1005         printf("testing FLAC__metadata_object_clone()... ");
1006         vorbiscomment = FLAC__metadata_object_clone(block);
1007         if(0 == vorbiscomment) {
1008                 printf("FAILED, returned NULL\n");
1009                 return false;
1010         }
1011         if(!compare_block_(vorbiscomment, block))
1012                 return false;
1013         printf("OK\n");
1014
1015         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on empty array...");
1016         vc_insert_new_(&entry, vorbiscomment, 0, "name1=field1");
1017         entry_clone_(&entry);
1018         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/false)) {
1019                 printf("FAILED, returned false\n");
1020                 return false;
1021         }
1022         if(!compare_block_(vorbiscomment, block))
1023                 return false;
1024         printf("OK\n");
1025
1026         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on beginning of non-empty array...");
1027         vc_insert_new_(&entry, vorbiscomment, 0, "name2=field2");
1028         entry_clone_(&entry);
1029         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/false)) {
1030                 printf("FAILED, returned false\n");
1031                 return false;
1032         }
1033         if(!compare_block_(vorbiscomment, block))
1034                 return false;
1035         printf("OK\n");
1036
1037         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on middle of non-empty array...");
1038         vc_insert_new_(&entry, vorbiscomment, 1, "name3=field3");
1039         entry_clone_(&entry);
1040         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 1, entry, /*copy=*/false)) {
1041                 printf("FAILED, returned false\n");
1042                 return false;
1043         }
1044         if(!compare_block_(vorbiscomment, block))
1045                 return false;
1046         printf("OK\n");
1047
1048         printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on end of non-empty array...");
1049         vc_insert_new_(&entry, vorbiscomment, 3, "name4=field4");
1050         entry_clone_(&entry);
1051         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 3, entry, /*copy=*/false)) {
1052                 printf("FAILED, returned false\n");
1053                 return false;
1054         }
1055         if(!compare_block_(vorbiscomment, block))
1056                 return false;
1057         printf("OK\n");
1058
1059         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on middle of array...");
1060         vc_delete_(vorbiscomment, 2);
1061         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) {
1062                 printf("FAILED, returned false\n");
1063                 return false;
1064         }
1065         if(!compare_block_(vorbiscomment, block))
1066                 return false;
1067         printf("OK\n");
1068
1069         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on end of array...");
1070         vc_delete_(vorbiscomment, 2);
1071         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) {
1072                 printf("FAILED, returned false\n");
1073                 return false;
1074         }
1075         if(!compare_block_(vorbiscomment, block))
1076                 return false;
1077         printf("OK\n");
1078
1079         printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on beginning of array...");
1080         vc_delete_(vorbiscomment, 0);
1081         if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 0)) {
1082                 printf("FAILED, returned false\n");
1083                 return false;
1084         }
1085         if(!compare_block_(vorbiscomment, block))
1086                 return false;
1087         printf("OK\n");
1088
1089         printf("testing FLAC__metadata_object_vorbiscomment_set_comment(own)...");
1090         vc_set_new_(&entry, vorbiscomment, 0, "name5=field5");
1091         entry_clone_(&entry);
1092         FLAC__metadata_object_vorbiscomment_set_comment(block, 0, entry, /*copy=*/false);
1093         if(!compare_block_(vorbiscomment, block))
1094                 return false;
1095         printf("OK\n");
1096
1097         printf("testing FLAC__metadata_object_vorbiscomment_set_vendor_string(own)...");
1098         vc_set_vs_new_(&entry, vorbiscomment, "name6=field6");
1099         entry_clone_(&entry);
1100         FLAC__metadata_object_vorbiscomment_set_vendor_string(block, entry, /*copy=*/false);
1101         if(!compare_block_(vorbiscomment, block))
1102                 return false;
1103         printf("OK\n");
1104
1105         printf("testing FLAC__metadata_object_delete()... ");
1106         FLAC__metadata_object_delete(vorbiscomment);
1107         FLAC__metadata_object_delete(block);
1108         printf("OK\n");
1109
1110
1111
1112         printf("testing CUESHEET\n");
1113
1114         {
1115                 FLAC__StreamMetadata_CueSheet_Track *track_, *trackcopy_;
1116
1117                 printf("testing FLAC__metadata_object_cuesheet_track_new()... ");
1118                 track_ = FLAC__metadata_object_cuesheet_track_new();
1119                 if(0 == track_) {
1120                         printf("FAILED, returned NULL\n");
1121                         return false;
1122                 }
1123                 printf("OK\n");
1124
1125                 printf("testing FLAC__metadata_object_cuesheet_track_clone()... ");
1126                 trackcopy_ = FLAC__metadata_object_cuesheet_track_clone(track_);
1127                 if(0 == trackcopy_) {
1128                         printf("FAILED, returned NULL\n");
1129                         return false;
1130                 }
1131                 if(!compare_track_(trackcopy_, track_))
1132                         return false;
1133                 printf("OK\n");
1134
1135                 printf("testing FLAC__metadata_object_cuesheet_track_delete()... ");
1136                 FLAC__metadata_object_cuesheet_track_delete(trackcopy_);
1137                 FLAC__metadata_object_cuesheet_track_delete(track_);
1138                 printf("OK\n");
1139         }
1140
1141
1142         printf("testing FLAC__metadata_object_new()... ");
1143         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET);
1144         if(0 == block) {
1145                 printf("FAILED, returned NULL\n");
1146                 return false;
1147         }
1148         expected_length = (
1149                 FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
1150                 FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
1151                 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
1152                 FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
1153         ) / 8;
1154         if(block->length != expected_length) {
1155                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length);
1156                 return false;
1157         }
1158         printf("OK\n");
1159
1160         printf("testing FLAC__metadata_object_clone()... ");
1161         cuesheet = FLAC__metadata_object_clone(block);
1162         if(0 == cuesheet) {
1163                 printf("FAILED, returned NULL\n");
1164                 return false;
1165         }
1166         if(!compare_block_(cuesheet, block))
1167                 return false;
1168         printf("OK\n");
1169
1170         cs_resize_(cuesheet, 2);
1171         printf("testing FLAC__metadata_object_cuesheet_resize_tracks(grow to %u)...", cuesheet->data.cue_sheet.num_tracks);
1172         if(!FLAC__metadata_object_cuesheet_resize_tracks(block, cuesheet->data.cue_sheet.num_tracks)) {
1173                 printf("FAILED, returned false\n");
1174                 return false;
1175         }
1176         if(!compare_block_(cuesheet, block))
1177                 return false;
1178         printf("OK\n");
1179
1180         cs_resize_(cuesheet, 1);
1181         printf("testing FLAC__metadata_object_cuesheet_resize_tracks(shrink to %u)...", cuesheet->data.cue_sheet.num_tracks);
1182         if(!FLAC__metadata_object_cuesheet_resize_tracks(block, cuesheet->data.cue_sheet.num_tracks)) {
1183                 printf("FAILED, returned false\n");
1184                 return false;
1185         }
1186         if(!compare_block_(cuesheet, block))
1187                 return false;
1188         printf("OK\n");
1189
1190         cs_resize_(cuesheet, 0);
1191         printf("testing FLAC__metadata_object_cuesheet_resize_tracks(shrink to %u)...", cuesheet->data.cue_sheet.num_tracks);
1192         if(!FLAC__metadata_object_cuesheet_resize_tracks(block, cuesheet->data.cue_sheet.num_tracks)) {
1193                 printf("FAILED, returned false\n");
1194                 return false;
1195         }
1196         if(!compare_block_(cuesheet, block))
1197                 return false;
1198         printf("OK\n");
1199
1200         printf("testing FLAC__metadata_object_cuesheet_insert_track(copy) on empty array...");
1201         cs_insert_new_(&track, cuesheet, 0, 0, 1, "ABCDE1234567", false, false);
1202         if(!FLAC__metadata_object_cuesheet_insert_track(block, 0, &track, /*copy=*/true)) {
1203                 printf("FAILED, returned false\n");
1204                 return false;
1205         }
1206         if(!compare_block_(cuesheet, block))
1207                 return false;
1208         printf("OK\n");
1209
1210         printf("testing FLAC__metadata_object_cuesheet_insert_track(copy) on beginning of non-empty array...");
1211         cs_insert_new_(&track, cuesheet, 0, 10, 2, "BBCDE1234567", false, false);
1212         if(!FLAC__metadata_object_cuesheet_insert_track(block, 0, &track, /*copy=*/true)) {
1213                 printf("FAILED, returned false\n");
1214                 return false;
1215         }
1216         if(!compare_block_(cuesheet, block))
1217                 return false;
1218         printf("OK\n");
1219
1220         printf("testing FLAC__metadata_object_cuesheet_insert_track(copy) on middle of non-empty array...");
1221         cs_insert_new_(&track, cuesheet, 1, 20, 3, "CBCDE1234567", false, false);
1222         if(!FLAC__metadata_object_cuesheet_insert_track(block, 1, &track, /*copy=*/true)) {
1223                 printf("FAILED, returned false\n");
1224                 return false;
1225         }
1226         if(!compare_block_(cuesheet, block))
1227                 return false;
1228         printf("OK\n");
1229
1230         printf("testing FLAC__metadata_object_cuesheet_insert_track(copy) on end of non-empty array...");
1231         cs_insert_new_(&track, cuesheet, 3, 30, 4, "DBCDE1234567", false, false);
1232         if(!FLAC__metadata_object_cuesheet_insert_track(block, 3, &track, /*copy=*/true)) {
1233                 printf("FAILED, returned false\n");
1234                 return false;
1235         }
1236         if(!compare_block_(cuesheet, block))
1237                 return false;
1238         printf("OK\n");
1239
1240         printf("testing FLAC__metadata_object_clone()... ");
1241         blockcopy = FLAC__metadata_object_clone(block);
1242         if(0 == blockcopy) {
1243                 printf("FAILED, returned NULL\n");
1244                 return false;
1245         }
1246         if(!compare_block_(block, blockcopy))
1247                 return false;
1248         printf("OK\n");
1249
1250         printf("testing FLAC__metadata_object_delete()... ");
1251         FLAC__metadata_object_delete(blockcopy);
1252         printf("OK\n");
1253
1254         printf("testing FLAC__metadata_object_cuesheet_delete_track() on middle of array...");
1255         cs_delete_(cuesheet, 2);
1256         if(!FLAC__metadata_object_cuesheet_delete_track(block, 2)) {
1257                 printf("FAILED, returned false\n");
1258                 return false;
1259         }
1260         if(!compare_block_(cuesheet, block))
1261                 return false;
1262         printf("OK\n");
1263
1264         printf("testing FLAC__metadata_object_cuesheet_delete_track() on end of array...");
1265         cs_delete_(cuesheet, 2);
1266         if(!FLAC__metadata_object_cuesheet_delete_track(block, 2)) {
1267                 printf("FAILED, returned false\n");
1268                 return false;
1269         }
1270         if(!compare_block_(cuesheet, block))
1271                 return false;
1272         printf("OK\n");
1273
1274         printf("testing FLAC__metadata_object_cuesheet_delete_track() on beginning of array...");
1275         cs_delete_(cuesheet, 0);
1276         if(!FLAC__metadata_object_cuesheet_delete_track(block, 0)) {
1277                 printf("FAILED, returned false\n");
1278                 return false;
1279         }
1280         if(!compare_block_(cuesheet, block))
1281                 return false;
1282         printf("OK\n");
1283
1284         printf("testing FLAC__metadata_object_cuesheet_set_track(copy)...");
1285         cs_set_new_(&track, cuesheet, 0, 40, 5, "EBCDE1234567", false, false);
1286         FLAC__metadata_object_cuesheet_set_track(block, 0, &track, /*copy=*/true);
1287         if(!compare_block_(cuesheet, block))
1288                 return false;
1289         printf("OK\n");
1290
1291         tr_resize_(cuesheet, 0, 2);
1292         printf("testing FLAC__metadata_object_cuesheet_track_resize_indices(grow to %u)...", cuesheet->data.cue_sheet.tracks[0].num_indices);
1293         if(!FLAC__metadata_object_cuesheet_track_resize_indices(block, 0, cuesheet->data.cue_sheet.tracks[0].num_indices)) {
1294                 printf("FAILED, returned false\n");
1295                 return false;
1296         }
1297         if(!compare_block_(cuesheet, block))
1298                 return false;
1299         printf("OK\n");
1300
1301         tr_resize_(cuesheet, 0, 1);
1302         printf("testing FLAC__metadata_object_cuesheet_track_resize_indices(shrink to %u)...", cuesheet->data.cue_sheet.tracks[0].num_indices);
1303         if(!FLAC__metadata_object_cuesheet_track_resize_indices(block, 0, cuesheet->data.cue_sheet.tracks[0].num_indices)) {
1304                 printf("FAILED, returned false\n");
1305                 return false;
1306         }
1307         if(!compare_block_(cuesheet, block))
1308                 return false;
1309         printf("OK\n");
1310
1311         tr_resize_(cuesheet, 0, 0);
1312         printf("testing FLAC__metadata_object_cuesheet_track_resize_indices(shrink to %u)...", cuesheet->data.cue_sheet.tracks[0].num_indices);
1313         if(!FLAC__metadata_object_cuesheet_track_resize_indices(block, 0, cuesheet->data.cue_sheet.tracks[0].num_indices)) {
1314                 printf("FAILED, returned false\n");
1315                 return false;
1316         }
1317         if(!compare_block_(cuesheet, block))
1318                 return false;
1319         printf("OK\n");
1320
1321         index.offset = 0;
1322         index.number = 1;
1323         printf("testing FLAC__metadata_object_cuesheet_track_insert_index() on empty array...");
1324         tr_insert_new_(cuesheet, 0, 0, index);
1325         if(!FLAC__metadata_object_cuesheet_track_insert_index(block, 0, 0, index)) {
1326                 printf("FAILED, returned false\n");
1327                 return false;
1328         }
1329         if(!compare_block_(cuesheet, block))
1330                 return false;
1331         printf("OK\n");
1332
1333         index.offset = 10;
1334         index.number = 2;
1335         printf("testing FLAC__metadata_object_cuesheet_track_insert_index() on beginning of non-empty array...");
1336         tr_insert_new_(cuesheet, 0, 0, index);
1337         if(!FLAC__metadata_object_cuesheet_track_insert_index(block, 0, 0, index)) {
1338                 printf("FAILED, returned false\n");
1339                 return false;
1340         }
1341         if(!compare_block_(cuesheet, block))
1342                 return false;
1343         printf("OK\n");
1344
1345         index.offset = 20;
1346         index.number = 3;
1347         printf("testing FLAC__metadata_object_cuesheet_track_insert_index() on middle of non-empty array...");
1348         tr_insert_new_(cuesheet, 0, 1, index);
1349         if(!FLAC__metadata_object_cuesheet_track_insert_index(block, 0, 1, index)) {
1350                 printf("FAILED, returned false\n");
1351                 return false;
1352         }
1353         if(!compare_block_(cuesheet, block))
1354                 return false;
1355         printf("OK\n");
1356
1357         index.offset = 30;
1358         index.number = 4;
1359         printf("testing FLAC__metadata_object_cuesheet_track_insert_index() on end of non-empty array...");
1360         tr_insert_new_(cuesheet, 0, 3, index);
1361         if(!FLAC__metadata_object_cuesheet_track_insert_index(block, 0, 3, index)) {
1362                 printf("FAILED, returned false\n");
1363                 return false;
1364         }
1365         if(!compare_block_(cuesheet, block))
1366                 return false;
1367         printf("OK\n");
1368
1369         printf("testing FLAC__metadata_object_clone()... ");
1370         blockcopy = FLAC__metadata_object_clone(block);
1371         if(0 == blockcopy) {
1372                 printf("FAILED, returned NULL\n");
1373                 return false;
1374         }
1375         if(!compare_block_(block, blockcopy))
1376                 return false;
1377         printf("OK\n");
1378
1379         printf("testing FLAC__metadata_object_delete()... ");
1380         FLAC__metadata_object_delete(blockcopy);
1381         printf("OK\n");
1382
1383         printf("testing FLAC__metadata_object_cuesheet_track_delete_index() on middle of array...");
1384         tr_delete_(cuesheet, 0, 2);
1385         if(!FLAC__metadata_object_cuesheet_track_delete_index(block, 0, 2)) {
1386                 printf("FAILED, returned false\n");
1387                 return false;
1388         }
1389         if(!compare_block_(cuesheet, block))
1390                 return false;
1391         printf("OK\n");
1392
1393         printf("testing FLAC__metadata_object_cuesheet_track_delete_index() on end of array...");
1394         tr_delete_(cuesheet, 0, 2);
1395         if(!FLAC__metadata_object_cuesheet_track_delete_index(block, 0, 2)) {
1396                 printf("FAILED, returned false\n");
1397                 return false;
1398         }
1399         if(!compare_block_(cuesheet, block))
1400                 return false;
1401         printf("OK\n");
1402
1403         printf("testing FLAC__metadata_object_cuesheet_track_delete_index() on beginning of array...");
1404         tr_delete_(cuesheet, 0, 0);
1405         if(!FLAC__metadata_object_cuesheet_track_delete_index(block, 0, 0)) {
1406                 printf("FAILED, returned false\n");
1407                 return false;
1408         }
1409         if(!compare_block_(cuesheet, block))
1410                 return false;
1411         printf("OK\n");
1412
1413         printf("testing FLAC__metadata_object_delete()... ");
1414         FLAC__metadata_object_delete(cuesheet);
1415         FLAC__metadata_object_delete(block);
1416         printf("OK\n");
1417
1418
1419         printf("testing FLAC__metadata_object_new()... ");
1420         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET);
1421         if(0 == block) {
1422                 printf("FAILED, returned NULL\n");
1423                 return false;
1424         }
1425         printf("OK\n");
1426
1427         printf("testing FLAC__metadata_object_clone()... ");
1428         cuesheet = FLAC__metadata_object_clone(block);
1429         if(0 == cuesheet) {
1430                 printf("FAILED, returned NULL\n");
1431                 return false;
1432         }
1433         if(!compare_block_(cuesheet, block))
1434                 return false;
1435         printf("OK\n");
1436
1437         printf("testing FLAC__metadata_object_cuesheet_insert_track(own) on empty array...");
1438         cs_insert_new_(&track, cuesheet, 0, 60, 7, "GBCDE1234567", false, false);
1439         track_clone_(&track);
1440         if(!FLAC__metadata_object_cuesheet_insert_track(block, 0, &track, /*copy=*/false)) {
1441                 printf("FAILED, returned false\n");
1442                 return false;
1443         }
1444         if(!compare_block_(cuesheet, block))
1445                 return false;
1446         printf("OK\n");
1447
1448         printf("testing FLAC__metadata_object_cuesheet_insert_track(own) on beginning of non-empty array...");
1449         cs_insert_new_(&track, cuesheet, 0, 70, 8, "HBCDE1234567", false, false);
1450         track_clone_(&track);
1451         if(!FLAC__metadata_object_cuesheet_insert_track(block, 0, &track, /*copy=*/false)) {
1452                 printf("FAILED, returned false\n");
1453                 return false;
1454         }
1455         if(!compare_block_(cuesheet, block))
1456                 return false;
1457         printf("OK\n");
1458
1459         printf("testing FLAC__metadata_object_cuesheet_insert_track(own) on middle of non-empty array...");
1460         cs_insert_new_(&track, cuesheet, 1, 80, 9, "IBCDE1234567", false, false);
1461         track_clone_(&track);
1462         if(!FLAC__metadata_object_cuesheet_insert_track(block, 1, &track, /*copy=*/false)) {
1463                 printf("FAILED, returned false\n");
1464                 return false;
1465         }
1466         if(!compare_block_(cuesheet, block))
1467                 return false;
1468         printf("OK\n");
1469
1470         printf("testing FLAC__metadata_object_cuesheet_insert_track(own) on end of non-empty array...");
1471         cs_insert_new_(&track, cuesheet, 3, 90, 10, "JBCDE1234567", false, false);
1472         track_clone_(&track);
1473         if(!FLAC__metadata_object_cuesheet_insert_track(block, 3, &track, /*copy=*/false)) {
1474                 printf("FAILED, returned false\n");
1475                 return false;
1476         }
1477         if(!compare_block_(cuesheet, block))
1478                 return false;
1479         printf("OK\n");
1480
1481         printf("testing FLAC__metadata_object_cuesheet_delete_track() on middle of array...");
1482         cs_delete_(cuesheet, 2);
1483         if(!FLAC__metadata_object_cuesheet_delete_track(block, 2)) {
1484                 printf("FAILED, returned false\n");
1485                 return false;
1486         }
1487         if(!compare_block_(cuesheet, block))
1488                 return false;
1489         printf("OK\n");
1490
1491         printf("testing FLAC__metadata_object_cuesheet_delete_track() on end of array...");
1492         cs_delete_(cuesheet, 2);
1493         if(!FLAC__metadata_object_cuesheet_delete_track(block, 2)) {
1494                 printf("FAILED, returned false\n");
1495                 return false;
1496         }
1497         if(!compare_block_(cuesheet, block))
1498                 return false;
1499         printf("OK\n");
1500
1501         printf("testing FLAC__metadata_object_cuesheet_delete_track() on beginning of array...");
1502         cs_delete_(cuesheet, 0);
1503         if(!FLAC__metadata_object_cuesheet_delete_track(block, 0)) {
1504                 printf("FAILED, returned false\n");
1505                 return false;
1506         }
1507         if(!compare_block_(cuesheet, block))
1508                 return false;
1509         printf("OK\n");
1510
1511         printf("testing FLAC__metadata_object_cuesheet_set_track(own)...");
1512         cs_set_new_(&track, cuesheet, 0, 100, 11, "KBCDE1234567", false, false);
1513         track_clone_(&track);
1514         FLAC__metadata_object_cuesheet_set_track(block, 0, &track, /*copy=*/false);
1515         if(!compare_block_(cuesheet, block))
1516                 return false;
1517         printf("OK\n");
1518
1519         printf("testing FLAC__metadata_object_cuesheet_is_legal()...");
1520         {
1521                 const char *violation;
1522                 if(FLAC__metadata_object_cuesheet_is_legal(block, /*check_cd_da_subset=*/true, &violation)) {
1523                         printf("FAILED, returned true when expecting false\n");
1524                         return false;
1525                 }
1526                 printf("returned false as expected, violation=\"%s\" OK\n", violation);
1527         }
1528
1529         printf("testing FLAC__metadata_object_delete()... ");
1530         FLAC__metadata_object_delete(cuesheet);
1531         FLAC__metadata_object_delete(block);
1532         printf("OK\n");
1533
1534
1535         return true;
1536 }