1/* test file for mpc_strtoc. 2 3Copyright (C) INRIA, 2009, 2011 4 5This file is part of the MPC Library. 6 7The MPC Library is free software; you can redistribute it and/or modify 8it under the terms of the GNU Lesser General Public License as published by 9the Free Software Foundation; either version 2.1 of the License, or (at your 10option) any later version. 11 12The MPC Library is distributed in the hope that it will be useful, but 13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 14or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 15License for more details. 16 17You should have received a copy of the GNU Lesser General Public License 18along with the MPC Library; see the file COPYING.LIB. If not, write to 19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 20MA 02111-1307, USA. */ 21 22#include <string.h> 23#include <stdlib.h> 24 25#include "mpc-tests.h" 26 27extern unsigned long line_number; 28extern int nextchar; 29extern char *pathname; 30 31/* names of rounding modes */ 32extern const char *rnd_mode[]; 33 34static void 35check_file (const char* file_name) 36{ 37 FILE *fp; 38 unsigned long test_line_number; 39 40 size_t str_len = 255; 41 char *str = NULL; 42 size_t rstr_len = 255; 43 char *rstr = NULL; 44 char *end = NULL; 45 46 int base; 47 int inex_re; 48 int inex_im; 49 mpc_t expected, got; 50 mpc_rnd_t rnd = MPC_RNDNN; 51 int inex = 0, inex_expected; 52 known_signs_t ks = {1, 1}; 53 54 55 fp = open_data_file (file_name); 56 57 /* initializations */ 58 str = (char *) malloc (str_len); 59 if (str == NULL) 60 { 61 printf ("Cannot allocate memory\n"); 62 exit (1); 63 } 64 rstr = (char *) malloc (rstr_len); 65 if (rstr == NULL) 66 { 67 printf ("Cannot allocate memory\n"); 68 exit (1); 69 } 70 mpc_init2 (expected, 53); 71 mpc_init2 (got, 53); 72 73 /* read data file */ 74 line_number = 1; 75 nextchar = getc (fp); 76 while (nextchar != EOF) 77 { 78 skip_whitespace_comments (fp); 79 80 /* 1. read a line of data: expected result, base, rounding mode */ 81 test_line_number = line_number; 82 read_ternary (fp, &inex_re); 83 read_ternary (fp, &inex_im); 84 read_mpc (fp, expected, NULL); 85 if (inex_re == TERNARY_ERROR || inex_im == TERNARY_ERROR) 86 inex_expected = -1; 87 else 88 inex_expected = MPC_INEX (inex_re, inex_im); 89 90 str_len = read_string (fp, &str, str_len, "number string"); 91 rstr_len = read_string (fp, &rstr, rstr_len, "string remainder"); 92 read_int (fp, &base, "base"); 93 read_mpc_rounding_mode (fp, &rnd); 94 95 /* 2. convert string at the same precision as the expected result */ 96 mpfr_set_prec (MPC_RE (got), MPC_PREC_RE (expected)); 97 mpfr_set_prec (MPC_IM (got), MPC_PREC_IM (expected)); 98 inex = mpc_strtoc (got, str, &end, base, rnd); 99 100 /* 3. compare this result with the expected one */ 101 if (inex != inex_expected 102 || !same_mpc_value (got, expected, ks) 103 || strcmp (end, rstr) != 0) 104 { 105 printf ("mpc_strtoc(str) failed (line %lu)\nwith base=%d and " 106 "rounding mode %s\n", test_line_number, base, 107 rnd_mode[rnd]); 108 if (inex != MPC_INEX (inex_re, inex_im)) 109 printf ("ternary value: got %s, expected (%s, %s)\n", 110 MPC_INEX_STR (inex), 111 (inex_re == +1 ? "+1" : (inex_re == -1 ? "-1" : "0")), 112 (inex_im == +1 ? "+1" : (inex_im == -1 ? "-1" : "0"))); 113 printf ("str = \"%s\"\n", str); 114 if (strcmp (end, rstr) != 0) 115 printf ("string remainder expected \"%s\"\n" 116 " got \"%s\"\n", 117 rstr, end); 118 else 119 { 120 printf (" "); 121 MPC_OUT (got); 122 MPC_OUT (expected); 123 } 124 exit (1); 125 } 126 127 end = NULL; 128 } 129 130 mpc_clear (expected); 131 mpc_clear (got); 132 if (str != NULL) 133 free (str); 134 if (rstr != NULL) 135 free (rstr); 136 close_data_file (fp); 137} 138 139static void 140check_null (void) 141{ 142 int inex; 143 char *end; 144 mpc_t z; 145 146 mpc_init2 (z, 53); 147 148 inex = mpc_strtoc (z, NULL, &end, 10, MPC_RNDNN); 149 if (end != NULL || inex != -1 || mpfr_nan_p (MPC_RE (z)) == 0 150 || mpfr_nan_p (MPC_IM (z)) == 0) 151 { 152 printf ("Error: mpc_strtoc(z, NULL) with a NULL pointer should fail" 153 " and the z value should be set to NaN +I*NaN\ngot "); 154 MPC_OUT (z); 155 exit (1); 156 } 157 158 mpc_clear (z); 159} 160 161int 162main (void) 163{ 164 check_null (); 165 check_file ("strtoc.dat"); 166 return 0; 167} 168