• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** Copyright (C) 2006-2016 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
conversion_test(char endian)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.ptr, 1, psf->header.indx, psf) ;
74 	free (psf->header.ptr) ;
75 	psf_fclose (psf) ;
76 
77 	memset (psf, 0, sizeof (sf_private)) ;
78 
79 	psf->file.mode = SFM_READ ;
80 	snprintf (psf->file.path.c, sizeof (psf->file.path.c), "%s", filename) ;
81 
82 	if (psf_fopen (psf) != 0)
83 	{	printf ("\n\nError : failed to open file '%s' for read.\n\n", filename) ;
84 		exit (1) ;
85 		} ;
86 
87 	bytes = psf_binheader_readf (psf, format_str, &t8, &t16, &t24, &t32, &t64) ;
88 	free (psf->header.ptr) ;
89 	psf_fclose (psf) ;
90 
91 	if (bytes != 18)
92 	{	printf ("\n\nLine %d : read %d bytes.\n\n", __LINE__, bytes) ;
93 		exit (1) ;
94 		} ;
95 
96 	cmp_test (__LINE__, i8, t8, "\n\nLine %d : 8 bit int failed %d -> %d.\n\n") ;
97 	cmp_test (__LINE__, i16, t16, "\n\nLine %d : 16 bit int failed 0x%x -> 0x%x.\n\n") ;
98 	cmp_test (__LINE__, i24, t24, "\n\nLine %d : 24 bit int failed 0x%x -> 0x%x.\n\n") ;
99 	cmp_test (__LINE__, i32, t32, "\n\nLine %d : 32 bit int failed 0x%x -> 0x%x.\n\n") ;
100 	cmp_test (__LINE__, i64, t64, "\n\nLine %d : 64 bit int failed 0x%" PRIx64 "x -> 0x%" PRIx64 "x.\n\n") ;
101 
102 	remove (filename) ;
103 	puts ("ok") ;
104 } /* conversion_test */
105 
106 void
test_conversions(void)107 test_conversions (void)
108 {
109 	conversion_test ('E') ;
110 	conversion_test ('e') ;
111 } /* test_conversion */
112 
113