xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpz/t-set_si.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* Test mpz_set_si and mpz_init_set_si.
2 
3 Copyright 2000-2002 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 
26 void
check_data(void)27 check_data (void)
28 {
29 #if GMP_NUMB_BITS <= BITS_PER_ULONG
30 #define ENTRY(n)   { n, { n, 0 } }
31 #else
32 #define ENTRY(n)   { n, { (n) & GMP_NUMB_MASK, (n) >> GMP_NUMB_BITS } }
33 #endif
34 
35   static const struct {
36     long       n;
37     mp_size_t  want_size;
38     mp_limb_t  want_data[2];
39   } data[] = {
40 
41     {  0L,  0 },
42     {  1L,  1, { 1 } },
43     { -1L, -1, { 1 } },
44 
45 #if GMP_NUMB_BITS >= BITS_PER_ULONG
46     { LONG_MAX,  1, { LONG_MAX, 0 } },
47     { -LONG_MAX,  -1, { LONG_MAX, 0 } },
48     { LONG_HIGHBIT,  -1, { ULONG_HIGHBIT, 0 } },
49 #else
50     { LONG_MAX,  2, { LONG_MAX & GMP_NUMB_MASK, LONG_MAX >> GMP_NUMB_BITS } },
51     { -LONG_MAX,  -2, { LONG_MAX & GMP_NUMB_MASK, LONG_MAX >> GMP_NUMB_BITS }},
52     { LONG_HIGHBIT,  -2, { 0, ULONG_HIGHBIT >> GMP_NUMB_BITS } },
53 #endif
54   };
55 
56   mpz_t  n;
57   int    i;
58 
59   for (i = 0; i < numberof (data); i++)
60     {
61       mpz_init (n);
62       mpz_set_si (n, data[i].n);
63       MPZ_CHECK_FORMAT (n);
64       if (n->_mp_size != data[i].want_size
65           || refmpn_cmp_allowzero (n->_mp_d, data[i].want_data,
66                                    ABS (data[i].want_size)) != 0)
67         {
68           printf ("mpz_set_si wrong on data[%d]\n", i);
69           abort();
70         }
71       mpz_clear (n);
72 
73       mpz_init_set_si (n, data[i].n);
74       MPZ_CHECK_FORMAT (n);
75       if (n->_mp_size != data[i].want_size
76           || refmpn_cmp_allowzero (n->_mp_d, data[i].want_data,
77                                    ABS (data[i].want_size)) != 0)
78         {
79           printf ("mpz_init_set_si wrong on data[%d]\n", i);
80           abort();
81         }
82       mpz_clear (n);
83     }
84 }
85 
86 
87 int
main(void)88 main (void)
89 {
90   tests_start ();
91 
92   check_data ();
93 
94   tests_end ();
95   exit (0);
96 }
97