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

     1  /* Test mpz_mul, mpz_divexact.
     2  
     3  Copyright 1996, 2001, 2002 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  int
    28  main (int argc, char **argv)
    29  {
    30    mpz_t op1, op2;
    31    mpz_t prod, quot;
    32    mp_size_t size;
    33    int i;
    34    int reps = 5000;
    35    gmp_randstate_ptr rands;
    36    mpz_t bs;
    37    unsigned long bsi, size_range;
    38  
    39    tests_start ();
    40    TESTS_REPS (reps, argv, argc);
    41  
    42    rands = RANDS;
    43  
    44    mp_trace_base = -16;
    45  
    46    mpz_init (bs);
    47  
    48    mpz_init (op1);
    49    mpz_init (op2);
    50    mpz_init (prod);
    51    mpz_init (quot);
    52  
    53    for (i = 0; i < reps; i++)
    54      {
    55        mpz_urandomb (bs, rands, 32);
    56        size_range = mpz_get_ui (bs) % 17 + 2; /* 0..2047 bit operands */
    57  
    58        mpz_urandomb (bs, rands, size_range);
    59        size = mpz_get_ui (bs);
    60        mpz_rrandomb (op1, rands, size);
    61  
    62        do
    63  	{
    64  	  mpz_urandomb (bs, rands, size_range);
    65  	  size = mpz_get_ui (bs);
    66  	  mpz_rrandomb (op2, rands, size);
    67  	}
    68        while (mpz_sgn (op2) == 0);
    69  
    70        mpz_urandomb (bs, rands, 2);
    71        bsi = mpz_get_ui (bs);
    72        if ((bsi & 1) != 0)
    73  	mpz_neg (op1, op1);
    74        if ((bsi & 2) != 0)
    75  	mpz_neg (op2, op2);
    76  
    77        mpz_mul (prod, op1, op2);
    78  
    79        mpz_divexact (quot, prod, op2);
    80        MPZ_CHECK_FORMAT (quot);
    81  
    82        if (mpz_cmp (quot, op1) != 0)
    83          {
    84            printf ("Wrong results:\n");
    85            mpz_trace ("  got     ", quot);
    86            mpz_trace ("  want    ", op1);
    87            mpz_trace ("  dividend", prod);
    88            mpz_trace ("  divisor ", op2);
    89            abort ();
    90          }
    91      }
    92  
    93    mpz_clear (bs);
    94    mpz_clear (op1);
    95    mpz_clear (op2);
    96    mpz_clear (prod);
    97    mpz_clear (quot);
    98  
    99    tests_end ();
   100    exit (0);
   101  }