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

     1  /* Test mpq_cmp.
     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.h"
    24  #include "gmp-impl.h"
    25  #include "tests.h"
    26  
    27  #define SGN(x) ((x) < 0 ? -1 : (x) > 0 ? 1 : 0)
    28  
    29  int
    30  ref_mpq_cmp (mpq_t a, mpq_t b)
    31  {
    32    mpz_t ai, bi;
    33    int cc;
    34  
    35    mpz_init (ai);
    36    mpz_init (bi);
    37  
    38    mpz_mul (ai, NUM (a), DEN (b));
    39    mpz_mul (bi, NUM (b), DEN (a));
    40    cc = mpz_cmp (ai, bi);
    41    mpz_clear (ai);
    42    mpz_clear (bi);
    43    return cc;
    44  }
    45  
    46  #ifndef SIZE
    47  #define SIZE 8	/* increasing this lowers the probability of finding an error */
    48  #endif
    49  
    50  int
    51  main (int argc, char **argv)
    52  {
    53    mpq_t a, b;
    54    mp_size_t size;
    55    int reps = 10000;
    56    int i;
    57    int cc, ccref;
    58  
    59    tests_start ();
    60  
    61    if (argc == 2)
    62       reps = atoi (argv[1]);
    63  
    64    mpq_init (a);
    65    mpq_init (b);
    66  
    67    for (i = 0; i < reps; i++)
    68      {
    69        size = urandom () % SIZE - SIZE/2;
    70        mpz_random2 (NUM (a), size);
    71        do
    72  	{
    73  	  size = urandom () % SIZE - SIZE/2;
    74  	  mpz_random2 (DEN (a), size);
    75  	}
    76        while (mpz_cmp_ui (DEN (a), 0) == 0);
    77  
    78        size = urandom () % SIZE - SIZE/2;
    79        mpz_random2 (NUM (b), size);
    80        do
    81  	{
    82  	  size = urandom () % SIZE - SIZE/2;
    83  	  mpz_random2 (DEN (b), size);
    84  	}
    85        while (mpz_cmp_ui (DEN (b), 0) == 0);
    86  
    87        mpq_canonicalize (a);
    88        mpq_canonicalize (b);
    89  
    90        ccref = ref_mpq_cmp (a, b);
    91        cc = mpq_cmp (a, b);
    92  
    93        if (SGN (ccref) != SGN (cc))
    94  	abort ();
    95      }
    96  
    97    mpq_clear (a);
    98    mpq_clear (b);
    99  
   100    tests_end ();
   101    exit (0);
   102  }