Mercurial > hg > sv-dependency-builds
comparison src/libsamplerate-0.1.8/tests/float_short_test.c @ 0:c7265573341e
Import initial set of sources
| author | Chris Cannam |
|---|---|
| date | Mon, 18 Mar 2013 14:12:14 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:c7265573341e |
|---|---|
| 1 /* | |
| 2 ** Copyright (C) 2003-2011 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 General Public License as published by | |
| 6 ** the Free Software Foundation; either version 2 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 General Public License for more details. | |
| 13 ** | |
| 14 ** You should have received a copy of the GNU 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 <stdio.h> | |
| 20 #include <stdlib.h> | |
| 21 #include <string.h> | |
| 22 | |
| 23 #include <samplerate.h> | |
| 24 | |
| 25 #include "util.h" | |
| 26 | |
| 27 #define BUFFER_LEN 10000 | |
| 28 | |
| 29 static void float_to_short_test (void) ; | |
| 30 static void short_to_float_test (void) ; | |
| 31 | |
| 32 static void float_to_int_test (void) ; | |
| 33 static void int_to_float_test (void) ; | |
| 34 | |
| 35 int | |
| 36 main (void) | |
| 37 { | |
| 38 puts ("") ; | |
| 39 | |
| 40 float_to_short_test () ; | |
| 41 short_to_float_test () ; | |
| 42 | |
| 43 float_to_int_test () ; | |
| 44 int_to_float_test () ; | |
| 45 | |
| 46 puts ("") ; | |
| 47 | |
| 48 return 0 ; | |
| 49 } /* main */ | |
| 50 | |
| 51 /*===================================================================================== | |
| 52 */ | |
| 53 | |
| 54 static void | |
| 55 float_to_short_test (void) | |
| 56 { | |
| 57 static float fpos [] = | |
| 58 { 0.95, 0.99, 1.0, 1.01, 1.1, 2.0, 11.1, 111.1, 2222.2, 33333.3 | |
| 59 } ; | |
| 60 static float fneg [] = | |
| 61 { -0.95, -0.99, -1.0, -1.01, -1.1, -2.0, -11.1, -111.1, -2222.2, -33333.3 | |
| 62 } ; | |
| 63 | |
| 64 static short out [MAX (ARRAY_LEN (fpos), ARRAY_LEN (fneg))] ; | |
| 65 | |
| 66 int k ; | |
| 67 | |
| 68 printf ("\tfloat_to_short_test ............................. ") ; | |
| 69 | |
| 70 src_float_to_short_array (fpos, out, ARRAY_LEN (fpos)) ; | |
| 71 | |
| 72 for (k = 0 ; k < ARRAY_LEN (fpos) ; k++) | |
| 73 if (out [k] < 30000) | |
| 74 { printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ; | |
| 75 exit (1) ; | |
| 76 } ; | |
| 77 | |
| 78 src_float_to_short_array (fneg, out, ARRAY_LEN (fneg)) ; | |
| 79 | |
| 80 for (k = 0 ; k < ARRAY_LEN (fneg) ; k++) | |
| 81 if (out [k] > -30000) | |
| 82 { printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ; | |
| 83 exit (1) ; | |
| 84 } ; | |
| 85 | |
| 86 puts ("ok") ; | |
| 87 | |
| 88 return ; | |
| 89 } /* float_to_short_test */ | |
| 90 | |
| 91 /*------------------------------------------------------------------------------------- | |
| 92 */ | |
| 93 | |
| 94 static void | |
| 95 short_to_float_test (void) | |
| 96 { | |
| 97 static short input [BUFFER_LEN] ; | |
| 98 static short output [BUFFER_LEN] ; | |
| 99 static float temp [BUFFER_LEN] ; | |
| 100 | |
| 101 int k ; | |
| 102 | |
| 103 printf ("\tshort_to_float_test ............................. ") ; | |
| 104 | |
| 105 for (k = 0 ; k < ARRAY_LEN (input) ; k++) | |
| 106 input [k] = (k * 0x8000) / ARRAY_LEN (input) ; | |
| 107 | |
| 108 src_short_to_float_array (input, temp, ARRAY_LEN (temp)) ; | |
| 109 src_float_to_short_array (temp, output, ARRAY_LEN (output)) ; | |
| 110 | |
| 111 for (k = 0 ; k < ARRAY_LEN (input) ; k++) | |
| 112 if (ABS (input [k] - output [k]) > 0) | |
| 113 { printf ("\n\n\tLine %d : index %d %d -> %d\n", __LINE__, k, input [k], output [k]) ; | |
| 114 exit (1) ; | |
| 115 } ; | |
| 116 | |
| 117 puts ("ok") ; | |
| 118 | |
| 119 return ; | |
| 120 } /* short_to_float_test */ | |
| 121 | |
| 122 /*===================================================================================== | |
| 123 */ | |
| 124 | |
| 125 static void | |
| 126 float_to_int_test (void) | |
| 127 { | |
| 128 static float fpos [] = | |
| 129 { 0.95, 0.99, 1.0, 1.01, 1.1, 2.0, 11.1, 111.1, 2222.2, 33333.3 | |
| 130 } ; | |
| 131 static float fneg [] = | |
| 132 { -0.95, -0.99, -1.0, -1.01, -1.1, -2.0, -11.1, -111.1, -2222.2, -33333.3 | |
| 133 } ; | |
| 134 | |
| 135 static int out [MAX (ARRAY_LEN (fpos), ARRAY_LEN (fneg))] ; | |
| 136 | |
| 137 int k ; | |
| 138 | |
| 139 printf ("\tfloat_to_int_test ............................... ") ; | |
| 140 | |
| 141 src_float_to_int_array (fpos, out, ARRAY_LEN (fpos)) ; | |
| 142 | |
| 143 for (k = 0 ; k < ARRAY_LEN (fpos) ; k++) | |
| 144 if (out [k] < 30000 * 0x10000) | |
| 145 { printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ; | |
| 146 exit (1) ; | |
| 147 } ; | |
| 148 | |
| 149 src_float_to_int_array (fneg, out, ARRAY_LEN (fneg)) ; | |
| 150 | |
| 151 for (k = 0 ; k < ARRAY_LEN (fneg) ; k++) | |
| 152 if (out [k] > -30000 * 0x1000) | |
| 153 { printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ; | |
| 154 exit (1) ; | |
| 155 } ; | |
| 156 | |
| 157 puts ("ok") ; | |
| 158 | |
| 159 return ; | |
| 160 } /* float_to_int_test */ | |
| 161 | |
| 162 /*------------------------------------------------------------------------------------- | |
| 163 */ | |
| 164 | |
| 165 static void | |
| 166 int_to_float_test (void) | |
| 167 { | |
| 168 static int input [BUFFER_LEN] ; | |
| 169 static int output [BUFFER_LEN] ; | |
| 170 static float temp [BUFFER_LEN] ; | |
| 171 | |
| 172 int k ; | |
| 173 | |
| 174 printf ("\tint_to_float_test ............................... ") ; | |
| 175 | |
| 176 for (k = 0 ; k < ARRAY_LEN (input) ; k++) | |
| 177 input [k] = (k * 0x80000000) / ARRAY_LEN (input) ; | |
| 178 | |
| 179 src_int_to_float_array (input, temp, ARRAY_LEN (temp)) ; | |
| 180 src_float_to_int_array (temp, output, ARRAY_LEN (output)) ; | |
| 181 | |
| 182 for (k = 0 ; k < ARRAY_LEN (input) ; k++) | |
| 183 if (ABS (input [k] - output [k]) > 0) | |
| 184 { printf ("\n\n\tLine %d : index %d %d -> %d\n", __LINE__, k, input [k], output [k]) ; | |
| 185 exit (1) ; | |
| 186 } ; | |
| 187 | |
| 188 puts ("ok") ; | |
| 189 | |
| 190 return ; | |
| 191 } /* int_to_float_test */ | |
| 192 |
