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

     1  /* Test mpf_fits_*_p
     2  
     3  Copyright 2001, 2002, 2013 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  
    27  /* Nothing sophisticated here, just exercise mpf_fits_*_p on a small amount
    28     of data. */
    29  
    30  #define EXPECT_S(fun,name,answer)                                        \
    31    got = fun (f);                                                         \
    32    if (got != answer)                                                     \
    33      {                                                                    \
    34        printf ("%s (%s) got %d want %d\n", name, expr, got, answer);      \
    35        printf (" f size %d exp %ld\n", SIZ(f), EXP(f));                   \
    36        printf (" f dec "); mpf_out_str (stdout, 10, 0, f); printf ("\n"); \
    37        printf (" f hex "); mpf_out_str (stdout, 16, 0, f); printf ("\n"); \
    38        error = 1;                                                         \
    39      }
    40  
    41  #define EXPECT(fun,answer)  EXPECT_S(fun,#fun,answer)
    42  
    43  int
    44  main (void)
    45  {
    46    mpf_t       f, f0p5;
    47    int         got;
    48    const char  *expr;
    49    int         error = 0;
    50  
    51    tests_start ();
    52    mpf_init2 (f, 200L);
    53    mpf_init2 (f0p5, 200L);
    54  
    55    /* 0.5 */
    56    mpf_set_ui (f0p5, 1L);
    57    mpf_div_2exp (f0p5, f0p5, 1L);
    58  
    59    mpf_set_ui (f, 0L);
    60    expr = "0";
    61    EXPECT (mpf_fits_ulong_p, 1);
    62    EXPECT (mpf_fits_uint_p, 1);
    63    EXPECT (mpf_fits_ushort_p, 1);
    64    EXPECT (mpf_fits_slong_p, 1);
    65    EXPECT (mpf_fits_sint_p, 1);
    66    EXPECT (mpf_fits_sshort_p, 1);
    67  
    68    mpf_set_ui (f, 1L);
    69    expr = "1";
    70    EXPECT (mpf_fits_ulong_p, 1);
    71    EXPECT (mpf_fits_uint_p, 1);
    72    EXPECT (mpf_fits_ushort_p, 1);
    73    EXPECT (mpf_fits_slong_p, 1);
    74    EXPECT (mpf_fits_sint_p, 1);
    75    EXPECT (mpf_fits_sshort_p, 1);
    76  
    77    mpf_set_si (f, -1L);
    78    expr = "-1";
    79    EXPECT (mpf_fits_ulong_p, 0);
    80    EXPECT (mpf_fits_uint_p, 0);
    81    EXPECT (mpf_fits_ushort_p, 0);
    82    EXPECT (mpf_fits_slong_p, 1);
    83    EXPECT (mpf_fits_sint_p, 1);
    84    EXPECT (mpf_fits_sshort_p, 1);
    85  
    86  
    87    mpf_set_ui (f, (unsigned long) USHRT_MAX);
    88    expr = "USHRT_MAX";
    89    EXPECT (mpf_fits_ulong_p, 1);
    90    EXPECT (mpf_fits_uint_p, 1);
    91    EXPECT (mpf_fits_ushort_p, 1);
    92  
    93    mpf_set_ui (f, (unsigned long) USHRT_MAX);
    94    mpf_add (f, f, f0p5);
    95    expr = "USHRT_MAX + 0.5";
    96    EXPECT (mpf_fits_ulong_p, 1);
    97    EXPECT (mpf_fits_uint_p, 1);
    98    EXPECT (mpf_fits_ushort_p, 1);
    99  
   100    mpf_set_ui (f, (unsigned long) USHRT_MAX);
   101    mpf_add_ui (f, f, 1L);
   102    expr = "USHRT_MAX + 1";
   103    EXPECT (mpf_fits_ushort_p, 0);
   104  
   105  
   106    mpf_set_ui (f, (unsigned long) UINT_MAX);
   107    expr = "UINT_MAX";
   108    EXPECT (mpf_fits_ulong_p, 1);
   109    EXPECT (mpf_fits_uint_p, 1);
   110  
   111    mpf_set_ui (f, (unsigned long) UINT_MAX);
   112    mpf_add (f, f, f0p5);
   113    expr = "UINT_MAX + 0.5";
   114    EXPECT (mpf_fits_ulong_p, 1);
   115    EXPECT (mpf_fits_uint_p, 1);
   116  
   117    mpf_set_ui (f, (unsigned long) UINT_MAX);
   118    mpf_add_ui (f, f, 1L);
   119    expr = "UINT_MAX + 1";
   120    EXPECT (mpf_fits_uint_p, 0);
   121  
   122  
   123    mpf_set_ui (f, ULONG_MAX);
   124    expr = "ULONG_MAX";
   125    EXPECT (mpf_fits_ulong_p, 1);
   126  
   127    mpf_set_ui (f, ULONG_MAX);
   128    mpf_add (f, f, f0p5);
   129    expr = "ULONG_MAX + 0.5";
   130    EXPECT (mpf_fits_ulong_p, 1);
   131  
   132    mpf_set_ui (f, ULONG_MAX);
   133    mpf_add_ui (f, f, 1L);
   134    expr = "ULONG_MAX + 1";
   135    EXPECT (mpf_fits_ulong_p, 0);
   136  
   137  
   138    mpf_set_si (f, (long) SHRT_MAX);
   139    expr = "SHRT_MAX";
   140    EXPECT (mpf_fits_slong_p, 1);
   141    EXPECT (mpf_fits_sint_p, 1);
   142    EXPECT (mpf_fits_sshort_p, 1);
   143  
   144    mpf_set_si (f, (long) SHRT_MAX);
   145    expr = "SHRT_MAX + 0.5";
   146    mpf_add (f, f, f0p5);
   147    EXPECT (mpf_fits_slong_p, 1);
   148    EXPECT (mpf_fits_sint_p, 1);
   149    EXPECT (mpf_fits_sshort_p, 1);
   150  
   151    mpf_set_si (f, (long) SHRT_MAX);
   152    mpf_add_ui (f, f, 1L);
   153    expr = "SHRT_MAX + 1";
   154    EXPECT (mpf_fits_sshort_p, 0);
   155  
   156  
   157    mpf_set_si (f, (long) INT_MAX);
   158    expr = "INT_MAX";
   159    EXPECT (mpf_fits_slong_p, 1);
   160    EXPECT (mpf_fits_sint_p, 1);
   161  
   162    mpf_set_si (f, (long) INT_MAX);
   163    mpf_add (f, f, f0p5);
   164    expr = "INT_MAX + 0.5";
   165    EXPECT (mpf_fits_slong_p, 1);
   166    EXPECT (mpf_fits_sint_p, 1);
   167  
   168    mpf_set_si (f, (long) INT_MAX);
   169    mpf_add_ui (f, f, 1L);
   170    expr = "INT_MAX + 1";
   171    EXPECT (mpf_fits_sint_p, 0);
   172  
   173  
   174    mpf_set_si (f, LONG_MAX);
   175    expr = "LONG_MAX";
   176    EXPECT (mpf_fits_slong_p, 1);
   177  
   178    mpf_set_si (f, LONG_MAX);
   179    mpf_add (f, f, f0p5);
   180    expr = "LONG_MAX + 0.5";
   181    EXPECT (mpf_fits_slong_p, 1);
   182  
   183    mpf_set_si (f, LONG_MAX);
   184    mpf_add_ui (f, f, 1L);
   185    expr = "LONG_MAX + 1";
   186    EXPECT (mpf_fits_slong_p, 0);
   187  
   188  
   189    mpf_set_si (f, (long) SHRT_MIN);
   190    expr = "SHRT_MIN";
   191    EXPECT (mpf_fits_slong_p, 1);
   192    EXPECT (mpf_fits_sint_p, 1);
   193    EXPECT (mpf_fits_sshort_p, 1);
   194  
   195    mpf_set_si (f, (long) SHRT_MIN);
   196    mpf_sub (f, f, f0p5);
   197    expr = "SHRT_MIN - 0.5";
   198    EXPECT (mpf_fits_slong_p, 1);
   199    EXPECT (mpf_fits_sint_p, 1);
   200    EXPECT (mpf_fits_sshort_p, 1);
   201  
   202    mpf_set_si (f, (long) SHRT_MIN);
   203    mpf_sub_ui (f, f, 1L);
   204    expr = "SHRT_MIN - 1";
   205    EXPECT (mpf_fits_sshort_p, 0);
   206  
   207  
   208    mpf_set_si (f, (long) INT_MIN);
   209    expr = "INT_MIN";
   210    EXPECT (mpf_fits_slong_p, 1);
   211    EXPECT (mpf_fits_sint_p, 1);
   212  
   213    mpf_set_si (f, (long) INT_MIN);
   214    mpf_sub (f, f, f0p5);
   215    expr = "INT_MIN - 0.5";
   216    EXPECT (mpf_fits_slong_p, 1);
   217    EXPECT (mpf_fits_sint_p, 1);
   218  
   219    mpf_set_si (f, (long) INT_MIN);
   220    mpf_sub_ui (f, f, 1L);
   221    expr = "INT_MIN - 1";
   222    EXPECT (mpf_fits_sint_p, 0);
   223  
   224  
   225    mpf_set_si (f, LONG_MIN);
   226    expr = "LONG_MIN";
   227    EXPECT (mpf_fits_slong_p, 1);
   228  
   229    mpf_set_si (f, LONG_MIN);
   230    mpf_sub (f, f, f0p5);
   231    expr = "LONG_MIN - 0.5";
   232    EXPECT (mpf_fits_slong_p, 1);
   233  
   234    mpf_set_si (f, LONG_MIN);
   235    mpf_sub_ui (f, f, 1L);
   236    expr = "LONG_MIN - 1";
   237    EXPECT (mpf_fits_slong_p, 0);
   238  
   239  
   240    mpf_set_str_or_abort (f, "0.5", 10);
   241    expr = "0.5";
   242    EXPECT (mpf_fits_ulong_p, 1);
   243    EXPECT (mpf_fits_uint_p, 1);
   244    EXPECT (mpf_fits_ushort_p, 1);
   245    EXPECT (mpf_fits_slong_p, 1);
   246    EXPECT (mpf_fits_sint_p, 1);
   247    EXPECT (mpf_fits_sshort_p, 1);
   248  
   249    mpf_set_str_or_abort (f, "-0.5", 10);
   250    expr = "-0.5";
   251    EXPECT (mpf_fits_ulong_p, 1);
   252    EXPECT (mpf_fits_uint_p, 1);
   253    EXPECT (mpf_fits_ushort_p, 1);
   254    EXPECT (mpf_fits_slong_p, 1);
   255    EXPECT (mpf_fits_sint_p, 1);
   256    EXPECT (mpf_fits_sshort_p, 1);
   257  
   258    mpf_set_str_or_abort (f, "-1.5", 10);
   259    expr = "-1.5";
   260    EXPECT (mpf_fits_ulong_p, 0);
   261    EXPECT (mpf_fits_uint_p, 0);
   262    EXPECT (mpf_fits_ushort_p, 0);
   263    EXPECT (mpf_fits_slong_p, 1);
   264    EXPECT (mpf_fits_sint_p, 1);
   265    EXPECT (mpf_fits_sshort_p, 1);
   266  
   267  
   268    mpf_set_str_or_abort (f, "1.000000000000000000000000000000000001", 16);
   269    expr = "1.000000000000000000000000000000000001 base 16";
   270    EXPECT (mpf_fits_ulong_p, 1);
   271    EXPECT (mpf_fits_uint_p, 1);
   272    EXPECT (mpf_fits_ushort_p, 1);
   273    EXPECT (mpf_fits_slong_p, 1);
   274    EXPECT (mpf_fits_sint_p, 1);
   275    EXPECT (mpf_fits_sshort_p, 1);
   276  
   277    mpf_set_str_or_abort (f, "1@1000", 16);
   278    expr = "1@1000 base 16";
   279    EXPECT (mpf_fits_ulong_p, 0);
   280    EXPECT (mpf_fits_uint_p, 0);
   281    EXPECT (mpf_fits_ushort_p, 0);
   282    EXPECT (mpf_fits_slong_p, 0);
   283    EXPECT (mpf_fits_sint_p, 0);
   284    EXPECT (mpf_fits_sshort_p, 0);
   285  
   286  
   287    mpf_set_ui (f, 1L);
   288    mpf_mul_2exp (f, f, BITS_PER_ULONG + 1);
   289    mpf_sub_ui (f, f, 1L);
   290    expr = "2^(BITS_PER_ULONG+1) - 1";
   291    EXPECT (mpf_fits_ulong_p, 0);
   292    EXPECT (mpf_fits_uint_p, 0);
   293    EXPECT (mpf_fits_ushort_p, 0);
   294    EXPECT (mpf_fits_slong_p, 0);
   295    EXPECT (mpf_fits_sint_p, 0);
   296    EXPECT (mpf_fits_sshort_p, 0);
   297  
   298    mpf_set_ui (f, 1L);
   299    mpf_mul_2exp (f, f, BITS_PER_ULONG + 1);
   300    mpf_ui_sub (f, 1L, f);
   301    expr = "- (2^(BITS_PER_ULONG+1) - 1)";
   302    EXPECT (mpf_fits_ulong_p, 0);
   303    EXPECT (mpf_fits_uint_p, 0);
   304    EXPECT (mpf_fits_ushort_p, 0);
   305    EXPECT (mpf_fits_slong_p, 0);
   306    EXPECT (mpf_fits_sint_p, 0);
   307    EXPECT (mpf_fits_sshort_p, 0);
   308  
   309    mpf_set_ui (f, 1L);
   310    mpf_mul_2exp (f, f, BITS_PER_ULONG + 5);
   311    mpf_sub_ui (f, f, 1L);
   312    expr = "2^(BITS_PER_ULONG+5) - 1";
   313    EXPECT (mpf_fits_ulong_p, 0);
   314    EXPECT (mpf_fits_uint_p, 0);
   315    EXPECT (mpf_fits_ushort_p, 0);
   316    EXPECT (mpf_fits_slong_p, 0);
   317    EXPECT (mpf_fits_sint_p, 0);
   318    EXPECT (mpf_fits_sshort_p, 0);
   319  
   320  
   321    if (error)
   322      abort ();
   323  
   324    mpf_clear (f);
   325    mpf_clear (f0p5);
   326    tests_end ();
   327    exit (0);
   328  }