| OLD | NEW |
| (Empty) |
| 1 /* tsum -- test file for the list summation function | |
| 2 | |
| 3 Copyright 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. | |
| 4 Contributed by the Arenaire and Cacao projects, INRIA. | |
| 5 | |
| 6 This file is part of the GNU MPFR Library. | |
| 7 | |
| 8 The GNU MPFR Library is free software; you can redistribute it and/or modify | |
| 9 it under the terms of the GNU Lesser General Public License as published by | |
| 10 the Free Software Foundation; either version 2.1 of the License, or (at your | |
| 11 option) any later version. | |
| 12 | |
| 13 The GNU MPFR Library is distributed in the hope that it will be useful, but | |
| 14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
| 15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | |
| 16 License for more details. | |
| 17 | |
| 18 You should have received a copy of the GNU Lesser General Public License | |
| 19 along with the GNU MPFR Library; see the file COPYING.LIB. If not, write to | |
| 20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 21 MA 02110-1301, USA. */ | |
| 22 | |
| 23 #include <stdlib.h> | |
| 24 #include <stdio.h> | |
| 25 #include "mpfr-test.h" | |
| 26 | |
| 27 static int | |
| 28 check_is_sorted (unsigned long n, mpfr_srcptr *perm) | |
| 29 { | |
| 30 unsigned long i; | |
| 31 | |
| 32 for (i = 0; i < n - 1; i++) | |
| 33 if (MPFR_GET_EXP(perm[i]) < MPFR_GET_EXP(perm[i+1])) | |
| 34 return 0; | |
| 35 return 1; | |
| 36 } | |
| 37 | |
| 38 static int | |
| 39 sum_tab (mpfr_ptr ret, mpfr_t *tab, unsigned long n, mp_rnd_t rnd) | |
| 40 { | |
| 41 mpfr_ptr *tabtmp; | |
| 42 unsigned long i; | |
| 43 int inexact; | |
| 44 MPFR_TMP_DECL(marker); | |
| 45 | |
| 46 MPFR_TMP_MARK(marker); | |
| 47 tabtmp = (mpfr_ptr *) MPFR_TMP_ALLOC(n * sizeof(mpfr_srcptr)); | |
| 48 for (i = 0; i < n; i++) | |
| 49 tabtmp[i] = tab[i]; | |
| 50 | |
| 51 inexact = mpfr_sum (ret, tabtmp, n, rnd); | |
| 52 MPFR_TMP_FREE(marker); | |
| 53 return inexact; | |
| 54 } | |
| 55 | |
| 56 | |
| 57 static mp_prec_t | |
| 58 get_prec_max (mpfr_t *tab, unsigned long n, mp_prec_t f) | |
| 59 { | |
| 60 mp_prec_t res; | |
| 61 mp_exp_t min, max; | |
| 62 unsigned long i; | |
| 63 | |
| 64 for (i = 0; MPFR_IS_ZERO (tab[i]); i++) | |
| 65 MPFR_ASSERTD (i < n); | |
| 66 min = max = MPFR_GET_EXP(tab[i]); | |
| 67 for (i++; i < n; i++) | |
| 68 { | |
| 69 if (!MPFR_IS_ZERO (tab[i])) { | |
| 70 if (MPFR_GET_EXP(tab[i]) > max) | |
| 71 max = MPFR_GET_EXP(tab[i]); | |
| 72 if (MPFR_GET_EXP(tab[i]) < min) | |
| 73 min = MPFR_GET_EXP(tab[i]); | |
| 74 } | |
| 75 } | |
| 76 res = max - min; | |
| 77 res += f; | |
| 78 res += __gmpfr_ceil_log2 (n) + 1; | |
| 79 return res; | |
| 80 } | |
| 81 | |
| 82 | |
| 83 static void | |
| 84 algo_exact (mpfr_t somme, mpfr_t *tab, unsigned long n, mp_prec_t f) | |
| 85 { | |
| 86 unsigned long i; | |
| 87 mp_prec_t prec_max; | |
| 88 | |
| 89 prec_max = get_prec_max(tab, n, f); | |
| 90 mpfr_set_prec (somme, prec_max); | |
| 91 mpfr_set_ui (somme, 0, GMP_RNDN); | |
| 92 for (i = 0; i < n; i++) | |
| 93 { | |
| 94 if (mpfr_add(somme, somme, tab[i], GMP_RNDN)) | |
| 95 { | |
| 96 printf ("FIXME: algo_exact is buggy.\n"); | |
| 97 exit (1); | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 /* Test the sorting function */ | |
| 103 static void | |
| 104 test_sort (mp_prec_t f, unsigned long n) | |
| 105 { | |
| 106 mpfr_t *tab; | |
| 107 mpfr_ptr *tabtmp; | |
| 108 mpfr_srcptr *perm; | |
| 109 unsigned long i; | |
| 110 | |
| 111 /* Init stuff */ | |
| 112 tab = (mpfr_t *) (*__gmp_allocate_func) (n * sizeof (mpfr_t)); | |
| 113 for (i = 0; i < n; i++) | |
| 114 mpfr_init2 (tab[i], f); | |
| 115 tabtmp = (mpfr_ptr *) (*__gmp_allocate_func) (n * sizeof(mpfr_ptr)); | |
| 116 perm = (mpfr_srcptr *) (*__gmp_allocate_func) (n * sizeof(mpfr_srcptr)); | |
| 117 | |
| 118 for (i = 0; i < n; i++) | |
| 119 { | |
| 120 mpfr_urandomb (tab[i], RANDS); | |
| 121 tabtmp[i] = tab[i]; | |
| 122 } | |
| 123 | |
| 124 mpfr_sum_sort ((mpfr_srcptr *)tabtmp, n, perm); | |
| 125 | |
| 126 if (check_is_sorted (n, perm) == 0) | |
| 127 { | |
| 128 printf ("mpfr_sum_sort incorrect.\n"); | |
| 129 for (i = 0; i < n; i++) | |
| 130 mpfr_dump (perm[i]); | |
| 131 exit (1); | |
| 132 } | |
| 133 | |
| 134 /* Clear stuff */ | |
| 135 for (i = 0; i < n; i++) | |
| 136 mpfr_clear (tab[i]); | |
| 137 (*__gmp_free_func) (tab, n * sizeof (mpfr_t)); | |
| 138 (*__gmp_free_func) (tabtmp, n * sizeof(mpfr_ptr)); | |
| 139 (*__gmp_free_func) (perm, n * sizeof(mpfr_srcptr)); | |
| 140 } | |
| 141 | |
| 142 static void | |
| 143 test_sum (mp_prec_t f, unsigned long n) | |
| 144 { | |
| 145 mpfr_t sum, real_sum, real_non_rounded; | |
| 146 mpfr_t *tab; | |
| 147 unsigned long i; | |
| 148 int rnd_mode; | |
| 149 | |
| 150 /* Init */ | |
| 151 tab = (mpfr_t *) (*__gmp_allocate_func) (n * sizeof(mpfr_t)); | |
| 152 for (i = 0; i < n; i++) | |
| 153 mpfr_init2 (tab[i], f); | |
| 154 mpfr_inits2 (f, sum, real_sum, real_non_rounded, (mpfr_ptr) 0); | |
| 155 | |
| 156 /* First Uniform */ | |
| 157 for (i = 0; i < n; i++) | |
| 158 mpfr_urandomb (tab[i], RANDS); | |
| 159 algo_exact (real_non_rounded, tab, n, f); | |
| 160 for (rnd_mode = 0; rnd_mode < GMP_RND_MAX; rnd_mode++) | |
| 161 { | |
| 162 sum_tab (sum, tab, n, (mp_rnd_t) rnd_mode); | |
| 163 mpfr_set (real_sum, real_non_rounded, (mp_rnd_t) rnd_mode); | |
| 164 if (mpfr_cmp (real_sum, sum) != 0) | |
| 165 { | |
| 166 printf ("mpfr_sum incorrect.\n"); | |
| 167 mpfr_dump (real_sum); | |
| 168 mpfr_dump (sum); | |
| 169 exit (1); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 /* Then non uniform */ | |
| 174 for (i = 0; i < n; i++) | |
| 175 { | |
| 176 mpfr_urandomb (tab[i], RANDS); | |
| 177 mpfr_set_exp (tab[i], randlimb () %1000); | |
| 178 } | |
| 179 algo_exact (real_non_rounded, tab, n, f); | |
| 180 for (rnd_mode = 0; rnd_mode < GMP_RND_MAX; rnd_mode++) | |
| 181 { | |
| 182 sum_tab (sum, tab, n, (mp_rnd_t) rnd_mode); | |
| 183 mpfr_set (real_sum, real_non_rounded, (mp_rnd_t) rnd_mode); | |
| 184 if (mpfr_cmp (real_sum, sum) != 0) | |
| 185 { | |
| 186 printf ("mpfr_sum incorrect.\n"); | |
| 187 mpfr_dump (real_sum); | |
| 188 mpfr_dump (sum); | |
| 189 exit (1); | |
| 190 } | |
| 191 } | |
| 192 | |
| 193 /* Clear stuff */ | |
| 194 for (i = 0; i < n; i++) | |
| 195 mpfr_clear (tab[i]); | |
| 196 mpfr_clears (sum, real_sum, real_non_rounded, (mpfr_ptr) 0); | |
| 197 (*__gmp_free_func) (tab, n * sizeof(mpfr_t)); | |
| 198 } | |
| 199 | |
| 200 static | |
| 201 void check_special (void) | |
| 202 { | |
| 203 mpfr_t tab[3], r; | |
| 204 mpfr_ptr tabp[3]; | |
| 205 int i; | |
| 206 | |
| 207 mpfr_inits (tab[0], tab[1], tab[2], r, (mpfr_ptr) 0); | |
| 208 tabp[0] = tab[0]; | |
| 209 tabp[1] = tab[1]; | |
| 210 tabp[2] = tab[2]; | |
| 211 | |
| 212 i = mpfr_sum (r, tabp, 0, GMP_RNDN); | |
| 213 if (!MPFR_IS_ZERO (r) || !MPFR_IS_POS (r) || i != 0) | |
| 214 { | |
| 215 printf ("Special case n==0 failed!\n"); | |
| 216 exit (1); | |
| 217 } | |
| 218 | |
| 219 mpfr_set_ui (tab[0], 42, GMP_RNDN); | |
| 220 i = mpfr_sum (r, tabp, 1, GMP_RNDN); | |
| 221 if (mpfr_cmp_ui (r, 42) || i != 0) | |
| 222 { | |
| 223 printf ("Special case n==1 failed!\n"); | |
| 224 exit (1); | |
| 225 } | |
| 226 | |
| 227 mpfr_set_ui (tab[1], 17, GMP_RNDN); | |
| 228 MPFR_SET_NAN (tab[2]); | |
| 229 i = mpfr_sum (r, tabp, 3, GMP_RNDN); | |
| 230 if (!MPFR_IS_NAN (r) || i != 0) | |
| 231 { | |
| 232 printf ("Special case NAN failed!\n"); | |
| 233 exit (1); | |
| 234 } | |
| 235 | |
| 236 MPFR_SET_INF (tab[2]); | |
| 237 MPFR_SET_POS (tab[2]); | |
| 238 i = mpfr_sum (r, tabp, 3, GMP_RNDN); | |
| 239 if (!MPFR_IS_INF (r) || !MPFR_IS_POS (r) || i != 0) | |
| 240 { | |
| 241 printf ("Special case +INF failed!\n"); | |
| 242 exit (1); | |
| 243 } | |
| 244 | |
| 245 MPFR_SET_INF (tab[2]); | |
| 246 MPFR_SET_NEG (tab[2]); | |
| 247 i = mpfr_sum (r, tabp, 3, GMP_RNDN); | |
| 248 if (!MPFR_IS_INF (r) || !MPFR_IS_NEG (r) || i != 0) | |
| 249 { | |
| 250 printf ("Special case -INF failed!\n"); | |
| 251 exit (1); | |
| 252 } | |
| 253 | |
| 254 MPFR_SET_ZERO (tab[1]); | |
| 255 i = mpfr_sum (r, tabp, 2, GMP_RNDN); | |
| 256 if (mpfr_cmp_ui (r, 42) || i != 0) | |
| 257 { | |
| 258 printf ("Special case 42+0 failed!\n"); | |
| 259 exit (1); | |
| 260 } | |
| 261 | |
| 262 MPFR_SET_NAN (tab[0]); | |
| 263 i = mpfr_sum (r, tabp, 3, GMP_RNDN); | |
| 264 if (!MPFR_IS_NAN (r) || i != 0) | |
| 265 { | |
| 266 printf ("Special case NAN+0+-INF failed!\n"); | |
| 267 exit (1); | |
| 268 } | |
| 269 | |
| 270 mpfr_set_inf (tab[0], 1); | |
| 271 mpfr_set_ui (tab[1], 59, GMP_RNDN); | |
| 272 mpfr_set_inf (tab[2], -1); | |
| 273 i = mpfr_sum (r, tabp, 3, GMP_RNDN); | |
| 274 if (!MPFR_IS_NAN (r) || i != 0) | |
| 275 { | |
| 276 printf ("Special case +INF + 59 +-INF failed!\n"); | |
| 277 exit (1); | |
| 278 } | |
| 279 | |
| 280 mpfr_clears (tab[0], tab[1], tab[2], r, (mpfr_ptr) 0); | |
| 281 } | |
| 282 | |
| 283 | |
| 284 int | |
| 285 main (void) | |
| 286 { | |
| 287 mp_prec_t p; | |
| 288 unsigned long n; | |
| 289 | |
| 290 tests_start_mpfr (); | |
| 291 | |
| 292 check_special (); | |
| 293 test_sort (1764, 1026); | |
| 294 for (p = 2 ; p < 444 ; p += 17) | |
| 295 for (n = 2 ; n < 1026 ; n += 42 + p) | |
| 296 test_sum (p, n); | |
| 297 | |
| 298 tests_end_mpfr (); | |
| 299 return 0; | |
| 300 } | |
| OLD | NEW |