summaryrefslogtreecommitdiff
path: root/src/test_float.c
blob: 9d0b09d8b54014663f1bd9a43c51e27beb6d2261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
** Copyright (C) 2006-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2.1 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "sfconfig.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <math.h>

#include "common.h"
#include "test_main.h"

void
test_float_convert (void)
{	static float data [] =
	{	0.0, 1.0, -1.0, 1.0 * M_PI, -1.0 * M_PI,
		1e9, -1e9, 1e-9, -1e-9, 1e-10, -1e-10,
		1e-19, -1e-19, 1e19, -1e19, 1e-20, -1e-20,
		} ;

	int k ;

	print_test_name (__func__) ;

	for (k = 0 ; k < ARRAY_LEN (data) ; k++)
	{	unsigned char bytes [4] ;
		float test ;

		float32_le_write (data [k], bytes) ;
		test = float32_le_read (bytes) ;

		if (fabs (data [k] - test) > 1e-20)
		{	printf ("\n\nLine %d : Test %d, little endian error %.15g -> %.15g.\n\n", __LINE__, k, data [k], test ) ;
			exit (1) ;
			} ;

		float32_be_write (data [k], bytes) ;
		test = float32_be_read (bytes) ;

		if (fabs (data [k] - test) > 1e-20)
		{	printf ("\n\nLine %d : Test %d, big endian error %.15g -> %.15g.\n\n", __LINE__, k, data [k], test) ;
			exit (1) ;
			} ;

		} ;

	puts ("ok") ;
} /* test_float_convert */

void
test_double_convert (void)
{	static double data [] =
	{	0.0, 1.0, -1.0, 1.0 * M_PI, -1.0 * M_PI,
		1e9, -1e9, 1e-9, -1e-9, 1e-10, -1e-10,
		1e-19, -1e-19, 1e19, -1e19, 1e-20, -1e-20,
		} ;

	int k ;

	print_test_name (__func__) ;

	for (k = 0 ; k < ARRAY_LEN (data) ; k++)
	{	unsigned char bytes [8] ;
		double test ;

		double64_le_write (data [k], bytes) ;
		test = double64_le_read (bytes) ;

		if (fabs (data [k] - test) > 1e-20)
		{	printf ("\n\nLine %d : Test %d, little endian error %.15g -> %.15g.\n\n", __LINE__, k, data [k], test ) ;
			exit (1) ;
			} ;

		double64_be_write (data [k], bytes) ;
		test = double64_be_read (bytes) ;

		if (fabs (data [k] - test) > 1e-20)
		{	printf ("\n\nLine %d : Test %d, big endian error %.15g -> %.15g.\n\n", __LINE__, k, data [k], test) ;
			exit (1) ;
			} ;

		} ;

	puts ("ok") ;
} /* test_double_convert */