1/* Test mpq_inp_str.
2
3Copyright 2001, 2002 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library test suite.
6
7The GNU MP Library test suite is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 3 of the License,
10or (at your option) any later version.
11
12The GNU MP Library test suite is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15Public License for more details.
16
17You should have received a copy of the GNU General Public License along with
18the GNU MP Library test suite.  If not, see https://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-impl.h"
30#include "tests.h"
31
32
33#define FILENAME  "t-inp_str.tmp"
34
35
36void
37check_data (void)
38{
39  static const struct {
40    const char  *inp;
41    int         base;
42    const char  *want;
43    int         want_nread;
44
45  } data[] = {
46
47    { "0",   10, "0", 1 },
48    { "0/1", 10, "0", 3 },
49
50    { "0/",   10, "0", 0 },
51    { "/123", 10, "0", 0 },
52    { "blah", 10, "0", 0 },
53    { "123/blah", 10, "0", 0 },
54    { "5 /8", 10, "5", 1 },
55    { "5/ 8", 10, "0", 0 },
56
57    {  "ff", 16,  "255", 2 },
58    { "-ff", 16, "-255", 3 },
59    {  "FF", 16,  "255", 2 },
60    { "-FF", 16, "-255", 3 },
61
62    { "z", 36, "35", 1 },
63    { "Z", 36, "35", 1 },
64
65    {  "0x0",    0,   "0", 3 },
66    {  "0x10",   0,  "16", 4 },
67    { "-0x0",    0,   "0", 4 },
68    { "-0x10",   0, "-16", 5 },
69    { "-0x10/5", 0, "-16/5", 7 },
70
71    {  "00",   0,  "0", 2 },
72    {  "010",  0,  "8", 3 },
73    { "-00",   0,  "0", 3 },
74    { "-010",  0, "-8", 4 },
75  };
76
77  mpq_t  got, want;
78  long   ftell_nread;
79  int    i, post, j, got_nread;
80  FILE   *fp;
81
82  mpq_init (got);
83  mpq_init (want);
84
85  for (i = 0; i < numberof (data); i++)
86    {
87      for (post = 0; post <= 2; post++)
88	{
89	  mpq_set_str_or_abort (want, data[i].want, 0);
90	  MPQ_CHECK_FORMAT (want);
91
92	  fp = fopen (FILENAME, "w+");
93	  ASSERT_ALWAYS (fp != NULL);
94	  fputs (data[i].inp, fp);
95	  for (j = 0; j < post; j++)
96	    putc (' ', fp);
97	  fflush (fp);
98	  ASSERT_ALWAYS (! ferror(fp));
99
100	  rewind (fp);
101	  got_nread = mpq_inp_str (got, fp, data[i].base);
102
103	  if (got_nread != 0)
104	    {
105	      ftell_nread = ftell (fp);
106	      if (got_nread != ftell_nread)
107		{
108		  printf ("mpq_inp_str nread wrong\n");
109		  printf ("  inp          \"%s\"\n", data[i].inp);
110		  printf ("  base         %d\n", data[i].base);
111		  printf ("  got_nread    %d\n", got_nread);
112		  printf ("  ftell_nread  %ld\n", ftell_nread);
113		  abort ();
114		}
115	    }
116
117	  if (post == 0 && data[i].want_nread == strlen(data[i].inp))
118	    {
119	      int  c = getc(fp);
120	      if (c != EOF)
121		{
122		  printf ("mpq_inp_str didn't read to EOF\n");
123		  printf ("  inp         \"%s\"\n", data[i].inp);
124		  printf ("  base        %d\n", data[i].base);
125		  printf ("  c '%c' %#x\n", c, c);
126		  abort ();
127		}
128	    }
129
130	  if (got_nread != data[i].want_nread)
131	    {
132	      printf ("mpq_inp_str nread wrong\n");
133	      printf ("  inp         \"%s\"\n", data[i].inp);
134	      printf ("  base        %d\n", data[i].base);
135	      printf ("  got_nread   %d\n", got_nread);
136	      printf ("  want_nread  %d\n", data[i].want_nread);
137	      abort ();
138	    }
139
140	  MPQ_CHECK_FORMAT (got);
141
142	  if (! mpq_equal (got, want))
143	    {
144	      printf ("mpq_inp_str wrong result\n");
145	      printf ("  inp   \"%s\"\n", data[i].inp);
146	      printf ("  base  %d\n", data[i].base);
147	      mpq_trace ("  got ",  got);
148	      mpq_trace ("  want", want);
149	      abort ();
150	    }
151
152	  ASSERT_ALWAYS (fclose (fp) == 0);
153	}
154    }
155
156  mpq_clear (got);
157  mpq_clear (want);
158}
159
160int
161main (void)
162{
163  tests_start ();
164
165  check_data ();
166
167  unlink (FILENAME);
168  tests_end ();
169
170  exit (0);
171}
172