Commit | Line | Data |
a0d0e21e |
1 | #!./perl |
2 | |
ec761cee |
3 | # From Tom Phoenix <rootbeer@teleport.com> 22 Feb 1997 |
4 | # Based upon a test script by kgb@ast.cam.ac.uk (Karl Glazebrook) |
a0d0e21e |
5 | |
ec761cee |
6 | # Looking for the hints? You're in the right place. |
7 | # The hints are near each test, so search for "TEST #", where |
8 | # the pound sign is replaced by the number of the test. |
a0d0e21e |
9 | |
ec761cee |
10 | # I'd like to include some more robust tests, but anything |
11 | # too subtle to be detected here would require a time-consuming |
12 | # test. Also, of course, we're here to detect only flaws in Perl; |
13 | # if there are flaws in the underlying system rand, that's not |
14 | # our responsibility. But if you want better tests, see |
15 | # The Art of Computer Programming, Donald E. Knuth, volume 2, |
16 | # chapter 3. ISBN 0-201-03822-6 (v. 2) |
a0d0e21e |
17 | |
ec761cee |
18 | BEGIN { |
19 | chdir "t" if -d "t"; |
20 | @INC = "../lib" if -d "../lib"; |
748a9306 |
21 | } |
748a9306 |
22 | |
ec761cee |
23 | use strict; |
24 | use Config; |
3524d3b9 |
25 | |
ec761cee |
26 | print "1..11\n"; |
27 | |
28 | srand; # Shouldn't need this with 5.004... |
29 | # But I'll include it now and test for |
30 | # whether we needed it later. |
31 | |
32 | my $reps = 1000; # How many times to try rand each time. |
33 | # May be changed, but should be over 500. |
34 | # The more the better! (But slower.) |
35 | |
36 | sub bits ($) { |
37 | # Takes a small integer and returns the number of one-bits in it. |
38 | my $total; |
39 | my $bits = sprintf "%o", $_[0]; |
40 | while (length $bits) { |
41 | $total += (0,1,1,2,1,2,2,3)[chop $bits]; # Oct to bits |
42 | } |
43 | $total; |
a0d0e21e |
44 | } |
45 | |
ec761cee |
46 | # First, let's see whether randbits is set right |
47 | { |
48 | my($max, $min, $sum); # Characteristics of rand |
49 | my($off, $shouldbe); # Problems with randbits |
50 | my($dev, $bits); # Number of one bits |
51 | my $randbits = $Config{randbits}; |
52 | $max = $min = rand(1); |
53 | for (1..$reps) { |
54 | my $n = rand(1); |
55 | $sum += $n; |
56 | $bits += bits($n * 256); # Don't be greedy; 8 is enough |
57 | # It's too many if randbits is less than 8! |
58 | # But that should never be the case... I hope. |
59 | # Note: If you change this, you must adapt the |
60 | # formula for absolute standard deviation, below. |
61 | $max = $n if $n > $max; |
62 | $min = $n if $n < $min; |
63 | } |
64 | |
65 | |
66 | # Hints for TEST 1 |
67 | # |
68 | # This test checks for one of Perl's most frequent |
69 | # mis-configurations. Your system's documentation |
70 | # for rand(2) should tell you what value you need |
71 | # for randbits. Usually the diagnostic message |
72 | # has the right value as well. Just fix it and |
73 | # recompile, and you'll usually be fine. (The main |
74 | # reason that the diagnostic message might get the |
75 | # wrong value is that Config.pm is incorrect.) |
76 | # |
4afbaa31 |
77 | if ($max <= 0 or $max >= (2 ** $randbits)) {# Just in case... |
ec761cee |
78 | print "not ok 1\n"; |
79 | print "# This perl was compiled with randbits=$randbits\n"; |
80 | print "# which is _way_ off. Or maybe your system rand is broken,\n"; |
81 | print "# or your C compiler can't multiply, or maybe Martians\n"; |
82 | print "# have taken over your computer. For starters, see about\n"; |
1e422769 |
83 | print "# trying a better value for randbits, probably smaller.\n"; |
ec761cee |
84 | # If that isn't the problem, we'll have |
85 | # to put d_martians into Config.pm |
86 | print "# Skipping remaining tests until randbits is fixed.\n"; |
87 | exit; |
88 | } |
89 | |
90 | $off = log($max) / log(2); # log2 |
91 | $off = int($off) + ($off > 0); # Next more positive int |
92 | if ($off) { |
93 | $shouldbe = $Config{randbits} + $off; |
94 | print "not ok 1\n"; |
95 | print "# This perl was compiled with randbits=$randbits on $^O.\n"; |
96 | print "# Consider using randbits=$shouldbe instead.\n"; |
97 | # And skip the remaining tests; they would be pointless now. |
98 | print "# Skipping remaining tests until randbits is fixed.\n"; |
99 | exit; |
100 | } else { |
101 | print "ok 1\n"; |
102 | } |
103 | |
104 | # Hints for TEST 2 |
105 | # |
106 | # This should always be true: 0 <= rand(1) < 1 |
107 | # If this test is failing, something is seriously wrong, |
108 | # either in perl or your system's rand function. |
109 | # |
110 | if ($min < 0 or $max >= 1) { # Slightly redundant... |
111 | print "not ok 2\n"; |
112 | print "# min too low\n" if $min < 0; |
113 | print "# max too high\n" if $max >= 1; |
114 | } else { |
115 | print "ok 2\n"; |
116 | } |
117 | |
118 | # Hints for TEST 3 |
119 | # |
120 | # This is just a crude test. The average number produced |
121 | # by rand should be about one-half. But once in a while |
122 | # it will be relatively far away. Note: This test will |
123 | # occasionally fail on a perfectly good system! |
124 | # See the hints for test 4 to see why. |
125 | # |
126 | $sum /= $reps; |
127 | if ($sum < 0.4 or $sum > 0.6) { |
128 | print "not ok 3\n# Average random number is far from 0.5\n"; |
129 | } else { |
130 | print "ok 3\n"; |
131 | } |
a0d0e21e |
132 | |
ec761cee |
133 | # Hints for TEST 4 |
134 | # |
135 | # NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE |
136 | # This test will fail .1% of the time on a normal system. |
137 | # also |
138 | # This test asks you to see these hints 100% of the time! |
139 | # NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE |
140 | # |
141 | # There is probably no reason to be alarmed that |
142 | # something is wrong with your rand function. But, |
143 | # if you're curious or if you can't help being |
144 | # alarmed, keep reading. |
145 | # |
146 | # This is a less-crude test than test 3. But it has |
147 | # the same basic flaw: Unusually distributed random |
148 | # values should occasionally appear in every good |
149 | # random number sequence. (If you flip a fair coin |
150 | # twenty times every day, you'll see it land all |
151 | # heads about one time in a million days, on the |
152 | # average. That might alarm you if you saw it happen |
153 | # on the first day!) |
154 | # |
155 | # So, if this test failed on you once, run it a dozen |
156 | # times. If it keeps failing, it's likely that your |
157 | # rand is bogus. If it keeps passing, it's likely |
158 | # that the one failure was bogus. If it's a mix, |
159 | # read on to see about how to interpret the tests. |
160 | # |
161 | # The number printed in square brackets is the |
162 | # standard deviation, a statistical measure |
163 | # of how unusual rand's behavior seemed. It should |
164 | # fall in these ranges with these *approximate* |
165 | # probabilities: |
166 | # |
167 | # under 1 68.26% of the time |
168 | # 1-2 27.18% of the time |
169 | # 2-3 4.30% of the time |
170 | # over 3 0.26% of the time |
171 | # |
172 | # If the numbers you see are not scattered approximately |
173 | # (not exactly!) like that table, check with your vendor |
174 | # to find out what's wrong with your rand. Or with this |
175 | # algorithm. :-) |
176 | # |
177 | # Calculating absoulute standard deviation for number of bits set |
178 | # (eight bits per rep) |
179 | $dev = abs ($bits - $reps * 4) / sqrt($reps * 2); |
748a9306 |
180 | |
ec761cee |
181 | if ($dev < 1.96) { |
182 | print "ok 4\n"; # 95% of the time. |
183 | print "# Your rand seems fine. If this test failed\n"; |
184 | print "# previously, you may want to run it again.\n"; |
185 | } elsif ($dev < 2.575) { |
186 | print "ok 4\n# In here about 4% of the time. Hmmm...\n"; |
187 | print "# This is ok, but suspicious. But it will happen\n"; |
188 | print "# one time out of 25, more or less.\n"; |
189 | print "# You should run this test again to be sure.\n"; |
190 | } elsif ($dev < 3.3) { |
191 | print "ok 4\n# In this range about 1% of the time.\n"; |
192 | print "# This is very suspicious. It will happen only\n"; |
193 | print "# about one time out of 100, more or less.\n"; |
194 | print "# You should run this test again to be sure.\n"; |
195 | } elsif ($dev < 3.9) { |
196 | print "not ok 4\n# In this range very rarely.\n"; |
197 | print "# This is VERY suspicious. It will happen only\n"; |
198 | print "# about one time out of 1000, more or less.\n"; |
199 | print "# You should run this test again to be sure.\n"; |
200 | } else { |
201 | print "not ok 4\n# Seriously whacked.\n"; |
202 | print "# This is VERY VERY suspicious.\n"; |
203 | print "# Your rand seems to be bogus.\n"; |
204 | } |
205 | print "#\n# If you are having random number troubles,\n"; |
206 | print "# see the hints within the test script for more\n"; |
207 | printf "# information on why this might fail. [ %.3f ]\n", $dev; |
748a9306 |
208 | } |
748a9306 |
209 | |
ec761cee |
210 | { |
211 | srand; # These three lines are for test 7 |
212 | my $time = time; # It's just faster to do them here. |
270f084f |
213 | my $rand = join ", ", rand, rand, rand; |
ec761cee |
214 | |
215 | # Hints for TEST 5 |
216 | # |
217 | # This test checks that the argument to srand actually |
218 | # sets the seed for generating random numbers. |
219 | # |
220 | srand(3.14159); |
221 | my $r = rand; |
222 | srand(3.14159); |
223 | if (rand != $r) { |
224 | print "not ok 5\n"; |
225 | print "# srand is not consistent.\n"; |
226 | } else { |
227 | print "ok 5\n"; |
228 | } |
229 | |
230 | # Hints for TEST 6 |
231 | # |
232 | # This test just checks that the previous one didn't |
233 | # give us false confidence! |
234 | # |
235 | if (rand == $r) { |
236 | print "not ok 6\n"; |
237 | print "# rand is now unchanging!\n"; |
238 | } else { |
239 | print "ok 6\n"; |
240 | } |
241 | |
242 | # Hints for TEST 7 |
243 | # |
244 | # This checks that srand without arguments gives |
245 | # different sequences each time. Note: You shouldn't |
246 | # be calling srand more than once unless you know |
247 | # what you're doing! But if this fails on your |
248 | # system, run perlbug and let the developers know |
249 | # what other sources of randomness srand should |
250 | # tap into. |
251 | # |
252 | while ($time == time) { } # Wait for new second, just in case. |
253 | srand; |
270f084f |
254 | if ((join ", ", rand, rand, rand) eq $rand) { |
ec761cee |
255 | print "not ok 7\n"; |
256 | print "# srand without args isn't varying.\n"; |
257 | } else { |
258 | print "ok 7\n"; |
259 | } |
a0d0e21e |
260 | } |
261 | |
ec761cee |
262 | # Now, let's see whether rand accepts its argument |
263 | { |
264 | my($max, $min); |
265 | $max = $min = rand(100); |
266 | for (1..$reps) { |
267 | my $n = rand(100); |
268 | $max = $n if $n > $max; |
269 | $min = $n if $n < $min; |
270 | } |
271 | |
272 | # Hints for TEST 8 |
273 | # |
274 | # This test checks to see that rand(100) really falls |
275 | # within the range 0 - 100, and that the numbers produced |
276 | # have a reasonably-large range among them. |
277 | # |
278 | if ($min < 0 or $max >= 100 or ($max - $min) < 65) { |
279 | print "not ok 8\n"; |
280 | print "# min too low\n" if $min < 0; |
281 | print "# max too high\n" if $max >= 100; |
282 | print "# range too narrow\n" if ($max - $min) < 65; |
283 | } else { |
284 | print "ok 8\n"; |
285 | } |
3524d3b9 |
286 | |
ec761cee |
287 | # Hints for TEST 9 |
288 | # |
289 | # This test checks that rand without an argument |
290 | # is equivalent to rand(1). |
291 | # |
292 | $_ = 12345; # Just for fun. |
293 | srand 12345; |
294 | my $r = rand; |
295 | srand 12345; |
296 | if (rand(1) == $r) { |
297 | print "ok 9\n"; |
298 | } else { |
299 | print "not ok 9\n"; |
300 | print "# rand without arguments isn't rand(1)!\n"; |
301 | } |
748a9306 |
302 | |
ec761cee |
303 | # Hints for TEST 10 |
304 | # |
305 | # This checks that rand without an argument is not |
306 | # rand($_). (In case somebody got overzealous.) |
307 | # |
308 | if ($r >= 1) { |
309 | print "not ok 10\n"; |
310 | print "# rand without arguments isn't under 1!\n"; |
311 | } else { |
312 | print "ok 10\n"; |
313 | } |
314 | } |
315 | |
316 | # Hints for TEST 11 |
317 | # |
318 | # This test checks whether Perl called srand for you. This should |
319 | # be the case in version 5.004 and later. Note: You must still |
320 | # call srand if your code might ever be run on a pre-5.004 system! |
321 | # |
322 | AUTOSRAND: |
323 | { |
324 | unless ($Config{d_fork}) { |
325 | # Skip this test. It's not likely to be system-specific, anyway. |
326 | print "ok 11\n# Skipping this test on this platform.\n"; |
327 | last; |
328 | } |
329 | |
330 | my($pid, $first); |
331 | for (1..5) { |
68dc0745 |
332 | my $PERL = (($^O eq 'VMS') ? "MCR $^X" |
333 | : ($^O eq 'MSWin32') ? '.\perl' |
334 | : './perl'); |
335 | $pid = open PERL, qq[$PERL -e "print rand"|]; |
ec761cee |
336 | die "Couldn't pipe from perl: $!" unless defined $pid; |
337 | if (defined $first) { |
338 | if ($first ne <PERL>) { |
339 | print "ok 11\n"; |
340 | last AUTOSRAND; |
341 | } |
342 | } else { |
343 | $first = <PERL>; |
344 | } |
345 | close PERL or die "perl returned error code $?"; |
346 | } |
347 | print "not ok 11\n# srand isn't being autocalled.\n"; |
348 | } |