Home | History | Annotate | Line # | Download | only in mpz
      1      1.1  mrg /* Test mpz_set_f.
      2      1.1  mrg 
      3      1.1  mrg Copyright 2001 Free Software Foundation, Inc.
      4      1.1  mrg 
      5  1.1.1.2  mrg This file is part of the GNU MP Library test suite.
      6      1.1  mrg 
      7  1.1.1.2  mrg The GNU MP Library test suite is free software; you can redistribute it
      8  1.1.1.2  mrg and/or modify it under the terms of the GNU General Public License as
      9  1.1.1.2  mrg published by the Free Software Foundation; either version 3 of the License,
     10  1.1.1.2  mrg or (at your option) any later version.
     11  1.1.1.2  mrg 
     12  1.1.1.2  mrg The GNU MP Library test suite is distributed in the hope that it will be
     13  1.1.1.2  mrg useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1.1.2  mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
     15  1.1.1.2  mrg Public License for more details.
     16      1.1  mrg 
     17  1.1.1.2  mrg You should have received a copy of the GNU General Public License along with
     18  1.1.1.3  mrg the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
     19      1.1  mrg 
     20      1.1  mrg #include <stdio.h>
     21      1.1  mrg #include <stdlib.h>
     22      1.1  mrg #include "gmp-impl.h"
     23      1.1  mrg #include "tests.h"
     24      1.1  mrg 
     25      1.1  mrg 
     26      1.1  mrg void
     27      1.1  mrg check_one (mpz_srcptr z)
     28      1.1  mrg {
     29      1.1  mrg   static const int shift[] = {
     30      1.1  mrg     0, 1, GMP_LIMB_BITS, 2*GMP_LIMB_BITS, 5*GMP_LIMB_BITS
     31      1.1  mrg   };
     32      1.1  mrg 
     33      1.1  mrg   int    sh, shneg, neg;
     34      1.1  mrg   mpf_t  f;
     35      1.1  mrg   mpz_t  got, want;
     36      1.1  mrg 
     37      1.1  mrg   mpf_init2 (f, mpz_sizeinbase(z,2));
     38      1.1  mrg   mpz_init (got);
     39      1.1  mrg   mpz_init (want);
     40      1.1  mrg 
     41      1.1  mrg   for (sh = 0; sh < numberof(shift); sh++)
     42      1.1  mrg     {
     43      1.1  mrg       for (shneg = 0; shneg <= 1; shneg++)
     44      1.1  mrg 	{
     45      1.1  mrg 	  for (neg = 0; neg <= 1; neg++)
     46      1.1  mrg 	    {
     47      1.1  mrg 	      mpf_set_z (f, z);
     48      1.1  mrg 	      mpz_set (want, z);
     49      1.1  mrg 
     50      1.1  mrg 	      if (neg)
     51      1.1  mrg 		{
     52      1.1  mrg 		  mpf_neg (f, f);
     53      1.1  mrg 		  mpz_neg (want, want);
     54      1.1  mrg 		}
     55      1.1  mrg 
     56      1.1  mrg 	      if (shneg)
     57      1.1  mrg 		{
     58      1.1  mrg 		  mpz_tdiv_q_2exp (want, want, shift[sh]);
     59      1.1  mrg 		  mpf_div_2exp (f, f, shift[sh]);
     60      1.1  mrg 		}
     61      1.1  mrg 	      else
     62      1.1  mrg 		{
     63      1.1  mrg 		  mpz_mul_2exp (want, want, shift[sh]);
     64      1.1  mrg 		  mpf_mul_2exp (f, f, shift[sh]);
     65      1.1  mrg 		}
     66      1.1  mrg 
     67      1.1  mrg 	      mpz_set_f (got, f);
     68      1.1  mrg 	      MPZ_CHECK_FORMAT (got);
     69      1.1  mrg 
     70      1.1  mrg 	      if (mpz_cmp (got, want) != 0)
     71      1.1  mrg 		{
     72      1.1  mrg 		  printf ("wrong result\n");
     73      1.1  mrg 		  printf ("  shift  %d\n", shneg ? -shift[sh] : shift[sh]);
     74      1.1  mrg 		  printf ("  neg    %d\n", neg);
     75      1.1  mrg 		  mpf_trace ("     f", f);
     76      1.1  mrg 		  mpz_trace ("   got", got);
     77      1.1  mrg 		  mpz_trace ("  want", want);
     78      1.1  mrg 		  abort ();
     79      1.1  mrg 		}
     80      1.1  mrg 	    }
     81      1.1  mrg 	}
     82      1.1  mrg     }
     83      1.1  mrg 
     84      1.1  mrg   mpf_clear (f);
     85      1.1  mrg   mpz_clear (got);
     86      1.1  mrg   mpz_clear (want);
     87      1.1  mrg }
     88      1.1  mrg 
     89      1.1  mrg 
     90      1.1  mrg void
     91      1.1  mrg check_various (void)
     92      1.1  mrg {
     93      1.1  mrg   mpz_t  z;
     94      1.1  mrg 
     95      1.1  mrg   mpz_init (z);
     96      1.1  mrg 
     97      1.1  mrg   mpz_set_ui (z, 0L);
     98      1.1  mrg   check_one (z);
     99      1.1  mrg 
    100      1.1  mrg   mpz_set_si (z, 123L);
    101      1.1  mrg   check_one (z);
    102      1.1  mrg 
    103      1.1  mrg   mpz_rrandomb (z, RANDS, 2*GMP_LIMB_BITS);
    104      1.1  mrg   check_one (z);
    105      1.1  mrg 
    106      1.1  mrg   mpz_rrandomb (z, RANDS, 5*GMP_LIMB_BITS);
    107      1.1  mrg   check_one (z);
    108      1.1  mrg 
    109      1.1  mrg   mpz_clear (z);
    110      1.1  mrg }
    111      1.1  mrg 
    112      1.1  mrg 
    113      1.1  mrg int
    114      1.1  mrg main (int argc, char *argv[])
    115      1.1  mrg {
    116      1.1  mrg #if GMP_NAIL_BITS == 0
    117      1.1  mrg   tests_start ();
    118      1.1  mrg   mp_trace_base = 16;
    119      1.1  mrg 
    120      1.1  mrg   check_various ();
    121      1.1  mrg 
    122      1.1  mrg   tests_end ();
    123      1.1  mrg #endif
    124      1.1  mrg   exit (0);
    125      1.1  mrg }
    126