(root)/
gmp-6.3.0/
tests/
mpz/
dive_ui.c
       1  /* Test mpz_divexact_ui.
       2  
       3  Copyright 1996, 2001 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-impl.h"
      24  #include "tests.h"
      25  
      26  
      27  void
      28  check_random (int argc, char *argv[])
      29  {
      30    gmp_randstate_ptr rands = RANDS;
      31    int    reps = 500000;
      32    mpz_t  a, q, got;
      33    int    i, qneg;
      34    unsigned long  d;
      35  
      36    if (argc == 2)
      37      reps = atoi (argv[1]);
      38  
      39    mpz_init (a);
      40    mpz_init (q);
      41    mpz_init (got);
      42  
      43    for (i = 0; i < reps; i++)
      44      {
      45        do
      46  	d = (unsigned long) urandom();
      47        while (d == 0);
      48        mpz_erandomb (q, rands, 512);
      49        mpz_mul_ui (a, q, d);
      50  
      51        for (qneg = 0; qneg <= 1; qneg++)
      52          {
      53            mpz_divexact_ui (got, a, d);
      54            MPZ_CHECK_FORMAT (got);
      55            if (mpz_cmp (got, q) != 0)
      56              {
      57                printf    ("mpz_divexact_ui wrong\n");
      58                mpz_trace ("    a", a);
      59                printf    ("    d=%lu\n", d);
      60                mpz_trace ("    q", q);
      61                mpz_trace ("  got", got);
      62                abort ();
      63              }
      64  
      65            mpz_neg (q, q);
      66            mpz_neg (a, a);
      67          }
      68  
      69      }
      70  
      71    mpz_clear (a);
      72    mpz_clear (q);
      73    mpz_clear (got);
      74  }
      75  
      76  
      77  int
      78  main (int argc, char **argv)
      79  {
      80    tests_start ();
      81  
      82    check_random (argc, argv);
      83  
      84    tests_end ();
      85    exit (0);
      86  }