[BACK]Return to convert.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / tests / mpz

Annotation of OpenXM_contrib/gmp/tests/mpz/convert.c, Revision 1.1

1.1     ! ohara       1: /* Test conversion using mpz_get_str and mpz_set_str.
        !             2:
        !             3: Copyright 1993, 1994, 1996, 1999, 2000, 2001, 2002 Free Software Foundation,
        !             4: Inc.
        !             5:
        !             6: This file is part of the GNU MP Library.
        !             7:
        !             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 2.1 of the License, or (at your
        !            11: option) any later version.
        !            12:
        !            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.
        !            17:
        !            18: You should have received a copy of the GNU Lesser General Public License
        !            19: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
        !            20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
        !            21: MA 02111-1307, USA. */
        !            22:
        !            23: #include <stdio.h>
        !            24: #include <stdlib.h>
        !            25: #include <string.h> /* for strlen */
        !            26:
        !            27: #include "gmp.h"
        !            28: #include "gmp-impl.h"
        !            29: #include "tests.h"
        !            30:
        !            31: void debug_mp _PROTO ((mpz_t, int));
        !            32:
        !            33: int
        !            34: main (int argc, char **argv)
        !            35: {
        !            36:   mpz_t op1, op2;
        !            37:   mp_size_t size;
        !            38:   int i;
        !            39:   int reps = 10000;
        !            40:   char *str, *buf;
        !            41:   int base;
        !            42:   gmp_randstate_ptr rands;
        !            43:   mpz_t bs;
        !            44:   unsigned long bsi, size_range;
        !            45:
        !            46:   tests_start ();
        !            47:   rands = RANDS;
        !            48:
        !            49:   mpz_init (bs);
        !            50:
        !            51:   if (argc == 2)
        !            52:      reps = atoi (argv[1]);
        !            53:
        !            54:   mpz_init (op1);
        !            55:   mpz_init (op2);
        !            56:
        !            57:   for (i = 0; i < reps; i++)
        !            58:     {
        !            59:       /* 1. Generate random mpz_t and convert to a string and back to mpz_t
        !            60:         again.  */
        !            61:       mpz_urandomb (bs, rands, 32);
        !            62:       size_range = mpz_get_ui (bs) % 12 + 2;   /* 2..13 */
        !            63:       mpz_urandomb (bs, rands, size_range);    /* 3..8191 bits */
        !            64:       size = mpz_get_ui (bs);
        !            65:       mpz_rrandomb (op1, rands, size);
        !            66:
        !            67:       mpz_urandomb (bs, rands, 1);
        !            68:       bsi = mpz_get_ui (bs);
        !            69:       if ((bsi & 1) != 0)
        !            70:        mpz_neg (op1, op1);
        !            71:
        !            72:       mpz_urandomb (bs, rands, 32);
        !            73:       bsi = mpz_get_ui (bs);
        !            74:       base = bsi % 36 + 1;
        !            75:       if (base == 1)
        !            76:        base = 0;
        !            77:
        !            78:       str = mpz_get_str ((char *) 0, base, op1);
        !            79:       mpz_set_str_or_abort (op2, str, base);
        !            80:
        !            81:       if (mpz_cmp (op1, op2))
        !            82:        {
        !            83:          fprintf (stderr, "ERROR, op1 and op2 different in test %d\n", i);
        !            84:          fprintf (stderr, "str  = %s\n", str);
        !            85:          fprintf (stderr, "base = %d\n", base);
        !            86:          fprintf (stderr, "op1  = "); debug_mp (op1, -16);
        !            87:          fprintf (stderr, "op2  = "); debug_mp (op2, -16);
        !            88:          abort ();
        !            89:        }
        !            90:
        !            91:       (*__gmp_free_func) (str, strlen (str) + 1);
        !            92:
        !            93: #if 0
        !            94:       /* 2. Generate random string and convert to mpz_t and back to a string
        !            95:         again.  */
        !            96:       mpz_urandomb (bs, rands, 32);
        !            97:       size_range = mpz_get_ui (bs) % 10 + 2;   /* 2..11 */
        !            98:       mpz_urandomb (bs, rands, size_range);    /* 3..2047 bits */
        !            99:       len = mpz_get_ui (bs);
        !           100:       buf = (*__gmp_allocate_func) (len + 1);
        !           101:       string_urandomb (buf, len, base);
        !           102:       mpz_set_str_or_abort (op1, buf, base);
        !           103:       str = mpz_get_str ((char *) 0, base, op1);
        !           104:
        !           105:       if (strcmp (str, buf) != 0)
        !           106:        {
        !           107:          fprintf (stderr, "ERROR, str and buf different\n");
        !           108:          fprintf (stderr, "str  = %s\n", str);
        !           109:          fprintf (stderr, "buf  = %s\n", buf);
        !           110:          fprintf (stderr, "base = %d\n", base);
        !           111:          fprintf (stderr, "op1  = "); debug_mp (op1, -16);
        !           112:          abort ();
        !           113:        }
        !           114:
        !           115:       (*__gmp_free_func) (buf, len + 1);
        !           116:       (*__gmp_free_func) (str, strlen (str) + 1);
        !           117: #endif
        !           118:     }
        !           119:
        !           120:   mpz_clear (bs);
        !           121:   mpz_clear (op1);
        !           122:   mpz_clear (op2);
        !           123:
        !           124:   tests_end ();
        !           125:   exit (0);
        !           126: }
        !           127:
        !           128: void
        !           129: debug_mp (mpz_t x, int base)
        !           130: {
        !           131:   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
        !           132: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>