Upload Tizen:Base source
[external/gmp.git] / tests / rand / t-lc2exp.c
1 /* Exercise the lc2exp random functions.
2
3 Copyright 2002 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 3 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.  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. */
28 void
29 check_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 */
58 void
59 check_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
86 void
87 check_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         }
112     }
113
114   mpz_clear (a);
115   gmp_randclear (r);
116 }
117
118 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
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
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
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 }