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