r663: This commit was generated by cvs2svn to compensate for changes in r662,
[cinelerra_cv.git] / libsndfile / tests / string_test.c
blob9a242a8dce87f2a6cf6e5e7ae298164363e7fee5
1 /*
2 ** Copyright (C) 2003 Erik de Castro Lopo <erikd@zip.com.au>
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.
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.
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <math.h>
24 #include <sndfile.h>
26 #include "utils.h"
28 #define BUFFER_LEN (1 << 10)
29 #define LOG_BUFFER_SIZE 1024
31 static void string_test (char *filename, int typemajor) ;
33 int
34 main (int argc, char *argv [])
35 { int do_all = 0 ;
36 int test_count = 0 ;
38 if (argc != 2)
39 { printf ("Usage : %s <test>\n", argv [0]) ;
40 printf (" Where <test> is one of the following:\n") ;
41 printf (" wav - test adding strings to WAV files\n") ;
42 printf (" aiff - test adding strings to AIFF files\n") ;
43 printf (" all - perform all tests\n") ;
44 exit (1) ;
45 } ;
47 do_all =! strcmp (argv [1], "all") ;
49 if (do_all || ! strcmp (argv [1], "wav"))
50 { string_test ("strings.wav", SF_FORMAT_WAV) ;
51 test_count++ ;
52 } ;
54 if (do_all || ! strcmp (argv [1], "aiff"))
55 { string_test ("strings.aiff", SF_FORMAT_AIFF) ;
56 test_count++ ;
57 } ;
59 if (test_count == 0)
60 { printf ("Mono : ************************************\n") ;
61 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
62 printf ("Mono : ************************************\n") ;
63 return 1 ;
64 } ;
66 return 0 ;
67 } /* main */
70 /*============================================================================================
71 ** Here are the test functions.
74 static char
75 software [] = "software",
76 artist [] = "The Artist",
77 copyright [] = "Copyright (c) 2003 The Artist",
78 comment [] = "Comment goes here!!!",
79 date [] = "2003/01/27" ;
81 static short data_out [BUFFER_LEN] ;
83 static void
84 string_test (char *filename, int typemajor)
85 { const char *cptr ;
86 SNDFILE *file ;
87 SF_INFO sfinfo ;
88 int frames, errors = 0 ;
90 typemajor = typemajor ;
92 print_test_name ("string_test", filename) ;
94 sfinfo.samplerate = 44100 ;
95 sfinfo.channels = 1 ;
96 sfinfo.frames = 0 ;
98 switch (typemajor)
99 { case SF_FORMAT_WAV :
100 sfinfo.format = (typemajor | SF_FORMAT_PCM_U8) ;
101 break ;
103 default :
104 sfinfo.format = (typemajor | SF_FORMAT_PCM_S8) ;
105 break ;
108 frames = BUFFER_LEN / sfinfo.channels ;
110 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, __LINE__) ;
112 /* Write stuff at start of file. */
113 sf_set_string (file, SF_STR_TITLE, filename) ;
114 sf_set_string (file, SF_STR_SOFTWARE, software) ;
115 sf_set_string (file, SF_STR_ARTIST, artist) ;
117 /* Write data to file. */
118 test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;
119 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
121 /* Write more stuff at end of file. */
122 sf_set_string (file, SF_STR_COPYRIGHT, copyright) ;
123 sf_set_string (file, SF_STR_COMMENT, comment) ;
124 sf_set_string (file, SF_STR_DATE, date) ;
126 sf_close (file) ;
128 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, __LINE__) ;
130 check_log_buffer_or_die (file) ;
132 if (sfinfo.frames != BUFFER_LEN)
133 { printf ("***** Bad frame count %d (should be %d)\n\n", (int) sfinfo.frames, BUFFER_LEN) ;
134 errors ++ ;
137 cptr = sf_get_string (file, SF_STR_TITLE) ;
138 if (cptr == NULL || strcmp (filename, cptr) != 0)
139 { if (errors++ == 0)
140 puts ("\n") ;
141 printf (" Bad filename : %s\n", cptr) ;
144 cptr = sf_get_string (file, SF_STR_COPYRIGHT) ;
145 if (cptr == NULL || strcmp (copyright, cptr) != 0)
146 { if (errors++ == 0)
147 puts ("\n") ;
148 printf (" Bad copyright : %s\n", cptr) ;
151 cptr = sf_get_string (file, SF_STR_SOFTWARE) ;
152 if (cptr == NULL || strstr (cptr, software) != cptr)
153 { if (errors++ == 0)
154 puts ("\n") ;
155 printf (" Bad software : %s\n", cptr) ;
158 cptr = sf_get_string (file, SF_STR_ARTIST) ;
159 if (cptr == NULL || strcmp (artist, cptr) != 0)
160 { if (errors++ == 0)
161 puts ("\n") ;
162 printf (" Bad artist : %s\n", cptr) ;
165 cptr = sf_get_string (file, SF_STR_COMMENT) ;
166 if (cptr == NULL || strcmp (comment, cptr) != 0)
167 { if (errors++ == 0)
168 puts ("\n") ;
169 printf (" Bad comment : %s\n", cptr) ;
172 if (typemajor != SF_FORMAT_AIFF)
173 { cptr = sf_get_string (file, SF_STR_DATE) ;
174 if (cptr == NULL || strcmp (date, cptr) != 0)
175 { if (errors++ == 0)
176 puts ("\n") ;
177 printf (" Bad date : %s\n", cptr) ;
181 if (errors > 0)
182 { printf ("\n*** Error count : %d ***\n\n", errors) ;
183 dump_log_buffer (file) ;
184 exit (1) ;
187 sf_close (file) ;
188 unlink (filename) ;
190 puts ("ok") ;
191 } /* string_test */