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