add 2206 to copyright notice
[platform/upstream/flac.git] / src / test_libFLAC / format.c
1 /* test_libFLAC - Unit tester for libFLAC
2  * Copyright (C) 2004,2005,2006  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/format.h"
21 #include <stdio.h>
22
23 static const char *true_false_string_[2] = { "false", "true" };
24
25 static struct {
26         unsigned rate;
27         FLAC__bool valid;
28 } SAMPLE_RATES[] = {
29         { 0      , false },
30         { 1      , true  },
31         { 9      , true  },
32         { 10     , true  },
33         { 4000   , true  },
34         { 8000   , true  },
35         { 11025  , true  },
36         { 12000  , true  },
37         { 16000  , true  },
38         { 22050  , true  },
39         { 24000  , true  },
40         { 32000  , true  },
41         { 32768  , true  },
42         { 44100  , true  },
43         { 48000  , true  },
44         { 65000  , true  },
45         { 65535  , true  },
46         { 65536  , false },
47         { 65540  , true  },
48         { 65550  , true  },
49         { 65555  , false },
50         { 66000  , true  },
51         { 66001  , false },
52         { 96000  , true  },
53         { 100000 , true  },
54         { 100001 , false },
55         { 192000 , true  },
56         { 500000 , true  },
57         { 500001 , false },
58         { 500010 , true  },
59         { 700000 , false },
60         { 700010 , false },
61         { 1000000, false },
62         { 1100000, false }
63 };
64
65 static struct {
66         const char *string;
67         FLAC__bool valid;
68 } VCENTRY_NAMES[] = {
69         { ""    , true  },
70         { "a"   , true  },
71         { "="   , false },
72         { "a="  , false },
73         { "\x01", false },
74         { "\x1f", false },
75         { "\x7d", true  },
76         { "\x7e", false },
77         { "\xff", false }
78 };
79
80 static struct {
81         unsigned length;
82         const FLAC__byte *string;
83         FLAC__bool valid;
84 } VCENTRY_VALUES[] = {
85         { 0, (const FLAC__byte*)""            , true  },
86         { 1, (const FLAC__byte*)""            , true  },
87         { 1, (const FLAC__byte*)"\x01"        , true  },
88         { 1, (const FLAC__byte*)"\x7f"        , true  },
89         { 1, (const FLAC__byte*)"\x80"        , false },
90         { 1, (const FLAC__byte*)"\x81"        , false },
91         { 1, (const FLAC__byte*)"\xc0"        , false },
92         { 1, (const FLAC__byte*)"\xe0"        , false },
93         { 1, (const FLAC__byte*)"\xf0"        , false },
94         { 2, (const FLAC__byte*)"\xc0\x41"    , false },
95         { 2, (const FLAC__byte*)"\xc1\x41"    , false },
96         { 2, (const FLAC__byte*)"\xc0\x85"    , false }, /* non-shortest form */
97         { 2, (const FLAC__byte*)"\xc1\x85"    , false }, /* non-shortest form */
98         { 2, (const FLAC__byte*)"\xc2\x85"    , true  },
99         { 2, (const FLAC__byte*)"\xe0\x41"    , false },
100         { 2, (const FLAC__byte*)"\xe1\x41"    , false },
101         { 2, (const FLAC__byte*)"\xe0\x85"    , false },
102         { 2, (const FLAC__byte*)"\xe1\x85"    , false },
103         { 3, (const FLAC__byte*)"\xe0\x85\x41", false },
104         { 3, (const FLAC__byte*)"\xe1\x85\x41", false },
105         { 3, (const FLAC__byte*)"\xe0\x85\x80", false }, /* non-shortest form */
106         { 3, (const FLAC__byte*)"\xe0\x95\x80", false }, /* non-shortest form */
107         { 3, (const FLAC__byte*)"\xe0\xa5\x80", true  },
108         { 3, (const FLAC__byte*)"\xe1\x85\x80", true  },
109         { 3, (const FLAC__byte*)"\xe1\x95\x80", true  },
110         { 3, (const FLAC__byte*)"\xe1\xa5\x80", true  }
111 };
112
113 static struct {
114         const FLAC__byte *string;
115         FLAC__bool valid;
116 } VCENTRY_VALUES_NT[] = {
117         { (const FLAC__byte*)""            , true  },
118         { (const FLAC__byte*)"\x01"        , true  },
119         { (const FLAC__byte*)"\x7f"        , true  },
120         { (const FLAC__byte*)"\x80"        , false },
121         { (const FLAC__byte*)"\x81"        , false },
122         { (const FLAC__byte*)"\xc0"        , false },
123         { (const FLAC__byte*)"\xe0"        , false },
124         { (const FLAC__byte*)"\xf0"        , false },
125         { (const FLAC__byte*)"\xc0\x41"    , false },
126         { (const FLAC__byte*)"\xc1\x41"    , false },
127         { (const FLAC__byte*)"\xc0\x85"    , false }, /* non-shortest form */
128         { (const FLAC__byte*)"\xc1\x85"    , false }, /* non-shortest form */
129         { (const FLAC__byte*)"\xc2\x85"    , true  },
130         { (const FLAC__byte*)"\xe0\x41"    , false },
131         { (const FLAC__byte*)"\xe1\x41"    , false },
132         { (const FLAC__byte*)"\xe0\x85"    , false },
133         { (const FLAC__byte*)"\xe1\x85"    , false },
134         { (const FLAC__byte*)"\xe0\x85\x41", false },
135         { (const FLAC__byte*)"\xe1\x85\x41", false },
136         { (const FLAC__byte*)"\xe0\x85\x80", false }, /* non-shortest form */
137         { (const FLAC__byte*)"\xe0\x95\x80", false }, /* non-shortest form */
138         { (const FLAC__byte*)"\xe0\xa5\x80", true  },
139         { (const FLAC__byte*)"\xe1\x85\x80", true  },
140         { (const FLAC__byte*)"\xe1\x95\x80", true  },
141         { (const FLAC__byte*)"\xe1\xa5\x80", true  }
142 };
143
144 static struct {
145         unsigned length;
146         const FLAC__byte *string;
147         FLAC__bool valid;
148 } VCENTRIES[] = {
149         { 0, (const FLAC__byte*)""              , false },
150         { 1, (const FLAC__byte*)"a"             , false },
151         { 1, (const FLAC__byte*)"="             , true  },
152         { 2, (const FLAC__byte*)"a="            , true  },
153         { 2, (const FLAC__byte*)"\x01="         , false },
154         { 2, (const FLAC__byte*)"\x1f="         , false },
155         { 2, (const FLAC__byte*)"\x7d="         , true  },
156         { 2, (const FLAC__byte*)"\x7e="         , false },
157         { 2, (const FLAC__byte*)"\xff="         , false },
158         { 3, (const FLAC__byte*)"a=\x01"        , true  },
159         { 3, (const FLAC__byte*)"a=\x7f"        , true  },
160         { 3, (const FLAC__byte*)"a=\x80"        , false },
161         { 3, (const FLAC__byte*)"a=\x81"        , false },
162         { 3, (const FLAC__byte*)"a=\xc0"        , false },
163         { 3, (const FLAC__byte*)"a=\xe0"        , false },
164         { 3, (const FLAC__byte*)"a=\xf0"        , false },
165         { 4, (const FLAC__byte*)"a=\xc0\x41"    , false },
166         { 4, (const FLAC__byte*)"a=\xc1\x41"    , false },
167         { 4, (const FLAC__byte*)"a=\xc0\x85"    , false }, /* non-shortest form */
168         { 4, (const FLAC__byte*)"a=\xc1\x85"    , false }, /* non-shortest form */
169         { 4, (const FLAC__byte*)"a=\xc2\x85"    , true  },
170         { 4, (const FLAC__byte*)"a=\xe0\x41"    , false },
171         { 4, (const FLAC__byte*)"a=\xe1\x41"    , false },
172         { 4, (const FLAC__byte*)"a=\xe0\x85"    , false },
173         { 4, (const FLAC__byte*)"a=\xe1\x85"    , false },
174         { 5, (const FLAC__byte*)"a=\xe0\x85\x41", false },
175         { 5, (const FLAC__byte*)"a=\xe1\x85\x41", false },
176         { 5, (const FLAC__byte*)"a=\xe0\x85\x80", false }, /* non-shortest form */
177         { 5, (const FLAC__byte*)"a=\xe0\x95\x80", false }, /* non-shortest form */
178         { 5, (const FLAC__byte*)"a=\xe0\xa5\x80", true  },
179         { 5, (const FLAC__byte*)"a=\xe1\x85\x80", true  },
180         { 5, (const FLAC__byte*)"a=\xe1\x95\x80", true  },
181         { 5, (const FLAC__byte*)"a=\xe1\xa5\x80", true  }
182 };
183
184 FLAC__bool test_format()
185 {
186         unsigned i;
187
188         printf("\n+++ libFLAC unit test: format\n\n");
189
190         for(i = 0; i < sizeof(SAMPLE_RATES)/sizeof(SAMPLE_RATES[0]); i++) {
191                 printf("testing FLAC__format_sample_rate_is_valid(%u)... ", SAMPLE_RATES[i].rate);
192                 if(FLAC__format_sample_rate_is_valid(SAMPLE_RATES[i].rate) != SAMPLE_RATES[i].valid) {
193                         printf("FAILED, expected %s, got %s\n", true_false_string_[SAMPLE_RATES[i].valid], true_false_string_[!SAMPLE_RATES[i].valid]);
194                         return false;
195                 }
196                 printf("OK\n");
197         }
198
199         for(i = 0; i < sizeof(VCENTRY_NAMES)/sizeof(VCENTRY_NAMES[0]); i++) {
200                 printf("testing FLAC__format_vorbiscomment_entry_name_is_legal(\"%s\")... ", VCENTRY_NAMES[i].string);
201                 if(FLAC__format_vorbiscomment_entry_name_is_legal(VCENTRY_NAMES[i].string) != VCENTRY_NAMES[i].valid) {
202                         printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_NAMES[i].valid], true_false_string_[!VCENTRY_NAMES[i].valid]);
203                         return false;
204                 }
205                 printf("OK\n");
206         }
207
208         for(i = 0; i < sizeof(VCENTRY_VALUES)/sizeof(VCENTRY_VALUES[0]); i++) {
209                 printf("testing FLAC__format_vorbiscomment_entry_value_is_legal(\"%s\", %u)... ", VCENTRY_VALUES[i].string, VCENTRY_VALUES[i].length);
210                 if(FLAC__format_vorbiscomment_entry_value_is_legal(VCENTRY_VALUES[i].string, VCENTRY_VALUES[i].length) != VCENTRY_VALUES[i].valid) {
211                         printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_VALUES[i].valid], true_false_string_[!VCENTRY_VALUES[i].valid]);
212                         return false;
213                 }
214                 printf("OK\n");
215         }
216
217         for(i = 0; i < sizeof(VCENTRY_VALUES_NT)/sizeof(VCENTRY_VALUES_NT[0]); i++) {
218                 printf("testing FLAC__format_vorbiscomment_entry_value_is_legal(\"%s\", -1)... ", VCENTRY_VALUES_NT[i].string);
219                 if(FLAC__format_vorbiscomment_entry_value_is_legal(VCENTRY_VALUES_NT[i].string, (unsigned)(-1)) != VCENTRY_VALUES_NT[i].valid) {
220                         printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_VALUES_NT[i].valid], true_false_string_[!VCENTRY_VALUES_NT[i].valid]);
221                         return false;
222                 }
223                 printf("OK\n");
224         }
225
226         for(i = 0; i < sizeof(VCENTRIES)/sizeof(VCENTRIES[0]); i++) {
227                 printf("testing FLAC__format_vorbiscomment_entry_is_legal(\"%s\", %u)... ", VCENTRIES[i].string, VCENTRIES[i].length);
228                 if(FLAC__format_vorbiscomment_entry_is_legal(VCENTRIES[i].string, VCENTRIES[i].length) != VCENTRIES[i].valid) {
229                         printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRIES[i].valid], true_false_string_[!VCENTRIES[i].valid]);
230                         return false;
231                 }
232                 printf("OK\n");
233         }
234
235         printf("\nPASSED!\n");
236         return true;
237 }