Annotation of OpenXM_contrib/gmp/tests/mpz/t-bin.c, Revision 1.1.1.1
1.1 ohara 1: /* Exercise mpz_bin_ui and mpz_bin_uiui.
2:
3: Copyright 2000, 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 2.1 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; see the file COPYING.LIB. If not, write to
19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20: MA 02111-1307, USA. */
21:
22: #include <stdio.h>
23: #include <stdlib.h>
24: #include "gmp.h"
25: #include "gmp-impl.h"
26: #include "tests.h"
27:
28:
29: void
30: try_mpz_bin_ui (mpz_srcptr want, mpz_srcptr n, unsigned long k)
31: {
32: mpz_t got;
33:
34: mpz_init (got);
35: mpz_bin_ui (got, n, k);
36: MPZ_CHECK_FORMAT (got);
37: if (mpz_cmp (got, want) != 0)
38: {
39: printf ("mpz_bin_ui wrong\n");
40: printf (" n="); mpz_out_str (stdout, 10, n); printf ("\n");
41: printf (" k=%lu\n", k);
42: printf (" got="); mpz_out_str (stdout, 10, got); printf ("\n");
43: printf (" want="); mpz_out_str (stdout, 10, want); printf ("\n");
44: abort();
45: }
46: mpz_clear (got);
47: }
48:
49:
50: void
51: try_mpz_bin_uiui (mpz_srcptr want, unsigned long n, unsigned long k)
52: {
53: mpz_t got;
54:
55: mpz_init (got);
56: mpz_bin_uiui (got, n, k);
57: MPZ_CHECK_FORMAT (got);
58: if (mpz_cmp (got, want) != 0)
59: {
60: printf ("mpz_bin_uiui wrong\n");
61: printf (" n=%lu\n", n);
62: printf (" k=%lu\n", k);
63: printf (" got="); mpz_out_str (stdout, 10, got); printf ("\n");
64: printf (" want="); mpz_out_str (stdout, 10, want); printf ("\n");
65: abort();
66: }
67: mpz_clear (got);
68: }
69:
70:
71: void
72: samples (void)
73: {
74: static const struct {
75: const char *n;
76: unsigned long k;
77: const char *want;
78: } data[] = {
79:
80: { "0", 0, "1" },
81: { "0", 1, "0" },
82: { "0", 2, "0" },
83: { "0", 3, "0" },
84: { "0", 4, "0" },
85: { "0", 123456, "0" },
86:
87: { "1", 0, "1" },
88: { "1", 1, "1" },
89: { "1", 2, "0" },
90: { "1", 3, "0" },
91: { "1", 4, "0" },
92: { "1", 123456, "0" },
93:
94: { "2", 0, "1" },
95: { "2", 1, "2" },
96: { "2", 2, "1" },
97: { "2", 3, "0" },
98: { "2", 4, "0" },
99: { "2", 123456, "0" },
100:
101: { "3", 0, "1" },
102: { "3", 1, "3" },
103: { "3", 2, "3" },
104: { "3", 3, "1" },
105: { "3", 4, "0" },
106: { "3", 5, "0" },
107: { "3", 123456, "0" },
108:
109: { "4", 0, "1" },
110: { "4", 1, "4" },
111: { "4", 2, "6" },
112: { "4", 3, "4" },
113: { "4", 4, "1" },
114: { "4", 5, "0" },
115: { "4", 6, "0" },
116: { "4", 123456, "0" },
117:
118: { "10", 0, "1" },
119: { "10", 1, "10" },
120: { "10", 2, "45" },
121: { "10", 3, "120" },
122: { "10", 4, "210" },
123: { "10", 5, "252" },
124: { "10", 6, "210" },
125: { "10", 7, "120" },
126: { "10", 8, "45" },
127: { "10", 9, "10" },
128: { "10", 10, "1" },
129: { "10", 11, "0" },
130: { "10", 12, "0" },
131: { "10", 123456, "0" },
132:
133: /* negatives, using bin(-n,k)=bin(n+k-1,k) */
134: { "-1", 0, "1" },
135: { "-1", 1, "-1" },
136: { "-1", 2, "1" },
137: { "-1", 3, "-1" },
138: { "-1", 4, "1" },
139:
140: { "-2", 0, "1" },
141: { "-2", 1, "-2" },
142: { "-2", 2, "3" },
143: { "-2", 3, "-4" },
144: { "-2", 4, "5" },
145: { "-2", 5, "-6" },
146: { "-2", 6, "7" },
147:
148: { "-3", 0, "1" },
149: { "-3", 1, "-3" },
150: { "-3", 2, "6" },
151: { "-3", 3, "-10" },
152: { "-3", 4, "15" },
153: { "-3", 5, "-21" },
154: { "-3", 6, "28" },
155:
156: { "40", 20, "137846528820" },
157: { "60", 30, "118264581564861424" },
158: };
159:
160: mpz_t n, want;
161: int i;
162:
163: mpz_init (n);
164: mpz_init (want);
165:
166: for (i = 0; i < numberof (data); i++)
167: {
168: mpz_set_str_or_abort (n, data[i].n, 0);
169: mpz_set_str_or_abort (want, data[i].want, 0);
170:
171: try_mpz_bin_ui (want, n, data[i].k);
172:
173: if (mpz_fits_ulong_p (n))
174: try_mpz_bin_uiui (want, mpz_get_ui (n), data[i].k);
175: }
176:
177: mpz_clear (n);
178: mpz_clear (want);
179: }
180:
181:
182: /* Test some bin(2k,k) cases. This produces some biggish numbers to
183: exercise the limb accumulating code. */
184: void
185: twos (void)
186: {
187: mpz_t n, want;
188: unsigned long k;
189:
190: mpz_init (n);
191: mpz_init (want);
192:
193: mpz_set_ui (want, (unsigned long) 2);
194: for (k = 1; k < 200; k++)
195: {
196: mpz_set_ui (n, 2*k);
197: try_mpz_bin_ui (want, n, k);
198:
199: try_mpz_bin_uiui (want, 2*k, k);
200:
201: mpz_mul_ui (want, want, 2*(2*k+1));
202: mpz_fdiv_q_ui (want, want, k+1);
203: }
204:
205: mpz_clear (n);
206: mpz_clear (want);
207: }
208:
209:
210: int
211: main (void)
212: {
213: tests_start ();
214:
215: samples ();
216: twos ();
217:
218: tests_end ();
219: exit (0);
220: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>