1 /* tio_str-- Test file for mpc_inp_str and mpc_out_str.
3 Copyright (C) 2009, 2011 INRIA
5 This file is part of GNU MPC.
7 GNU MPC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see http://www.gnu.org/licenses/ .
21 #include "mpc-tests.h"
24 #define _POSIX_C_SOURCE 1 /* apparently needed on Darwin */
25 #include <unistd.h> /* for dup, dup2, STDIN_FILENO and STDOUT_FILENO */
27 #define STDIN_FILENO 0
28 #define STDOUT_FILENO 1
31 extern unsigned long line_number;
32 /* character appearing next in the file, may be EOF */
34 extern const char *rnd_mode[];
37 check_file (const char* file_name)
46 mpc_rnd_t rnd = MPC_RNDNN;
47 int inex = 0, expected_inex;
48 size_t expected_size, size;
49 known_signs_t ks = {1, 1};
51 fp = open_data_file (file_name);
53 mpc_init2 (expected, 53);
59 skip_whitespace_comments (fp);
61 while (nextchar != EOF)
63 /* 1. read a line of data: expected result, base, rounding mode */
64 read_ternary (fp, &inex_re);
65 read_ternary (fp, &inex_im);
66 read_mpc (fp, expected, &ks);
67 if (inex_re == TERNARY_ERROR || inex_im == TERNARY_ERROR)
70 expected_inex = MPC_INEX (inex_re, inex_im);
71 read_int (fp, &tmp, "size");
72 expected_size = (size_t)tmp;
73 read_int (fp, &base, "base");
74 read_mpc_rounding_mode (fp, &rnd);
76 /* 2. read string at the same precision as the expected result */
77 while (nextchar != '"')
79 mpfr_set_prec (mpc_realref (got), MPC_PREC_RE (expected));
80 mpfr_set_prec (mpc_imagref (got), MPC_PREC_IM (expected));
81 inex = mpc_inp_str (got, fp, &size, base, rnd);
83 /* 3. compare this result with the expected one */
84 if (inex != expected_inex || !same_mpc_value (got, expected, ks)
85 || size != expected_size)
87 printf ("mpc_inp_str failed (line %lu) with rounding mode %s\n",
88 line_number, rnd_mode[rnd]);
89 if (inex != expected_inex)
90 printf(" got inexact value: %d\nexpected inexact value: %d\n",
92 if (size != expected_size)
93 printf (" got size: %lu\nexpected size: %lu\n ",
94 (unsigned long int) size, (unsigned long int) expected_size);
102 while ((nextchar = getc (fp)) != '"');
103 nextchar = getc (fp);
105 skip_whitespace_comments (fp);
108 mpc_clear (expected);
110 close_data_file (fp);
114 check_io_str (mpc_ptr read_number, mpc_ptr expected)
116 char tmp_file[] = "mpc_test";
120 if (!(fp = fopen (tmp_file, "w")))
122 printf ("Error: Could not open file %s in write mode\n", tmp_file);
126 mpc_out_str (fp, 10, 0, expected, MPC_RNDNN);
129 if (!(fp = fopen (tmp_file, "r")))
131 printf ("Error: Could not open file %s in read mode\n", tmp_file);
134 if (mpc_inp_str (read_number, fp, &sz, 10, MPC_RNDNN) == -1)
136 printf ("Error: mpc_inp_str cannot correctly re-read number "
137 "in file %s\n", tmp_file);
143 /* mpc_cmp set erange flag when an operand is a NaN */
145 if (mpc_cmp (read_number, expected) != 0 || mpfr_erangeflag_p())
147 printf ("Error: inp_str o out_str <> Id\n");
148 MPC_OUT (read_number);
155 #ifndef MPC_NO_STREAM_REDIRECTION
156 /* test out_str with stream=NULL */
158 check_stdout (mpc_ptr read_number, mpc_ptr expected)
160 char tmp_file[] = "mpc_test";
165 fd = dup (STDOUT_FILENO);
166 if (freopen (tmp_file, "w", stdout) == NULL)
168 printf ("mpc_inp_str cannot redirect stdout\n");
171 mpc_out_str (NULL, 2, 0, expected, MPC_RNDNN);
173 dup2 (fd, STDOUT_FILENO);
178 fd = dup (STDIN_FILENO);
179 if (freopen (tmp_file, "r", stdin) == NULL)
181 printf ("mpc_inp_str cannot redirect stdout\n");
184 if (mpc_inp_str (read_number, NULL, &sz, 2, MPC_RNDNN) == -1)
186 printf ("mpc_inp_str cannot correctly re-read number "
187 "in file %s\n", tmp_file);
190 mpfr_clear_flags (); /* mpc_cmp set erange flag when an operand is
192 if (mpc_cmp (read_number, expected) != 0 || mpfr_erangeflag_p())
194 printf ("mpc_inp_str did not read the number which was written by "
196 MPC_OUT (read_number);
201 dup2 (fd, STDIN_FILENO);
205 #endif /* MPC_NO_STREAM_REDIRECTION */
218 check_file ("inp_str.dat");
220 for (prec = 2; prec <= 1000; prec+=7)
222 mpc_set_prec (z, prec);
223 mpc_set_prec (x, prec);
225 mpc_set_si_si (x, 1, 1, MPC_RNDNN);
228 mpc_set_si_si (x, -1, 1, MPC_RNDNN);
231 mpfr_set_inf (mpc_realref(x), -1);
232 mpfr_set_inf (mpc_imagref(x), +1);
235 test_default_random (x, -1024, 1024, 128, 25);
239 #ifndef MPC_NO_STREAM_REDIRECTION
240 mpc_set_si_si (x, 1, -4, MPC_RNDNN);
241 mpc_div_ui (x, x, 3, MPC_RNDDU);