libsndfile source files.
[Faustine.git] / interpretor / libsndfile-1.0.25 / src / test_conversions.c
1 /*
2 ** Copyright (C) 2006-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 Lesser General Public License as published by
6 ** the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser 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 <stdarg.h>
25 #include <errno.h>
26 #include <inttypes.h>
27
28 #include "common.h"
29 #include "test_main.h"
30
31
32 /*
33 ** This is a bit rough, but it is the nicest way to do it.
34 */
35
36 #define cmp_test(line,ival,tval,str) \
37 if (ival != tval) \
38 { printf (str, line, ival, tval) ; \
39 exit (1) ; \
40 } ;
41
42 static void
43 conversion_test (char endian)
44 {
45 SF_PRIVATE sf_private, *psf ;
46 const char * filename = "conversion.bin" ;
47 int64_t i64 = SF_PLATFORM_S64 (0x0123456789abcdef), t64 = 0 ;
48 char format_str [16] ;
49 char test_name [64] ;
50 char i8 = 12, t8 = 0 ;
51 short i16 = 0x123, t16 = 0 ;
52 int i24 = 0x23456, t24 = 0 ;
53 int i32 = 0x0a0b0c0d, t32 = 0 ;
54 int bytes ;
55
56 snprintf (format_str, sizeof (format_str), "%c12348", endian) ;
57
58 snprintf (test_name, sizeof (test_name), "Testing %s conversions", endian == 'e' ? "little endian" : "big endian") ;
59 print_test_name (test_name) ;
60
61 psf = &sf_private ;
62 memset (psf, 0, sizeof (sf_private)) ;
63
64 psf->file.mode = SFM_WRITE ;
65 snprintf (psf->file.path.c, sizeof (psf->file.path.c), "%s", filename) ;
66
67 if (psf_fopen (psf) != 0)
68 { printf ("\n\nError : failed to open file '%s' for write.\n\n", filename) ;
69 exit (1) ;
70 } ;
71
72 psf_binheader_writef (psf, format_str, i8, i16, i24, i32, i64) ;
73 psf_fwrite (psf->header, 1, psf->headindex, psf) ;
74 psf_fclose (psf) ;
75
76 memset (psf, 0, sizeof (sf_private)) ;
77
78 psf->file.mode = SFM_READ ;
79 snprintf (psf->file.path.c, sizeof (psf->file.path.c), "%s", filename) ;
80
81 if (psf_fopen (psf) != 0)
82 { printf ("\n\nError : failed to open file '%s' for read.\n\n", filename) ;
83 exit (1) ;
84 } ;
85
86 bytes = psf_binheader_readf (psf, format_str, &t8, &t16, &t24, &t32, &t64) ;
87 psf_fclose (psf) ;
88
89 if (bytes != 18)
90 { printf ("\n\nLine %d : read %d bytes.\n\n", __LINE__, bytes) ;
91 exit (1) ;
92 } ;
93
94 cmp_test (__LINE__, i8, t8, "\n\nLine %d : 8 bit int failed %d -> %d.\n\n") ;
95 cmp_test (__LINE__, i16, t16, "\n\nLine %d : 16 bit int failed 0x%x -> 0x%x.\n\n") ;
96 cmp_test (__LINE__, i24, t24, "\n\nLine %d : 24 bit int failed 0x%x -> 0x%x.\n\n") ;
97 cmp_test (__LINE__, i32, t32, "\n\nLine %d : 32 bit int failed 0x%x -> 0x%x.\n\n") ;
98 cmp_test (__LINE__, i64, t64, "\n\nLine %d : 64 bit int failed 0x%" PRIx64 "x -> 0x%" PRIx64 "x.\n\n") ;
99
100 remove (filename) ;
101 puts ("ok") ;
102 } /* conversion_test */
103
104 void
105 test_conversions (void)
106 {
107 conversion_test ('E') ;
108 conversion_test ('e') ;
109 } /* test_conversion */
110