~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to lib-src/libsndfile/tests/headerless_test.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-18 21:58:19 UTC
  • mfrom: (13.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080218215819-tmbcf1rx238r8gdv
Tags: 1.3.4-1.1ubuntu1~gutsy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
** Copyright (C) 1999-2004 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_SIZE             (2000)
35
 
 
36
 
static void old_test (void) ;
37
 
static void headerless_test (const char * filename, int format, int expected) ;
38
 
 
39
 
int
40
 
main (void)
41
 
{
42
 
        old_test () ;
43
 
 
44
 
        headerless_test ("raw.vox", SF_FORMAT_VOX_ADPCM, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM) ;
45
 
        headerless_test ("raw.gsm", SF_FORMAT_GSM610, SF_FORMAT_RAW | SF_FORMAT_GSM610) ;
46
 
 
47
 
        headerless_test ("raw.snd", SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ;
48
 
        headerless_test ("raw.au" , SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ;
49
 
 
50
 
        return 0 ;
51
 
} /* main */
52
 
 
53
 
static void
54
 
headerless_test (const char * filename, int format, int expected)
55
 
{       static  short   buffer [BUFFER_SIZE] ;
56
 
        SNDFILE         *file ;
57
 
        SF_INFO         sfinfo ;
58
 
        int                     k ;
59
 
 
60
 
        format &= SF_FORMAT_SUBMASK ;
61
 
 
62
 
        print_test_name (__func__, filename) ;
63
 
 
64
 
        for (k = 0 ; k < BUFFER_SIZE ; k++)
65
 
                buffer [k] = k ;
66
 
 
67
 
        sfinfo.samplerate       = 8000 ;
68
 
        sfinfo.frames           = 0 ;
69
 
        sfinfo.channels         = 1 ;
70
 
        sfinfo.format           = SF_FORMAT_RAW | format ;
71
 
 
72
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
73
 
 
74
 
        if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
75
 
        {       printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ;
76
 
                fflush (stdout) ;
77
 
                puts (sf_strerror (file)) ;
78
 
                exit (1) ;
79
 
                } ;
80
 
 
81
 
        sf_close (file) ;
82
 
 
83
 
        memset (buffer, 0, sizeof (buffer)) ;
84
 
 
85
 
        /* We should be able to detect these so clear sfinfo. */
86
 
        memset (&sfinfo, 0, sizeof (sfinfo)) ;
87
 
 
88
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
89
 
 
90
 
        if (sfinfo.format != expected)
91
 
        {       printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, expected, sfinfo.format) ;
92
 
                exit (1) ;
93
 
                } ;
94
 
 
95
 
        if (sfinfo.frames < BUFFER_SIZE)
96
 
        {       printf ("Line %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
97
 
                exit (1) ;
98
 
                } ;
99
 
 
100
 
        if (sfinfo.channels != 1)
101
 
        {       printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
102
 
                exit (1) ;
103
 
                } ;
104
 
 
105
 
        check_log_buffer_or_die (file, __LINE__) ;
106
 
 
107
 
        printf ("ok\n") ;
108
 
        unlink (filename) ;
109
 
} /* headerless_test */
110
 
 
111
 
static void
112
 
old_test (void)
113
 
{       static  short   buffer [BUFFER_SIZE] ;
114
 
        SNDFILE         *file ;
115
 
        SF_INFO         sfinfo ;
116
 
        int                     k, filetype ;
117
 
        const char      *filename = "headerless.wav" ;
118
 
 
119
 
        print_test_name (__func__, "") ;
120
 
 
121
 
        for (k = 0 ; k < BUFFER_SIZE ; k++)
122
 
                buffer [k] = k ;
123
 
 
124
 
        filetype = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
125
 
 
126
 
        sfinfo.samplerate       = 32000 ;
127
 
        sfinfo.frames           = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
128
 
        sfinfo.channels         = 1 ;
129
 
        sfinfo.format           = filetype ;
130
 
 
131
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
132
 
 
133
 
        if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
134
 
        {       printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ;
135
 
                fflush (stdout) ;
136
 
                puts (sf_strerror (file)) ;
137
 
                exit (1) ;
138
 
                } ;
139
 
 
140
 
        sf_close (file) ;
141
 
 
142
 
        memset (buffer, 0, sizeof (buffer)) ;
143
 
 
144
 
        /* Read as RAW but get the bit width and endian-ness correct. */
145
 
        sfinfo.format = filetype = SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
146
 
 
147
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
148
 
 
149
 
        if (sfinfo.format != filetype)
150
 
        {       printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
151
 
                exit (1) ;
152
 
                } ;
153
 
 
154
 
        if (sfinfo.frames < BUFFER_SIZE)
155
 
        {       printf ("Line %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
156
 
                exit (1) ;
157
 
                } ;
158
 
 
159
 
        if (sfinfo.channels != 1)
160
 
        {       printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
161
 
                exit (1) ;
162
 
                } ;
163
 
 
164
 
        check_log_buffer_or_die (file, __LINE__) ;
165
 
 
166
 
        if ((k = sf_read_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
167
 
        {       printf ("Line %d: short read (%d).\n", __LINE__, k) ;
168
 
                exit (1) ;
169
 
                } ;
170
 
 
171
 
        for (k = 0 ; k < BUFFER_SIZE - 22 ; k++)
172
 
                if (buffer [k + 22] != k)
173
 
                {       printf ("Line %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__, k, k, buffer [k]) ;
174
 
                        exit (1) ;
175
 
                        } ;
176
 
 
177
 
        printf ("ok\n") ;
178
 
        unlink (filename) ;
179
 
} /* old_test */
180
 
 
181
 
/*
182
 
** Do not edit or modify anything in this comment block.
183
 
** The arch-tag line is a file identity tag for the GNU Arch 
184
 
** revision control system.
185
 
**
186
 
** arch-tag: 0820bc1a-c396-4e66-9997-096999b0bc40
187
 
*/