Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 105 lines (78 sloc) 2.731 kb
58135b30 » moritz
2008-05-08 [t/spec] changed 'use v6-alpha;' to 'use v6;'
1 use v6;
ad1b86bb » cosimo
2008-01-24 [t] Test suite refactoring.
2
3 use Test;
4
fdbef333 » kyle
2009-06-29 [t/spec] Test the distribution of results from rand()
5 plan 110;
ad1b86bb » cosimo
2008-01-24 [t] Test suite refactoring.
6
626a59c1 » particle
2008-01-27 [rakudo] add directives to S29-num/ and convert pod to pod6
7 =begin pod
ad1b86bb » cosimo
2008-01-24 [t] Test suite refactoring.
8
8f4e54ff » lwall
2008-08-02 [t/*] fix all the rands and $*PID."... instances
9 Basic tests for the rand builtin
ad1b86bb » cosimo
2008-01-24 [t] Test suite refactoring.
10
626a59c1 » particle
2008-01-27 [rakudo] add directives to S29-num/ and convert pod to pod6
11 =end pod
ad1b86bb » cosimo
2008-01-24 [t] Test suite refactoring.
12
b8d99dd7 » moritz
2010-03-15 [t/spec] fix more smartlinks
13 # L<S32::Numeric/Numeric/"=item rand">
ad1b86bb » cosimo
2008-01-24 [t] Test suite refactoring.
14
8f4e54ff » lwall
2008-08-02 [t/*] fix all the rands and $*PID."... instances
15 ok(rand >= 0, 'rand returns numbers greater than or equal to 0');
16 ok(rand < 1, 'rand returns numbers less than 1');
ad1b86bb » cosimo
2008-01-24 [t] Test suite refactoring.
17
09f6345f » frew
2009-02-12 [t] Added .rand
18 sub test_rand_range(Int $num) {
19 for 1..20 {
20 my $result = $num.rand;
21 ok($num > $result >= 0, "rand returns numbers in [0, $num)");
22 }
23 }
24
25 test_rand_range(2);
26 test_rand_range(3);
27 test_rand_range(5);
28 test_rand_range(7);
29 test_rand_range(11);
30
b8d99dd7 » moritz
2010-03-15 [t/spec] fix more smartlinks
31 # L<S32::Numeric/Real/"=item srand">
ad1b86bb » cosimo
2008-01-24 [t] Test suite refactoring.
32
dde38e07 » moritz
2008-06-25 [spec] rand.t: unfudged one test, changed fudge message of other
33 lives_ok { srand(1) }, 'srand(1) lives and parses';
ad1b86bb » cosimo
2008-01-24 [t] Test suite refactoring.
34
626a59c1 » particle
2008-01-27 [rakudo] add directives to S29-num/ and convert pod to pod6
35 {
db108703 » lwall
2009-06-20 [t/spec] patching difficulties spotted by STD
36 my sub repeat_rand ($seed) {
2abdf4ce » pmichaud
2008-06-26 S29-num/rand.t: Update #?rakudo skip message to link to RT#56274.
37 srand($seed);
8f4e54ff » lwall
2008-08-02 [t/*] fix all the rands and $*PID."... instances
38 for 1..99 { rand; }
39 return rand;
626a59c1 » particle
2008-01-27 [rakudo] add directives to S29-num/ and convert pod to pod6
40 }
ad1b86bb » cosimo
2008-01-24 [t] Test suite refactoring.
41
626a59c1 » particle
2008-01-27 [rakudo] add directives to S29-num/ and convert pod to pod6
42 ok(repeat_rand(314159) == repeat_rand(314159),
8f4e54ff » lwall
2008-08-02 [t/*] fix all the rands and $*PID."... instances
43 'srand() provides repeatability for rand');
ad1b86bb » cosimo
2008-01-24 [t] Test suite refactoring.
44
626a59c1 » particle
2008-01-27 [rakudo] add directives to S29-num/ and convert pod to pod6
45 ok(repeat_rand(0) == repeat_rand(0),
46 'edge case: srand(0) provides repeatability');
ad1b86bb » cosimo
2008-01-24 [t] Test suite refactoring.
47
626a59c1 » particle
2008-01-27 [rakudo] add directives to S29-num/ and convert pod to pod6
48 ok(repeat_rand(0) != repeat_rand(1),
49 'edge case: srand(0) not the same as srand(1)');
50 }
576906b4 » frew
2009-01-31 [t] added named arg tests to trig and num functions
51
52 {
db108703 » lwall
2009-06-20 [t/spec] patching difficulties spotted by STD
53 my sub repeat_rand ($seed) {
19614fbd » moritz
2011-05-23 [S32] unfossile more tests
54 srand($seed);
576906b4 » frew
2009-01-31 [t] added named arg tests to trig and num functions
55 for 1..99 { rand; }
56 return rand;
57 }
58
59 ok(repeat_rand(314159) == repeat_rand(314159),
19614fbd » moritz
2011-05-23 [S32] unfossile more tests
60 'srand(...) provides repeatability for rand');
576906b4 » frew
2009-01-31 [t] added named arg tests to trig and num functions
61
62 ok(repeat_rand(0) == repeat_rand(0),
19614fbd » moritz
2011-05-23 [S32] unfossile more tests
63 'edge case: srand(0) provides repeatability');
576906b4 » frew
2009-01-31 [t] added named arg tests to trig and num functions
64
65 ok(repeat_rand(0) != repeat_rand(1),
19614fbd » moritz
2011-05-23 [S32] unfossile more tests
66 'edge case: srand(0) not the same as srand(:seed(1))');
576906b4 » frew
2009-01-31 [t] added named arg tests to trig and num functions
67 }
9890f966 » moritz
2009-06-16 [t/spec] modeline
68
fdbef333 » kyle
2009-06-29 [t/spec] Test the distribution of results from rand()
69 #?rakudo skip 'Test is too slow'
74f51513 » colomon
2011-12-20 Fudge for niecza.
70 #?niecza skip 'Test is too slow'
fdbef333 » kyle
2009-06-29 [t/spec] Test the distribution of results from rand()
71 # Similar code under Perl 5 runs in < 15s.
72 {
73 srand;
74
75 my $cells = 2 ** 16; # possible values from rand()
76 my $samples = 500 * $cells; # how many rand() calls we'll make
77 my $freq_wanted = $samples / $cells; # ideal samples per cell
78 # my @freq_observed[$cells];
79 my @freq_observed;
80
81 @freq_observed[ $cells.rand ]++ for 1 .. $samples;
82
83 my $cs = 0;
84 for @freq_observed -> $obsfreq {
85 $cs += (($obsfreq // 0) - $freq_wanted) ** 2;
86 }
87 $cs /= $freq_wanted;
88
89 my $badness = abs( 1 - $cs / ( $cells - 1 ) );
90
91 # XXX: My confidence in this test is rather low.
92 # I got the number below by running the same test repeatedly with Perl 5
93 # and observing its results then again with deliberately corrupted
94 # "results". The value I picked is between the worst of the natural
95 # results and the best of the b0rked results.
96 # My hope is that someone who understands Chi Squared tests
97 # better than I do will find what I've written easier to fix
98 # than to write a good test from scratch.
99 # The good news is it passes with Rakudo when I cut down on $samples
100 # and wait a while.
101
102 ok( $badness < 0.15, 'rand is pretty random' );
103 }
104
9890f966 » moritz
2009-06-16 [t/spec] modeline
105 # vim: ft=perl6
Something went wrong with that request. Please try again.