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