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