t-inp_str.c revision 1.1.1.1
1/* Test mpf_inp_str. 2 3Copyright 2001, 2002 Free Software Foundation, Inc. 4 5This file is part of the GNU MP Library. 6 7The GNU MP 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 3 of the License, or (at your 10option) any later version. 11 12The GNU MP 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 GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 19 20#include "config.h" 21 22#include <stdio.h> 23#include <stdlib.h> 24#include <string.h> 25#if HAVE_UNISTD_H 26#include <unistd.h> /* for unlink */ 27#endif 28 29#include "gmp.h" 30#include "gmp-impl.h" 31#include "tests.h" 32 33 34#define FILENAME "t-inp_str.tmp" 35 36 37void 38check_data (void) 39{ 40 static const struct { 41 const char *inp; 42 int base; 43 const char *want; 44 int want_nread; 45 46 } data[] = { 47 48 { "0", 10, "0", 1 }, 49 50 { "abc", 10, "0", 0 }, 51 { "ghi", 16, "0", 0 }, 52 53 { "125", 10, "125", 3 }, 54 { "125e1", 10, "1250", 5 }, 55 { "125e-1", 10, "12.5", 6 }, 56 57 { "ff", 16, "255", 2 }, 58 { "-ff", 16, "-255", 3 }, 59 { "FF", 16, "255", 2 }, 60 { "-FF", 16, "-255", 3 }, 61 62 { "100", 16, "256", 3 }, 63 { "100@1", 16, "4096", 5 }, 64 { "100@10", 16, "4722366482869645213696", 6 }, 65 { "100@10", -16, "281474976710656", 6 }, 66 { "100@-1", 16, "16", 6 }, 67 { "10000000000000000@-10", 16, "1", 21 }, 68 { "10000000000@-10", -16, "1", 15 }, 69 70 { "z", 36, "35", 1 }, 71 { "Z", 36, "35", 1 }, 72 { "z@1", 36, "1260", 3 }, 73 { "Z@1", 36, "1260", 3 }, 74 75 { "0", 0, "0", 1 }, 76 }; 77 78 mpf_t got, want; 79 long ftell_nread; 80 int i, pre, post, j, got_nread, want_nread; 81 FILE *fp; 82 83 mpf_init (got); 84 mpf_init (want); 85 86 for (i = 0; i < numberof (data); i++) 87 { 88 for (pre = 0; pre <= 3; pre++) 89 { 90 for (post = 0; post <= 2; post++) 91 { 92 mpf_set_str_or_abort (want, data[i].want, 10); 93 MPF_CHECK_FORMAT (want); 94 95 /* create the file new each time to ensure its length is what 96 we want */ 97 fp = fopen (FILENAME, "w+"); 98 ASSERT_ALWAYS (fp != NULL); 99 for (j = 0; j < pre; j++) 100 putc (' ', fp); 101 fputs (data[i].inp, fp); 102 for (j = 0; j < post; j++) 103 putc (' ', fp); 104 fflush (fp); 105 ASSERT_ALWAYS (! ferror(fp)); 106 107 rewind (fp); 108 got_nread = mpf_inp_str (got, fp, data[i].base); 109 110 if (got_nread != 0) 111 { 112 ftell_nread = ftell (fp); 113 if (got_nread != ftell_nread) 114 { 115 printf ("mpf_inp_str nread wrong\n"); 116 printf (" inp \"%s\"\n", data[i].inp); 117 printf (" base %d\n", data[i].base); 118 printf (" pre %d\n", pre); 119 printf (" post %d\n", post); 120 printf (" got_nread %d\n", got_nread); 121 printf (" ftell_nread %ld\n", ftell_nread); 122 abort (); 123 } 124 } 125 126 /* if data[i].inp is a whole string to read and there's no post 127 whitespace then expect to have EOF */ 128 if (post == 0 && data[i].want_nread == strlen(data[i].inp)) 129 { 130 int c = getc(fp); 131 if (c != EOF) 132 { 133 printf ("mpf_inp_str didn't read to EOF\n"); 134 printf (" inp \"%s\"\n", data[i].inp); 135 printf (" base %d\n", data[i].base); 136 printf (" pre %d\n", pre); 137 printf (" post %d\n", post); 138 printf (" c '%c' %#x\n", c, c); 139 abort (); 140 } 141 } 142 143 /* only expect "pre" included in the count when non-zero */ 144 want_nread = data[i].want_nread; 145 if (want_nread != 0) 146 want_nread += pre; 147 148 if (got_nread != want_nread) 149 { 150 printf ("mpf_inp_str nread wrong\n"); 151 printf (" inp \"%s\"\n", data[i].inp); 152 printf (" base %d\n", data[i].base); 153 printf (" pre %d\n", pre); 154 printf (" post %d\n", post); 155 printf (" got_nread %d\n", got_nread); 156 printf (" want_nread %d\n", want_nread); 157 abort (); 158 } 159 160 MPF_CHECK_FORMAT (got); 161 162 if (mpf_cmp (got, want) != 0) 163 { 164 printf ("mpf_inp_str wrong result\n"); 165 printf (" inp \"%s\"\n", data[i].inp); 166 printf (" base %d\n", data[i].base); 167 mpf_trace (" got ", got); 168 mpf_trace (" want", want); 169 abort (); 170 } 171 172 ASSERT_ALWAYS (fclose (fp) == 0); 173 } 174 } 175 } 176 177 mpf_clear (got); 178 mpf_clear (want); 179} 180 181int 182main (void) 183{ 184 tests_start (); 185 186 check_data (); 187 188 unlink (FILENAME); 189 tests_end (); 190 exit (0); 191} 192