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