1 /* tstrtoc -- test file for mpc_strtoc.
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/ .
24 #include "mpc-tests.h"
26 extern unsigned long line_number;
28 extern char *pathname;
30 /* names of rounding modes */
31 extern const char *rnd_mode[];
34 check_file (const char* file_name)
37 unsigned long test_line_number;
41 size_t rstr_len = 255;
49 mpc_rnd_t rnd = MPC_RNDNN;
50 int inex = 0, inex_expected;
51 known_signs_t ks = {1, 1};
54 fp = open_data_file (file_name);
57 str = (char *) malloc (str_len);
60 printf ("Cannot allocate memory\n");
63 rstr = (char *) malloc (rstr_len);
66 printf ("Cannot allocate memory\n");
69 mpc_init2 (expected, 53);
75 while (nextchar != EOF)
77 skip_whitespace_comments (fp);
79 /* 1. read a line of data: expected result, base, rounding mode */
80 test_line_number = line_number;
81 read_ternary (fp, &inex_re);
82 read_ternary (fp, &inex_im);
83 read_mpc (fp, expected, NULL);
84 if (inex_re == TERNARY_ERROR || inex_im == TERNARY_ERROR)
87 inex_expected = MPC_INEX (inex_re, inex_im);
89 str_len = read_string (fp, &str, str_len, "number string");
90 rstr_len = read_string (fp, &rstr, rstr_len, "string remainder");
91 read_int (fp, &base, "base");
92 read_mpc_rounding_mode (fp, &rnd);
94 /* 2. convert string at the same precision as the expected result */
95 mpfr_set_prec (mpc_realref (got), MPC_PREC_RE (expected));
96 mpfr_set_prec (mpc_imagref (got), MPC_PREC_IM (expected));
97 inex = mpc_strtoc (got, str, &end, base, rnd);
99 /* 3. compare this result with the expected one */
100 if (inex != inex_expected
101 || !same_mpc_value (got, expected, ks)
102 || strcmp (end, rstr) != 0)
104 printf ("mpc_strtoc(str) failed (line %lu)\nwith base=%d and "
105 "rounding mode %s\n", test_line_number, base,
107 if (inex != MPC_INEX (inex_re, inex_im))
108 printf ("ternary value: got %s, expected (%s, %s)\n",
110 (inex_re == +1 ? "+1" : (inex_re == -1 ? "-1" : "0")),
111 (inex_im == +1 ? "+1" : (inex_im == -1 ? "-1" : "0")));
112 printf ("str = \"%s\"\n", str);
113 if (strcmp (end, rstr) != 0)
114 printf ("string remainder expected \"%s\"\n"
129 mpc_clear (expected);
135 close_data_file (fp);
147 inex = mpc_strtoc (z, NULL, &end, 10, MPC_RNDNN);
148 if (end != NULL || inex != -1 || mpfr_nan_p (mpc_realref (z)) == 0
149 || mpfr_nan_p (mpc_imagref (z)) == 0)
151 printf ("Error: mpc_strtoc(z, NULL) with a NULL pointer should fail"
152 " and the z value should be set to NaN +I*NaN\ngot ");
164 check_file ("strtoc.dat");