1/* Test mpz_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    {  "ff", 16,  "255", 2 },
54    { "-ff", 16, "-255", 3 },
55    {  "FF", 16,  "255", 2 },
56    { "-FF", 16, "-255", 3 },
57
58    { "z", 36, "35", 1 },
59    { "Z", 36, "35", 1 },
60
61    {  "0x0",    0,   "0", 3 },
62    {  "0x10",   0,  "16", 4 },
63    { "-0x0",    0,   "0", 4 },
64    { "-0x10",   0, "-16", 5 },
65
66    {  "00",   0,  "0", 2 },
67    {  "010",  0,  "8", 3 },
68    { "-00",   0,  "0", 3 },
69    { "-010",  0, "-8", 4 },
70
71    {  "0x",     0,   "0", 2 },
72    {  "0",      0,   "0", 1 },
73  };
74
75  mpz_t  got, want;
76  long   ftell_nread;
77  int    i, pre, post, j, got_nread, want_nread;
78  FILE   *fp;
79
80  mpz_init (got);
81  mpz_init (want);
82
83  for (i = 0; i < numberof (data); i++)
84    {
85      for (pre = 0; pre <= 3; pre++)
86	{
87	  for (post = 0; post <= 2; post++)
88	    {
89	      mpz_set_str_or_abort (want, data[i].want, 0);
90	      MPZ_CHECK_FORMAT (want);
91
92	      /* create the file new each time to ensure its length is what
93		 we want */
94	      fp = fopen (FILENAME, "w+");
95	      ASSERT_ALWAYS (fp != NULL);
96	      for (j = 0; j < pre; j++)
97		putc (' ', fp);
98	      fputs (data[i].inp, fp);
99	      for (j = 0; j < post; j++)
100		putc (' ', fp);
101	      fflush (fp);
102	      ASSERT_ALWAYS (! ferror(fp));
103
104	      rewind (fp);
105	      got_nread = mpz_inp_str (got, fp, data[i].base);
106
107	      if (got_nread != 0)
108		{
109		  ftell_nread = ftell (fp);
110		  if (got_nread != ftell_nread)
111		    {
112		      printf ("mpz_inp_str nread wrong\n");
113		      printf ("  inp          \"%s\"\n", data[i].inp);
114		      printf ("  base         %d\n", data[i].base);
115		      printf ("  pre          %d\n", pre);
116		      printf ("  post         %d\n", post);
117		      printf ("  got_nread    %d\n", got_nread);
118		      printf ("  ftell_nread  %ld\n", ftell_nread);
119		      abort ();
120		    }
121		}
122
123	      /* if data[i].inp is a whole string to read and there's no post
124		 whitespace then expect to have EOF */
125	      if (post == 0 && data[i].want_nread == strlen(data[i].inp))
126		{
127		  int  c = getc(fp);
128		  if (c != EOF)
129		    {
130		      printf ("mpz_inp_str didn't read to EOF\n");
131		      printf ("  inp   \"%s\"\n", data[i].inp);
132		      printf ("  base  %d\n", data[i].base);
133		      printf ("  pre   %d\n", pre);
134		      printf ("  post  %d\n", post);
135		      printf ("  c     '%c' %#x\n", c, c);
136		      abort ();
137		    }
138		}
139
140	      /* only expect "pre" included in the count when non-zero */
141	      want_nread = data[i].want_nread;
142	      if (want_nread != 0)
143		want_nread += pre;
144
145	      if (got_nread != want_nread)
146		{
147		  printf ("mpz_inp_str nread wrong\n");
148		  printf ("  inp         \"%s\"\n", data[i].inp);
149		  printf ("  base        %d\n", data[i].base);
150		  printf ("  pre         %d\n", pre);
151		  printf ("  post        %d\n", post);
152		  printf ("  got_nread   %d\n", got_nread);
153		  printf ("  want_nread  %d\n", want_nread);
154		  abort ();
155		}
156
157	      MPZ_CHECK_FORMAT (got);
158
159	      if (mpz_cmp (got, want) != 0)
160		{
161		  printf ("mpz_inp_str wrong result\n");
162		  printf ("  inp   \"%s\"\n", data[i].inp);
163		  printf ("  base  %d\n", data[i].base);
164		  mpz_trace ("  got ",  got);
165		  mpz_trace ("  want", want);
166		  abort ();
167		}
168
169	      ASSERT_ALWAYS (fclose (fp) == 0);
170	    }
171	}
172    }
173
174  mpz_clear (got);
175  mpz_clear (want);
176}
177
178int
179main (void)
180{
181  tests_start ();
182
183  check_data ();
184
185  unlink (FILENAME);
186  tests_end ();
187  exit (0);
188}
189