1/* tstrtoc -- test file for mpc_strtoc. 2 3Copyright (C) 2009, 2011 INRIA 4 5This file is part of GNU MPC. 6 7GNU MPC is free software; you can redistribute it and/or modify it under 8the terms of the GNU Lesser General Public License as published by the 9Free Software Foundation; either version 3 of the License, or (at your 10option) any later version. 11 12GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY 13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 14FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for 15more details. 16 17You should have received a copy of the GNU Lesser General Public License 18along with this program. If not, see http://www.gnu.org/licenses/ . 19*/ 20 21#include <string.h> 22#include <stdlib.h> 23 24#include "mpc-tests.h" 25 26extern unsigned long line_number; 27extern int nextchar; 28extern char *pathname; 29 30/* names of rounding modes */ 31extern const char *rnd_mode[]; 32 33static void 34check_file (const char* file_name) 35{ 36 FILE *fp; 37 unsigned long test_line_number; 38 39 size_t str_len = 255; 40 char *str = NULL; 41 size_t rstr_len = 255; 42 char *rstr = NULL; 43 char *end = NULL; 44 45 int base; 46 int inex_re; 47 int inex_im; 48 mpc_t expected, got; 49 mpc_rnd_t rnd = MPC_RNDNN; 50 int inex = 0, inex_expected; 51 known_signs_t ks = {1, 1}; 52 53 54 fp = open_data_file (file_name); 55 56 /* initializations */ 57 str = (char *) malloc (str_len); 58 if (str == NULL) 59 { 60 printf ("Cannot allocate memory\n"); 61 exit (1); 62 } 63 rstr = (char *) malloc (rstr_len); 64 if (rstr == NULL) 65 { 66 printf ("Cannot allocate memory\n"); 67 exit (1); 68 } 69 mpc_init2 (expected, 53); 70 mpc_init2 (got, 53); 71 72 /* read data file */ 73 line_number = 1; 74 nextchar = getc (fp); 75 while (nextchar != EOF) 76 { 77 skip_whitespace_comments (fp); 78 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) 85 inex_expected = -1; 86 else 87 inex_expected = MPC_INEX (inex_re, inex_im); 88 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); 93 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); 98 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) 103 { 104 printf ("mpc_strtoc(str) failed (line %lu)\nwith base=%d and " 105 "rounding mode %s\n", test_line_number, base, 106 rnd_mode[rnd]); 107 if (inex != MPC_INEX (inex_re, inex_im)) 108 printf ("ternary value: got %s, expected (%s, %s)\n", 109 MPC_INEX_STR (inex), 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" 115 " got \"%s\"\n", 116 rstr, end); 117 else 118 { 119 printf (" "); 120 MPC_OUT (got); 121 MPC_OUT (expected); 122 } 123 exit (1); 124 } 125 126 end = NULL; 127 } 128 129 mpc_clear (expected); 130 mpc_clear (got); 131 if (str != NULL) 132 free (str); 133 if (rstr != NULL) 134 free (rstr); 135 close_data_file (fp); 136} 137 138static void 139check_null (void) 140{ 141 int inex; 142 char *end; 143 mpc_t z; 144 145 mpc_init2 (z, 53); 146 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) 150 { 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 "); 153 MPC_OUT (z); 154 exit (1); 155 } 156 157 mpc_clear (z); 158} 159 160int 161main (void) 162{ 163 check_null (); 164 check_file ("strtoc.dat"); 165 return 0; 166} 167