~ubuntu-branches/ubuntu/vivid/gcl/vivid

« back to all changes in this revision

Viewing changes to gmp/mpz/tests/t-bin.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2002-03-04 14:29:59 UTC
  • Revision ID: james.westby@ubuntu.com-20020304142959-dey14w08kr7lldu3
Tags: upstream-2.5.0.cvs20020219
ImportĀ upstreamĀ versionĀ 2.5.0.cvs20020219

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Exercise mpz_bin_ui and mpz_bin_uiui. */
 
2
 
 
3
/*
 
4
Copyright (C) 2000 Free Software Foundation, Inc.
 
5
 
 
6
This file is part of the GNU MP Library.
 
7
 
 
8
The GNU MP Library is free software; you can redistribute it and/or modify
 
9
it under the terms of the GNU Lesser General Public License as published by
 
10
the Free Software Foundation; either version 2.1 of the License, or (at your
 
11
option) any later version.
 
12
 
 
13
The GNU MP Library is distributed in the hope that it will be useful, but
 
14
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
15
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
16
License for more details.
 
17
 
 
18
You should have received a copy of the GNU Lesser General Public License
 
19
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
 
20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
21
MA 02111-1307, USA.
 
22
*/
 
23
 
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
#include "gmp.h"
 
28
#include "gmp-impl.h"
 
29
 
 
30
size_t
 
31
#if defined(__STDC__)
 
32
  mpz_out_str (FILE *stream, int base, mpz_srcptr x);
 
33
#else
 
34
  mpz_out_str (stream, base, x);
 
35
#endif
 
36
 
 
37
void
 
38
try_mpz_bin_ui (mpz_srcptr want, mpz_srcptr n, unsigned long k)
 
39
{
 
40
  mpz_t  got;
 
41
 
 
42
  mpz_init (got);
 
43
  mpz_bin_ui (got, n, k);
 
44
  MPZ_CHECK_FORMAT (got);
 
45
  if (mpz_cmp (got, want) != 0)
 
46
    {
 
47
      printf ("mpz_bin_ui wrong\n");
 
48
      printf ("  n="); mpz_out_str (stdout, 10, n); printf ("\n");
 
49
      printf ("  k=%lu\n", k);
 
50
      printf ("  got="); mpz_out_str (stdout, 10, got); printf ("\n");
 
51
      printf ("  want="); mpz_out_str (stdout, 10, want); printf ("\n");
 
52
      abort();                                    
 
53
    }
 
54
  mpz_clear (got);
 
55
}
 
56
 
 
57
 
 
58
void
 
59
try_mpz_bin_uiui (mpz_srcptr want, unsigned long n, unsigned long k)
 
60
{
 
61
  mpz_t  got;
 
62
 
 
63
  mpz_init (got);
 
64
  mpz_bin_uiui (got, n, k);
 
65
  MPZ_CHECK_FORMAT (got);
 
66
  if (mpz_cmp (got, want) != 0)
 
67
    {
 
68
      printf ("mpz_bin_uiui wrong\n");
 
69
      printf ("  n=%lu\n", n);
 
70
      printf ("  k=%lu\n", k);
 
71
      printf ("  got="); mpz_out_str (stdout, 10, got); printf ("\n");
 
72
      printf ("  want="); mpz_out_str (stdout, 10, want); printf ("\n");
 
73
      abort();                                    
 
74
    }
 
75
  mpz_clear (got);
 
76
}
 
77
 
 
78
 
 
79
void
 
80
samples (void)
 
