cannam@85
|
1 /*
|
cannam@85
|
2 ** Copyright (C) 2006-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
|
cannam@85
|
3 **
|
cannam@85
|
4 ** This program is free software; you can redistribute it and/or modify
|
cannam@85
|
5 ** it under the terms of the GNU Lesser General Public License as published by
|
cannam@85
|
6 ** the Free Software Foundation; either version 2.1 of the License, or
|
cannam@85
|
7 ** (at your option) any later version.
|
cannam@85
|
8 **
|
cannam@85
|
9 ** This program is distributed in the hope that it will be useful,
|
cannam@85
|
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
cannam@85
|
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
cannam@85
|
12 ** GNU Lesser General Public License for more details.
|
cannam@85
|
13 **
|
cannam@85
|
14 ** You should have received a copy of the GNU Lesser General Public License
|
cannam@85
|
15 ** along with this program; if not, write to the Free Software
|
cannam@85
|
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
cannam@85
|
17 */
|
cannam@85
|
18
|
cannam@85
|
19 #include "sfconfig.h"
|
cannam@85
|
20
|
cannam@85
|
21 #include <stdio.h>
|
cannam@85
|
22 #include <stdlib.h>
|
cannam@85
|
23 #include <string.h>
|
cannam@85
|
24 #include <stdarg.h>
|
cannam@85
|
25 #include <errno.h>
|
cannam@85
|
26 #include <math.h>
|
cannam@85
|
27
|
cannam@85
|
28 #include "common.h"
|
cannam@85
|
29 #include "test_main.h"
|
cannam@85
|
30
|
cannam@85
|
31 void
|
cannam@85
|
32 test_float_convert (void)
|
cannam@85
|
33 { static float data [] =
|
cannam@85
|
34 { 0.0, 1.0, -1.0, 1.0 * M_PI, -1.0 * M_PI,
|
cannam@85
|
35 1e9, -1e9, 1e-9, -1e-9, 1e-10, -1e-10,
|
cannam@85
|
36 1e-19, -1e-19, 1e19, -1e19, 1e-20, -1e-20,
|
cannam@85
|
37 } ;
|
cannam@85
|
38
|
cannam@85
|
39 int k ;
|
cannam@85
|
40
|
cannam@85
|
41 print_test_name (__func__) ;
|
cannam@85
|
42
|
cannam@85
|
43 for (k = 0 ; k < ARRAY_LEN (data) ; k++)
|
cannam@85
|
44 { unsigned char bytes [4] ;
|
cannam@85
|
45 float test ;
|
cannam@85
|
46
|
cannam@85
|
47 float32_le_write (data [k], bytes) ;
|
cannam@85
|
48 test = float32_le_read (bytes) ;
|
cannam@85
|
49
|
cannam@85
|
50 if (fabs (data [k] - test) > 1e-20)
|
cannam@85
|
51 { printf ("\n\nLine %d : Test %d, little endian error %.15g -> %.15g.\n\n", __LINE__, k, data [k], test ) ;
|
cannam@85
|
52 exit (1) ;
|
cannam@85
|
53 } ;
|
cannam@85
|
54
|
cannam@85
|
55 float32_be_write (data [k], bytes) ;
|
cannam@85
|
56 test = float32_be_read (bytes) ;
|
cannam@85
|
57
|
cannam@85
|
58 if (fabs (data [k] - test) > 1e-20)
|
cannam@85
|
59 { printf ("\n\nLine %d : Test %d, big endian error %.15g -> %.15g.\n\n", __LINE__, k, data [k], test) ;
|
cannam@85
|
60 exit (1) ;
|
cannam@85
|
61 } ;
|
cannam@85
|
62
|
cannam@85
|
63 } ;
|
cannam@85
|
64
|
cannam@85
|
65 puts ("ok") ;
|
cannam@85
|
66 } /* test_float_convert */
|
cannam@85
|
67
|
cannam@85
|
68 void
|
cannam@85
|
69 test_double_convert (void)
|
cannam@85
|
70 { static double data [] =
|
cannam@85
|
71 { 0.0, 1.0, -1.0, 1.0 * M_PI, -1.0 * M_PI,
|
cannam@85
|
72 1e9, -1e9, 1e-9, -1e-9, 1e-10, -1e-10,
|
cannam@85
|
73 1e-19, -1e-19, 1e19, -1e19, 1e-20, -1e-20,
|
cannam@85
|
74 } ;
|
cannam@85
|
75
|
cannam@85
|
76 int k ;
|
cannam@85
|
77
|
cannam@85
|
78 print_test_name (__func__) ;
|
cannam@85
|
79
|
cannam@85
|
80 for (k = 0 ; k < ARRAY_LEN (data) ; k++)
|
cannam@85
|
81 { unsigned char bytes [8] ;
|
cannam@85
|
82 double test ;
|
cannam@85
|
83
|
cannam@85
|
84 double64_le_write (data [k], bytes) ;
|
cannam@85
|
85 test = double64_le_read (bytes) ;
|
cannam@85
|
86
|
cannam@85
|
87 if (fabs (data [k] - test) > 1e-20)
|
cannam@85
|
88 { printf ("\n\nLine %d : Test %d, little endian error %.15g -> %.15g.\n\n", __LINE__, k, data [k], test ) ;
|
cannam@85
|
89 exit (1) ;
|
cannam@85
|
90 } ;
|
cannam@85
|
91
|
cannam@85
|
92 double64_be_write (data [k], bytes) ;
|
cannam@85
|
93 test = double64_be_read (bytes) ;
|
cannam@85
|
94
|
cannam@85
|
95 if (fabs (data [k] - test) > 1e-20)
|
cannam@85
|
96 { printf ("\n\nLine %d : Test %d, big endian error %.15g -> %.15g.\n\n", __LINE__, k, data [k], test) ;
|
cannam@85
|
97 exit (1) ;
|
cannam@85
|
98 } ;
|
cannam@85
|
99
|
cannam@85
|
100 } ;
|
cannam@85
|
101
|
cannam@85
|
102 puts ("ok") ;
|
cannam@85
|
103 } /* test_double_convert */
|
cannam@85
|
104
|