3 Copyright 2001, 2002 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library test suite.
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 Public License for more details.
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
26 #include <unistd.h> /* for unlink */
34 #define FILENAME "t-inp_str.tmp"
50 { "abc", 10, "0", 0 },
51 { "0xf", 10, "0", 1 },
52 { "ghi", 16, "0", 0 },
53 { "100", 90, "0", 0 },
55 { "ff", 16, "255", 2 },
56 { "-ff", 16, "-255", 3 },
57 { "FF", 16, "255", 2 },
58 { "-FF", 16, "-255", 3 },
62 { "1B", 59, "70", 2 },
67 { "0X10", 0, "16", 4 },
68 { "-0X0", 0, "0", 4 },
69 { "-0x10", 0, "-16", 5 },
72 { "0B10", 0, "2", 4 },
73 { "-0B0", 0, "0", 4 },
74 { "-0b10", 0, "-2", 5 },
79 { "-010", 0, "-8", 4 },
83 { " 030", 10, "30", 4 },
88 int i, pre, post, j, got_nread, want_nread;
94 for (i = 0; i < numberof (data); i++)
96 for (pre = 0; pre <= 3; pre++)
98 for (post = 0; post <= 2; post++)
100 mpz_set_str_or_abort (want, data[i].want, 0);
101 MPZ_CHECK_FORMAT (want);
103 /* create the file new each time to ensure its length is what
105 fp = fopen (FILENAME, "w+");
106 ASSERT_ALWAYS (fp != NULL);
107 for (j = 0; j < pre; j++)
109 fputs (data[i].inp, fp);
110 for (j = 0; j < post; j++)
113 ASSERT_ALWAYS (! ferror(fp));
116 got_nread = mpz_inp_str (got, fp, data[i].base);
120 ftell_nread = ftell (fp);
121 if (got_nread != ftell_nread)
123 printf ("mpz_inp_str nread wrong\n");
124 printf (" inp \"%s\"\n", data[i].inp);
125 printf (" base %d\n", data[i].base);
126 printf (" pre %d\n", pre);
127 printf (" post %d\n", post);
128 printf (" got_nread %d\n", got_nread);
129 printf (" ftell_nread %ld\n", ftell_nread);
134 /* if data[i].inp is a whole string to read and there's no post
135 whitespace then expect to have EOF */
136 if (post == 0 && data[i].want_nread == strlen(data[i].inp))
141 printf ("mpz_inp_str didn't read to EOF\n");
142 printf (" inp \"%s\"\n", data[i].inp);
143 printf (" base %d\n", data[i].base);
144 printf (" pre %d\n", pre);
145 printf (" post %d\n", post);
146 printf (" c '%c' %#x\n", c, c);
151 /* only expect "pre" included in the count when non-zero */
152 want_nread = data[i].want_nread;
156 if (got_nread != want_nread)
158 printf ("mpz_inp_str nread wrong\n");
159 printf (" inp \"%s\"\n", data[i].inp);
160 printf (" base %d\n", data[i].base);
161 printf (" pre %d\n", pre);
162 printf (" post %d\n", post);
163 printf (" got_nread %d\n", got_nread);
164 printf (" want_nread %d\n", want_nread);
168 MPZ_CHECK_FORMAT (got);
170 if (mpz_cmp (got, want) != 0)
172 printf ("mpz_inp_str wrong result\n");
173 printf (" inp \"%s\"\n", data[i].inp);
174 printf (" base %d\n", data[i].base);
175 mpz_trace (" got ", got);
176 mpz_trace (" want", want);
180 ASSERT_ALWAYS (fclose (fp) == 0);