Annotation of OpenXM_contrib/gmp/tests/mpq/t-inp_str.c, Revision 1.1.1.1
1.1 ohara 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 2.1 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; see the file COPYING.LIB. If not, write to
19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20: MA 02111-1307, USA. */
21:
22: #include "config.h"
23:
24: #include <stdio.h>
25: #include <stdlib.h>
26: #include <string.h>
27: #if HAVE_UNISTD_H
28: #include <unistd.h> /* for unlink */
29: #endif
30:
31: #include "gmp.h"
32: #include "gmp-impl.h"
33: #include "tests.h"
34:
35:
36: #define FILENAME "t-inp_str.tmp"
37:
38:
39: void
40: check_data (void)
41: {
42: static const struct {
43: const char *inp;
44: int base;
45: const char *want;
46: int want_nread;
47:
48: } data[] = {
49:
50: { "0", 10, "0", 1 },
51: { "0/1", 10, "0", 3 },
52:
53: { "0/", 10, "0", 0 },
54: { "/123", 10, "0", 0 },
55: { "blah", 10, "0", 0 },
56: { "123/blah", 10, "0", 0 },
57: { "5 /8", 10, "5", 1 },
58: { "5/ 8", 10, "0", 0 },
59:
60: { "ff", 16, "255", 2 },
61: { "-ff", 16, "-255", 3 },
62: { "FF", 16, "255", 2 },
63: { "-FF", 16, "-255", 3 },
64:
65: { "z", 36, "35", 1 },
66: { "Z", 36, "35", 1 },
67:
68: { "0x0", 0, "0", 3 },
69: { "0x10", 0, "16", 4 },
70: { "-0x0", 0, "0", 4 },
71: { "-0x10", 0, "-16", 5 },
72: { "-0x10/5", 0, "-16/5", 7 },
73:
74: { "00", 0, "0", 2 },
75: { "010", 0, "8", 3 },
76: { "-00", 0, "0", 3 },
77: { "-010", 0, "-8", 4 },
78: };
79:
80: mpq_t got, want;
81: long ftell_nread;
82: int i, post, j, got_nread;
83: FILE *fp;
84:
85: mpq_init (got);
86: mpq_init (want);
87:
88: for (i = 0; i < numberof (data); i++)
89: {
90: for (post = 0; post <= 2; post++)
91: {
92: mpq_set_str_or_abort (want, data[i].want, 0);
93: MPQ_CHECK_FORMAT (want);
94:
95: fp = fopen (FILENAME, "w+");
96: ASSERT_ALWAYS (fp != NULL);
97: fputs (data[i].inp, fp);
98: for (j = 0; j < post; j++)
99: putc (' ', fp);
100: fflush (fp);
101: ASSERT_ALWAYS (! ferror(fp));
102:
103: rewind (fp);
104: got_nread = mpq_inp_str (got, fp, data[i].base);
105:
106: if (got_nread != 0)
107: {
108: ftell_nread = ftell (fp);
109: if (got_nread != ftell_nread)
110: {
111: printf ("mpq_inp_str nread wrong\n");
112: printf (" inp \"%s\"\n", data[i].inp);
113: printf (" base %d\n", data[i].base);
114: printf (" got_nread %d\n", got_nread);
115: printf (" ftell_nread %ld\n", ftell_nread);
116: abort ();
117: }
118: }
119:
120: if (post == 0 && data[i].want_nread == strlen(data[i].inp))
121: {
122: int c = getc(fp);
123: if (c != EOF)
124: {
125: printf ("mpq_inp_str didn't read to EOF\n");
126: printf (" inp \"%s\"\n", data[i].inp);
127: printf (" base %d\n", data[i].base);
128: printf (" c '%c' %#x\n", c, c);
129: abort ();
130: }
131: }
132:
133: if (got_nread != data[i].want_nread)
134: {
135: printf ("mpq_inp_str nread wrong\n");
136: printf (" inp \"%s\"\n", data[i].inp);
137: printf (" base %d\n", data[i].base);
138: printf (" got_nread %d\n", got_nread);
139: printf (" want_nread %d\n", data[i].want_nread);
140: abort ();
141: }
142:
143: MPQ_CHECK_FORMAT (got);
144:
145: if (! mpq_equal (got, want))
146: {
147: printf ("mpq_inp_str wrong result\n");
148: printf (" inp \"%s\"\n", data[i].inp);
149: printf (" base %d\n", data[i].base);
150: mpq_trace (" got ", got);
151: mpq_trace (" want", want);
152: abort ();
153: }
154:
155: ASSERT_ALWAYS (fclose (fp) == 0);
156: }
157: }
158:
159: mpq_clear (got);
160: mpq_clear (want);
161: }
162:
163: int
164: main (void)
165: {
166: tests_start ();
167:
168: check_data ();
169:
170: unlink (FILENAME);
171: tests_end ();
172:
173: exit (0);
174: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>