t-lc2exp.c revision 1.1.1.2
1/* Exercise the lc2exp random functions.
2
3Copyright 2002, 2011 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library test suite.
6
7The GNU MP Library test suite is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 3 of the License,
10or (at your option) any later version.
11
12The GNU MP Library test suite is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15Public License for more details.
16
17You should have received a copy of the GNU General Public License along with
18the GNU MP Library test suite.  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/* a=0 and c=0 produces zero results always. */
28void
29check_zero (unsigned long m2exp)
30{
31  gmp_randstate_t  r;
32  mpz_t            a;
33  unsigned long    c;
34  int              i;
35
36  mpz_init_set_ui (a, 0L);
37  c = 0L;
38
39  gmp_randinit_lc_2exp (r, a, c, m2exp);
40  gmp_randseed_ui (r, 0L);
41
42  for (i = 0; i < 5; i++)
43    {
44      mpz_urandomb (a, r, 123L);
45      if (mpz_sgn (a) != 0)
46        {
47          printf ("check_zero m2exp=%lu: didn't get zero\n", m2exp);
48          gmp_printf ("  rand=%#Zx\n", a);
49          abort ();
50        }
51    }
52
53  mpz_clear (a);
54  gmp_randclear (r);
55}
56
57/* negative a */
58void
59check_nega (void)
60{
61  gmp_randstate_t  r;
62  mpz_t            a;
63  unsigned long    c, m2exp;
64  int              i;
65
66  mpz_init (a);
67  mpz_setbit (a, 1000L);
68  mpz_neg (a, a);
69  c = 0L;
70  m2exp = 45L;
71
72  gmp_randinit_lc_2exp (r, a, c, m2exp);
73  gmp_randseed_ui (r, 0L);
74
75  for (i = 0; i < 5; i++)
76    {
77      mpz_urandomb (a, r, 123L);
78      if (mpz_sgn (a) != 0)
79        printf ("check_nega m2exp=%lu: didn't get zero\n", m2exp);
80    }
81
82  mpz_clear (a);
83  gmp_randclear (r);
84}
85
86void
87check_bigc (void)
88{
89  gmp_randstate_t  r;
90  mpz_t            a;
91  unsigned long    c, m2exp, bits;
92  int              i;
93
94  mpz_init_set_ui (a, 0L);
95  c = ULONG_MAX;
96  m2exp = 8;
97
98  gmp_randinit_lc_2exp (r, a, c, m2exp);
99  gmp_randseed_ui (r, 0L);
100
101  for (i = 0; i < 20; i++)
102    {
103      bits = 123L;
104      mpz_urandomb (a, r, bits);
105      if (mpz_sgn (a) < 0 || mpz_sizeinbase (a, 2) > bits)
106        {
107          printf     ("check_bigc: mpz_urandomb out of range\n");
108          printf     ("   m2exp=%lu\n", m2exp);
109          gmp_printf ("   rand=%#ZX\n", a);
110          gmp_printf ("   sizeinbase2=%u\n", mpz_sizeinbase (a, 2));
111	  abort ();
112        }
113    }
114
115  mpz_clear (a);
116  gmp_randclear (r);
117}
118
119void
120check_bigc1 (void)
121{
122  gmp_randstate_t  r;
123  mpz_t            a;
124  unsigned long    c, m2exp;
125  int              i;
126
127  mpz_init_set_ui (a, 0L);
128  c = ULONG_MAX;
129  m2exp = 2;
130
131  gmp_randinit_lc_2exp (r, a, c, m2exp);
132  gmp_randseed_ui (r, 0L);
133
134  for (i = 0; i < 20; i++)
135    {
136      mpz_urandomb (a, r, 1L);
137      if (mpz_cmp_ui (a, 1L) != 0)
138        {
139          printf     ("check_bigc1: mpz_urandomb didn't give 1\n");
140          printf     ("   m2exp=%lu\n", m2exp);
141          gmp_printf ("   got rand=%#ZX\n", a);
142          abort ();
143        }
144    }
145
146  mpz_clear (a);
147  gmp_randclear (r);
148}
149
150/* Checks parameters which triggered an assertion failure in the past.
151   Happened when limbs(a)+limbs(c) < bits_to_limbs(m2exp).  */
152void
153check_bigm (void)
154{
155  gmp_randstate_t rstate;
156  mpz_t a;
157
158  mpz_init_set_ui (a, 5L);
159  gmp_randinit_lc_2exp (rstate, a, 1L, 384L);
160
161  mpz_urandomb (a, rstate, 20L);
162
163  gmp_randclear (rstate);
164  mpz_clear (a);
165}
166
167/* Checks for seeds bigger than the modulus.  */
168void
169check_bigs (void)
170{
171  gmp_randstate_t rstate;
172  mpz_t sd, a;
173  int i;
174
175  mpz_init (sd);
176  mpz_setbit (sd, 300L);
177  mpz_sub_ui (sd, sd, 1L);
178  mpz_clrbit (sd, 13L);
179  mpz_init_set_ui (a, 123456789L);
180
181  gmp_randinit_lc_2exp (rstate, a, 5L, 64L);
182
183  for (i = 0; i < 20; i++)
184    {
185      mpz_neg (sd, sd);
186      gmp_randseed (rstate, sd);
187      mpz_mul_ui (sd, sd, 7L);
188
189      mpz_urandomb (a, rstate, 80L);
190    }
191
192  gmp_randclear (rstate);
193  mpz_clear (a);
194  mpz_clear (sd);
195}
196
197int
198main (void)
199{
200  tests_start ();
201
202  check_zero (2L);
203  check_zero (7L);
204  check_zero (32L);
205  check_zero (64L);
206  check_zero (1000L);
207
208  check_nega ();
209  check_bigc ();
210  check_bigc1 ();
211
212  check_bigm ();
213  check_bigs ();
214
215  tests_end ();
216  exit (0);
217}
218