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

     1  /* Exercise mpf_get_ui.
     2  
     3  Copyright 2004 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  void
    28  check_limbdata (void)
    29  {
    30  #define M  GMP_NUMB_MAX
    31  
    32    static const struct {
    33      mp_exp_t       exp;
    34      mp_size_t      size;
    35      mp_limb_t      d[10];
    36      unsigned long  want;
    37  
    38    } data[] = {
    39  
    40      /* in the comments here, a "_" indicates a digit (ie. limb) position not
    41         included in the d data, and therefore zero */
    42  
    43      { 0, 0, { 0 }, 0L },    /* 0 */
    44  
    45      { 1,  1, { 1 }, 1L },   /* 1 */
    46      { 1, -1, { 1 }, 1L },   /* -1 */
    47  
    48      { 0,  1, { 1 }, 0L },   /* .1 */
    49      { 0, -1, { 1 }, 0L },   /* -.1 */
    50  
    51      { -1,  1, { 1 }, 0L },  /* ._1 */
    52      { -1, -1, { 1 }, 0L },  /* -._1 */
    53  
    54      { -999,          1, { 1 }, 0L },   /* .___1 small */
    55      { MP_EXP_T_MIN,  1, { 1 }, 0L },   /* .____1 very small */
    56  
    57      { 999,          1, { 1 }, 0L },    /* 1____. big */
    58      { MP_EXP_T_MAX, 1, { 1 }, 0L },    /* 1_____. very big */
    59  
    60      { 1, 2, { 999, 2 }, 2L },                  /* 2.9 */
    61      { 5, 8, { 7, 8, 9, 3, 0, 0, 0, 1 }, 3L },  /* 10003.987 */
    62  
    63      { 2, 2, { M, M },    ULONG_MAX }, /* FF. */
    64      { 2, 2, { M, M, M }, ULONG_MAX }, /* FF.F */
    65      { 3, 3, { M, M, M }, ULONG_MAX }, /* FFF. */
    66  
    67  #if GMP_NUMB_BITS >= BITS_PER_ULONG
    68      /* normal case, numb bigger than long */
    69      { 2,  1, { 1 },    0L },      /* 1_. */
    70      { 2,  2, { 0, 1 }, 0L },      /* 10. */
    71      { 2,  2, { 999, 1 }, 999L },  /* 19. */
    72      { 3,  2, { 999, 1 }, 0L },    /* 19_. */
    73  
    74  #else
    75      /* nails case, numb smaller than long */
    76      { 2,  1, { 1 }, 1L << GMP_NUMB_BITS },  /* 1_. */
    77      { 3,  1, { 1 }, 0L },                   /* 1__. */
    78  
    79      { 2,  2, { 99, 1 },    99L + (1L << GMP_NUMB_BITS) },  /* 19. */
    80      { 3,  2, { 1, 99 },    1L << GMP_NUMB_BITS },          /* 91_. */
    81      { 3,  3, { 0, 1, 99 }, 1L << GMP_NUMB_BITS },          /* 910. */
    82  
    83  #endif
    84    };
    85  
    86    mpf_t          f;
    87    unsigned long  got;
    88    int            i;
    89    mp_limb_t      buf[20 + numberof(data[i].d)];
    90  
    91    for (i = 0; i < numberof (data); i++)
    92      {
    93        refmpn_fill (buf, 10, CNST_LIMB(0xDEADBEEF));
    94        refmpn_copy (buf+10, data[i].d, ABS(data[i].size));
    95        refmpn_fill (buf+10+ABS(data[i].size), 10, CNST_LIMB(0xDEADBEEF));
    96  
    97        PTR(f) = buf+10;
    98        EXP(f) = data[i].exp;
    99        SIZ(f) = data[i].size;
   100        PREC(f) = numberof (data[i].d);
   101        MPF_CHECK_FORMAT (f);
   102  
   103        got = mpf_get_ui (f);
   104        if (got != data[i].want)
   105  	{
   106  	  printf    ("mpf_get_ui wrong at limb data[%d]\n", i);
   107  	  mpf_trace ("  f", f);
   108  	  mpn_trace ("  d", data[i].d, data[i].size);
   109  	  printf    ("  size %ld\n", (long) data[i].size);
   110  	  printf    ("  exp %ld\n", (long) data[i].exp);
   111  	  printf    ("  got   %lu (0x%lX)\n", got, got);
   112  	  printf    ("  want  %lu (0x%lX)\n", data[i].want, data[i].want);
   113  	  abort();
   114  	}
   115      }
   116  }
   117  
   118  int
   119  main (void)
   120  {
   121    tests_start ();
   122    mp_trace_base = 16;
   123  
   124    check_limbdata ();
   125  
   126    tests_end ();
   127    exit (0);
   128  }