1/* Test mpf_set_si and mpf_init_set_si. 2 3Copyright 2000, 2001, 2003 Free Software Foundation, Inc. 4 5This file is part of the GNU MP Library. 6 7The GNU MP Library is free software; you can redistribute it and/or modify 8it under the terms of the GNU Lesser General Public License as published by 9the Free Software Foundation; either version 3 of the License, or (at your 10option) any later version. 11 12The GNU MP Library is distributed in the hope that it will be useful, but 13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 14or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 15License for more details. 16 17You should have received a copy of the GNU Lesser General Public License 18along 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 26void 27check_data (void) 28{ 29 static const struct { 30 long x; 31 mp_size_t want_size; 32 mp_limb_t want_data[2]; 33 } data[] = { 34 35 { 0L, 0 }, 36 { 1L, 1, { 1 } }, 37 { -1L, -1, { 1 } }, 38 39#if GMP_NUMB_BITS >= BITS_PER_ULONG 40 { LONG_MAX, 1, { LONG_MAX, 0 } }, 41 { -LONG_MAX, -1, { LONG_MAX, 0 } }, 42 { LONG_HIGHBIT, -1, { ULONG_HIGHBIT, 0 } }, 43#else 44 { LONG_MAX, 2, { LONG_MAX & GMP_NUMB_MASK, LONG_MAX >> GMP_NUMB_BITS } }, 45 { -LONG_MAX, -2, { LONG_MAX & GMP_NUMB_MASK, LONG_MAX >> GMP_NUMB_BITS }}, 46 { LONG_HIGHBIT, -2, { 0, ULONG_HIGHBIT >> GMP_NUMB_BITS } }, 47#endif 48 }; 49 50 mpf_t x; 51 int i; 52 53 for (i = 0; i < numberof (data); i++) 54 { 55 mpf_init (x); 56 mpf_set_si (x, data[i].x); 57 MPF_CHECK_FORMAT (x); 58 if (x->_mp_size != data[i].want_size 59 || refmpn_cmp_allowzero (x->_mp_d, data[i].want_data, 60 ABS (data[i].want_size)) != 0 61 || x->_mp_exp != ABS (data[i].want_size)) 62 { 63 printf ("mpf_set_si wrong on data[%d]\n", i); 64 abort(); 65 } 66 mpf_clear (x); 67 68 mpf_init_set_si (x, data[i].x); 69 MPF_CHECK_FORMAT (x); 70 if (x->_mp_size != data[i].want_size 71 || refmpn_cmp_allowzero (x->_mp_d, data[i].want_data, 72 ABS (data[i].want_size)) != 0 73 || x->_mp_exp != ABS (data[i].want_size)) 74 { 75 printf ("mpf_init_set_si wrong on data[%d]\n", i); 76 abort(); 77 } 78 mpf_clear (x); 79 } 80} 81 82int 83main (void) 84{ 85 tests_start (); 86 87 check_data (); 88 89 tests_end (); 90 exit (0); 91} 92