Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 117 lines (88 sloc) 3.659 kB
a25207b @colomon [t/spec] Add about eighty pick and roll tests, including a new S32-li…
colomon authored
1 use v6;
2
3 use Test;
4
b13a406 @moritz ().pick and .roll return Nil
moritz authored
5 plan 36;
a25207b @colomon [t/spec] Add about eighty pick and roll tests, including a new S32-li…
colomon authored
6
7 =begin description
8
9 This test tests the C<roll> builtin. See S32::Containers#roll.
10
11 =end description
12
13 # L<S32::Containers/List/=item roll>
14
15 my @array = <a b c d>;
16 ok ?(@array.roll eq any <a b c d>), "roll works on arrays";
17
9b1c972 @colomon Refudge for niecza.
colomon authored
18 #?niecza skip '.roll on empty list'
b13a406 @moritz ().pick and .roll return Nil
moritz authored
19 ok ().roll === Nil, '.roll on the empty list is Nil';
20
a25207b @colomon [t/spec] Add about eighty pick and roll tests, including a new S32-li…
colomon authored
21 my $junc = (1|2|3);
22 #?rakudo skip 'dubious: roll on Junctions (unspecced?)'
23 ok ?(1|2|3 == $junc.roll), "roll works on junctions";
24
25 my @arr = <z z z>;
26
27 ok ~(@arr.roll(2)) eq 'z z', 'method roll with $num < +@values';
28 ok ~(@arr.roll(4)) eq 'z z z z', 'method roll with $num > +@values';
29
30 #?pugs 2 todo 'feature'
31 is roll(2, @arr), <z z>, 'sub roll with $num < +@values, implicit no-replace';
32 is roll(4, @arr), <z z z z>, 'sub roll with $num > +@values';
33
34 is <a b c d>.roll(*)[^10].elems, 10, 'roll(*) generates at least ten elements';
35
36 {
37 my @items = <1 2 3 4>;
38 my @shuffled_items_10;
39 push @shuffled_items_10, @items.roll(4) for ^10;
40 isnt(@shuffled_items_10, @items xx 10,
41 'roll(4) returned the items of the array in a random order');
42 }
43
44 is (0, 1).roll(*).[^10].elems, 10, '.roll(*) returns at least ten elements';
45
46 {
47 # Test that List.roll doesn't flatten array refs
48 ok ?([[1, 2], [3, 4]].roll.join('|') eq any('1|2', '3|4')), '[[1,2],[3,4]].roll does not flatten';
49 }
50
51 {
52 ok <5 5>.roll() == 5,
53 '.roll() returns something can be used as single scalar';
54 }
55
56 {
57 my @a = 1..100;
58 my @b = roll(100, @a);
59 is @b.elems, 100, "roll(100, @a) returns the correct number of elements";
60 is ~@b.grep(Int).elems, 100, "roll(100, @a) returns Ints (if @a is Ints)";
61 is ~@b.grep(1..100).elems, 100, "roll(100, @a) returns numbers in the correct range";
62
63 isa_ok @a.roll, Int, "rolling a single element from an array of Ints produces an Int";
64 ok @a.roll ~~ 1..100, "rolling a single element from an array of Ints produces one of them";
65
66 isa_ok @a.roll(1), Int, "rolling 1 from an array of Ints produces an Int";
67 ok @a.roll(1) ~~ 1..100, "rolling 1 from an array of Ints produces one of them";
68
69 my @c = @a.roll(2);
70 isa_ok @c[0], Int, "rolling 2 from an array of Ints produces an Int...";
71 isa_ok @c[1], Int, "... and an Int";
72 ok (@c[0] ~~ 1..100) && (@c[1] ~~ 1..100), "rolling 2 from an array of Ints produces two of them";
73
74 is @a.roll("25").elems, 25, ".roll works Str arguments";
75 is roll("25", @a).elems, 25, "roll works Str arguments";
76 }
77
0ced986 @colomon Add a couple of tests, unfudge for niecza.
colomon authored
78 # enums + roll
79 {
80 is Bool.roll(3).grep(Bool).elems, 3, 'Bool.roll works';
81
82 enum A <b c d>;
83 is A.roll(4).grep(A).elems, 4, 'RandomEnum.roll works';
84 }
85
b896f6d @moritz tests for Range.roll and .pick
moritz authored
86 # ranges + roll
3480b41 @colomon Fudge tests that work but are deathly slow until we can optimize the …
colomon authored
87 #?niecza skip "Too slow"
b896f6d @moritz tests for Range.roll and .pick
moritz authored
88 {
89 my @matches = (1..1_000_000).roll(20);
90 is @matches.elems, 20, 'right number of elements from Range.roll';
91 ok (so 1 <= all(@matches) <= 1_000_000), 'all the elems are in range';
92 }
93
3480b41 @colomon Fudge tests that work but are deathly slow until we can optimize the …
colomon authored
94 #?niecza skip "Too slow"
b896f6d @moritz tests for Range.roll and .pick
moritz authored
95 {
96 my @matches = (1^..1_000_000).roll(20);
97 is @matches.elems, 20, 'right number of elements from Range.roll (min exclusive)';
98 ok (so 1 < all(@matches) <= 1_000_000), 'all the elems are in range';
99 }
3480b41 @colomon Fudge tests that work but are deathly slow until we can optimize the …
colomon authored
100 #?niecza skip "Too slow"
b896f6d @moritz tests for Range.roll and .pick
moritz authored
101 {
102 my @matches = (1..^1_000_000).roll(20);
103 is @matches.elems, 20, 'right number of elements from Range.roll (max exclusive)';
104 ok (so 1 <= all(@matches) < 1_000_000), 'all the elems are in range';
105 }
3480b41 @colomon Fudge tests that work but are deathly slow until we can optimize the …
colomon authored
106 #?niecza skip "Too slow"
b896f6d @moritz tests for Range.roll and .pick
moritz authored
107 {
108 my @matches = (1^..^1_000_000).roll(20);
109 is @matches.elems, 20, 'right number of elements from Range.roll (both exclusive)';
110 ok (so 1 < all(@matches) < 1_000_000), 'all the elems are in range';
111 }
0ced986 @colomon Add a couple of tests, unfudge for niecza.
colomon authored
112
2fae5d6 @moritz more Range.{roll,pick} tests
moritz authored
113 is (1..^2).roll, 1, '1-elem Range roll';
114 ok ('a' .. 'z').roll ~~ /\w/, 'Str-Range roll';
115
a25207b @colomon [t/spec] Add about eighty pick and roll tests, including a new S32-li…
colomon authored
116 # vim: ft=perl6
Something went wrong with that request. Please try again.