xref: /third_party/libsnd/src/test_float.c (revision b815c7f3)
1/*
2** Copyright (C) 2006-2012 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 <math.h>
27
28#include "common.h"
29#include "test_main.h"
30
31void
32test_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,
37		} ;
38
39	int k ;
40
41	print_test_name (__func__) ;
42
43	for (k = 0 ; k < ARRAY_LEN (data) ; k++)
44	{	unsigned char bytes [4] ;
45		float test ;
46
47		float32_le_write (data [k], bytes) ;
48		test = float32_le_read (bytes) ;
49
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) ;
52			exit (1) ;
53			} ;
54
55		float32_be_write (data [k], bytes) ;
56		test = float32_be_read (bytes) ;
57
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) ;
60			exit (1) ;
61			} ;
62
63		} ;
64
65	puts ("ok") ;
66} /* test_float_convert */
67
68void
69test_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,
74		} ;
75
76	int k ;
77
78	print_test_name (__func__) ;
79
80	for (k = 0 ; k < ARRAY_LEN (data) ; k++)
81	{	unsigned char bytes [8] ;
82		double test ;
83
84		double64_le_write (data [k], bytes) ;
85		test = double64_le_read (bytes) ;
86
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) ;
89			exit (1) ;
90			} ;
91
92		double64_be_write (data [k], bytes) ;
93		test = double64_be_read (bytes) ;
94
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) ;
97			exit (1) ;
98			} ;
99
100		} ;
101
102	puts ("ok") ;
103} /* test_double_convert */
104
105