fix uninitialized var
[flac.git] / src / test_libFLAC++ / metadata_object.cpp
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 <stdio.h>
22 #include <stdlib.h> /* for malloc() */
23 #include <string.h> /* for memcmp() */
24
25 static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application_, vorbiscomment_, cuesheet_;
26
27 static bool die_(const char *msg)
28 {
29         printf("FAILED, %s\n", msg);
30         return false;
31 }
32
33 static void *malloc_or_die_(size_t size)
34 {
35         void *x = malloc(size);
36         if(0 == x) {
37                 fprintf(stderr, "ERROR: out of memory allocating %u bytes\n", (unsigned)size);
38                 exit(1);
39         }
40         return x;
41 }
42
43 static bool index_is_equal_(const ::FLAC__StreamMetadata_CueSheet_Index &index, const ::FLAC__StreamMetadata_CueSheet_Index &indexcopy)
44 {
45         if(indexcopy.offset != index.offset)
46                 return false;
47         if(indexcopy.number != index.number)
48                 return false;
49         return true;
50 }
51
52 static bool track_is_equal_(const ::FLAC__StreamMetadata_CueSheet_Track *track, const ::FLAC__StreamMetadata_CueSheet_Track *trackcopy)
53 {
54         unsigned i;
55
56         if(trackcopy->offset != track->offset)
57                 return false;
58         if(trackcopy->number != track->number)
59                 return false;
60         if(0 != strcmp(trackcopy->isrc, track->isrc))
61                 return false;
62         if(trackcopy->type != track->type)
63                 return false;
64         if(trackcopy->pre_emphasis != track->pre_emphasis)
65                 return false;
66         if(trackcopy->num_indices != track->num_indices)
67                 return false;
68         if(0 == track->indices || 0 == trackcopy->indices) {
69                 if(track->indices != trackcopy->indices)
70                         return false;
71         }
72         else {
73                 for(i = 0; i < track->num_indices; i++) {
74                         if(!index_is_equal_(trackcopy->indices[i], track->indices[i]))
75                                 return false;
76                 }
77         }
78         return true;
79 }
80
81 static void init_metadata_blocks_()
82 {
83         streaminfo_.is_last = false;
84         streaminfo_.type = ::FLAC__METADATA_TYPE_STREAMINFO;
85         streaminfo_.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
86         streaminfo_.data.stream_info.min_blocksize = 576;
87         streaminfo_.data.stream_info.max_blocksize = 576;
88         streaminfo_.data.stream_info.min_framesize = 0;
89         streaminfo_.data.stream_info.max_framesize = 0;
90         streaminfo_.data.stream_info.sample_rate = 44100;
91         streaminfo_.data.stream_info.channels = 1;
92         streaminfo_.data.stream_info.bits_per_sample = 8;
93         streaminfo_.data.stream_info.total_samples = 0;
94         memset(streaminfo_.data.stream_info.md5sum, 0, 16);
95
96         padding_.is_last = false;
97         padding_.type = ::FLAC__METADATA_TYPE_PADDING;
98         padding_.length = 1234;
99
100         seektable_.is_last = false;
101         seektable_.type = ::FLAC__METADATA_TYPE_SEEKTABLE;
102         seektable_.data.seek_table.num_points = 2;
103         seektable_.length = seektable_.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
104         seektable_.data.seek_table.points = (::FLAC__StreamMetadata_SeekPoint*)malloc_or_die_(seektable_.data.seek_table.num_points * sizeof(::FLAC__StreamMetadata_SeekPoint));
105         seektable_.data.seek_table.points[0].sample_number = 0;
106         seektable_.data.seek_table.points[0].stream_offset = 0;
107         seektable_.data.seek_table.points[0].frame_samples = streaminfo_.data.stream_info.min_blocksize;
108         seektable_.data.seek_table.points[1].sample_number = ::FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
109         seektable_.data.seek_table.points[1].stream_offset = 1000;
110         seektable_.data.seek_table.points[1].frame_samples = streaminfo_.data.stream_info.min_blocksize;
111
112         application_.is_last = false;
113         application_.type = ::FLAC__METADATA_TYPE_APPLICATION;
114         application_.length = 8;
115         memcpy(application_.data.application.id, "\xfe\xdc\xba\x98", 4);
116         application_.data.application.data = (FLAC__byte*)malloc_or_die_(4);
117         memcpy(application_.data.application.data, "\xf0\xe1\xd2\xc3", 4);
118
119         vorbiscomment_.is_last = false;
120         vorbiscomment_.type = ::FLAC__METADATA_TYPE_VORBIS_COMMENT;
121         vorbiscomment_.length = (4 + 5) + 4 + (4 + 12) + (4 + 12);
122         vorbiscomment_.data.vorbis_comment.vendor_string.length = 5;
123         vorbiscomment_.data.vorbis_comment.vendor_string.entry = (FLAC__byte*)malloc_or_die_(5);
124         memcpy(vorbiscomment_.data.vorbis_comment.vendor_string.entry, "name0", 5);
125         vorbiscomment_.data.vorbis_comment.num_comments = 2;
126         vorbiscomment_.data.vorbis_comment.comments = (::FLAC__StreamMetadata_VorbisComment_Entry*)malloc_or_die_(vorbiscomment_.data.vorbis_comment.num_comments * sizeof(::FLAC__StreamMetadata_VorbisComment_Entry));
127         vorbiscomment_.data.vorbis_comment.comments[0].length = 12;
128         vorbiscomment_.data.vorbis_comment.comments[0].entry = (FLAC__byte*)malloc_or_die_(12);
129         memcpy(vorbiscomment_.data.vorbis_comment.comments[0].entry, "name2=value2", 12);
130         vorbiscomment_.data.vorbis_comment.comments[1].length = 12;
131         vorbiscomment_.data.vorbis_comment.comments[1].entry = (FLAC__byte*)malloc_or_die_(12);
132         memcpy(vorbiscomment_.data.vorbis_comment.comments[1].entry, "name3=value3", 12);
133
134         cuesheet_.is_last = true;
135         cuesheet_.type = ::FLAC__METADATA_TYPE_CUESHEET;
136         cuesheet_.length =
137                 /* cuesheet guts */
138                 (
139                         FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
140                         FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
141                         FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
142                         FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
143                         FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
144                 ) / 8 +
145                 /* 2 tracks */
146                 2 * (
147                         FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
148                         FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
149                         FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
150                         FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
151                         FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
152                         FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
153                         FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
154                 ) / 8 +
155                 /* 3 index points */
156                 3 * (
157                         FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
158                         FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
159                         FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
160                 ) / 8
161         ;
162         memset(cuesheet_.data.cue_sheet.media_catalog_number, 0, sizeof(cuesheet_.data.cue_sheet.media_catalog_number));
163         cuesheet_.data.cue_sheet.media_catalog_number[0] = 'j';
164         cuesheet_.data.cue_sheet.media_catalog_number[1] = 'C';
165         cuesheet_.data.cue_sheet.lead_in = 159;
166         cuesheet_.data.cue_sheet.is_cd = true;
167         cuesheet_.data.cue_sheet.num_tracks = 2;
168         cuesheet_.data.cue_sheet.tracks = (FLAC__StreamMetadata_CueSheet_Track*)malloc_or_die_(cuesheet_.data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track));
169         cuesheet_.data.cue_sheet.tracks[0].offset = 1;
170         cuesheet_.data.cue_sheet.tracks[0].number = 1;
171         memcpy(cuesheet_.data.cue_sheet.tracks[0].isrc, "ACBDE1234567", sizeof(cuesheet_.data.cue_sheet.tracks[0].isrc));
172         cuesheet_.data.cue_sheet.tracks[0].type = 0;
173         cuesheet_.data.cue_sheet.tracks[0].pre_emphasis = 1;
174         cuesheet_.data.cue_sheet.tracks[0].num_indices = 2;
175         cuesheet_.data.cue_sheet.tracks[0].indices = (FLAC__StreamMetadata_CueSheet_Index*)malloc_or_die_(cuesheet_.data.cue_sheet.tracks[0].num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
176         cuesheet_.data.cue_sheet.tracks[0].indices[0].offset = 0;
177         cuesheet_.data.cue_sheet.tracks[0].indices[0].number = 0;
178         cuesheet_.data.cue_sheet.tracks[0].indices[1].offset = 1234567890;
179         cuesheet_.data.cue_sheet.tracks[0].indices[1].number = 1;
180         cuesheet_.data.cue_sheet.tracks[1].offset = 2345678901u;
181         cuesheet_.data.cue_sheet.tracks[1].number = 2;
182         memcpy(cuesheet_.data.cue_sheet.tracks[1].isrc, "ACBDE7654321", sizeof(cuesheet_.data.cue_sheet.tracks[1].isrc));
183         cuesheet_.data.cue_sheet.tracks[1].type = 1;
184         cuesheet_.data.cue_sheet.tracks[1].pre_emphasis = 0;
185         cuesheet_.data.cue_sheet.tracks[1].num_indices = 1;
186         cuesheet_.data.cue_sheet.tracks[1].indices = (FLAC__StreamMetadata_CueSheet_Index*)malloc_or_die_(cuesheet_.data.cue_sheet.tracks[1].num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
187         cuesheet_.data.cue_sheet.tracks[1].indices[0].offset = 0;
188         cuesheet_.data.cue_sheet.tracks[1].indices[0].number = 1;
189 }
190
191 static void free_metadata_blocks_()
192 {
193         free(seektable_.data.seek_table.points);
194         free(application_.data.application.data);
195         free(vorbiscomment_.data.vorbis_comment.vendor_string.entry);
196         free(vorbiscomment_.data.vorbis_comment.comments[0].entry);
197         free(vorbiscomment_.data.vorbis_comment.comments);
198         free(cuesheet_.data.cue_sheet.tracks[0].indices);
199         free(cuesheet_.data.cue_sheet.tracks[1].indices);
200         free(cuesheet_.data.cue_sheet.tracks);
201 }
202
203 bool test_metadata_object_streaminfo()
204 {
205         unsigned expected_length;
206
207         printf("testing class FLAC::Metadata::StreamInfo\n");
208
209         printf("testing StreamInfo::StreamInfo()... ");
210         FLAC::Metadata::StreamInfo block;
211         if(!block.is_valid())
212                 return die_("!block.is_valid()");
213         expected_length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
214         if(block.get_length() != expected_length) {
215                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block.get_length());
216                 return false;
217         }
218         printf("OK\n");
219
220         printf("testing StreamInfo::StreamInfo(const StreamInfo &)... +\n");
221         printf("        StreamInfo::operator!=(const StreamInfo &)... ");
222         {
223                 FLAC::Metadata::StreamInfo blockcopy(block);
224                 if(!blockcopy.is_valid())
225                         return die_("!block.is_valid()");
226                 if(blockcopy != block)
227                         return die_("copy is not identical to original");
228                 printf("OK\n");
229
230                 printf("testing StreamInfo::~StreamInfo()... ");
231         }
232         printf("OK\n");
233
234         printf("testing StreamInfo::StreamInfo(const ::FLAC__StreamMetadata &)... +\n");
235         printf("        StreamInfo::operator!=(const ::FLAC__StreamMetadata &)... ");
236         {
237                 FLAC::Metadata::StreamInfo blockcopy(streaminfo_);
238                 if(!blockcopy.is_valid())
239                         return die_("!block.is_valid()");
240                 if(blockcopy != streaminfo_)
241                         return die_("copy is not identical to original");
242                 printf("OK\n");
243         }
244
245         printf("testing StreamInfo::StreamInfo(const ::FLAC__StreamMetadata *)... +\n");
246         printf("        StreamInfo::operator!=(const ::FLAC__StreamMetadata *)... ");
247         {
248                 FLAC::Metadata::StreamInfo blockcopy(&streaminfo_);
249                 if(!blockcopy.is_valid())
250                         return die_("!block.is_valid()");
251                 if(blockcopy != streaminfo_)
252                         return die_("copy is not identical to original");
253                 printf("OK\n");
254         }
255
256         printf("testing StreamInfo::operator=(const StreamInfo &)... +\n");
257         printf("        StreamInfo::operator==(const StreamInfo &)... ");
258         {
259                 FLAC::Metadata::StreamInfo blockcopy = block;
260                 if(!blockcopy.is_valid())
261                         return die_("!block.is_valid()");
262                 if(!(blockcopy == block))
263                         return die_("copy is not identical to original");
264                 printf("OK\n");
265         }
266
267         printf("testing StreamInfo::operator=(const ::FLAC__StreamMetadata &)... +\n");
268         printf("        StreamInfo::operator==(const ::FLAC__StreamMetadata &)... ");
269         {
270                 FLAC::Metadata::StreamInfo blockcopy = streaminfo_;
271                 if(!blockcopy.is_valid())
272                         return die_("!block.is_valid()");
273                 if(!(blockcopy == streaminfo_))
274                         return die_("copy is not identical to original");
275                 printf("OK\n");
276         }
277
278         printf("testing StreamInfo::operator=(const ::FLAC__StreamMetadata *)... +\n");
279         printf("        StreamInfo::operator==(const ::FLAC__StreamMetadata *)... ");
280         {
281                 FLAC::Metadata::StreamInfo blockcopy = &streaminfo_;
282                 if(!blockcopy.is_valid())
283                         return die_("!block.is_valid()");
284                 if(!(blockcopy == streaminfo_))
285                         return die_("copy is not identical to original");
286                 printf("OK\n");
287         }
288
289         printf("testing StreamInfo::set_min_blocksize()... ");
290         block.set_min_blocksize(streaminfo_.data.stream_info.min_blocksize);
291         printf("OK\n");
292
293         printf("testing StreamInfo::set_max_blocksize()... ");
294         block.set_max_blocksize(streaminfo_.data.stream_info.max_blocksize);
295         printf("OK\n");
296
297         printf("testing StreamInfo::set_min_framesize()... ");
298         block.set_min_framesize(streaminfo_.data.stream_info.min_framesize);
299         printf("OK\n");
300
301         printf("testing StreamInfo::set_max_framesize()... ");
302         block.set_max_framesize(streaminfo_.data.stream_info.max_framesize);
303         printf("OK\n");
304
305         printf("testing StreamInfo::set_sample_rate()... ");
306         block.set_sample_rate(streaminfo_.data.stream_info.sample_rate);
307         printf("OK\n");
308
309         printf("testing StreamInfo::set_channels()... ");
310         block.set_channels(streaminfo_.data.stream_info.channels);
311         printf("OK\n");
312
313         printf("testing StreamInfo::set_bits_per_sample()... ");
314         block.set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample);
315         printf("OK\n");
316
317         printf("testing StreamInfo::set_total_samples()... ");
318         block.set_total_samples(streaminfo_.data.stream_info.total_samples);
319         printf("OK\n");
320
321         printf("testing StreamInfo::set_md5sum()... ");
322         block.set_md5sum(streaminfo_.data.stream_info.md5sum);
323         printf("OK\n");
324
325         printf("testing StreamInfo::get_min_blocksize()... ");
326         if(block.get_min_blocksize() != streaminfo_.data.stream_info.min_blocksize)
327                 return die_("value mismatch, doesn't match previously set value");
328         printf("OK\n");
329
330         printf("testing StreamInfo::get_max_blocksize()... ");
331         if(block.get_max_blocksize() != streaminfo_.data.stream_info.max_blocksize)
332                 return die_("value mismatch, doesn't match previously set value");
333         printf("OK\n");
334
335         printf("testing StreamInfo::get_min_framesize()... ");
336         if(block.get_min_framesize() != streaminfo_.data.stream_info.min_framesize)
337                 return die_("value mismatch, doesn't match previously set value");
338         printf("OK\n");
339
340         printf("testing StreamInfo::get_max_framesize()... ");
341         if(block.get_max_framesize() != streaminfo_.data.stream_info.max_framesize)
342                 return die_("value mismatch, doesn't match previously set value");
343         printf("OK\n");
344
345         printf("testing StreamInfo::get_sample_rate()... ");
346         if(block.get_sample_rate() != streaminfo_.data.stream_info.sample_rate)
347                 return die_("value mismatch, doesn't match previously set value");
348         printf("OK\n");
349
350         printf("testing StreamInfo::get_channels()... ");
351         if(block.get_channels() != streaminfo_.data.stream_info.channels)
352                 return die_("value mismatch, doesn't match previously set value");
353         printf("OK\n");
354
355         printf("testing StreamInfo::get_bits_per_sample()... ");
356         if(block.get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample)
357                 return die_("value mismatch, doesn't match previously set value");
358         printf("OK\n");
359
360         printf("testing StreamInfo::get_total_samples()... ");
361         if(block.get_total_samples() != streaminfo_.data.stream_info.total_samples)
362                 return die_("value mismatch, doesn't match previously set value");
363         printf("OK\n");
364
365         printf("testing StreamInfo::get_md5sum()... ");
366         if(0 != memcmp(block.get_md5sum(), streaminfo_.data.stream_info.md5sum, 16))
367                 return die_("value mismatch, doesn't match previously set value");
368         printf("OK\n");
369
370         printf("testing FLAC::Metadata::clone(const FLAC::Metadata::Prototype *)... ");
371         FLAC::Metadata::Prototype *clone_ = FLAC::Metadata::clone(&block);
372         if(0 == clone_)
373                 return die_("returned NULL");
374         if(0 == dynamic_cast<FLAC::Metadata::StreamInfo *>(clone_))
375                 return die_("downcast is NULL");
376         if(*dynamic_cast<FLAC::Metadata::StreamInfo *>(clone_) != block)
377                 return die_("clone is not identical");
378         printf("OK\n");
379
380
381         printf("PASSED\n\n");
382         return true;
383 }
384
385 bool test_metadata_object_padding()
386 {
387         unsigned expected_length;
388
389         printf("testing class FLAC::Metadata::Padding\n");
390
391         printf("testing Padding::Padding()... ");
392         FLAC::Metadata::Padding block;
393         if(!block.is_valid())
394                 return die_("!block.is_valid()");
395         expected_length = 0;
396         if(block.get_length() != expected_length) {
397                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block.get_length());
398                 return false;
399         }
400         printf("OK\n");
401
402         printf("testing Padding::Padding(const Padding &)... +\n");
403         printf("        Padding::operator!=(const Padding &)... ");
404         {
405                 FLAC::Metadata::Padding blockcopy(block);
406                 if(!blockcopy.is_valid())
407                         return die_("!block.is_valid()");
408                 if(blockcopy != block)
409                         return die_("copy is not identical to original");
410                 printf("OK\n");
411
412                 printf("testing Padding::~Padding()... ");
413         }
414         printf("OK\n");
415
416         printf("testing Padding::Padding(const ::FLAC__StreamMetadata &)... +\n");
417         printf("        Padding::operator!=(const ::FLAC__StreamMetadata &)... ");
418         {
419                 FLAC::Metadata::Padding blockcopy(padding_);
420                 if(!blockcopy.is_valid())
421                         return die_("!block.is_valid()");
422                 if(blockcopy != padding_)
423                         return die_("copy is not identical to original");
424                 printf("OK\n");
425         }
426
427         printf("testing Padding::Padding(const ::FLAC__StreamMetadata *)... +\n");
428         printf("        Padding::operator!=(const ::FLAC__StreamMetadata *)... ");
429         {
430                 FLAC::Metadata::Padding blockcopy(&padding_);
431                 if(!blockcopy.is_valid())
432                         return die_("!block.is_valid()");
433                 if(blockcopy != padding_)
434                         return die_("copy is not identical to original");
435                 printf("OK\n");
436         }
437
438         printf("testing Padding::operator=(const Padding &)... +\n");
439         printf("        Padding::operator==(const Padding &)... ");
440         {
441                 FLAC::Metadata::Padding blockcopy = block;
442                 if(!blockcopy.is_valid())
443                         return die_("!block.is_valid()");
444                 if(!(blockcopy == block))
445                         return die_("copy is not identical to original");
446                 printf("OK\n");
447         }
448
449         printf("testing Padding::operator=(const ::FLAC__StreamMetadata &)... +\n");
450         printf("        Padding::operator==(const ::FLAC__StreamMetadata &)... ");
451         {
452                 FLAC::Metadata::Padding blockcopy = padding_;
453                 if(!blockcopy.is_valid())
454                         return die_("!block.is_valid()");
455                 if(!(blockcopy == padding_))
456                         return die_("copy is not identical to original");
457                 printf("OK\n");
458         }
459
460         printf("testing Padding::operator=(const ::FLAC__StreamMetadata *)... +\n");
461         printf("        Padding::operator==(const ::FLAC__StreamMetadata *)... ");
462         {
463                 FLAC::Metadata::Padding blockcopy = &padding_;
464                 if(!blockcopy.is_valid())
465                         return die_("!block.is_valid()");
466                 if(!(blockcopy == padding_))
467                         return die_("copy is not identical to original");
468                 printf("OK\n");
469         }
470
471         printf("testing Padding::set_length()... ");
472         block.set_length(padding_.length);
473         printf("OK\n");
474
475         printf("testing Prototype::get_length()... ");
476         if(block.get_length() != padding_.length)
477                 return die_("value mismatch, doesn't match previously set value");
478         printf("OK\n");
479
480         printf("testing FLAC::Metadata::clone(const FLAC::Metadata::Prototype *)... ");
481         FLAC::Metadata::Prototype *clone_ = FLAC::Metadata::clone(&block);
482         if(0 == clone_)
483                 return die_("returned NULL");
484         if(0 == dynamic_cast<FLAC::Metadata::Padding *>(clone_))
485                 return die_("downcast is NULL");
486         if(*dynamic_cast<FLAC::Metadata::Padding *>(clone_) != block)
487                 return die_("clone is not identical");
488         printf("OK\n");
489
490
491         printf("PASSED\n\n");
492         return true;
493 }
494
495 bool test_metadata_object_application()
496 {
497         unsigned expected_length;
498
499         printf("testing class FLAC::Metadata::Application\n");
500
501         printf("testing Application::Application()... ");
502         FLAC::Metadata::Application block;
503         if(!block.is_valid())
504                 return die_("!block.is_valid()");
505         expected_length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
506         if(block.get_length() != expected_length) {
507                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block.get_length());
508                 return false;
509         }
510         printf("OK\n");
511
512         printf("testing Application::Application(const Application &)... +\n");
513         printf("        Application::operator!=(const Application &)... ");
514         {
515                 FLAC::Metadata::Application blockcopy(block);
516                 if(!blockcopy.is_valid())
517                         return die_("!block.is_valid()");
518                 if(blockcopy != block)
519                         return die_("copy is not identical to original");
520                 printf("OK\n");
521
522                 printf("testing Application::~Application()... ");
523         }
524         printf("OK\n");
525
526         printf("testing Application::Application(const ::FLAC__StreamMetadata &)... +\n");
527         printf("        Application::operator!=(const ::FLAC__StreamMetadata &)... ");
528         {
529                 FLAC::Metadata::Application blockcopy(application_);
530                 if(!blockcopy.is_valid())
531                         return die_("!block.is_valid()");
532                 if(blockcopy != application_)
533                         return die_("copy is not identical to original");
534                 printf("OK\n");
535         }
536
537         printf("testing Application::Application(const ::FLAC__StreamMetadata *)... +\n");
538         printf("        Application::operator!=(const ::FLAC__StreamMetadata *)... ");
539         {
540                 FLAC::Metadata::Application blockcopy(&application_);
541                 if(!blockcopy.is_valid())
542                         return die_("!block.is_valid()");
543                 if(blockcopy != application_)
544                         return die_("copy is not identical to original");
545                 printf("OK\n");
546         }
547
548         printf("testing Application::operator=(const Application &)... +\n");
549         printf("        Application::operator==(const Application &)... ");
550         {
551                 FLAC::Metadata::Application blockcopy = block;
552                 if(!blockcopy.is_valid())
553                         return die_("!block.is_valid()");
554                 if(!(blockcopy == block))
555                         return die_("copy is not identical to original");
556                 printf("OK\n");
557         }
558
559         printf("testing Application::operator=(const ::FLAC__StreamMetadata &)... +\n");
560         printf("        Application::operator==(const ::FLAC__StreamMetadata &)... ");
561         {
562                 FLAC::Metadata::Application blockcopy = application_;
563                 if(!blockcopy.is_valid())
564                         return die_("!block.is_valid()");
565                 if(!(blockcopy == application_))
566                         return die_("copy is not identical to original");
567                 printf("OK\n");
568         }
569
570         printf("testing Application::operator=(const ::FLAC__StreamMetadata *)... +\n");
571         printf("        Application::operator==(const ::FLAC__StreamMetadata *)... ");
572         {
573                 FLAC::Metadata::Application blockcopy = &application_;
574                 if(!blockcopy.is_valid())
575                         return die_("!block.is_valid()");
576                 if(!(blockcopy == application_))
577                         return die_("copy is not identical to original");
578                 printf("OK\n");
579         }
580
581         printf("testing Application::set_id()... ");
582         block.set_id(application_.data.application.id);
583         printf("OK\n");
584
585         printf("testing Application::set_data()... ");
586         block.set_data(application_.data.application.data, application_.length - sizeof(application_.data.application.id), /*copy=*/true);
587         printf("OK\n");
588
589         printf("testing Application::get_id()... ");
590         if(0 != memcmp(block.get_id(), application_.data.application.id, sizeof(application_.data.application.id)))
591                 return die_("value mismatch, doesn't match previously set value");
592         printf("OK\n");
593
594         printf("testing Application::get_data()... ");
595         if(0 != memcmp(block.get_data(), application_.data.application.data, application_.length - sizeof(application_.data.application.id)))
596                 return die_("value mismatch, doesn't match previously set value");
597         printf("OK\n");
598
599         printf("testing FLAC::Metadata::clone(const FLAC::Metadata::Prototype *)... ");
600         FLAC::Metadata::Prototype *clone_ = FLAC::Metadata::clone(&block);
601         if(0 == clone_)
602                 return die_("returned NULL");
603         if(0 == dynamic_cast<FLAC::Metadata::Application *>(clone_))
604                 return die_("downcast is NULL");
605         if(*dynamic_cast<FLAC::Metadata::Application *>(clone_) != block)
606                 return die_("clone is not identical");
607         printf("OK\n");
608
609
610         printf("PASSED\n\n");
611         return true;
612 }
613
614 bool test_metadata_object_seektable()
615 {
616         unsigned expected_length;
617
618         printf("testing class FLAC::Metadata::SeekTable\n");
619
620         printf("testing SeekTable::SeekTable()... ");
621         FLAC::Metadata::SeekTable block;
622         if(!block.is_valid())
623                 return die_("!block.is_valid()");
624         expected_length = 0;
625         if(block.get_length() != expected_length) {
626                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block.get_length());
627                 return false;
628         }
629         printf("OK\n");
630
631         printf("testing SeekTable::SeekTable(const SeekTable &)... +\n");
632         printf("        SeekTable::operator!=(const SeekTable &)... ");
633         {
634                 FLAC::Metadata::SeekTable blockcopy(block);
635                 if(!blockcopy.is_valid())
636                         return die_("!block.is_valid()");
637                 if(blockcopy != block)
638                         return die_("copy is not identical to original");
639                 printf("OK\n");
640
641                 printf("testing SeekTable::~SeekTable()... ");
642         }
643         printf("OK\n");
644
645         printf("testing SeekTable::SeekTable(const ::FLAC__StreamMetadata &)... +\n");
646         printf("        SeekTable::operator!=(const ::FLAC__StreamMetadata &)... ");
647         {
648                 FLAC::Metadata::SeekTable blockcopy(seektable_);
649                 if(!blockcopy.is_valid())
650                         return die_("!block.is_valid()");
651                 if(blockcopy != seektable_)
652                         return die_("copy is not identical to original");
653                 printf("OK\n");
654         }
655
656         printf("testing SeekTable::SeekTable(const ::FLAC__StreamMetadata *)... +\n");
657         printf("        SeekTable::operator!=(const ::FLAC__StreamMetadata *)... ");
658         {
659                 FLAC::Metadata::SeekTable blockcopy(&seektable_);
660                 if(!blockcopy.is_valid())
661                         return die_("!block.is_valid()");
662                 if(blockcopy != seektable_)
663                         return die_("copy is not identical to original");
664                 printf("OK\n");
665         }
666
667         printf("testing SeekTable::operator=(const SeekTable &)... +\n");
668         printf("        SeekTable::operator==(const SeekTable &)... ");
669         {
670                 FLAC::Metadata::SeekTable blockcopy = block;
671                 if(!blockcopy.is_valid())
672                         return die_("!block.is_valid()");
673                 if(!(blockcopy == block))
674                         return die_("copy is not identical to original");
675                 printf("OK\n");
676         }
677
678         printf("testing SeekTable::operator=(const ::FLAC__StreamMetadata &)... +\n");
679         printf("        SeekTable::operator==(const ::FLAC__StreamMetadata &)... ");
680         {
681                 FLAC::Metadata::SeekTable blockcopy = seektable_;
682                 if(!blockcopy.is_valid())
683                         return die_("!block.is_valid()");
684                 if(!(blockcopy == seektable_))
685                         return die_("copy is not identical to original");
686                 printf("OK\n");
687         }
688
689         printf("testing SeekTable::operator=(const ::FLAC__StreamMetadata *)... +\n");
690         printf("        SeekTable::operator==(const ::FLAC__StreamMetadata *)... ");
691         {
692                 FLAC::Metadata::SeekTable blockcopy = &seektable_;
693                 if(!blockcopy.is_valid())
694                         return die_("!block.is_valid()");
695                 if(!(blockcopy == seektable_))
696                         return die_("copy is not identical to original");
697                 printf("OK\n");
698         }
699
700         printf("testing SeekTable::insert_point() x 3... ");
701         if(!block.insert_point(0, seektable_.data.seek_table.points[1]))
702                 return die_("returned false");
703         if(!block.insert_point(0, seektable_.data.seek_table.points[1]))
704                 return die_("returned false");
705         if(!block.insert_point(1, seektable_.data.seek_table.points[0]))
706                 return die_("returned false");
707         printf("OK\n");
708
709         printf("testing SeekTable::is_legal()... ");
710         if(block.is_legal())
711                 return die_("returned true");
712         printf("OK\n");
713
714         printf("testing SeekTable::set_point()... ");
715         block.set_point(0, seektable_.data.seek_table.points[0]);
716         printf("OK\n");
717
718         printf("testing SeekTable::delete_point()... ");
719         if(!block.delete_point(0))
720                 return die_("returned false");
721         printf("OK\n");
722
723         printf("testing SeekTable::is_legal()... ");
724         if(!block.is_legal())
725                 return die_("returned false");
726         printf("OK\n");
727
728         printf("testing SeekTable::get_num_points()... ");
729         if(block.get_num_points() != seektable_.data.seek_table.num_points)
730                 return die_("number mismatch");
731         printf("OK\n");
732
733         printf("testing SeekTable::operator!=(const ::FLAC__StreamMetadata &)... ");
734         if(block != seektable_)
735                 return die_("data mismatch");
736         printf("OK\n");
737
738         printf("testing SeekTable::get_point()... ");
739         if(
740                 block.get_point(1).sample_number != seektable_.data.seek_table.points[1].sample_number ||
741                 block.get_point(1).stream_offset != seektable_.data.seek_table.points[1].stream_offset ||
742                 block.get_point(1).frame_samples != seektable_.data.seek_table.points[1].frame_samples
743         )
744                 return die_("point mismatch");
745         printf("OK\n");
746
747         printf("testing FLAC::Metadata::clone(const FLAC::Metadata::Prototype *)... ");
748         FLAC::Metadata::Prototype *clone_ = FLAC::Metadata::clone(&block);
749         if(0 == clone_)
750                 return die_("returned NULL");
751         if(0 == dynamic_cast<FLAC::Metadata::SeekTable *>(clone_))
752                 return die_("downcast is NULL");
753         if(*dynamic_cast<FLAC::Metadata::SeekTable *>(clone_) != block)
754                 return die_("clone is not identical");
755         printf("OK\n");
756
757
758         printf("PASSED\n\n");
759         return true;
760 }
761
762 bool test_metadata_object_vorbiscomment()
763 {
764         unsigned expected_length;
765
766         printf("testing class FLAC::Metadata::VorbisComment::Entry\n");
767
768         printf("testing Entry::Entry()... ");
769         {
770                 FLAC::Metadata::VorbisComment::Entry entry1;
771                 if(!entry1.is_valid())
772                         return die_("!is_valid()");
773                 printf("OK\n");
774
775                 printf("testing Entry::~Entry()... ");
776         }
777         printf("OK\n");
778
779         printf("testing Entry::Entry(const char *field, unsigned field_length)... ");
780         FLAC::Metadata::VorbisComment::Entry entry2("name2=value2", strlen("name2=value2"));
781         if(!entry2.is_valid())
782                 return die_("!is_valid()");
783         printf("OK\n");
784
785         printf("testing Entry::Entry(const char *field_name, const char *field_value, unsigned field_value_length)... ");
786         FLAC::Metadata::VorbisComment::Entry entry3("name3", "value3", strlen("value3"));
787         if(!entry3.is_valid())
788                 return die_("!is_valid()");
789         printf("OK\n");
790
791         printf("testing Entry::Entry(const Entry &entry)... ");
792         {
793                 FLAC::Metadata::VorbisComment::Entry entry2copy(entry2);
794                 if(!entry2copy.is_valid())
795                         return die_("!is_valid()");
796                 printf("OK\n");
797
798                 printf("testing Entry::~Entry()... ");
799         }
800         printf("OK\n");
801
802         printf("testing Entry::operator=(const Entry &entry)... ");
803         FLAC::Metadata::VorbisComment::Entry entry1 = entry2;
804         if(!entry2.is_valid())
805                 return die_("!is_valid()");
806         printf("OK\n");
807
808         printf("testing Entry::get_field_length()... ");
809         if(entry1.get_field_length() != strlen("name2=value2"))
810                 return die_("value mismatch");
811         printf("OK\n");
812
813         printf("testing Entry::get_field_name_length()... ");
814         if(entry1.get_field_name_length() != strlen("name2"))
815                 return die_("value mismatch");
816         printf("OK\n");
817
818         printf("testing Entry::get_field_value_length()... ");
819         if(entry1.get_field_value_length() != strlen("value2"))
820                 return die_("value mismatch");
821         printf("OK\n");
822
823         printf("testing Entry::get_entry()... ");
824         {
825                 ::FLAC__StreamMetadata_VorbisComment_Entry entry = entry1.get_entry();
826                 if(entry.length != strlen("name2=value2"))
827                         return die_("entry length mismatch");
828                 if(0 != memcmp(entry.entry, "name2=value2", entry.length))
829                         return die_("entry value mismatch");
830         }
831         printf("OK\n");
832
833         printf("testing Entry::get_field()... ");
834         if(0 != memcmp(entry1.get_field(), "name2=value2", strlen("name2=value2")))
835                 return die_("value mismatch");
836         printf("OK\n");
837
838         printf("testing Entry::get_field_name()... ");
839         if(0 != memcmp(entry1.get_field_name(), "name2", strlen("name2")))
840                 return die_("value mismatch");
841         printf("OK\n");
842
843         printf("testing Entry::get_field_value()... ");
844         if(0 != memcmp(entry1.get_field_value(), "value2", strlen("value2")))
845                 return die_("value mismatch");
846         printf("OK\n");
847
848         printf("testing Entry::set_field_name()... ");
849         if(!entry1.set_field_name("name1"))
850                 return die_("returned false");
851         if(0 != memcmp(entry1.get_field_name(), "name1", strlen("name1")))
852                 return die_("value mismatch");
853         if(0 != memcmp(entry1.get_field(), "name1=value2", strlen("name1=value2")))
854                 return die_("entry mismatch");
855         printf("OK\n");
856
857         printf("testing Entry::set_field_value()... ");
858         if(!entry1.set_field_value("value1", strlen("value1")))
859                 return die_("returned false");
860         if(0 != memcmp(entry1.get_field_value(), "value1", strlen("value1")))
861                 return die_("value mismatch");
862         if(0 != memcmp(entry1.get_field(), "name1=value1", strlen("name1=value1")))
863                 return die_("entry mismatch");
864         printf("OK\n");
865
866         printf("testing Entry::set_field()... ");
867         if(!entry1.set_field("name0=value0", strlen("name0=value0")))
868                 return die_("returned false");
869         if(0 != memcmp(entry1.get_field_name(), "name0", strlen("name0")))
870                 return die_("value mismatch");
871         if(0 != memcmp(entry1.get_field_value(), "value0", strlen("value0")))
872                 return die_("value mismatch");
873         if(0 != memcmp(entry1.get_field(), "name0=value0", strlen("name0=value0")))
874                 return die_("entry mismatch");
875         printf("OK\n");
876
877         printf("PASSED\n\n");
878
879
880         printf("testing class FLAC::Metadata::VorbisComment\n");
881
882         printf("testing VorbisComment::VorbisComment()... ");
883         FLAC::Metadata::VorbisComment block;
884         if(!block.is_valid())
885                 return die_("!block.is_valid()");
886         expected_length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + strlen(::FLAC__VENDOR_STRING) + FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN/8);
887         if(block.get_length() != expected_length) {
888                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block.get_length());
889                 return false;
890         }
891         printf("OK\n");
892
893         printf("testing VorbisComment::VorbisComment(const VorbisComment &)... +\n");
894         printf("        VorbisComment::operator!=(const VorbisComment &)... ");
895         {
896                 FLAC::Metadata::VorbisComment blockcopy(block);
897                 if(!blockcopy.is_valid())
898                         return die_("!block.is_valid()");
899                 if(blockcopy != block)
900                         return die_("copy is not identical to original");
901                 printf("OK\n");
902
903                 printf("testing VorbisComment::~VorbisComment()... ");
904         }
905         printf("OK\n");
906
907         printf("testing VorbisComment::VorbisComment(const ::FLAC__StreamMetadata &)... +\n");
908         printf("        VorbisComment::operator!=(const ::FLAC__StreamMetadata &)... ");
909         {
910                 FLAC::Metadata::VorbisComment blockcopy(vorbiscomment_);
911                 if(!blockcopy.is_valid())
912                         return die_("!block.is_valid()");
913                 if(blockcopy != vorbiscomment_)
914                         return die_("copy is not identical to original");
915                 printf("OK\n");
916         }
917
918         printf("testing VorbisComment::VorbisComment(const ::FLAC__StreamMetadata *)... +\n");
919         printf("        VorbisComment::operator!=(const ::FLAC__StreamMetadata *)... ");
920         {
921                 FLAC::Metadata::VorbisComment blockcopy(&vorbiscomment_);
922                 if(!blockcopy.is_valid())
923                         return die_("!block.is_valid()");
924                 if(blockcopy != vorbiscomment_)
925                         return die_("copy is not identical to original");
926                 printf("OK\n");
927         }
928
929         printf("testing VorbisComment::operator=(const VorbisComment &)... +\n");
930         printf("        VorbisComment::operator==(const VorbisComment &)... ");
931         {
932                 FLAC::Metadata::VorbisComment blockcopy = block;
933                 if(!blockcopy.is_valid())
934                         return die_("!block.is_valid()");
935                 if(!(blockcopy == block))
936                         return die_("copy is not identical to original");
937                 printf("OK\n");
938         }
939
940         printf("testing VorbisComment::operator=(const ::FLAC__StreamMetadata &)... +\n");
941         printf("        VorbisComment::operator==(const ::FLAC__StreamMetadata &)... ");
942         {
943                 FLAC::Metadata::VorbisComment blockcopy = vorbiscomment_;
944                 if(!blockcopy.is_valid())
945                         return die_("!block.is_valid()");
946                 if(!(blockcopy == vorbiscomment_))
947                         return die_("copy is not identical to original");
948                 printf("OK\n");
949         }
950
951         printf("testing VorbisComment::operator=(const ::FLAC__StreamMetadata *)... +\n");
952         printf("        VorbisComment::operator==(const ::FLAC__StreamMetadata *)... ");
953         {
954                 FLAC::Metadata::VorbisComment blockcopy = &vorbiscomment_;
955                 if(!blockcopy.is_valid())
956                         return die_("!block.is_valid()");
957                 if(!(blockcopy == vorbiscomment_))
958                         return die_("copy is not identical to original");
959                 printf("OK\n");
960         }
961
962         printf("testing VorbisComment::get_num_comments()... ");
963         if(block.get_num_comments() != 0)
964                 return die_("value mismatch, expected 0");
965         printf("OK\n");
966
967         printf("testing VorbisComment::set_vendor_string()... ");
968         if(!block.set_vendor_string(entry1))
969                 return die_("returned false");
970         printf("OK\n");
971
972         printf("testing VorbisComment::get_vendor_string()... ");
973         if(block.get_vendor_string().get_field_name_length() != vorbiscomment_.data.vorbis_comment.vendor_string.length)
974                 return die_("length mismatch");
975         if(0 != memcmp(block.get_vendor_string().get_field_name(), vorbiscomment_.data.vorbis_comment.vendor_string.entry, vorbiscomment_.data.vorbis_comment.vendor_string.length))
976                 return die_("value mismatch");
977         printf("OK\n");
978
979         printf("testing VorbisComment::insert_comment()... +\n");
980         printf("        VorbisComment::get_comment()... ");
981         if(!block.insert_comment(0, entry3))
982                 return die_("returned false");
983         if(block.get_comment(0).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[1].length)
984                 return die_("length mismatch");
985         if(0 != memcmp(block.get_comment(0).get_field(), vorbiscomment_.data.vorbis_comment.comments[1].entry, vorbiscomment_.data.vorbis_comment.comments[1].length))
986                 return die_("value mismatch");
987         printf("OK\n");
988
989         printf("testing VorbisComment::insert_comment()... +\n");
990         printf("        VorbisComment::get_comment()... ");
991         if(!block.insert_comment(0, entry3))
992                 return die_("returned false");
993         if(block.get_comment(0).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[1].length)
994                 return die_("length mismatch");
995         if(0 != memcmp(block.get_comment(0).get_field(), vorbiscomment_.data.vorbis_comment.comments[1].entry, vorbiscomment_.data.vorbis_comment.comments[1].length))
996                 return die_("value mismatch");
997         printf("OK\n");
998
999         printf("testing VorbisComment::insert_comment()... +\n");
1000         printf("        VorbisComment::get_comment()... ");
1001         if(!block.insert_comment(1, entry2))
1002                 return die_("returned false");
1003         if(block.get_comment(1).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[0].length)
1004                 return die_("length mismatch");
1005         if(0 != memcmp(block.get_comment(1).get_field(), vorbiscomment_.data.vorbis_comment.comments[0].entry, vorbiscomment_.data.vorbis_comment.comments[0].length))
1006                 return die_("value mismatch");
1007         printf("OK\n");
1008
1009         printf("testing VorbisComment::set_comment()... +\n");
1010         printf("        VorbisComment::get_comment()... ");
1011         if(!block.set_comment(0, entry2))
1012                 return die_("returned false");
1013         if(block.get_comment(0).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[0].length)
1014                 return die_("length mismatch");
1015         if(0 != memcmp(block.get_comment(0).get_field(), vorbiscomment_.data.vorbis_comment.comments[0].entry, vorbiscomment_.data.vorbis_comment.comments[0].length))
1016                 return die_("value mismatch");
1017         printf("OK\n");
1018
1019         printf("testing VorbisComment::delete_comment()... +\n");
1020         printf("        VorbisComment::get_comment()... ");
1021         if(!block.delete_comment(0))
1022                 return die_("returned false");
1023         if(block.get_comment(0).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[0].length)
1024                 return die_("length[0] mismatch");
1025         if(0 != memcmp(block.get_comment(0).get_field(), vorbiscomment_.data.vorbis_comment.comments[0].entry, vorbiscomment_.data.vorbis_comment.comments[0].length))
1026                 return die_("value[0] mismatch");
1027         if(block.get_comment(1).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[1].length)
1028                 return die_("length[1] mismatch");
1029         if(0 != memcmp(block.get_comment(1).get_field(), vorbiscomment_.data.vorbis_comment.comments[1].entry, vorbiscomment_.data.vorbis_comment.comments[1].length))
1030                 return die_("value[0] mismatch");
1031         printf("OK\n");
1032
1033         printf("testing FLAC::Metadata::clone(const FLAC::Metadata::Prototype *)... ");
1034         FLAC::Metadata::Prototype *clone_ = FLAC::Metadata::clone(&block);
1035         if(0 == clone_)
1036                 return die_("returned NULL");
1037         if(0 == dynamic_cast<FLAC::Metadata::VorbisComment *>(clone_))
1038                 return die_("downcast is NULL");
1039         if(*dynamic_cast<FLAC::Metadata::VorbisComment *>(clone_) != block)
1040                 return die_("clone is not identical");
1041         printf("OK\n");
1042
1043
1044         printf("PASSED\n\n");
1045         return true;
1046 }
1047
1048 bool test_metadata_object_cuesheet()
1049 {
1050         unsigned expected_length;
1051
1052         printf("testing class FLAC::Metadata::CueSheet::Track\n");
1053
1054         printf("testing Track::Track()... ");
1055         FLAC::Metadata::CueSheet::Track track0;
1056         if(!track0.is_valid())
1057                 return die_("!is_valid()");
1058         printf("OK\n");
1059
1060         {
1061                 printf("testing Track::get_track()... ");
1062                 const ::FLAC__StreamMetadata_CueSheet_Track *trackp = track0.get_track();
1063                 if(0 == trackp)
1064                         return die_("returned pointer is NULL");
1065                 printf("OK\n");
1066
1067                 printf("testing Track::Track(const ::FLAC__StreamMetadata_CueSheet_Track*)... ");
1068                 FLAC::Metadata::CueSheet::Track track2(trackp);
1069                 if(!track2.is_valid())
1070                         return die_("!is_valid()");
1071                 if(!track_is_equal_(track2.get_track(), trackp))
1072                         return die_("copy is not equal");
1073                 printf("OK\n");
1074
1075                 printf("testing Track::~Track()... ");
1076         }
1077         printf("OK\n");
1078
1079         printf("testing Track::Track(const Track &track)... ");
1080         {
1081                 FLAC::Metadata::CueSheet::Track track0copy(track0);
1082                 if(!track0copy.is_valid())
1083                         return die_("!is_valid()");
1084                 if(!track_is_equal_(track0copy.get_track(), track0.get_track()))
1085                         return die_("copy is not equal");
1086                 printf("OK\n");
1087
1088                 printf("testing Track::~Track()... ");
1089         }
1090         printf("OK\n");
1091
1092         printf("testing Track::operator=(const Track &track)... ");
1093         FLAC::Metadata::CueSheet::Track track1 = track0;
1094         if(!track0.is_valid())
1095                 return die_("!is_valid()");
1096         if(!track_is_equal_(track1.get_track(), track0.get_track()))
1097                 return die_("copy is not equal");
1098         printf("OK\n");
1099
1100         printf("testing Track::get_offset()... ");
1101         if(track1.get_offset() != 0)
1102                 return die_("value mismatch");
1103         printf("OK\n");
1104
1105         printf("testing Track::get_number()... ");
1106         if(track1.get_number() != 0)
1107                 return die_("value mismatch");
1108         printf("OK\n");
1109
1110         printf("testing Track::get_isrc()... ");
1111         if(0 != memcmp(track1.get_isrc(), "\0\0\0\0\0\0\0\0\0\0\0\0\0", 13))
1112                 return die_("value mismatch");
1113         printf("OK\n");
1114
1115         printf("testing Track::get_type()... ");
1116         if(track1.get_type() != 0)
1117                 return die_("value mismatch");
1118         printf("OK\n");
1119
1120         printf("testing Track::get_pre_emphasis()... ");
1121         if(track1.get_pre_emphasis() != 0)
1122                 return die_("value mismatch");
1123         printf("OK\n");
1124
1125         printf("testing Track::get_num_indices()... ");
1126         if(track1.get_num_indices() != 0)
1127                 return die_("value mismatch");
1128         printf("OK\n");
1129
1130         printf("testing Track::set_offset()... ");
1131         track1.set_offset(588);
1132         if(track1.get_offset() != 588)
1133                 return die_("value mismatch");
1134         printf("OK\n");
1135
1136         printf("testing Track::set_number()... ");
1137         track1.set_number(1);
1138         if(track1.get_number() != 1)
1139                 return die_("value mismatch");
1140         printf("OK\n");
1141
1142         printf("testing Track::set_isrc()... ");
1143         track1.set_isrc("ABCDE1234567");
1144         if(0 != memcmp(track1.get_isrc(), "ABCDE1234567", 13))
1145                 return die_("value mismatch");
1146         printf("OK\n");
1147
1148         printf("testing Track::set_type()... ");
1149         track1.set_type(1);
1150         if(track1.get_type() != 1)
1151                 return die_("value mismatch");
1152         printf("OK\n");
1153
1154         printf("testing Track::set_pre_emphasis()... ");
1155         track1.set_pre_emphasis(1);
1156         if(track1.get_pre_emphasis() != 1)
1157                 return die_("value mismatch");
1158         printf("OK\n");
1159
1160         printf("PASSED\n\n");
1161
1162         printf("testing class FLAC::Metadata::CueSheet\n");
1163
1164         printf("testing CueSheet::CueSheet()... ");
1165         FLAC::Metadata::CueSheet block;
1166         if(!block.is_valid())
1167                 return die_("!block.is_valid()");
1168         expected_length = (
1169                 FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
1170                 FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
1171                 FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
1172                 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
1173                 FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
1174         ) / 8;
1175         if(block.get_length() != expected_length) {
1176                 printf("FAILED, bad length, expected %u, got %u\n", expected_length, block.get_length());
1177                 return false;
1178         }
1179         printf("OK\n");
1180
1181         printf("testing CueSheet::CueSheet(const CueSheet &)... +\n");
1182         printf("        CueSheet::operator!=(const CueSheet &)... ");
1183         {
1184                 FLAC::Metadata::CueSheet blockcopy(block);
1185                 if(!blockcopy.is_valid())
1186                         return die_("!block.is_valid()");
1187                 if(blockcopy != block)
1188                         return die_("copy is not identical to original");
1189                 printf("OK\n");
1190
1191                 printf("testing CueSheet::~CueSheet()... ");
1192         }
1193         printf("OK\n");
1194
1195         printf("testing CueSheet::CueSheet(const ::FLAC__StreamMetadata &)... +\n");
1196         printf("        CueSheet::operator!=(const ::FLAC__StreamMetadata &)... ");
1197         {
1198                 FLAC::Metadata::CueSheet blockcopy(cuesheet_);
1199                 if(!blockcopy.is_valid())
1200                         return die_("!block.is_valid()");
1201                 if(blockcopy != cuesheet_)
1202                         return die_("copy is not identical to original");
1203                 printf("OK\n");
1204         }
1205
1206         printf("testing CueSheet::CueSheet(const ::FLAC__StreamMetadata *)... +\n");
1207         printf("        CueSheet::operator!=(const ::FLAC__StreamMetadata *)... ");
1208         {
1209                 FLAC::Metadata::CueSheet blockcopy(&cuesheet_);
1210                 if(!blockcopy.is_valid())
1211                         return die_("!block.is_valid()");
1212                 if(blockcopy != cuesheet_)
1213                         return die_("copy is not identical to original");
1214                 printf("OK\n");
1215         }
1216
1217         printf("testing CueSheet::operator=(const CueSheet &)... +\n");
1218         printf("        CueSheet::operator==(const CueSheet &)... ");
1219         {
1220                 FLAC::Metadata::CueSheet blockcopy = block;
1221                 if(!blockcopy.is_valid())
1222                         return die_("!block.is_valid()");
1223                 if(!(blockcopy == block))
1224                         return die_("copy is not identical to original");
1225                 printf("OK\n");
1226         }
1227
1228         printf("testing CueSheet::operator=(const ::FLAC__StreamMetadata &)... +\n");
1229         printf("        CueSheet::operator==(const ::FLAC__StreamMetadata &)... ");
1230         {
1231                 FLAC::Metadata::CueSheet blockcopy = cuesheet_;
1232                 if(!blockcopy.is_valid())
1233                         return die_("!block.is_valid()");
1234                 if(!(blockcopy == cuesheet_))
1235                         return die_("copy is not identical to original");
1236                 printf("OK\n");
1237         }
1238
1239         printf("testing CueSheet::operator=(const ::FLAC__StreamMetadata *)... +\n");
1240         printf("        CueSheet::operator==(const ::FLAC__StreamMetadata *)... ");
1241         {
1242                 FLAC::Metadata::CueSheet blockcopy = &cuesheet_;
1243                 if(!blockcopy.is_valid())
1244                         return die_("!block.is_valid()");
1245                 if(!(blockcopy == cuesheet_))
1246                         return die_("copy is not identical to original");
1247                 printf("OK\n");
1248         }
1249
1250         printf("testing CueSheet::get_media_catalog_number()... ");
1251         if(0 != strcmp(block.get_media_catalog_number(), ""))
1252                 return die_("value mismatch");
1253         printf("OK\n");
1254
1255         printf("testing CueSheet::get_lead_in()... ");
1256         if(block.get_lead_in() != 0)
1257                 return die_("value mismatch, expected 0");
1258         printf("OK\n");
1259
1260         printf("testing CueSheet::get_is_cd()... ");
1261         if(block.get_is_cd())
1262                 return die_("value mismatch, expected false");
1263         printf("OK\n");
1264
1265         printf("testing CueSheet::get_num_tracks()... ");
1266         if(block.get_num_tracks() != 0)
1267                 return die_("value mismatch, expected 0");
1268         printf("OK\n");
1269
1270         printf("testing CueSheet::set_media_catalog_number()... ");
1271         {
1272                 char mcn[129];
1273                 memset(mcn, 0, sizeof(mcn));
1274                 strcpy(mcn, "1234567890123");
1275                 block.set_media_catalog_number(mcn);
1276                 if(0 != memcmp(block.get_media_catalog_number(), mcn, sizeof(mcn)))
1277                         return die_("value mismatch");
1278         }
1279         printf("OK\n");
1280
1281         printf("testing CueSheet::set_lead_in()... ");
1282         block.set_lead_in(588);
1283         if(block.get_lead_in() != 588)
1284                 return die_("value mismatch");
1285         printf("OK\n");
1286
1287         printf("testing CueSheet::set_is_cd()... ");
1288         block.set_is_cd(true);
1289         if(!block.get_is_cd())
1290                 return die_("value mismatch");
1291         printf("OK\n");
1292
1293         printf("testing CueSheet::insert_track()... +\n");
1294         printf("        CueSheet::get_track()... ");
1295         if(!block.insert_track(0, track0))
1296                 return die_("returned false");
1297         if(!track_is_equal_(block.get_track(0).get_track(), track0.get_track()))
1298                 return die_("value mismatch");
1299         printf("OK\n");
1300
1301         printf("testing CueSheet::insert_track()... +\n");
1302         printf("        CueSheet::get_track()... ");
1303         if(!block.insert_track(1, track1))
1304                 return die_("returned false");
1305         if(!track_is_equal_(block.get_track(1).get_track(), track1.get_track()))
1306                 return die_("value mismatch");
1307         printf("OK\n");
1308
1309         ::FLAC__StreamMetadata_CueSheet_Index index0;
1310         index0.offset = 588*4;
1311         index0.number = 1;
1312
1313         printf("testing CueSheet::insert_index(0)... +\n");
1314         printf("        CueSheet::get_track()... +\n");
1315         printf("        CueSheet::Track::get_index()... ");
1316         if(!block.insert_index(0, 0, index0))
1317                 return die_("returned false");
1318         if(!index_is_equal_(block.get_track(0).get_index(0), index0))
1319                 return die_("value mismatch");
1320         printf("OK\n");
1321
1322         index0.offset = 588*5;
1323         printf("testing CueSheet::Track::set_index()... ");
1324         {
1325                 FLAC::Metadata::CueSheet::Track track_ = block.get_track(0);
1326                 track_.set_index(0, index0);
1327                 if(!index_is_equal_(track_.get_index(0), index0))
1328                         return die_("value mismatch");
1329         }
1330         printf("OK\n");
1331
1332         index0.offset = 588*6;
1333         printf("testing CueSheet::set_index()... ");
1334         block.set_index(0, 0, index0);
1335         if(!index_is_equal_(block.get_track(0).get_index(0), index0))
1336                 return die_("value mismatch");
1337         printf("OK\n");
1338
1339         printf("testing CueSheet::delete_index()... ");
1340         if(!block.delete_index(0, 0))
1341                 return die_("returned false");
1342         if(block.get_track(0).get_num_indices() != 0)
1343                 return die_("num_indices mismatch");
1344         printf("OK\n");
1345
1346
1347         printf("testing CueSheet::set_track()... +\n");
1348         printf("        CueSheet::get_track()... ");
1349         if(!block.set_track(0, track1))
1350                 return die_("returned false");
1351         if(!track_is_equal_(block.get_track(0).get_track(), track1.get_track()))
1352                 return die_("value mismatch");
1353         printf("OK\n");
1354
1355         printf("testing CueSheet::delete_track()... ");
1356         if(!block.delete_track(0))
1357                 return die_("returned false");
1358         if(block.get_num_tracks() != 1)
1359                 return die_("num_tracks mismatch");
1360         printf("OK\n");
1361
1362         printf("testing FLAC::Metadata::clone(const FLAC::Metadata::Prototype *)... ");
1363         FLAC::Metadata::Prototype *clone_ = FLAC::Metadata::clone(&block);
1364         if(0 == clone_)
1365                 return die_("returned NULL");
1366         if(0 == dynamic_cast<FLAC::Metadata::CueSheet *>(clone_))
1367                 return die_("downcast is NULL");
1368         if(*dynamic_cast<FLAC::Metadata::CueSheet *>(clone_) != block)
1369                 return die_("clone is not identical");
1370         printf("OK\n");
1371
1372
1373         printf("PASSED\n\n");
1374         return true;
1375 }
1376
1377 bool test_metadata_object()
1378 {
1379         printf("\n+++ libFLAC++ unit test: metadata objects\n\n");
1380
1381         init_metadata_blocks_();
1382
1383         if(!test_metadata_object_streaminfo())
1384                 return false;
1385
1386         if(!test_metadata_object_padding())
1387                 return false;
1388
1389         if(!test_metadata_object_application())
1390                 return false;
1391
1392         if(!test_metadata_object_seektable())
1393                 return false;
1394
1395         if(!test_metadata_object_vorbiscomment())
1396                 return false;
1397
1398         if(!test_metadata_object_cuesheet())
1399                 return false;
1400
1401         free_metadata_blocks_();
1402
1403         return true;
1404 }