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

Annotation of OpenXM_contrib/gmp/mpz/tests/bit.c, Revision 1.1.1.1

1.1       maekawa     1: /* Test mpz_setbit, mpz_clrbit, mpz_tstbit.
                      2:
                      3: Copyright (C) 1997 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 <stdio.h>
                     23: #include "gmp.h"
                     24: #include "gmp-impl.h"
                     25: #include "urandom.h"
                     26:
                     27: void debug_mp ();
                     28:
                     29: #ifndef SIZE
                     30: #define SIZE 4
                     31: #endif
                     32:
                     33: main (argc, argv)
                     34:      int argc;
                     35:      char **argv;
                     36: {
                     37:   mpz_t x, s0, s1, s2, s3, m;
                     38:   mp_size_t xsize;
                     39:   int i;
                     40:   int reps = 100000;
                     41:   int bit0, bit1, bit2, bit3;
                     42:   unsigned long int bitindex;
                     43:
                     44:   if (argc == 2)
                     45:      reps = atoi (argv[1]);
                     46:
                     47:   mpz_init (x);
                     48:   mpz_init (s0);
                     49:   mpz_init (s1);
                     50:   mpz_init (s2);
                     51:   mpz_init (s3);
                     52:   mpz_init (m);
                     53:
                     54:   for (i = 0; i < reps; i++)
                     55:     {
                     56:       xsize = urandom () % (2 * SIZE) - SIZE;
                     57:       mpz_random2 (x, xsize);
                     58:       bitindex = urandom () % SIZE;
                     59:
                     60:       mpz_set (s0, x);
                     61:       bit0 = mpz_tstbit (x, bitindex);
                     62:       mpz_setbit (x, bitindex);
                     63:       MPZ_CHECK_FORMAT (x);
                     64:
                     65:       mpz_set (s1, x);
                     66:       bit1 = mpz_tstbit (x, bitindex);
                     67:       mpz_clrbit (x, bitindex);
                     68:       MPZ_CHECK_FORMAT (x);
                     69:
                     70:       mpz_set (s2, x);
                     71:       bit2 = mpz_tstbit (x, bitindex);
                     72:       mpz_setbit (x, bitindex);
                     73:       MPZ_CHECK_FORMAT (x);
                     74:
                     75:       mpz_set (s3, x);
                     76:       bit3 = mpz_tstbit (x, bitindex);
                     77:
                     78:       if (bit1 != 1 || bit2 != 0 || bit3 != 1)
                     79:        abort ();
                     80:
                     81:       if (bit0 == 0)
                     82:        {
                     83:          if (mpz_cmp (s0, s1) == 0 || mpz_cmp (s0, s2) != 0 || mpz_cmp (s0, s3) == 0)
                     84:            abort ();
                     85:        }
                     86:       else
                     87:        {
                     88:          if (mpz_cmp (s0, s1) != 0 || mpz_cmp (s0, s2) == 0 || mpz_cmp (s0, s3) != 0)
                     89:            abort ();
                     90:        }
                     91:
                     92:       if (mpz_cmp (s1, s2) == 0 || mpz_cmp (s1, s3) != 0)
                     93:        abort ();
                     94:       if (mpz_cmp (s2, s3) == 0)
                     95:        abort ();
                     96:
                     97:       mpz_ui_pow_ui (m, 2L, bitindex);
                     98:       MPZ_CHECK_FORMAT (m);
                     99:       mpz_ior (x, s2, m);
                    100:       MPZ_CHECK_FORMAT (x);
                    101:       if (mpz_cmp (x, s3) != 0)
                    102:        abort ();
                    103:
                    104:       mpz_com (m, m);
                    105:       MPZ_CHECK_FORMAT (m);
                    106:       mpz_and (x, s1, m);
                    107:       MPZ_CHECK_FORMAT (x);
                    108:       if (mpz_cmp (x, s2) != 0)
                    109:        abort ();
                    110:     }
                    111:
                    112:   exit (0);
                    113: }
                    114:
                    115: void
                    116: debug_mp (x, base)
                    117:      MP_INT *x;
                    118: {
                    119:   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
                    120: }

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