github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpz/logic.c (about)

     1  /* Test mpz_com, mpz_and, mpz_ior, and mpz_xor.
     2  
     3  Copyright 1993, 1994, 1996, 1997, 2001, 2013 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 <stdio.h>
    21  #include <stdlib.h>
    22  
    23  #include "gmp.h"
    24  #include "gmp-impl.h"
    25  #include "tests.h"
    26  
    27  void dump_abort (void);
    28  void debug_mp (mpz_t, int);
    29  
    30  int
    31  main (int argc, char **argv)
    32  {
    33    mpz_t x, y, r1, r2;
    34    mpz_t t1, t2, t3;
    35    mp_size_t xsize, ysize;
    36    int i;
    37    int reps = 100000;
    38    gmp_randstate_ptr rands;
    39    mpz_t bs;
    40    unsigned long bsi, size_range;
    41  
    42    tests_start ();
    43    rands = RANDS;
    44  
    45    mpz_init (bs);
    46  
    47    if (argc == 2)
    48       reps = atoi (argv[1]);
    49  
    50    mpz_init (x);
    51    mpz_init (y);
    52    mpz_init (r1);
    53    mpz_init (r2);
    54    mpz_init (t1);
    55    mpz_init (t2);
    56    mpz_init (t3);
    57  
    58    mpz_set_si (x, -1);
    59    mpz_set_ui (y, 0);
    60    for (i = 0; i < 300; i++)
    61      {
    62        mpz_mul_2exp (x, x, 1);
    63  
    64        mpz_and (r1, x, x);
    65        MPZ_CHECK_FORMAT (r1);
    66        if (mpz_cmp (r1, x) != 0)
    67  	dump_abort ();
    68  
    69        mpz_ior (r2, x, x);
    70        MPZ_CHECK_FORMAT (r2);
    71        if (mpz_cmp (r2, x) != 0)
    72  	dump_abort ();
    73  
    74        mpz_xor (t1, x, x);
    75        MPZ_CHECK_FORMAT (t1);
    76        if (mpz_cmp_si (t1, 0) != 0)
    77  	dump_abort ();
    78  
    79        mpz_ior (t1, x, y);
    80        MPZ_CHECK_FORMAT (t1);
    81        if (mpz_cmp (t1, x) != 0)
    82  	dump_abort ();
    83  
    84        mpz_xor (t2, x, y);
    85        MPZ_CHECK_FORMAT (t2);
    86        if (mpz_cmp (t2, x) != 0)
    87  	dump_abort ();
    88  
    89        mpz_com (t2, x);
    90        MPZ_CHECK_FORMAT (t2);
    91        mpz_xor (t3, t2, x);
    92        MPZ_CHECK_FORMAT (t3);
    93        if (mpz_cmp_si (t3, -1) != 0)
    94  	dump_abort ();
    95      }
    96  
    97    for (i = 0; i < reps; i++)
    98      {
    99        mpz_urandomb (bs, rands, 32);
   100        size_range = mpz_get_ui (bs) % 8 + 2;
   101  
   102        mpz_urandomb (bs, rands, size_range);
   103        xsize = mpz_get_ui (bs);
   104        mpz_rrandomb (x, rands, xsize);
   105        mpz_urandomb (bs, rands, 1);
   106        bsi = mpz_get_ui (bs);
   107        if ((bsi & 1) != 0)
   108  	mpz_neg (x, x);
   109  
   110        mpz_urandomb (bs, rands, size_range);
   111        ysize = mpz_get_ui (bs);
   112        mpz_rrandomb (y, rands, ysize);
   113        mpz_urandomb (bs, rands, 1);
   114        bsi = mpz_get_ui (bs);
   115        if ((bsi & 1) != 0)
   116  	mpz_neg (y, y);
   117  
   118        mpz_com (r1, x);
   119        MPZ_CHECK_FORMAT (r1);
   120        mpz_com (r1, r1);
   121        MPZ_CHECK_FORMAT (r1);
   122        if (mpz_cmp (r1, x) != 0)
   123  	dump_abort ();
   124  
   125        mpz_com (r1, y);
   126        MPZ_CHECK_FORMAT (r1);
   127        mpz_com (r2, r1);
   128        MPZ_CHECK_FORMAT (r2);
   129        if (mpz_cmp (r2, y) != 0)
   130  	dump_abort ();
   131  
   132        mpz_com (t1, x);
   133        MPZ_CHECK_FORMAT (t1);
   134        mpz_com (t2, y);
   135        MPZ_CHECK_FORMAT (t2);
   136        mpz_and (t3, t1, t2);
   137        MPZ_CHECK_FORMAT (t3);
   138        mpz_com (r1, t3);
   139        MPZ_CHECK_FORMAT (r1);
   140        mpz_ior (r2, x, y);
   141        MPZ_CHECK_FORMAT (r2);
   142        if (mpz_cmp (r1, r2) != 0)
   143  	dump_abort ();
   144  
   145        mpz_com (t1, x);
   146        MPZ_CHECK_FORMAT (t1);
   147        mpz_com (t2, y);
   148        MPZ_CHECK_FORMAT (t2);
   149        mpz_ior (t3, t1, t2);
   150        MPZ_CHECK_FORMAT (t3);
   151        mpz_com (r1, t3);
   152        MPZ_CHECK_FORMAT (r1);
   153        mpz_and (r2, x, y);
   154        MPZ_CHECK_FORMAT (r2);
   155        if (mpz_cmp (r1, r2) != 0)
   156  	dump_abort ();
   157  
   158        mpz_ior (t1, x, y);
   159        MPZ_CHECK_FORMAT (t1);
   160        mpz_and (t2, x, y);
   161        MPZ_CHECK_FORMAT (t2);
   162        mpz_com (t3, t2);
   163        MPZ_CHECK_FORMAT (t3);
   164        mpz_and (r1, t1, t3);
   165        MPZ_CHECK_FORMAT (r1);
   166        mpz_xor (r2, x, y);
   167        MPZ_CHECK_FORMAT (r2);
   168        if (mpz_cmp (r1, r2) != 0)
   169  	dump_abort ();
   170      }
   171  
   172    mpz_clear (bs);
   173    mpz_clear (x);
   174    mpz_clear (y);
   175    mpz_clear (r1);
   176    mpz_clear (r2);
   177    mpz_clear (t1);
   178    mpz_clear (t2);
   179    mpz_clear (t3);
   180  
   181    tests_end ();
   182    exit (0);
   183  }
   184  
   185  void
   186  dump_abort ()
   187  {
   188    abort();
   189  }
   190  
   191  void
   192  debug_mp (mpz_t x, int base)
   193  {
   194    mpz_out_str (stderr, base, x); fputc ('\n', stderr);
   195  }