(root)/
gmp-6.3.0/
tests/
mpf/
t-cmp_si.c
       1  /* Test mpf_cmp_si and mpf_cmp_z.
       2  
       3  Copyright 2000, 2001, 2004, 2015 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  #include "gmp-impl.h"
      23  #include "tests.h"
      24  
      25  #define SGN(x)       ((x) < 0 ? -1 : (x) == 0 ? 0 : 1)
      26  
      27  void
      28  check_data (void)
      29  {
      30    static const struct {
      31      int         a_base;
      32      const char  *a;
      33      const char  *b;
      34      int         want;
      35    } data[] = {
      36      { 10, "0",  "1", -1 },
      37      { 10, "0",  "0",  0 },
      38      { 10, "0", "-1",  1 },
      39  
      40      { 10, "1",  "1", 0 },
      41      { 10, "1",  "0", 1 },
      42      { 10, "1", "-1", 1 },
      43  
      44      { 10, "-1",  "1", -1 },
      45      { 10, "-1",  "0", -1 },
      46      { 10, "-1", "-1", 0 },
      47  
      48      { 10, "1.5", "2", -1 },
      49      { 10, "1.5", "1",  1 },
      50      { 10, "0.5", "1", -1 },
      51  
      52      { 10, "-1.5", "-2",  1 },
      53      { 10, "-1.5", "-1", -1 },
      54      { 10, "-0.5", "-1",  1 },
      55  
      56      { 16,         "0", "-0x80000000",  1 },
      57      { 16,  "80000000", "-0x80000000",  1 },
      58      { 16,  "80000001", "-0x80000000",  1 },
      59      { 16, "-80000000", "-0x80000000",  0 },
      60      { 16, "-80000001", "-0x80000000", -1 },
      61      { 16, "-FF0080000001", "-0x80000000", -1 },
      62  
      63      { 16,                 "0", "-0x8000000000000000",  1 },
      64      { 16,  "8000000000000000", "-0x8000000000000000",  1 },
      65      { 16,  "8000000000000001", "-0x8000000000000000",  1 },
      66      { 16, "-8000000000000000", "-0x8000000000000000",  0 },
      67      { 16, "-8000000000000000.1", "-0x8000000000000000", -1 },
      68      { 16, "-FF008000000000000001", "-0x8000000000000000", -1 },
      69  
      70      { 16,                 "0", "-0x876543210FEDCBA9876543210000000",  1 },
      71      { 16,  "876543210FEDCBA9876543210000000", "-0x876543210FEDCBA9876543210000000",  1 },
      72      { 16,  "876543210FEDCBA9876543210000001", "-0x876543210FEDCBA9876543210000000",  1 },
      73      { 16, "-876543210FEDCBA9876543210000000", "-0x876543210FEDCBA9876543210000000",  0 },
      74      { 16, "-876543210FEDCBA9876543210000000.1", "-0x876543210FEDCBA9876543210000000", -1 },
      75      { 16, "-FF00876543210FEDCBA9876543210000000", "-0x876543210FEDCBA9876543210000000", -1 },
      76    };
      77  
      78    mpf_t  a;
      79    mpz_t  bz;
      80    long   b;
      81    int    got;
      82    int    i;
      83  
      84    mpf_init2 (a, 128);
      85    mpz_init (bz);
      86    for (i = 0; i < numberof (data); i++)
      87      {
      88        mpf_set_str_or_abort (a, data[i].a, data[i].a_base);
      89        mpz_set_str_or_abort (bz, data[i].b, 0);
      90  
      91        if (mpz_fits_slong_p (bz))
      92          {
      93            b = mpz_get_si (bz);
      94            got = mpf_cmp_si (a, b);
      95            if (SGN (got) != data[i].want)
      96              {
      97                printf ("mpf_cmp_si wrong on data[%d]\n", i);
      98                printf ("  a="); mpf_out_str (stdout, 10, 0, a);
      99                printf (" (%s)\n", data[i].a);
     100                printf ("  b=%ld (%s)\n", b, data[i].b);
     101                printf ("  got=%d\n", got);
     102                printf ("  want=%d\n", data[i].want);
     103                abort();
     104              }
     105          }
     106  
     107        got = mpf_cmp_z (a, bz);
     108        if (SGN (got) != data[i].want)
     109  	{
     110  	  b = mpz_get_si (bz);
     111  	  printf ("mpf_cmp_z wrong on data[%d]\n", i);
     112  	  printf ("  a="); mpf_out_str (stdout, 10, 0, a);
     113  	  printf (" (%s)\n", data[i].a);
     114  	  printf ("  b=%ld (%s)\n", b, data[i].b);
     115  	  printf ("  got=%d\n", got);
     116  	  printf ("  want=%d\n", data[i].want);
     117  	  abort();
     118  	}
     119      }
     120  
     121    mpf_clear (a);
     122    mpz_clear (bz);
     123  }
     124  
     125  int
     126  main (void)
     127  {
     128    tests_start ();
     129  
     130    check_data ();
     131  
     132    tests_end ();
     133    exit (0);
     134  }