1/* Test file for mpfr_out_str. 2 3Copyright 1999, 2001-2023 Free Software Foundation, Inc. 4Contributed by the AriC and Caramba 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 20https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23/* FIXME: The output is not tested (thus coverage data are meaningless). */ 24 25#include <float.h> 26 27#include "mpfr-test.h" 28 29FILE *fout; 30 31#define check(d,r,b) check4(d,r,b,53) 32 33static void 34check4 (double d, mpfr_rnd_t rnd, int base, int prec) 35{ 36 mpfr_t x; 37 38 mpfr_init2 (x, prec); 39 mpfr_set_d (x, d, rnd); 40 fprintf (fout, "%1.19e base %d %s:\n ", d, base, mpfr_print_rnd_mode (rnd)); 41 mpfr_out_str (fout, base, (base == 2) ? prec : 0, x, rnd); 42 fputc ('\n', fout); 43 mpfr_clear (x); 44} 45 46static void 47special (void) 48{ 49 mpfr_t x; 50 unsigned int n; 51 52 mpfr_init (x); 53 54 mpfr_set_nan (x); 55 n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN); 56 fputc ('\n', fout); 57 if (n != 5) 58 { 59 printf ("Error: mpfr_out_str (file, 10, 0, NaN, MPFR_RNDN) wrote %u " 60 "characters instead of 5.\n", n); 61 exit (1); 62 } 63 64 mpfr_set_inf (x, 1); 65 n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN); 66 fputc ('\n', fout); 67 if (n != 5) 68 { 69 printf ("Error: mpfr_out_str (file, 10, 0, +Inf, MPFR_RNDN) wrote %u " 70 "characters instead of 5.\n", n); 71 exit (1); 72 } 73 74 mpfr_set_inf (x, -1); 75 n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN); 76 fputc ('\n', fout); 77 if (n != 6) 78 { 79 printf ("Error: mpfr_out_str (file, 10, 0, -Inf, MPFR_RNDN) wrote %u " 80 "characters instead of 6.\n", n); 81 exit (1); 82 } 83 84 mpfr_set_ui (x, 0, MPFR_RNDN); 85 n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN); 86 fputc ('\n', fout); 87 if (n != 1) 88 { 89 printf ("Error: mpfr_out_str (file, 10, 0, +0, MPFR_RNDN) wrote %u " 90 "characters instead of 1.\n", n); 91 exit (1); 92 } 93 94 mpfr_neg (x, x, MPFR_RNDN); 95 n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN); 96 fputc ('\n', fout); 97 if (n != 2) 98 { 99 printf ("Error: mpfr_out_str (file, 10, 0, -0, MPFR_RNDN) wrote %u " 100 "characters instead of 2.\n", n); 101 exit (1); 102 } 103 104 mpfr_clear (x); 105} 106 107int 108main (int argc, char *argv[]) 109{ 110 const char *fname = "tout_str_out.txt"; 111 int i, N = 10000; 112 113 tests_start_mpfr (); 114 115 /* with no argument: prints to a temporary file, 116 tout_str N: prints N tests to stdout */ 117 if (argc == 1) 118 { 119 fout = fopen (fname, "w"); 120 if (fout == NULL) 121 { 122 perror (NULL); 123 fprintf (stderr, "Failed to open \"%s\" for writing\n", fname); 124 exit (1); 125 } 126 } 127 else 128 { 129 fout = stdout; 130 N = atoi (argv[1]); 131 } 132 133 special (); 134 135 check (-1.37247529013405550000e+15, MPFR_RNDN, 7); 136 check (-1.5674376729569697500e+15, MPFR_RNDN, 19); 137 check (-5.71262771772792640000e-79, MPFR_RNDU, 16); 138 check (DBL_NEG_ZERO, MPFR_RNDU, 7); 139 check (-4.5306392613572974756e-308, MPFR_RNDN, 8); 140 check (-6.7265890111403371523e-165, MPFR_RNDN, 4); 141 check (-1.3242553591261807653e+156, MPFR_RNDN, 16); 142 check (-6.606499965302424244461355e233, MPFR_RNDN, 10); 143 check4 (1.0, MPFR_RNDN, 10, 120); 144 check (1.0, MPFR_RNDU, 10); 145 check (4.059650008e-83, MPFR_RNDN, 10); 146 check (-7.4, MPFR_RNDN, 10); 147 check (0.997, MPFR_RNDN, 10); 148 check (-4.53063926135729747564e-308, MPFR_RNDN, 10); 149 check (2.14478198760196000000e+16, MPFR_RNDN, 10); 150 check (7.02293374921793516813e-84, MPFR_RNDN, 10); 151 152 /* random tests */ 153 for (i = 0; i < N; i++) 154 { 155 double d; 156 mpfr_rnd_t rnd; 157 int b; 158 159 d = DBL_RAND (); 160 rnd = RND_RAND (); 161 do 162 b = (randlimb () % (62 + 36 + 1)) - 36; 163 while (b > -2 && b < 2); 164 check (d, rnd, b); 165 } 166 167 if (fout != stdout) 168 { 169 if (fclose (fout) != 0) 170 { 171 perror (NULL); 172 fprintf (stderr, "Failed to close \"%s\"\n", fname); 173 exit (1); 174 } 175 remove (fname); 176 } 177 178 tests_end_mpfr (); 179 return 0; 180} 181