(root)/
gmp-6.3.0/
mini-gmp/
tests/
t-bitops.c
       1  /*
       2  
       3  Copyright 2012, Free Software Foundation, Inc.
       4  
       5  This file is part of the GNU MP Library test suite.
       6  
       7  The GNU MP Library test suite is free software; you can redistribute it
       8  and/or modify it under the terms of the GNU General Public License as
       9  published by the Free Software Foundation; either version 3 of the License,
      10  or (at your option) any later version.
      11  
      12  The GNU MP Library test suite is distributed in the hope that it will be
      13  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
      14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
      15  Public License for more details.
      16  
      17  You should have received a copy of the GNU General Public License along with
      18  the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
      19  
      20  #include <limits.h>
      21  #include <stdlib.h>
      22  #include <stdio.h>
      23  
      24  #include "testutils.h"
      25  
      26  #define MAXBITS 400
      27  #define COUNT 10000
      28  
      29  void
      30  testmain (int argc, char **argv)
      31  {
      32    unsigned i;
      33    mpz_t a, res, ref;
      34    mp_bitcnt_t b;
      35  
      36    mpz_init (a);
      37    mpz_init (res);
      38    mpz_init (ref);
      39  
      40    for (i = 0; i < COUNT; i++)
      41      {
      42        mini_random_bit_op (OP_SETBIT, MAXBITS, a, &b, ref);
      43        mpz_set (res, a);
      44        mpz_setbit (res, b);
      45        if (mpz_cmp (res, ref))
      46  	{
      47  	  fprintf (stderr, "mpz_setbit failed:\n");
      48  	  dump ("a", a);
      49  	  fprintf (stderr, "b: %lu\n", b);
      50  	  dump ("r", res);
      51  	  dump ("ref", ref);
      52  	  abort ();
      53  	}
      54        if (!mpz_tstbit (res, b))
      55  	{
      56  	  fprintf (stderr, "mpz_tstbit failed (after mpz_setbit):\n");
      57  	  dump ("res", a);
      58  	  fprintf (stderr, "b: %lu\n", b);
      59  	  abort ();
      60  	}
      61        mini_random_bit_op (OP_CLRBIT, MAXBITS, a, &b, ref);
      62        mpz_set (res, a);
      63        mpz_clrbit (res, b);
      64        if (mpz_cmp (res, ref))
      65  	{
      66  	  fprintf (stderr, "mpz_clrbit failed:\n");
      67  	  dump ("a", a);
      68  	  fprintf (stderr, "b: %lu\n", b);
      69  	  dump ("r", res);
      70  	  dump ("ref", ref);
      71  	  abort ();
      72  	}
      73        if (mpz_tstbit (res, b))
      74  	{
      75  	  fprintf (stderr, "mpz_tstbit failed (after mpz_clrbit):\n");
      76  	  dump ("res", a);
      77  	  fprintf (stderr, "b: %lu\n", b);
      78  	  abort ();
      79  	}
      80        mini_random_bit_op (OP_COMBIT, MAXBITS, a, &b, ref);
      81        mpz_set (res, a);
      82        mpz_com (a, a);
      83        mpz_combit (res, b);
      84        if (mpz_cmp (res, ref))
      85  	{
      86  	  fprintf (stderr, "mpz_combit failed:\n");
      87  	  dump ("a", a);
      88  	  fprintf (stderr, "b: %lu\n", b);
      89  	  dump ("r", res);
      90  	  dump ("ref", ref);
      91  	  abort ();
      92  	}
      93        if (mpz_tstbit (res, b) != mpz_tstbit (a, b))
      94  	{
      95  	  fprintf (stderr, "mpz_tstbit failed (after mpz_combit):\n");
      96  	  dump ("res", a);
      97  	  fprintf (stderr, "b: %lu\n", b);
      98  	  abort ();
      99  	}
     100      }
     101    mpz_clear (a);
     102    mpz_clear (res);
     103    mpz_clear (ref);
     104  }