convert.c
Upload User: qaz666999
Upload Date: 2022-08-06
Package Size: 2570k
Code Size: 4k
Category:

Algorithm

Development Platform:

Unix_Linux

  1. /* Test conversion using mpz_get_str and mpz_set_str.
  2. Copyright 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2006, 2007 Free Software
  3. Foundation, Inc.
  4. This file is part of the GNU MP Library.
  5. The GNU MP Library is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or (at your
  8. option) any later version.
  9. The GNU MP Library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  12. License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h> /* for strlen */
  18. #include "gmp.h"
  19. #include "gmp-impl.h"
  20. #include "tests.h"
  21. void debug_mp __GMP_PROTO ((mpz_t, int));
  22. void
  23. string_urandomb (char *bp, size_t len, int base, gmp_randstate_ptr rands)
  24. {
  25.   mpz_t bs;
  26.   unsigned long bsi;
  27.   int d, l;
  28.   char *collseq = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  29.   mpz_init (bs);
  30.   mpz_urandomb (bs, rands, 32);
  31.   bsi = mpz_get_ui (bs);
  32.   d = bsi % base;
  33.   while (len != 0)
  34.     {
  35.       l = (bsi >> 16) % 20;
  36.       l = MIN (l, len);
  37.       memset (bp, collseq[d], l);
  38.       len -= l;
  39.       bp += l;
  40.       mpz_urandomb (bs, rands, 32);
  41.       bsi = mpz_get_ui (bs);
  42.       d = bsi & 0xfff;
  43.       if (d >= base)
  44. d = 0;
  45.     }
  46.   bp[0] = '';
  47.   mpz_clear (bs);
  48. }
  49. int
  50. main (int argc, char **argv)
  51. {
  52.   mpz_t op1, op2;
  53.   mp_size_t size;
  54.   int i;
  55.   int reps = 2000;
  56.   char *str, *buf, *bp;
  57.   int base;
  58.   gmp_randstate_ptr rands;
  59.   mpz_t bs;
  60.   unsigned long bsi, size_range;
  61.   size_t len;
  62.   tests_start ();
  63.   TESTS_REPS (reps, argv, argc);
  64.   rands = RANDS;
  65.   mpz_init (bs);
  66.   mpz_init (op1);
  67.   mpz_init (op2);
  68.   for (i = 0; i < reps; i++)
  69.     {
  70.       /* 1. Generate random mpz_t and convert to a string and back to mpz_t
  71.  again.  */
  72.       mpz_urandomb (bs, rands, 32);
  73.       size_range = mpz_get_ui (bs) % 17 + 2; /* 2..18 */
  74.       mpz_urandomb (bs, rands, size_range); /* 3..262144 bits */
  75.       size = mpz_get_ui (bs);
  76.       mpz_rrandomb (op1, rands, size);
  77.       mpz_urandomb (bs, rands, 1);
  78.       bsi = mpz_get_ui (bs);
  79.       if ((bsi & 1) != 0)
  80. mpz_neg (op1, op1);
  81.       mpz_urandomb (bs, rands, 32);
  82.       bsi = mpz_get_ui (bs);
  83.       base = bsi % 62 + 1;
  84.       if (base == 1)
  85. base = 0;
  86.       str = mpz_get_str ((char *) 0, base, op1);
  87.       mpz_set_str_or_abort (op2, str, base);
  88.       if (mpz_cmp (op1, op2))
  89. {
  90.   fprintf (stderr, "ERROR, op1 and op2 different in test %dn", i);
  91.   fprintf (stderr, "str  = %sn", str);
  92.   fprintf (stderr, "base = %dn", base);
  93.   fprintf (stderr, "op1  = "); debug_mp (op1, -16);
  94.   fprintf (stderr, "op2  = "); debug_mp (op2, -16);
  95.   abort ();
  96. }
  97.       (*__gmp_free_func) (str, strlen (str) + 1);
  98.       /* 2. Generate random string and convert to mpz_t and back to a string
  99.  again.  */
  100.       mpz_urandomb (bs, rands, 32);
  101.       size_range = mpz_get_ui (bs) % 16 + 1; /* 1..16 */
  102.       mpz_urandomb (bs, rands, size_range); /* 1..65536 digits */
  103.       len = mpz_get_ui (bs) + 1;
  104.       buf = (*__gmp_allocate_func) (len + 1);
  105.       if (base == 0)
  106. base = 10;
  107.       string_urandomb (buf, len, base, rands);
  108.       mpz_set_str_or_abort (op1, buf, base);
  109.       str = mpz_get_str ((char *) 0, base, op1);
  110.       /* Skip over leading zeros, but don't leave the string at zero length. */
  111.       for (bp = buf; bp[0] == '0' && bp[1] != ''; bp++)
  112. ;
  113.       if (strcasecmp (str, bp) != 0)
  114. {
  115.   fprintf (stderr, "ERROR, str and buf different in test %dn", i);
  116.   fprintf (stderr, "str  = %sn", str);
  117.   fprintf (stderr, "buf  = %sn", buf);
  118.   fprintf (stderr, "base = %dn", base);
  119.   fprintf (stderr, "op1  = "); debug_mp (op1, -16);
  120.   abort ();
  121. }
  122.       (*__gmp_free_func) (buf, len + 1);
  123.       (*__gmp_free_func) (str, strlen (str) + 1);
  124.     }
  125.   mpz_clear (bs);
  126.   mpz_clear (op1);
  127.   mpz_clear (op2);
  128.   tests_end ();
  129.   exit (0);
  130. }
  131. void
  132. debug_mp (mpz_t x, int base)
  133. {
  134.   mpz_out_str (stderr, base, x); fputc ('n', stderr);
  135. }