#
source:
trunk/third/gmp/tests/rand/t-lc2exp.c
@
22254

Revision 22254, 4.7 KB checked in by ghudson, 19 years ago (diff) |
---|

Line | |
---|---|

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 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 | /* a=0 and c=0 produces zero results always. */ |

30 | void |

31 | check_zero (unsigned long m2exp) |

32 | { |

33 | gmp_randstate_t r; |

34 | mpz_t a; |

35 | unsigned long c; |

36 | int i; |

37 | |

38 | mpz_init_set_ui (a, 0L); |

39 | c = 0L; |

40 | |

41 | gmp_randinit_lc_2exp (r, a, c, m2exp); |

42 | gmp_randseed_ui (r, 0L); |

43 | |

44 | for (i = 0; i < 5; i++) |

45 | { |

46 | mpz_urandomb (a, r, 123L); |

47 | if (mpz_sgn (a) != 0) |

48 | { |

49 | printf ("check_zero m2exp=%lu: didn't get zero\n", m2exp); |

50 | gmp_printf (" rand=%#Zx\n", a); |

51 | abort (); |

52 | } |

53 | } |

54 | |

55 | mpz_clear (a); |

56 | gmp_randclear (r); |

57 | } |

58 | |

59 | /* negative a */ |

60 | void |

61 | check_nega (void) |

62 | { |

63 | gmp_randstate_t r; |

64 | mpz_t a; |

65 | unsigned long c, m2exp; |

66 | int i; |

67 | |

68 | mpz_init (a); |

69 | mpz_setbit (a, 1000L); |

70 | mpz_neg (a, a); |

71 | c = 0L; |

72 | m2exp = 45L; |

73 | |

74 | gmp_randinit_lc_2exp (r, a, c, m2exp); |

75 | gmp_randseed_ui (r, 0L); |

76 | |

77 | for (i = 0; i < 5; i++) |

78 | { |

79 | mpz_urandomb (a, r, 123L); |

80 | if (mpz_sgn (a) != 0) |

81 | printf ("check_nega m2exp=%lu: didn't get zero\n", m2exp); |

82 | } |

83 | |

84 | mpz_clear (a); |

85 | gmp_randclear (r); |

86 | } |

87 | |

88 | void |

89 | check_bigc (void) |

90 | { |

91 | gmp_randstate_t r; |

92 | mpz_t a; |

93 | unsigned long c, m2exp, bits; |

94 | int i; |

95 | |

96 | mpz_init_set_ui (a, 0L); |

97 | c = ULONG_MAX; |

98 | m2exp = 8; |

99 | |

100 | gmp_randinit_lc_2exp (r, a, c, m2exp); |

101 | gmp_randseed_ui (r, 0L); |

102 | |

103 | for (i = 0; i < 20; i++) |

104 | { |

105 | bits = 123L; |

106 | mpz_urandomb (a, r, bits); |

107 | if (mpz_sgn (a) < 0 || mpz_sizeinbase (a, 2) > bits) |

108 | { |

109 | printf ("check_bigc: mpz_urandomb out of range\n"); |

110 | printf (" m2exp=%lu\n", m2exp); |

111 | gmp_printf (" rand=%#ZX\n", a); |

112 | gmp_printf (" sizeinbase2=%u\n", mpz_sizeinbase (a, 2)); |

113 | } |

114 | } |

115 | |

116 | mpz_clear (a); |

117 | gmp_randclear (r); |

118 | } |

119 | |

120 | /* In the past c bigger than m2exp bits was not truncated, and could end up |

121 | producing extra bits from _gmp_rand and consequently into mpz_urandomb. |

122 | The following tries a=0 c=ULONG_MAX m2exp=2 expecting to see mpz_urandomb |

123 | ask for 1 bit give a value 1 every time. */ |

124 | void |

125 | check_bigc1 (void) |

126 | { |

127 | gmp_randstate_t r; |

128 | mpz_t a; |

129 | unsigned long c, m2exp; |

130 | int i; |

131 | |

132 | mpz_init_set_ui (a, 0L); |

133 | c = ULONG_MAX; |

134 | m2exp = 2; |

135 | |

136 | gmp_randinit_lc_2exp (r, a, c, m2exp); |

137 | gmp_randseed_ui (r, 0L); |

138 | |

139 | for (i = 0; i < 20; i++) |

140 | { |

141 | mpz_urandomb (a, r, 1L); |

142 | if (mpz_cmp_ui (a, 1L) != 0) |

143 | { |

144 | printf ("check_bigc1: mpz_urandomb didn't give 1\n"); |

145 | printf (" m2exp=%lu\n", m2exp); |

146 | gmp_printf (" got rand=%#ZX\n", a); |

147 | abort (); |

148 | } |

149 | } |

150 | |

151 | mpz_clear (a); |

152 | gmp_randclear (r); |

153 | } |

154 | |

155 | /* Checks parameters which triggered an assertion failure in the past. |

156 | Happened when limbs(a)+limbs(c) < bits_to_limbs(m2exp). */ |

157 | void |

158 | check_bigm (void) |

159 | { |

160 | gmp_randstate_t rstate; |

161 | mpz_t a; |

162 | |

163 | mpz_init_set_ui (a, 5L); |

164 | gmp_randinit_lc_2exp (rstate, a, 1L, 384L); |

165 | |

166 | mpz_urandomb (a, rstate, 20L); |

167 | |

168 | gmp_randclear (rstate); |

169 | mpz_clear (a); |

170 | } |

171 | |

172 | /* Checks for seeds bigger than the modulus. */ |

173 | void |

174 | check_bigs (void) |

175 | { |

176 | gmp_randstate_t rstate; |

177 | mpz_t sd, a; |

178 | int i; |

179 | |

180 | mpz_init (sd); |

181 | mpz_setbit (sd, 300L); |

182 | mpz_sub_ui (sd, sd, 1L); |

183 | mpz_clrbit (sd, 13L); |

184 | mpz_init_set_ui (a, 123456789L); |

185 | |

186 | gmp_randinit_lc_2exp (rstate, a, 5L, 64L); |

187 | |

188 | for (i = 0; i < 20; i++) |

189 | { |

190 | mpz_neg (sd, sd); |

191 | gmp_randseed (rstate, sd); |

192 | mpz_mul_ui (sd, sd, 7L); |

193 | |

194 | mpz_urandomb (a, rstate, 80L); |

195 | } |

196 | |

197 | gmp_randclear (rstate); |

198 | mpz_clear (a); |

199 | mpz_clear (sd); |

200 | } |

201 | |

202 | int |

203 | main (void) |

204 | { |

205 | tests_start (); |

206 | |

207 | check_zero (2L); |

208 | check_zero (7L); |

209 | check_zero (32L); |

210 | check_zero (64L); |

211 | check_zero (1000L); |

212 | |

213 | check_nega (); |

214 | check_bigc (); |

215 | check_bigc1 (); |

216 | |

217 | check_bigm (); |

218 | check_bigs (); |

219 | |

220 | tests_end (); |

221 | exit (0); |

222 | } |

**Note:**See TracBrowser for help on using the repository browser.