add FLAC__format_sample_rate_is_subset()
[platform/upstream/flac.git] / src / test_libFLAC / format.c
1 /* test_libFLAC - Unit tester for libFLAC
2  * Copyright (C) 2004,2005,2006,2007  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "FLAC/assert.h"
24 #include "FLAC/format.h"
25 #include <stdio.h>
26
27 static const char *true_false_string_[2] = { "false", "true" };
28
29 static struct {
30         unsigned rate;
31         FLAC__bool valid;
32         FLAC__bool subset;
33 } SAMPLE_RATES[] = {
34         { 0      , false, false },
35         { 1      , true , true  },
36         { 9      , true , true  },
37         { 10     , true , true  },
38         { 4000   , true , true  },
39         { 8000   , true , true  },
40         { 11025  , true , true  },
41         { 12000  , true , true  },
42         { 16000  , true , true  },
43         { 22050  , true , true  },
44         { 24000  , true , true  },
45         { 32000  , true , true  },
46         { 32768  , true , true  },
47         { 44100  , true , true  },
48         { 48000  , true , true  },
49         { 65000  , true , true  },
50         { 65535  , true , true  },
51         { 65536  , true , false },
52         { 65540  , true , true  },
53         { 65550  , true , true  },
54         { 65555  , true , false },
55         { 66000  , true , true  },
56         { 66001  , true , false },
57         { 96000  , true , true  },
58         { 100000 , true , true  },
59         { 100001 , true , false },
60         { 192000 , true , true  },
61         { 500000 , true , true  },
62         { 500001 , true , false },
63         { 500010 , true , true  },
64         { 655349 , true , false },
65         { 655350 , true , true  },
66         { 655351 , false, false },
67         { 655360 , false, false },
68         { 700000 , false, false },
69         { 700010 , false, false },
70         { 1000000, false, false },
71         { 1100000, false, false }
72 };
73
74 static struct {
75         const char *string;
76         FLAC__bool valid;
77 } VCENTRY_NAMES[] = {
78         { ""    , true  },
79         { "a"   , true  },
80         { "="   , false },
81         { "a="  , false },
82         { "\x01", false },
83         { "\x1f", false },
84         { "\x7d", true  },
85         { "\x7e", false },
86         { "\xff", false }
87 };
88
89 static struct {
90         unsigned length;
91         const FLAC__byte *string;
92         FLAC__bool valid;
93 } VCENTRY_VALUES[] = {
94         { 0, (const FLAC__byte*)""            , true  },
95         { 1, (const FLAC__byte*)""            , true  },
96         { 1, (const FLAC__byte*)"\x01"        , true  },
97         { 1, (const FLAC__byte*)"\x7f"        , true  },
98         { 1, (const FLAC__byte*)"\x80"        , false },
99         { 1, (const FLAC__byte*)"\x81"        , false },
100         { 1, (const FLAC__byte*)"\xc0"        , false },
101         { 1, (const FLAC__byte*)"\xe0"        , false },
102         { 1, (const FLAC__byte*)"\xf0"        , false },
103         { 2, (const FLAC__byte*)"\xc0\x41"    , false },
104         { 2, (const FLAC__byte*)"\xc1\x41"    , false },
105         { 2, (const FLAC__byte*)"\xc0\x85"    , false }, /* non-shortest form */
106         { 2, (const FLAC__byte*)"\xc1\x85"    , false }, /* non-shortest form */
107         { 2, (const FLAC__byte*)"\xc2\x85"    , true  },
108         { 2, (const FLAC__byte*)"\xe0\x41"    , false },
109         { 2, (const FLAC__byte*)"\xe1\x41"    , false },
110         { 2, (const FLAC__byte*)"\xe0\x85"    , false },
111         { 2, (const FLAC__byte*)"\xe1\x85"    , false },
112         { 3, (const FLAC__byte*)"\xe0\x85\x41", false },
113         { 3, (const FLAC__byte*)"\xe1\x85\x41", false },
114         { 3, (const FLAC__byte*)"\xe0\x85\x80", false }, /* non-shortest form */
115         { 3, (const FLAC__byte*)"\xe0\x95\x80", false }, /* non-shortest form */
116         { 3, (const FLAC__byte*)"\xe0\xa5\x80", true  },
117         { 3, (const FLAC__byte*)"\xe1\x85\x80", true  },
118         { 3, (const FLAC__byte*)"\xe1\x95\x80", true  },
119         { 3, (const FLAC__byte*)"\xe1\xa5\x80", true  }
120 };
121
122 static struct {
123         const FLAC__byte *string;
124         FLAC__bool valid;
125 } VCENTRY_VALUES_NT[] = {
126         { (const FLAC__byte*)""            , true  },
127         { (const FLAC__byte*)"\x01"        , true  },
128         { (const FLAC__byte*)"\x7f"        , true  },
129         { (const FLAC__byte*)"\x80"        , false },
130         { (const FLAC__byte*)"\x81"        , false },
131         { (const FLAC__byte*)"\xc0"        , false },
132         { (const FLAC__byte*)"\xe0"        , false },
133         { (const FLAC__byte*)"\xf0"        , false },
134         { (const FLAC__byte*)"\xc0\x41"    , false },
135         { (const FLAC__byte*)"\xc1\x41"    , false },
136         { (const FLAC__byte*)"\xc0\x85"    , false }, /* non-shortest form */
137         { (const FLAC__byte*)"\xc1\x85"    , false }, /* non-shortest form */
138         { (const FLAC__byte*)"\xc2\x85"    , true  },
139         { (const FLAC__byte*)"\xe0\x41"    , false },
140         { (const FLAC__byte*)"\xe1\x41"    , false },
141         { (const FLAC__byte*)"\xe0\x85"    , false },
142         { (const FLAC__byte*)"\xe1\x85"    , false },
143         { (const FLAC__byte*)"\xe0\x85\x41", false },
144         { (const FLAC__byte*)"\xe1\x85\x41", false },
145         { (const FLAC__byte*)"\xe0\x85\x80", false }, /* non-shortest form */
146         { (const FLAC__byte*)"\xe0\x95\x80", false }, /* non-shortest form */
147         { (const FLAC__byte*)"\xe0\xa5\x80", true  },
148         { (const FLAC__byte*)"\xe1\x85\x80", true  },
149         { (const FLAC__byte*)"\xe1\x95\x80", true  },
150         { (const FLAC__byte*)"\xe1\xa5\x80", true  }
151 };
152
153 static struct {
154         unsigned length;
155         const FLAC__byte *string;
156         FLAC__bool valid;
157 } VCENTRIES[] = {
158         { 0, (const FLAC__byte*)""              , false },
159         { 1, (const FLAC__byte*)"a"             , false },
160         { 1, (const FLAC__byte*)"="             , true  },
161         { 2, (const FLAC__byte*)"a="            , true  },
162         { 2, (const FLAC__byte*)"\x01="         , false },
163         { 2, (const FLAC__byte*)"\x1f="         , false },
164         { 2, (const FLAC__byte*)"\x7d="         , true  },
165         { 2, (const FLAC__byte*)"\x7e="         , false },
166         { 2, (const FLAC__byte*)"\xff="         , false },
167         { 3, (const FLAC__byte*)"a=\x01"        , true  },
168         { 3, (const FLAC__byte*)"a=\x7f"        , true  },
169         { 3, (const FLAC__byte*)"a=\x80"        , false },
170         { 3, (const FLAC__byte*)"a=\x81"        , false },
171         { 3, (const FLAC__byte*)"a=\xc0"        , false },
172         { 3, (const FLAC__byte*)"a=\xe0"        , false },
173         { 3, (const FLAC__byte*)"a=\xf0"        , false },
174         { 4, (const FLAC__byte*)"a=\xc0\x41"    , false },
175         { 4, (const FLAC__byte*)"a=\xc1\x41"    , false },
176         { 4, (const FLAC__byte*)"a=\xc0\x85"    , false }, /* non-shortest form */
177         { 4, (const FLAC__byte*)"a=\xc1\x85"    , false }, /* non-shortest form */
178         { 4, (const FLAC__byte*)"a=\xc2\x85"    , true  },
179         { 4, (const FLAC__byte*)"a=\xe0\x41"    , false },
180         { 4, (const FLAC__byte*)"a=\xe1\x41"    , false },
181         { 4, (const FLAC__byte*)"a=\xe0\x85"    , false },
182         { 4, (const FLAC__byte*)"a=\xe1\x85"    , false },
183         { 5, (const FLAC__byte*)"a=\xe0\x85\x41", false },
184         { 5, (const FLAC__byte*)"a=\xe1\x85\x41", false },
185         { 5, (const FLAC__byte*)"a=\xe0\x85\x80", false }, /* non-shortest form */
186         { 5, (const FLAC__byte*)"a=\xe0\x95\x80", false }, /* non-shortest form */
187         { 5, (const FLAC__byte*)"a=\xe0\xa5\x80", true  },
188         { 5, (const FLAC__byte*)"a=\xe1\x85\x80", true  },
189         { 5, (const FLAC__byte*)"a=\xe1\x95\x80", true  },
190         { 5, (const FLAC__byte*)"a=\xe1\xa5\x80", true  }
191 };
192
193 FLAC__bool test_format(void)
194 {
195         unsigned i;
196
197         printf("\n+++ libFLAC unit test: format\n\n");
198
199         for(i = 0; i < sizeof(SAMPLE_RATES)/sizeof(SAMPLE_RATES[0]); i++) {
200                 printf("testing FLAC__format_sample_rate_is_valid(%u)... ", SAMPLE_RATES[i].rate);
201                 if(FLAC__format_sample_rate_is_valid(SAMPLE_RATES[i].rate) != SAMPLE_RATES[i].valid) {
202                         printf("FAILED, expected %s, got %s\n", true_false_string_[SAMPLE_RATES[i].valid], true_false_string_[!SAMPLE_RATES[i].valid]);
203                         return false;
204                 }
205                 printf("OK\n");
206         }
207
208         for(i = 0; i < sizeof(SAMPLE_RATES)/sizeof(SAMPLE_RATES[0]); i++) {
209                 printf("testing FLAC__format_sample_rate_is_subset(%u)... ", SAMPLE_RATES[i].rate);
210                 if(FLAC__format_sample_rate_is_subset(SAMPLE_RATES[i].rate) != SAMPLE_RATES[i].subset) {
211                         printf("FAILED, expected %s, got %s\n", true_false_string_[SAMPLE_RATES[i].subset], true_false_string_[!SAMPLE_RATES[i].subset]);
212                         return false;
213                 }
214                 printf("OK\n");
215         }
216
217         for(i = 0; i < sizeof(VCENTRY_NAMES)/sizeof(VCENTRY_NAMES[0]); i++) {
218                 printf("testing FLAC__format_vorbiscomment_entry_name_is_legal(\"%s\")... ", VCENTRY_NAMES[i].string);
219                 if(FLAC__format_vorbiscomment_entry_name_is_legal(VCENTRY_NAMES[i].string) != VCENTRY_NAMES[i].valid) {
220                         printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_NAMES[i].valid], true_false_string_[!VCENTRY_NAMES[i].valid]);
221                         return false;
222                 }
223                 printf("OK\n");
224         }
225
226         for(i = 0; i < sizeof(VCENTRY_VALUES)/sizeof(VCENTRY_VALUES[0]); i++) {
227                 printf("testing FLAC__format_vorbiscomment_entry_value_is_legal(\"%s\", %u)... ", VCENTRY_VALUES[i].string, VCENTRY_VALUES[i].length);
228                 if(FLAC__format_vorbiscomment_entry_value_is_legal(VCENTRY_VALUES[i].string, VCENTRY_VALUES[i].length) != VCENTRY_VALUES[i].valid) {
229                         printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_VALUES[i].valid], true_false_string_[!VCENTRY_VALUES[i].valid]);
230                         return false;
231                 }
232                 printf("OK\n");
233         }
234
235         for(i = 0; i < sizeof(VCENTRY_VALUES_NT)/sizeof(VCENTRY_VALUES_NT[0]); i++) {
236                 printf("testing FLAC__format_vorbiscomment_entry_value_is_legal(\"%s\", -1)... ", VCENTRY_VALUES_NT[i].string);
237                 if(FLAC__format_vorbiscomment_entry_value_is_legal(VCENTRY_VALUES_NT[i].string, (unsigned)(-1)) != VCENTRY_VALUES_NT[i].valid) {
238                         printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_VALUES_NT[i].valid], true_false_string_[!VCENTRY_VALUES_NT[i].valid]);
239                         return false;
240                 }
241                 printf("OK\n");
242         }
243
244         for(i = 0; i < sizeof(VCENTRIES)/sizeof(VCENTRIES[0]); i++) {
245                 printf("testing FLAC__format_vorbiscomment_entry_is_legal(\"%s\", %u)... ", VCENTRIES[i].string, VCENTRIES[i].length);
246                 if(FLAC__format_vorbiscomment_entry_is_legal(VCENTRIES[i].string, VCENTRIES[i].length) != VCENTRIES[i].valid) {
247                         printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRIES[i].valid], true_false_string_[!VCENTRIES[i].valid]);
248                         return false;
249                 }
250                 printf("OK\n");
251         }
252
253         printf("\nPASSED!\n");
254         return true;
255 }