81
{
 
82
  static const struct {
 
83
    const char     *n;
 
84
    unsigned long  k;
 
85
    const char     *want;
 
86
  } data[] = {
 
87
 
 
88
    {   "0",  0, "1"   },
 
89
    {   "0",  1, "0"   },
 
90
    {   "0",  2, "0"   },
 
91
    {   "0",  3, "0"   },
 
92
    {   "0",  4, "0"   },
 
93
    {   "0", 123456, "0" },
 
94
 
 
95
    {   "1",  0, "1"   },
 
96
    {   "1",  1, "1"   },
 
97
    {   "1",  2, "0"   },
 
98
    {   "1",  3, "0"   },
 
99
    {   "1",  4, "0"   },
 
100
    {   "1", 123456, "0" },
 
101
 
 
102
    {   "2",  0, "1"   },
 
103
    {   "2",  1, "2"   },
 
104
    {   "2",  2, "1"   },
 
105
    {   "2",  3, "0"   },
 
106
    {   "2",  4, "0"   },
 
107
    {   "2", 123456, "0" },
 
108
 
 
109
    {   "3",  0, "1"   },
 
110
    {   "3",  1, "3"   },
 
111
    {   "3",  2, "3"   },
 
112
    {   "3",  3, "1"   },
 
113
    {   "3",  4, "0"   },
 
114
    {   "3",  5, "0"   },
 
115
    {   "3", 123456, "0" },
 
116
 
 
117
    {   "4",  0, "1"   },
 
118
    {   "4",  1, "4"   },
 
119
    {   "4",  2, "6"   },
 
120
    {   "4",  3, "4"   },
 
121
    {   "4",  4, "1"   },
 
122
    {   "4",  5, "0"   },
 
123
    {   "4",  6, "0"   },
 
124
    {   "4", 123456, "0" },
 
125
 
 
126
    {   "10",  0, "1"   },
 
127
    {   "10",  1, "10"  },
 
128
    {   "10",  2, "45"  },
 
129
    {   "10",  3, "120" },
 
130
    {   "10",  4, "210" },
 
131
    {   "10",  5, "252" },
 
132
    {   "10",  6, "210" },
 
133
    {   "10",  7, "120" },
 
134
    {   "10",  8, "45"  },
 
135
    {   "10",  9, "10"  },
 
136
    {   "10", 10, "1"   },
 
137
    {   "10", 11,     "0" },
 
138
    {   "10", 12,     "0" },
 
139
    {   "10", 123456, "0" },
 
140
 
 
141
    /* negatives, using bin(-n,k)=bin(n+k-1,k) */
 
142
    {   "-1",  0,  "1"  },
 
143
    {   "-1",  1, "-1"  },
 
144
    {   "-1",  2,  "1"  },
 
145
    {   "-1",  3, "-1"  },
 
146
    {   "-1",  4,  "1"  },
 
147
 
 
148
    {   "-2",  0,  "1"  },
 
149
    {   "-2",  1, "-2"  },
 
150
    {   "-2",  2,  "3"  },
 
151
    {   "-2",  3, "-4"  },
 
152
    {   "-2",  4,  "5"  },
 
153
    {   "-2",  5, "-6"  },
 
154
    {   "-2",  6,  "7"  },
 
155
 
 
156
    {   "-3",  0,   "1"  },
 
157
    {   "-3",  1,  "-3"  },
 
158
    {   "-3",  2,   "6"  },
 
159
    {   "-3",  3, "-10"  },
 
160
    {   "-3",  4,  "15"  },
 
161
    {   "-3",  5, "-21"  },
 
162
    {   "-3",  6,  "28"  },
 
163
 
 
164
    {   "40", 20,  "137846528820" },
 
165
    {   "60", 30,  "118264581564861424" },
 
166
  };
 
167
 
 
168
  mpz_t  n, want;
 
169
  int    i;
 
170
 
 
171
  mpz_init (n);
 
172
  mpz_init (want);
 
173
 
 
174
  for (i = 0; i < numberof (data); i++)
 
175
    {
 
176
      mpz_set_str (n, data[i].n, 0);
 
177
      mpz_set_str (want, data[i].want, 0);
 
178
 
 
179
      try_mpz_bin_ui (want, n, data[i].k);
 
180
 
 
181
      if (mpz_fits_ulong_p (n))
 
182
        try_mpz_bin_uiui (want, mpz_get_ui (n), data[i].k);
 
183
    }
 
184
 
 
185
  mpz_clear (n);
 
186
  mpz_clear (want);
 
187
}
 
188
 
 
189
 
 
190
/* Test some bin(2k,k) cases.  This produces some biggish numbers to
 
191
   exercise the limb accumulating code.  */
 
192
void
 
193
twos (void)
 
194
{
 
195
  mpz_t          n, want;
 
196
  unsigned long  k;
 
197
 
 
198
  mpz_init (n);
 
199
  mpz_init (want);
 
200
 
 
201
  mpz_set_ui (want, (unsigned long) 2);
 
202
  for (k = 1; k < 200; k++)
 
203
    {
 
204
      mpz_set_ui (n, 2*k);
 
205
      try_mpz_bin_ui (want, n, k);
 
206
 
 
207
      try_mpz_bin_uiui (want, 2*k, k);
 
208
 
 
209
      mpz_mul_ui (want, want, 2*(2*k+1));
 
210
      mpz_fdiv_q_ui (want, want, k+1);
 
211
    }
 
212
 
 
213
  mpz_clear (n);
 
214
  mpz_clear (want);
 
215
}
 
216
 
 
217
 
 
218
int
 
219
main (void)
 
220
{
 
221
  samples ();
 
222
  twos ();
 
223
 
 
224
  exit (0);
 
225
}