(root)/
gmp-6.3.0/
tests/
mpf/
t-set_si.c
       1  /* Test mpf_set_si and mpf_init_set_si.
       2  
       3  Copyright 2000, 2001, 2003 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  void
      26  check_data (void)
      27  {
      28    static const struct {
      29      long       x;
      30      mp_size_t  want_size;
      31      mp_limb_t  want_data[2];
      32    } data[] = {
      33  
      34      {  0L,  0 },
      35      {  1L,  1, { 1 } },
      36      { -1L, -1, { 1 } },
      37  
      38  #if GMP_NUMB_BITS >= BITS_PER_ULONG
      39      { LONG_MAX,  1, { LONG_MAX, 0 } },
      40      { -LONG_MAX,  -1, { LONG_MAX, 0 } },
      41      { LONG_HIGHBIT,  -1, { ULONG_HIGHBIT, 0 } },
      42  #else
      43      { LONG_MAX,  2, { LONG_MAX & GMP_NUMB_MASK, LONG_MAX >> GMP_NUMB_BITS } },
      44      { -LONG_MAX,  -2, { LONG_MAX & GMP_NUMB_MASK, LONG_MAX >> GMP_NUMB_BITS }},
      45      { LONG_HIGHBIT,  -2, { 0, ULONG_HIGHBIT >> GMP_NUMB_BITS } },
      46  #endif
      47    };
      48  
      49    mpf_t  x;
      50    int    i;
      51  
      52    for (i = 0; i < numberof (data); i++)
      53      {
      54        mpf_init (x);
      55        mpf_set_si (x, data[i].x);
      56        MPF_CHECK_FORMAT (x);
      57        if (x->_mp_size != data[i].want_size
      58            || refmpn_cmp_allowzero (x->_mp_d, data[i].want_data,
      59                                     ABS (data[i].want_size)) != 0
      60            || x->_mp_exp != ABS (data[i].want_size))
      61          {
      62            printf ("mpf_set_si wrong on data[%d]\n", i);
      63            abort();
      64          }
      65        mpf_clear (x);
      66  
      67        mpf_init_set_si (x, data[i].x);
      68        MPF_CHECK_FORMAT (x);
      69        if (x->_mp_size != data[i].want_size
      70            || refmpn_cmp_allowzero (x->_mp_d, data[i].want_data,
      71                                     ABS (data[i].want_size)) != 0
      72            || x->_mp_exp != ABS (data[i].want_size))
      73          {
      74            printf ("mpf_init_set_si wrong on data[%d]\n", i);
      75            abort();
      76          }
      77        mpf_clear (x);
      78      }
      79  }
      80  
      81  int
      82  main (void)
      83  {
      84    tests_start ();
      85  
      86    check_data ();
      87  
      88    tests_end ();
      89    exit (0);
      90  }