Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 133 lines (98 sloc) 4.144 kb
a25207b Solomon Foster [t/spec] Add about eighty pick and roll tests, including a new S32-list/...
colomon authored
1 use v6;
2
3 use Test;
4
6320c7c Tobias Leich test that we get different results when re-running interpreter
FROGGS authored
5 plan 40;
a25207b Solomon Foster [t/spec] Add about eighty pick and roll tests, including a new S32-list/...
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 Solomon Foster Refudge for niecza.
colomon authored
18 #?niecza skip '.roll on empty list'
b13a406 Moritz Lenz ().pick and .roll return Nil
moritz authored
19 ok ().roll === Nil, '.roll on the empty list is Nil';
20
a25207b Solomon Foster [t/spec] Add about eighty pick and roll tests, including a new S32-list/...
colomon authored
21 my @arr = <z z z>;
22
23 ok ~(@arr.roll(2)) eq 'z z', 'method roll with $num < +@values';
24 ok ~(@arr.roll(4)) eq 'z z z z', 'method roll with $num > +@values';
25
26 #?pugs 2 todo 'feature'
27 is roll(2, @arr), <z z>, 'sub roll with $num < +@values, implicit no-replace';
28 is roll(4, @arr), <z z z z>, 'sub roll with $num > +@values';
29
30 is <a b c d>.roll(*)[^10].elems, 10, 'roll(*) generates at least ten elements';
31
32 {
33 my @items = <1 2 3 4>;
34 my @shuffled_items_10;
35 push @shuffled_items_10, @items.roll(4) for ^10;
36 isnt(@shuffled_items_10, @items xx 10,
37 'roll(4) returned the items of the array in a random order');
38 }
39
40 is (0, 1).roll(*).[^10].elems, 10, '.roll(*) returns at least ten elements';
41
42 {
43 # Test that List.roll doesn't flatten array refs
44 ok ?([[1, 2], [3, 4]].roll.join('|') eq any('1|2', '3|4')), '[[1,2],[3,4]].roll does not flatten';
45 }
46
47 {
48 ok <5 5>.roll() == 5,
49 '.roll() returns something can be used as single scalar';
50 }
51
52 {
53 my @a = 1..100;
54 my @b = roll(100, @a);
55 is @b.elems, 100, "roll(100, @a) returns the correct number of elements";
56 is ~@b.grep(Int).elems, 100, "roll(100, @a) returns Ints (if @a is Ints)";
57 is ~@b.grep(1..100).elems, 100, "roll(100, @a) returns numbers in the correct range";
58
59 isa_ok @a.roll, Int, "rolling a single element from an array of Ints produces an Int";
60 ok @a.roll ~~ 1..100, "rolling a single element from an array of Ints produces one of them";
61
62 isa_ok @a.roll(1), Int, "rolling 1 from an array of Ints produces an Int";
63 ok @a.roll(1) ~~ 1..100, "rolling 1 from an array of Ints produces one of them";
64
65 my @c = @a.roll(2);
66 isa_ok @c[0], Int, "rolling 2 from an array of Ints produces an Int...";
67 isa_ok @c[1], Int, "... and an Int";
68 ok (@c[0] ~~ 1..100) && (@c[1] ~~ 1..100), "rolling 2 from an array of Ints produces two of them";
69
70 is @a.roll("25").elems, 25, ".roll works Str arguments";
71 is roll("25", @a).elems, 25, "roll works Str arguments";
72 }
73
0ced986 Solomon Foster Add a couple of tests, unfudge for niecza.
colomon authored
74 # enums + roll
75 {
76 is Bool.roll(3).grep(Bool).elems, 3, 'Bool.roll works';
77
78 enum A <b c d>;
79 is A.roll(4).grep(A).elems, 4, 'RandomEnum.roll works';
80 }
81
b896f6d Moritz Lenz tests for Range.roll and .pick
moritz authored
82 # ranges + roll
92c0f96 Solomon Foster Add more sanity tests for Range.pick and Range.roll. Unfudge tests whic...
colomon authored
83 {
84 ok 1 <= (1..1_000_000).roll() <= 1_000_000, 'no argument roll works';
85
86 my @matches := (1..1_000_000).roll(*);
87 ok (so 1 <= all(@matches[^100]) <= 1_000_000), 'the first 100 elems are in range';
88 }
89
b896f6d Moritz Lenz tests for Range.roll and .pick
moritz authored
90 {
91 my @matches = (1..1_000_000).roll(20);
92 is @matches.elems, 20, 'right number of elements from Range.roll';
93 ok (so 1 <= all(@matches) <= 1_000_000), 'all the elems are in range';
94 }
95
96 {
97 my @matches = (1^..1_000_000).roll(20);
98 is @matches.elems, 20, 'right number of elements from Range.roll (min exclusive)';
99 ok (so 1 < all(@matches) <= 1_000_000), 'all the elems are in range';
100 }
92c0f96 Solomon Foster Add more sanity tests for Range.pick and Range.roll. Unfudge tests whic...
colomon authored
101
b896f6d Moritz Lenz tests for Range.roll and .pick
moritz authored
102 {
103 my @matches = (1..^1_000_000).roll(20);
104 is @matches.elems, 20, 'right number of elements from Range.roll (max exclusive)';
105 ok (so 1 <= all(@matches) < 1_000_000), 'all the elems are in range';
106 }
92c0f96 Solomon Foster Add more sanity tests for Range.pick and Range.roll. Unfudge tests whic...
colomon authored
107
b896f6d Moritz Lenz tests for Range.roll and .pick
moritz authored
108 {
109 my @matches = (1^..^1_000_000).roll(20);
110 is @matches.elems, 20, 'right number of elements from Range.roll (both exclusive)';
111 ok (so 1 < all(@matches) < 1_000_000), 'all the elems are in range';
112 }
0ced986 Solomon Foster Add a couple of tests, unfudge for niecza.
colomon authored
113
2d6c012 Solomon Foster Add tests for roll and pick given a huge integer range.
colomon authored
114 {
115 my @matches = (1..(10**1000)).roll(20);
116 is @matches.elems, 20, 'right number of elements from Range.roll, huge range';
117 ok (so 1 <= all(@matches) <= 10**1000), 'all the elems are in range';
118 }
119
120
2fae5d6 Moritz Lenz more Range.{roll,pick} tests
moritz authored
121 is (1..^2).roll, 1, '1-elem Range roll';
122 ok ('a' .. 'z').roll ~~ /\w/, 'Str-Range roll';
123
6320c7c Tobias Leich test that we get different results when re-running interpreter
FROGGS authored
124 # RT 89972
125 {
126 my $a = qqx{$*EXECUTABLE_NAME -e "print ~(1..10).pick(5)"};
127 my $b = qqx{$*EXECUTABLE_NAME -e "print ~(1..10).pick(5)"};
128 my $c = qqx{$*EXECUTABLE_NAME -e "print ~(1..10).pick(5)"};
c5f5026 Tobias Leich 'ne' operator instead of 'leg', [Coke]++
FROGGS authored
129 ok ($a ne $b || $b ne $c), 'different results due to random random-number seed';
6320c7c Tobias Leich test that we get different results when re-running interpreter
FROGGS authored
130 }
131
a25207b Solomon Foster [t/spec] Add about eighty pick and roll tests, including a new S32-list/...
colomon authored
132 # vim: ft=perl6
Something went wrong with that request. Please try again.