annotate src/libsndfile-1.0.27/src/test_float.c @ 125:cd6cdf86811e

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