(root)/
gmp-6.3.0/
tests/
mpq/
t-cmp_ui.c
       1  /* Test mpq_cmp_ui.
       2  
       3  Copyright 1996, 1997, 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-impl.h"
      24  #include "tests.h"
      25  
      26  #define SGN(x) ((x) < 0 ? -1 : (x) > 0 ? 1 : 0)
      27  
      28  int
      29  ref_mpq_cmp_ui (mpq_t a, unsigned long int bn, unsigned long int bd)
      30  {
      31    mpz_t ai, bi;
      32    int cc;
      33  
      34    mpz_init (ai);
      35    mpz_init (bi);
      36  
      37    mpz_mul_ui (ai, NUM (a), bd);
      38    mpz_mul_ui (bi, DEN (a), bn);
      39    cc = mpz_cmp (ai, bi);
      40    mpz_clear (ai);
      41    mpz_clear (bi);
      42    return cc;
      43  }
      44  
      45  #ifndef SIZE
      46  #define SIZE 8	/* increasing this lowers the probability of finding an error */
      47  #endif
      48  
      49  int
      50  main (int argc, char **argv)
      51  {
      52    mpq_t a, b;
      53    mp_size_t size;
      54    int reps = 10000;
      55    int i;
      56    int cc, ccref;
      57    unsigned long int bn, bd;
      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        mpz_random2 (NUM (b), (mp_size_t) 1);
      79        mpz_mod_ui (NUM (b), NUM (b), ~(unsigned long int) 0);
      80        mpz_add_ui (NUM (b), NUM (b), 1);
      81  
      82        mpz_random2 (DEN (b), (mp_size_t) 1);
      83        mpz_mod_ui (DEN (b), DEN (b), ~(unsigned long int) 0);
      84        mpz_add_ui (DEN (b), DEN (b), 1);
      85  
      86        mpq_canonicalize (a);
      87        mpq_canonicalize (b);
      88  
      89        ccref = ref_mpq_cmp_ui (a, 1, 1);
      90        cc = mpq_cmp_ui (a, 1, 1);
      91  
      92        if (SGN (ccref) != SGN (cc))
      93  	abort ();
      94  
      95        ccref = ref_mpq_cmp_ui (a, 0, 1);
      96        cc = mpq_cmp_ui (a, 0, 1);
      97  
      98        if (SGN (ccref) != SGN (cc))
      99  	abort ();
     100  
     101        bn = mpz_get_ui (NUM (b));
     102        bd = mpz_get_ui (DEN (b));
     103  
     104        ccref = ref_mpq_cmp_ui (a, bn, bd);
     105        cc = mpq_cmp_ui (a, bn, bd);
     106  
     107        if (SGN (ccref) != SGN (cc))
     108  	abort ();
     109      }
     110  
     111    mpq_clear (a);
     112    mpq_clear (b);
     113  
     114    tests_end ();
     115    exit (0);
     116  }