summaryrefslogtreecommitdiff
path: root/tests/mpz
diff options
context:
space:
mode:
authorKevin Ryde <user42@zip.com.au>2001-08-14 02:12:26 +0200
committerKevin Ryde <user42@zip.com.au>2001-08-14 02:12:26 +0200
commit3320cf1ecc61db80e80f8855106a9f74ff861a5b (patch)
treeaaf28a0dff6624ced9d84a887de8cec7ddc0adfd /tests/mpz
parent19b017f554da15ed9be29628ebcc8f800009b765 (diff)
downloadgmp-3320cf1ecc61db80e80f8855106a9f74ff861a5b.tar.gz
* tests/mpz/t-inp_str.c: New file.
Diffstat (limited to 'tests/mpz')
-rw-r--r--tests/mpz/t-inp_str.c185
1 files changed, 185 insertions, 0 deletions
diff --git a/tests/mpz/t-inp_str.c b/tests/mpz/t-inp_str.c
new file mode 100644
index 000000000..405c719a0
--- /dev/null
+++ b/tests/mpz/t-inp_str.c
@@ -0,0 +1,185 @@
+/* Test mpz_inp_str.
+
+Copyright 2001 Free Software Foundation, Inc.
+
+This file is part of the GNU MP Library.
+
+The GNU MP Library is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or (at your
+option) any later version.
+
+The GNU MP Library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with the GNU MP Library; see the file COPYING.LIB. If not, write to
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+MA 02111-1307, USA. */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#if HAVE_UNISTD_H
+#include <unistd.h> /* for unlink */
+#endif
+
+#include "gmp.h"
+#include "gmp-impl.h"
+#include "tests.h"
+
+
+#define FILENAME "t-inp_str.tmp"
+
+
+void
+check_data (void)
+{
+ static const struct {
+ const char *inp;
+ int base;
+ const char *want;
+ int want_nread;
+
+ } data[] = {
+
+ { "0", 10, "0", 1 },
+
+ { "abc", 10, "0", 0 },
+ { "ghi", 16, "0", 0 },
+
+ { "ff", 16, "255", 2 },
+ { "-ff", 16, "-255", 3 },
+ { "FF", 16, "255", 2 },
+ { "-FF", 16, "-255", 3 },
+
+ { "z", 36, "35", 1 },
+ { "Z", 36, "35", 1 },
+
+ { "0x0", 0, "0", 3 },
+ { "0x10", 0, "16", 4 },
+ { "-0x0", 0, "0", 4 },
+ { "-0x10", 0, "-16", 5 },
+
+ { "00", 0, "0", 2 },
+ { "010", 0, "8", 3 },
+ { "-00", 0, "0", 3 },
+ { "-010", 0, "-8", 4 },
+
+ { "0x", 0, "0", 2 },
+ { "0", 0, "0", 1 },
+ };
+
+ mpz_t got, want;
+ long ftell_nread;
+ int i, pre, post, j, got_nread, want_nread;
+ FILE *fp;
+
+ mpz_init (got);
+ mpz_init (want);
+
+ for (i = 0; i < numberof (data); i++)
+ {
+ for (pre = 0; pre <= 3; pre++)
+ {
+ for (post = 0; post <= 2; post++)
+ {
+ mpz_set_str_or_abort (want, data[i].want, 0);
+ MPZ_CHECK_FORMAT (want);
+
+ /* create the file new each time to ensure its length is what
+ we want */
+ ASSERT_ALWAYS ((fp = fopen (FILENAME, "w+")) != NULL);
+ for (j = 0; j < pre; j++)
+ putc (' ', fp);
+ fputs (data[i].inp, fp);
+ for (j = 0; j < post; j++)
+ putc (' ', fp);
+ fflush (fp);
+ ASSERT_ALWAYS (! ferror(fp));
+
+ rewind (fp);
+ got_nread = mpz_inp_str (got, fp, data[i].base);
+
+ if (got_nread != 0)
+ {
+ ftell_nread = ftell (fp);
+ if (got_nread != ftell_nread)
+ {
+ printf ("mpz_inp_str nread wrong\n");
+ printf (" inp \"%s\"\n", data[i].inp);
+ printf (" base %d\n", data[i].base);
+ printf (" got_nread %d\n", got_nread);
+ printf (" ftell_nread %ld\n", ftell_nread);
+ abort ();
+ }
+ }
+
+ /* if data[i].inp is a whole string to read and there's no post
+ whitespace then expect to have EOF */
+ if (post == 0 && data[i].want_nread == strlen(data[i].inp))
+ {
+ int c = getc(fp);
+ if (c != EOF)
+ {
+ printf ("mpz_inp_str didn't read to EOF\n");
+ printf (" inp \"%s\"\n", data[i].inp);
+ printf (" base %d\n", data[i].base);
+ printf (" c '%c' %#x\n", c, c);
+ abort ();
+ }
+ }
+
+ /* only expect "pre" included in the count when non-zero */
+ want_nread = data[i].want_nread;
+ if (want_nread != 0)
+ want_nread += pre;
+
+ if (got_nread != want_nread)
+ {
+ printf ("mpz_inp_str nread wrong\n");
+ printf (" inp \"%s\"\n", data[i].inp);
+ printf (" base %d\n", data[i].base);
+ printf (" pre %d\n", pre);
+ printf (" post %d\n", post);
+ printf (" got_nread %d\n", got_nread);
+ printf (" want_nread %d\n", want_nread);
+ abort ();
+ }
+
+ MPZ_CHECK_FORMAT (got);
+
+ if (mpz_cmp (got, want) != 0)
+ {
+ printf ("mpz_inp_str wrong result\n");
+ printf (" inp \"%s\"\n", data[i].inp);
+ printf (" base %d\n", data[i].base);
+ mpz_trace (" got ", got);
+ mpz_trace (" want", want);
+ abort ();
+ }
+
+ ASSERT_ALWAYS (fclose (fp) == 0);
+ }
+ }
+ }
+
+ mpz_clear (got);
+ mpz_clear (want);
+}
+
+int
+main (void)
+{
+ tests_start ();
+
+ check_data ();
+
+ unlink (FILENAME);
+ tests_end ();
+ exit (0);
+}