Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 110 lines (82 sloc) 3.468 kb
a25207ba »
2010-09-15 [t/spec] Add about eighty pick and roll tests, including a new S32-li…
1 use v6;
2
3 use Test;
4
2fae5d6b »
2012-01-17 more Range.{roll,pick} tests
5 plan 35;
a25207ba »
2010-09-15 [t/spec] Add about eighty pick and roll tests, including a new S32-li…
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
18 my $junc = (1|2|3);
19 #?rakudo skip 'dubious: roll on Junctions (unspecced?)'
20 ok ?(1|2|3 == $junc.roll), "roll works on junctions";
21
22 my @arr = <z z z>;
23
24 ok ~(@arr.roll(2)) eq 'z z', 'method roll with $num < +@values';
25 ok ~(@arr.roll(4)) eq 'z z z z', 'method roll with $num > +@values';
26
27 #?pugs 2 todo 'feature'
28 is roll(2, @arr), <z z>, 'sub roll with $num < +@values, implicit no-replace';
29 is roll(4, @arr), <z z z z>, 'sub roll with $num > +@values';
30
31 is <a b c d>.roll(*)[^10].elems, 10, 'roll(*) generates at least ten elements';
32
33 {
34 my @items = <1 2 3 4>;
35 my @shuffled_items_10;
36 push @shuffled_items_10, @items.roll(4) for ^10;
37 isnt(@shuffled_items_10, @items xx 10,
38 'roll(4) returned the items of the array in a random order');
39 }
40
41 is (0, 1).roll(*).[^10].elems, 10, '.roll(*) returns at least ten elements';
42
43 {
44 # Test that List.roll doesn't flatten array refs
45 ok ?([[1, 2], [3, 4]].roll.join('|') eq any('1|2', '3|4')), '[[1,2],[3,4]].roll does not flatten';
46 }
47
48 {
49 ok <5 5>.roll() == 5,
50 '.roll() returns something can be used as single scalar';
51 }
52
53 {
54 my @a = 1..100;
55 my @b = roll(100, @a);
56 is @b.elems, 100, "roll(100, @a) returns the correct number of elements";
57 is ~@b.grep(Int).elems, 100, "roll(100, @a) returns Ints (if @a is Ints)";
58 is ~@b.grep(1..100).elems, 100, "roll(100, @a) returns numbers in the correct range";
59
60 isa_ok @a.roll, Int, "rolling a single element from an array of Ints produces an Int";
61 ok @a.roll ~~ 1..100, "rolling a single element from an array of Ints produces one of them";
62
63 isa_ok @a.roll(1), Int, "rolling 1 from an array of Ints produces an Int";
64 ok @a.roll(1) ~~ 1..100, "rolling 1 from an array of Ints produces one of them";
65
66 my @c = @a.roll(2);
67 isa_ok @c[0], Int, "rolling 2 from an array of Ints produces an Int...";
68 isa_ok @c[1], Int, "... and an Int";
69 ok (@c[0] ~~ 1..100) && (@c[1] ~~ 1..100), "rolling 2 from an array of Ints produces two of them";
70
71 is @a.roll("25").elems, 25, ".roll works Str arguments";
72 is roll("25", @a).elems, 25, "roll works Str arguments";
73 }
74
0ced9862 »
2012-01-06 Add a couple of tests, unfudge for niecza.
75 # enums + roll
76 {
77 is Bool.roll(3).grep(Bool).elems, 3, 'Bool.roll works';
78
79 enum A <b c d>;
80 is A.roll(4).grep(A).elems, 4, 'RandomEnum.roll works';
81 }
82
b896f6d6 »
2012-01-17 tests for Range.roll and .pick
83 # ranges + roll
84 {
85 my @matches = (1..1_000_000).roll(20);
86 is @matches.elems, 20, 'right number of elements from Range.roll';
87 ok (so 1 <= all(@matches) <= 1_000_000), 'all the elems are in range';
88 }
89
90 {
91 my @matches = (1^..1_000_000).roll(20);
92 is @matches.elems, 20, 'right number of elements from Range.roll (min exclusive)';
93 ok (so 1 < all(@matches) <= 1_000_000), 'all the elems are in range';
94 }
95 {
96 my @matches = (1..^1_000_000).roll(20);
97 is @matches.elems, 20, 'right number of elements from Range.roll (max exclusive)';
98 ok (so 1 <= all(@matches) < 1_000_000), 'all the elems are in range';
99 }
100 {
101 my @matches = (1^..^1_000_000).roll(20);
102 is @matches.elems, 20, 'right number of elements from Range.roll (both exclusive)';
103 ok (so 1 < all(@matches) < 1_000_000), 'all the elems are in range';
104 }
0ced9862 »
2012-01-06 Add a couple of tests, unfudge for niecza.
105
2fae5d6b »
2012-01-17 more Range.{roll,pick} tests
106 is (1..^2).roll, 1, '1-elem Range roll';
107 ok ('a' .. 'z').roll ~~ /\w/, 'Str-Range roll';
108
a25207ba »
2010-09-15 [t/spec] Add about eighty pick and roll tests, including a new S32-li…
109 # vim: ft=perl6
Something went wrong with that request. Please try again.