1 /* Test conversion using mpz_get_str and mpz_set_str.
3 Copyright 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2006, 2007 Free Software
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
23 #include <string.h> /* for strlen */
29 void debug_mp __GMP_PROTO ((mpz_t, int));
33 string_urandomb (char *bp, size_t len, int base, gmp_randstate_ptr rands)
38 char *collseq = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
42 mpz_urandomb (bs, rands, 32);
43 bsi = mpz_get_ui (bs);
50 memset (bp, collseq[d], l);
55 mpz_urandomb (bs, rands, 32);
56 bsi = mpz_get_ui (bs);
67 main (int argc, char **argv)
75 gmp_randstate_ptr rands;
77 unsigned long bsi, size_range;
81 TESTS_REPS (reps, argv, argc);
90 for (i = 0; i < reps; i++)
92 /* 1. Generate random mpz_t and convert to a string and back to mpz_t
94 mpz_urandomb (bs, rands, 32);
95 size_range = mpz_get_ui (bs) % 17 + 2; /* 2..18 */
96 mpz_urandomb (bs, rands, size_range); /* 3..262144 bits */
97 size = mpz_get_ui (bs);
98 mpz_rrandomb (op1, rands, size);
100 mpz_urandomb (bs, rands, 1);
101 bsi = mpz_get_ui (bs);
105 mpz_urandomb (bs, rands, 32);
106 bsi = mpz_get_ui (bs);
111 str = mpz_get_str ((char *) 0, base, op1);
112 mpz_set_str_or_abort (op2, str, base);
114 if (mpz_cmp (op1, op2))
116 fprintf (stderr, "ERROR, op1 and op2 different in test %d\n", i);
117 fprintf (stderr, "str = %s\n", str);
118 fprintf (stderr, "base = %d\n", base);
119 fprintf (stderr, "op1 = "); debug_mp (op1, -16);
120 fprintf (stderr, "op2 = "); debug_mp (op2, -16);
124 (*__gmp_free_func) (str, strlen (str) + 1);
126 /* 2. Generate random string and convert to mpz_t and back to a string
128 mpz_urandomb (bs, rands, 32);
129 size_range = mpz_get_ui (bs) % 16 + 1; /* 1..16 */
130 mpz_urandomb (bs, rands, size_range); /* 1..65536 digits */
131 len = mpz_get_ui (bs) + 1;
132 buf = (*__gmp_allocate_func) (len + 1);
135 string_urandomb (buf, len, base, rands);
137 mpz_set_str_or_abort (op1, buf, base);
138 str = mpz_get_str ((char *) 0, base, op1);
140 /* Skip over leading zeros, but don't leave the string at zero length. */
141 for (bp = buf; bp[0] == '0' && bp[1] != '\0'; bp++)
144 if (strcasecmp (str, bp) != 0)
146 fprintf (stderr, "ERROR, str and buf different in test %d\n", i);
147 fprintf (stderr, "str = %s\n", str);
148 fprintf (stderr, "buf = %s\n", buf);
149 fprintf (stderr, "base = %d\n", base);
150 fprintf (stderr, "op1 = "); debug_mp (op1, -16);
154 (*__gmp_free_func) (buf, len + 1);
155 (*__gmp_free_func) (str, strlen (str) + 1);
167 debug_mp (mpz_t x, int base)
169 mpz_out_str (stderr, base, x); fputc ('\n', stderr);