xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpz/t-divis_2exp.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* test mpz_divisible_2exp_p */
2 
3 /*
4 Copyright 2001 Free Software Foundation, Inc.
5 
6 This file is part of the GNU MP Library test suite.
7 
8 The GNU MP Library test suite is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 3 of the License,
11 or (at your option) any later version.
12 
13 The GNU MP Library test suite is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
16 Public License for more details.
17 
18 You should have received a copy of the GNU General Public License along with
19 the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include "gmp-impl.h"
25 #include "tests.h"
26 
27 
28 void
check_one(mpz_srcptr a,unsigned long d,int want)29 check_one (mpz_srcptr a, unsigned long d, int want)
30 {
31   int   got;
32 
33   got = (mpz_divisible_2exp_p (a, d) != 0);
34   if (want != got)
35     {
36       printf ("mpz_divisible_2exp_p wrong\n");
37       printf ("   expected %d got %d\n", want, got);
38       mpz_trace ("   a", a);
39       printf    ("   d=%lu\n", d);
40       mp_trace_base = -16;
41       mpz_trace ("   a", a);
42       printf    ("   d=0x%lX\n", d);
43       abort ();
44     }
45 }
46 
47 void
check_data(void)48 check_data (void)
49 {
50   static const struct {
51     const char    *a;
52     unsigned long d;
53     int           want;
54 
55   } data[] = {
56 
57     { "0", 0, 1 },
58     { "0", 1, 1 },
59     { "0", 2, 1 },
60     { "0", 3, 1 },
61 
62     { "1", 0, 1 },
63     { "1", 1, 0 },
64     { "1", 2, 0 },
65     { "1", 3, 0 },
66     { "1", 10000, 0 },
67 
68     { "4", 0, 1 },
69     { "4", 1, 1 },
70     { "4", 2, 1 },
71     { "4", 3, 0 },
72     { "4", 4, 0 },
73     { "4", 10000, 0 },
74 
75     { "0x80000000", 31, 1 },
76     { "0x80000000", 32, 0 },
77     { "0x80000000", 64, 0 },
78 
79     { "0x100000000", 32, 1 },
80     { "0x100000000", 33, 0 },
81     { "0x100000000", 64, 0 },
82 
83     { "0x8000000000000000", 63, 1 },
84     { "0x8000000000000000", 64, 0 },
85     { "0x8000000000000000", 128, 0 },
86 
87     { "0x10000000000000000", 64, 1 },
88     { "0x10000000000000000", 65, 0 },
89     { "0x10000000000000000", 128, 0 },
90     { "0x10000000000000000", 256, 0 },
91 
92     { "0x10000000000000000100000000", 32, 1 },
93     { "0x10000000000000000100000000", 33, 0 },
94     { "0x10000000000000000100000000", 64, 0 },
95 
96     { "0x1000000000000000010000000000000000", 64, 1 },
97     { "0x1000000000000000010000000000000000", 65, 0 },
98     { "0x1000000000000000010000000000000000", 128, 0 },
99     { "0x1000000000000000010000000000000000", 256, 0 },
100     { "0x1000000000000000010000000000000000", 1024, 0 },
101 
102   };
103 
104   mpz_t   a, d;
105   int     i;
106 
107   mpz_init (a);
108   mpz_init (d);
109 
110   for (i = 0; i < numberof (data); i++)
111     {
112       mpz_set_str_or_abort (a, data[i].a, 0);
113       check_one (a, data[i].d, data[i].want);
114 
115       mpz_neg (a, a);
116       check_one (a, data[i].d, data[i].want);
117     }
118 
119   mpz_clear (a);
120   mpz_clear (d);
121 }
122 
123 int
main(int argc,char * argv[])124 main (int argc, char *argv[])
125 {
126   tests_start ();
127 
128   check_data ();
129 
130   tests_end ();
131   exit (0);
132 }
133