libsndfile compiling.
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / tests / raw_test.c
1 /*
2 ** Copyright (C) 2002-2011 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 "sfconfig.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 #include <sndfile.h>
31
32 #include "utils.h"
33
34 #define BUFFER_LEN (1<<10)
35 #define LOG_BUFFER_SIZE 1024
36
37 static void raw_offset_test (const char *filename, int typeminor) ;
38 static void bad_raw_test (void) ;
39
40 /* Force the start of this buffer to be double aligned. Sparc-solaris will
41 ** choke if its not.
42 */
43 static short data [BUFFER_LEN] ;
44
45 int
46 main (void)
47 {
48 raw_offset_test ("offset.raw", SF_FORMAT_PCM_16) ;
49 bad_raw_test () ;
50
51 return 0 ;
52 } /* main */
53
54 /*============================================================================================
55 ** Here are the test functions.
56 */
57
58 static void
59 raw_offset_test (const char *filename, int typeminor)
60 { SNDFILE *sndfile ;
61 SF_INFO sfinfo ;
62 sf_count_t start ;
63 int k ;
64
65 print_test_name ("raw_offset_test", filename) ;
66
67 sfinfo.samplerate = 44100 ;
68 sfinfo.format = SF_FORMAT_RAW | typeminor ;
69 sfinfo.channels = 1 ;
70 sfinfo.frames = 0 ;
71
72 sndfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
73
74 start = 0 ;
75 sf_command (sndfile, SFC_FILE_TRUNCATE, &start, sizeof (start)) ;
76
77 for (k = 0 ; k < BUFFER_LEN ; k++)
78 data [k] = k ;
79 test_write_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
80
81 sf_close (sndfile) ;
82
83 sndfile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
84 check_log_buffer_or_die (sndfile, __LINE__) ;
85
86 if (abs (BUFFER_LEN - sfinfo.frames) > 1)
87 { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), BUFFER_LEN) ;
88 dump_log_buffer (sndfile) ;
89 exit (1) ;
90 } ;
91
92 memset (data, 0 , sizeof (data)) ;
93 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
94 for (k = 0 ; k < BUFFER_LEN ; k++)
95 if (data [k] != k)
96 printf ("Error : line %d\n", __LINE__) ;
97
98 /* Set dataoffset to 2 bytes from beginning of file. */
99 start = 2 ;
100 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
101
102 /* Seek to new start */
103 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
104
105 memset (data, 0 , sizeof (data)) ;
106 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 1, __LINE__) ;
107 for (k = 0 ; k < BUFFER_LEN - 1 ; k++)
108 if (data [k] != k + 1)
109 { printf ("Error : line %d\n", __LINE__) ;
110 exit (1) ;
111 } ;
112
113 /* Set dataoffset to 4 bytes from beginning of file. */
114 start = 4 ;
115 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
116
117 /* Seek to new start */
118 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
119
120 memset (data, 0 , sizeof (data)) ;
121 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 2, __LINE__) ;
122 for (k = 0 ; k < BUFFER_LEN - 2 ; k++)
123 if (data [k] != k + 2)
124 { printf ("Error : line %d\n", __LINE__) ;
125 exit (1) ;
126 } ;
127
128 /* Set dataoffset back to 0 bytes from beginning of file. */
129 start = 0 ;
130 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
131
132 /* Seek to new start */
133 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
134
135 memset (data, 0 , sizeof (data)) ;
136 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
137 for (k = 0 ; k < BUFFER_LEN ; k++)
138 if (data [k] != k)
139 { printf ("Error : line %d\n", __LINE__) ;
140 exit (1) ;
141 } ;
142
143 sf_close (sndfile) ;
144 unlink (filename) ;
145
146 puts ("ok") ;
147 } /* raw_offset_test */
148
149 static void
150 bad_raw_test (void)
151 { FILE *textfile ;
152 SNDFILE *file ;
153 SF_INFO sfinfo ;
154 const char *errorstr, *filename = "bad.raw" ;
155
156 print_test_name ("bad_raw_test", filename) ;
157
158 if ((textfile = fopen (filename, "w")) == NULL)
159 { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__) ;
160 exit (1) ;
161 } ;
162
163 fprintf (textfile, "This is not a valid file.\n") ;
164 fclose (textfile) ;
165
166 sfinfo.samplerate = 44100 ;
167 sfinfo.format = SF_FORMAT_RAW | 0xABCD ;
168 sfinfo.channels = 1 ;
169
170 if ((file = sf_open (filename, SFM_READ, &sfinfo)) != NULL)
171 { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__ - 1) ;
172 exit (1) ;
173 } ;
174
175 errorstr = sf_strerror (file) ;
176
177 if (strstr (errorstr, "Bad format field in SF_INFO struct") == NULL)
178 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
179 exit (1) ;
180 } ;
181
182 unlink (filename) ;
183
184 puts ("ok") ;
185 } /* bad_raw_test */
186