2 ** Copyright (C) 2006-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
29 #include "test_main.h"
32 test_float_convert (void)
33 { static float data
[] =
34 { 0.0, 1.0, -1.0, 1.0 * M_PI
, -1.0 * M_PI
,
35 1e9
, -1e9
, 1e-9, -1e-9, 1e-10, -1e-10,
36 1e-19, -1e-19, 1e19
, -1e19
, 1e-20, -1e-20,
41 print_test_name (__func__
) ;
43 for (k
= 0 ; k
< ARRAY_LEN (data
) ; k
++)
44 { unsigned char bytes
[4] ;
47 float32_le_write (data
[k
], bytes
) ;
48 test
= float32_le_read (bytes
) ;
50 if (fabs (data
[k
] - test
) > 1e-20)
51 { printf ("\n\nLine %d : Test %d, little endian error %.15g -> %.15g.\n\n", __LINE__
, k
, data
[k
], test
) ;
55 float32_be_write (data
[k
], bytes
) ;
56 test
= float32_be_read (bytes
) ;
58 if (fabs (data
[k
] - test
) > 1e-20)
59 { printf ("\n\nLine %d : Test %d, big endian error %.15g -> %.15g.\n\n", __LINE__
, k
, data
[k
], test
) ;
66 } /* test_float_convert */
69 test_double_convert (void)
70 { static double data
[] =
71 { 0.0, 1.0, -1.0, 1.0 * M_PI
, -1.0 * M_PI
,
72 1e9
, -1e9
, 1e-9, -1e-9, 1e-10, -1e-10,
73 1e-19, -1e-19, 1e19
, -1e19
, 1e-20, -1e-20,
78 print_test_name (__func__
) ;
80 for (k
= 0 ; k
< ARRAY_LEN (data
) ; k
++)
81 { unsigned char bytes
[8] ;
84 double64_le_write (data
[k
], bytes
) ;
85 test
= double64_le_read (bytes
) ;
87 if (fabs (data
[k
] - test
) > 1e-20)
88 { printf ("\n\nLine %d : Test %d, little endian error %.15g -> %.15g.\n\n", __LINE__
, k
, data
[k
], test
) ;
92 double64_be_write (data
[k
], bytes
) ;
93 test
= double64_be_read (bytes
) ;
95 if (fabs (data
[k
] - test
) > 1e-20)
96 { printf ("\n\nLine %d : Test %d, big endian error %.15g -> %.15g.\n\n", __LINE__
, k
, data
[k
], test
) ;
103 } /* test_double_convert */