1/* Test file for mpfr_out_str. 2 3Copyright 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc. 4Contributed by the AriC and Caramel projects, INRIA. 5 6This file is part of the GNU MPFR Library. 7 8The GNU MPFR Library is free software; you can redistribute it and/or modify 9it under the terms of the GNU Lesser General Public License as published by 10the Free Software Foundation; either version 3 of the License, or (at your 11option) any later version. 12 13The GNU MPFR Library is distributed in the hope that it will be useful, but 14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16License for more details. 17 18You should have received a copy of the GNU Lesser General Public License 19along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 20http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23#include <float.h> 24#include <stdlib.h> 25 26#include "mpfr-test.h" 27 28FILE *fout; 29 30#define check(d,r,b) check4(d,r,b,53) 31 32static void 33check4 (double d, mpfr_rnd_t rnd, int base, int prec) 34{ 35 mpfr_t x; 36 37 mpfr_init2 (x, prec); 38 mpfr_set_d (x, d, rnd); 39 fprintf (fout, "%1.19e base %d rnd %d:\n ", d, base, rnd); 40 mpfr_out_str (fout, base, (base == 2) ? prec : 0, x, rnd); 41 fputc ('\n', fout); 42 mpfr_clear (x); 43} 44 45static void 46special (void) 47{ 48 mpfr_t x; 49 unsigned int n; 50 51 mpfr_init (x); 52 53 mpfr_set_nan (x); 54 n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN); 55 if (n != 5) 56 { 57 printf ("Error: mpfr_out_str (file, 10, 0, NaN, MPFR_RNDN) wrote %u " 58 "characters instead of 5.\n", n); 59 exit (1); 60 } 61 62 mpfr_set_inf (x, 1); 63 n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN); 64 if (n != 5) 65 { 66 printf ("Error: mpfr_out_str (file, 10, 0, +Inf, MPFR_RNDN) wrote %u " 67 "characters instead of 5.\n", n); 68 exit (1); 69 } 70 71 mpfr_set_inf (x, -1); 72 n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN); 73 if (n != 6) 74 { 75 printf ("Error: mpfr_out_str (file, 10, 0, -Inf, MPFR_RNDN) wrote %u " 76 "characters instead of 6.\n", n); 77 exit (1); 78 } 79 80 mpfr_set_ui (x, 0, MPFR_RNDN); 81 n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN); 82 if (n != 1) 83 { 84 printf ("Error: mpfr_out_str (file, 10, 0, +0, MPFR_RNDN) wrote %u " 85 "characters instead of 1.\n", n); 86 exit (1); 87 } 88 89 mpfr_neg (x, x, MPFR_RNDN); 90 n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN); 91 if (n != 2) 92 { 93 printf ("Error: mpfr_out_str (file, 10, 0, -0, MPFR_RNDN) wrote %u " 94 "characters instead of 2.\n", n); 95 exit (1); 96 } 97 98 mpfr_clear (x); 99} 100 101int 102main (int argc, char *argv[]) 103{ 104 int i, N=10000, p; 105 mpfr_rnd_t rnd; 106 double d; 107 108 tests_start_mpfr (); 109 110 /* with no argument: prints to /dev/null, 111 tout_str N: prints N tests to stdout */ 112 if (argc == 1) 113 { 114 fout = fopen ("/dev/null", "w"); 115 /* If we failed to open this device, try with a dummy file */ 116 if (fout == NULL) 117 fout = fopen ("mpfrtest.txt", "w"); 118 } 119 else 120 { 121 fout = stdout; 122 N = atoi (argv[1]); 123 } 124 125 if (fout == NULL) 126 { 127 printf ("Can't open /dev/null or stdout\n"); 128 exit (1); 129 } 130 131 special (); 132 133 check (-1.37247529013405550000e+15, MPFR_RNDN, 7); 134 check (-1.5674376729569697500e+15, MPFR_RNDN, 19); 135 check (-5.71262771772792640000e-79, MPFR_RNDU, 16); 136 check (DBL_NEG_ZERO, MPFR_RNDU, 7); 137 check (-4.5306392613572974756e-308, MPFR_RNDN, 8); 138 check (-6.7265890111403371523e-165, MPFR_RNDN, 4); 139 check (-1.3242553591261807653e+156, MPFR_RNDN, 16); 140 check (-6.606499965302424244461355e233, MPFR_RNDN, 10); 141 check4 (1.0, MPFR_RNDN, 10, 120); 142 check (1.0, MPFR_RNDU, 10); 143 check (4.059650008e-83, MPFR_RNDN, 10); 144 check (-7.4, MPFR_RNDN, 10); 145 check (0.997, MPFR_RNDN, 10); 146 check (-4.53063926135729747564e-308, MPFR_RNDN, 10); 147 check (2.14478198760196000000e+16, MPFR_RNDN, 10); 148 check (7.02293374921793516813e-84, MPFR_RNDN, 10); 149 150 /* random tests */ 151 for (i=0;i<N;i++) 152 { 153 do 154 { 155 d = DBL_RAND (); 156 } 157#ifdef HAVE_DENORMS 158 while (0); 159#else 160 while (ABS(d) < DBL_MIN); 161#endif 162 rnd = RND_RAND (); 163 p = 2 + randlimb () % 61; 164 check (d, rnd, p); 165 } 166 167 fclose (fout); 168 169 tests_end_mpfr (); 170 return 0; 171} 172