github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mini-gmp/tests/t-div_2exp.c (about)

     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 <assert.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  typedef void div_func (mpz_t, const mpz_t, mp_bitcnt_t);
    30  
    31  void
    32  testmain (int argc, char **argv)
    33  {
    34    unsigned i;
    35    mpz_t a, res, ref;
    36    mp_bitcnt_t b;
    37  
    38    mpz_init (a);
    39    mpz_init (res);
    40    mpz_init (ref);
    41  
    42    for (i = 0; i < COUNT; i++)
    43      {
    44        unsigned j;
    45        for (j = 0; j < 6; j++)
    46  	{
    47  	  static const enum hex_random_op ops[6] =
    48  	    {
    49  	      OP_CDIV_Q_2, OP_CDIV_R_2,
    50  	      OP_FDIV_Q_2, OP_FDIV_R_2,
    51  	      OP_TDIV_Q_2, OP_TDIV_R_2
    52  	    };
    53  	  static const char *name[6] =
    54  	    {
    55  	      "cdiv_q", "cdiv_r",
    56  	      "fdiv_q", "fdiv_r",
    57  	      "tdiv_q", "tdiv_r"
    58  	    };
    59  	  static div_func * const div [6] =
    60  	    {
    61  	      mpz_cdiv_q_2exp, mpz_cdiv_r_2exp,
    62  	      mpz_fdiv_q_2exp, mpz_fdiv_r_2exp,
    63  	      mpz_tdiv_q_2exp, mpz_tdiv_r_2exp
    64  	    };
    65  
    66  	  mini_random_bit_op (ops[j], MAXBITS, a, &b, ref);
    67  	  div[j] (res, a, b);
    68  	  if (mpz_cmp (ref, res))
    69  	    {
    70  	      fprintf (stderr, "mpz_%s_2exp failed:\n", name[j]);
    71  	      dump ("a", a);
    72  	      fprintf (stderr, "b: %lu\n", b);
    73  	      dump ("r", res);
    74  	      dump ("ref", ref);
    75  	      abort ();
    76  	    }
    77  	}
    78      }
    79    mpz_clear (a);
    80    mpz_clear (res);
    81    mpz_clear (ref);
    82  }