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