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

     1  /* Test mpq_set_f.
     2  
     3  Copyright 2000, 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  #include "gmp.h"
    23  #include "gmp-impl.h"
    24  #include "tests.h"
    25  
    26  int
    27  main (int argc, char **argv)
    28  {
    29  #if GMP_NAIL_BITS == 0
    30    static const struct {
    31      int         f_base;
    32      const char  *f;
    33      int         z_base;
    34      const char  *want_num;
    35      const char  *want_den;
    36  
    37    } data[] = {
    38  
    39      { -2, "0",    16, "0", "1" },
    40      { -2, "1",    16, "1", "1" },
    41      { -2, "1@1",  16, "2", "1" },
    42      { -2, "1@2",  16, "4", "1" },
    43      { -2, "1@3",  16, "8", "1" },
    44  
    45      { -2, "1@30", 16,  "40000000", "1" },
    46      { -2, "1@31", 16,  "80000000", "1" },
    47      { -2, "1@32", 16, "100000000", "1" },
    48      { -2, "1@33", 16, "200000000", "1" },
    49      { -2, "1@34", 16, "400000000", "1" },
    50  
    51      { -2, "1@62", 16,  "4000000000000000", "1" },
    52      { -2, "1@63", 16,  "8000000000000000", "1" },
    53      { -2, "1@64", 16, "10000000000000000", "1" },
    54      { -2, "1@65", 16, "20000000000000000", "1" },
    55      { -2, "1@66", 16, "40000000000000000", "1" },
    56  
    57      { -2, "1@126", 16,  "40000000000000000000000000000000", "1" },
    58      { -2, "1@127", 16,  "80000000000000000000000000000000", "1" },
    59      { -2, "1@128", 16, "100000000000000000000000000000000", "1" },
    60      { -2, "1@129", 16, "200000000000000000000000000000000", "1" },
    61      { -2, "1@130", 16, "400000000000000000000000000000000", "1" },
    62  
    63      { -2, "1@-1",  16, "1", "2" },
    64      { -2, "1@-2",  16, "1", "4" },
    65      { -2, "1@-3",  16, "1", "8" },
    66  
    67      { -2, "1@-30", 16, "1",  "40000000" },
    68      { -2, "1@-31", 16, "1",  "80000000" },
    69      { -2, "1@-32", 16, "1", "100000000" },
    70      { -2, "1@-33", 16, "1", "200000000" },
    71      { -2, "1@-34", 16, "1", "400000000" },
    72  
    73      { -2, "1@-62", 16, "1",  "4000000000000000" },
    74      { -2, "1@-63", 16, "1",  "8000000000000000" },
    75      { -2, "1@-64", 16, "1", "10000000000000000" },
    76      { -2, "1@-65", 16, "1", "20000000000000000" },
    77      { -2, "1@-66", 16, "1", "40000000000000000" },
    78  
    79      { -2, "1@-126", 16, "1",  "40000000000000000000000000000000" },
    80      { -2, "1@-127", 16, "1",  "80000000000000000000000000000000" },
    81      { -2, "1@-128", 16, "1", "100000000000000000000000000000000" },
    82      { -2, "1@-129", 16, "1", "200000000000000000000000000000000" },
    83      { -2, "1@-130", 16, "1", "400000000000000000000000000000000" },
    84  
    85      { -2, "1@-30", 16, "1",  "40000000" },
    86      { -2, "1@-31", 16, "1",  "80000000" },
    87      { -2, "1@-32", 16, "1", "100000000" },
    88      { -2, "1@-33", 16, "1", "200000000" },
    89      { -2, "1@-34", 16, "1", "400000000" },
    90  
    91      { -2, "11@-62", 16, "3",  "4000000000000000" },
    92      { -2, "11@-63", 16, "3",  "8000000000000000" },
    93      { -2, "11@-64", 16, "3", "10000000000000000" },
    94      { -2, "11@-65", 16, "3", "20000000000000000" },
    95      { -2, "11@-66", 16, "3", "40000000000000000" },
    96  
    97      { 16, "80000000.00000001", 16, "8000000000000001", "100000000" },
    98      { 16, "80000000.00000008", 16, "1000000000000001",  "20000000" },
    99      { 16, "80000000.8",        16, "100000001", "2" },
   100  
   101    };
   102  
   103    mpf_t  f;
   104    mpq_t  got;
   105    mpz_t  want_num, want_den;
   106    int    i, neg;
   107  
   108    tests_start ();
   109  
   110    mpf_init2 (f, 1024L);
   111    mpq_init (got);
   112    mpz_init (want_num);
   113    mpz_init (want_den);
   114  
   115    for (i = 0; i < numberof (data); i++)
   116      {
   117        for (neg = 0; neg <= 1; neg++)
   118          {
   119            mpf_set_str_or_abort (f, data[i].f, data[i].f_base);
   120            mpz_set_str_or_abort (want_num, data[i].want_num, data[i].z_base);
   121            mpz_set_str_or_abort (want_den, data[i].want_den, data[i].z_base);
   122  
   123            if (neg)
   124              {
   125                mpf_neg (f, f);
   126                mpz_neg (want_num, want_num);
   127              }
   128  
   129            mpq_set_f (got, f);
   130            MPQ_CHECK_FORMAT (got);
   131  
   132            if (mpz_cmp (mpq_numref(got), want_num) != 0
   133                || mpz_cmp (mpq_denref(got), want_den) != 0)
   134              {
   135                printf ("wrong at data[%d]\n", i);
   136                printf ("   f_base %d, z_base %d\n",
   137                        data[i].f_base, data[i].z_base);
   138  
   139                printf ("   f \"%s\" hex ", data[i].f);
   140                mpf_out_str (stdout, 16, 0, f);
   141                printf ("\n");
   142  
   143                printf ("   want num 0x");
   144                mpz_out_str (stdout, 16, want_num);
   145                printf ("\n");
   146                printf ("   want den 0x");
   147                mpz_out_str (stdout, 16, want_den);
   148                printf ("\n");
   149  
   150                printf ("   got num 0x");
   151                mpz_out_str (stdout, 16, mpq_numref(got));
   152                printf ("\n");
   153                printf ("   got den 0x");
   154                mpz_out_str (stdout, 16, mpq_denref(got));
   155                printf ("\n");
   156  
   157                abort ();
   158              }
   159          }
   160      }
   161  
   162    mpf_clear (f);
   163    mpq_clear (got);
   164    mpz_clear (want_num);
   165    mpz_clear (want_den);
   166  
   167    tests_end ();
   168  #endif
   169    exit (0);
   170  }