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