Git init
[external/libsndfile.git] / tests / cpp_test.cc
1 /*
2 ** Copyright (C) 2006 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (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 <cstdio>
20 #include <cstdlib>
21 #include <cstring>
22
23 #include <sndfile.hh>
24
25 #include "utils.h"
26
27 static short    sbuffer [100] ;
28 static int              ibuffer [100] ;
29 static float    fbuffer [100] ;
30 static double   dbuffer [100] ;
31
32 static void
33 create_file (const char * filename, int format)
34 {       SndfileHandle file ;
35
36         if (file.refCount () != 0)
37         {       printf ("\n\n%s %d : Error : Reference count (%d) should be zero.\n\n", __func__, __LINE__, file.refCount ()) ;
38                 exit (1) ;
39                 } ;
40
41         file = SndfileHandle (filename, SFM_WRITE, format, 2, 48000) ;
42
43         if (file.refCount () != 1)
44         {       printf ("\n\n%s %d : Error : Reference count (%d) should be 1.\n\n", __func__, __LINE__, file.refCount ()) ;
45                 exit (1) ;
46                 } ;
47
48         file.setString (SF_STR_TITLE, filename) ;
49
50         /* Item write. */
51         file.write (sbuffer, ARRAY_LEN (sbuffer)) ;
52         file.write (ibuffer, ARRAY_LEN (ibuffer)) ;
53         file.write (fbuffer, ARRAY_LEN (fbuffer)) ;
54         file.write (dbuffer, ARRAY_LEN (dbuffer)) ;
55
56         /* Frame write. */
57         file.writef (sbuffer, ARRAY_LEN (sbuffer) / file.channels ()) ;
58         file.writef (ibuffer, ARRAY_LEN (ibuffer) / file.channels ()) ;
59         file.writef (fbuffer, ARRAY_LEN (fbuffer) / file.channels ()) ;
60         file.writef (dbuffer, ARRAY_LEN (dbuffer) / file.channels ()) ;
61
62         /* RAII takes care of the SndfileHandle. */
63 } /* create_file */
64
65 static void
66 check_title (const SndfileHandle & file, const char * filename)
67 {       const char *title = NULL ;
68
69         title = file.getString (SF_STR_TITLE) ;
70
71         if (title == NULL)
72         {       printf ("\n\n%s %d : Error : No title.\n\n", __func__, __LINE__) ;
73                 exit (1) ;
74                 } ;
75
76         if (strcmp (filename, title) != 0)
77         {       printf ("\n\n%s %d : Error : title '%s' should be '%s'\n\n", __func__, __LINE__, title, filename) ;
78                 exit (1) ;
79                 } ;
80
81         return ;
82 } /* check_title */
83
84 static void
85 read_file (const char * filename, int format)
86 {       SndfileHandle file ;
87         sf_count_t count ;
88
89         if (file)
90         {       printf ("\n\n%s %d : Error : should not be here.\n\n", __func__, __LINE__) ;
91                 exit (1) ;
92                 } ;
93
94         file = SndfileHandle (filename) ;
95
96         if (1)
97         {       SndfileHandle file2 = file ;
98
99                 if (file.refCount () != 2 || file2.refCount () != 2)
100                 {       printf ("\n\n%s %d : Error : Reference count (%d) should be two.\n\n", __func__, __LINE__, file.refCount ()) ;
101                         exit (1) ;
102                         } ;
103                 } ;
104
105         if (file.refCount () != 1)
106         {       printf ("\n\n%s %d : Error : Reference count (%d) should be one.\n\n", __func__, __LINE__, file.refCount ()) ;
107                 exit (1) ;
108                 } ;
109
110         if (! file)
111         {       printf ("\n\n%s %d : Error : should not be here.\n\n", __func__, __LINE__) ;
112                 exit (1) ;
113                 } ;
114
115         if (file.format () != format)
116         {       printf ("\n\n%s %d : Error : format 0x%08x should be 0x%08x.\n\n", __func__, __LINE__, file.format (), format) ;
117                 exit (1) ;
118                 } ;
119
120         if (file.channels () != 2)
121         {       printf ("\n\n%s %d : Error : channels %d should be 2.\n\n", __func__, __LINE__, file.channels ()) ;
122                 exit (1) ;
123                 } ;
124
125         if (file.frames () != ARRAY_LEN (sbuffer) * 4)
126         {       printf ("\n\n%s %d : Error : frames %ld should be %lu.\n\n", __func__, __LINE__,
127                                 SF_COUNT_TO_LONG (file.frames ()), (long unsigned int) ARRAY_LEN (sbuffer) * 4 / 2) ;
128                 exit (1) ;
129                 } ;
130
131         switch (format & SF_FORMAT_TYPEMASK)
132         {       case SF_FORMAT_AU :
133                                 break ;
134
135                 default :
136                         check_title (file, filename) ;
137                         break ;
138                 } ;
139
140         /* Item read. */
141         file.read (sbuffer, ARRAY_LEN (sbuffer)) ;
142         file.read (ibuffer, ARRAY_LEN (ibuffer)) ;
143         file.read (fbuffer, ARRAY_LEN (fbuffer)) ;
144         file.read (dbuffer, ARRAY_LEN (dbuffer)) ;
145
146         /* Frame read. */
147         file.readf (sbuffer, ARRAY_LEN (sbuffer) / file.channels ()) ;
148         file.readf (ibuffer, ARRAY_LEN (ibuffer) / file.channels ()) ;
149         file.readf (fbuffer, ARRAY_LEN (fbuffer) / file.channels ()) ;
150         file.readf (dbuffer, ARRAY_LEN (dbuffer) / file.channels ()) ;
151
152         count = file.seek (file.frames () - 10, SEEK_SET) ;
153         if (count != file.frames () - 10)
154         {       printf ("\n\n%s %d : Error : offset (%ld) should be %ld\n\n", __func__, __LINE__,
155                                 SF_COUNT_TO_LONG (count), SF_COUNT_TO_LONG (file.frames () - 10)) ;
156                 exit (1) ;
157                 } ;
158
159         count = file.read (sbuffer, ARRAY_LEN (sbuffer)) ;
160         if (count != 10 * file.channels ())
161         {       printf ("\n\n%s %d : Error : count (%ld) should be %ld\n\n", __func__, __LINE__,
162                                 SF_COUNT_TO_LONG (count), SF_COUNT_TO_LONG (10 * file.channels ())) ;
163                 exit (1) ;
164                 } ;
165
166         /* RAII takes care of the SndfileHandle. */
167 } /* read_file */
168
169 static void
170 ceeplusplus_test (const char *filename, int format)
171 {
172         print_test_name ("ceeplusplus_test", filename) ;
173
174         create_file (filename, format) ;
175         read_file (filename, format) ;
176
177         remove (filename) ;
178         puts ("ok") ;
179 } /* ceeplusplus_test */
180
181 static void
182 ceeplusplus_extra_test (void)
183 {       SndfileHandle file ;
184         const char * filename = "bad_file_name.wav" ;
185         int error ;
186
187         print_test_name ("ceeplusplus_extra_test", filename) ;
188
189         file = SndfileHandle (filename) ;
190
191         error = file.error () ;
192         if (error == 0)
193         {       printf ("\n\n%s %d : error should be zero.\n\n", __func__, __LINE__) ;
194                 exit (1) ;
195                 } ;
196
197         if (file.strError () == NULL)
198         {       printf ("\n\n%s %d : strError should not return NULL.\n\n", __func__, __LINE__) ;
199                 exit (1) ;
200                 } ;
201
202         if (file.seek (0, SEEK_SET) != 0)
203         {       printf ("\n\n%s %d : bad seek ().\n\n", __func__, __LINE__) ;
204                 exit (1) ;
205                 } ;
206
207         puts ("ok") ;
208 } /* ceeplusplus_extra_test */
209
210 int
211 main (void)
212 {
213         ceeplusplus_test ("cpp_test.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
214         ceeplusplus_test ("cpp_test.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_S8) ;
215         ceeplusplus_test ("cpp_test.au", SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
216
217         ceeplusplus_extra_test () ;
218
219         return 0 ;
220 } /* main */
221