Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[t/spec] Add about eighty pick and roll tests, including a new S32-li…

…st/roll.t file.
  • Loading branch information...
commit a25207bacf2494e4ab5baba304d61c8179e43d6a 1 parent e2e65b2
@colomon colomon authored
Showing with 211 additions and 7 deletions.
  1. +110 −1 S02-builtin_data_types/range.t
  2. +24 −6 S32-list/pick.t
  3. +77 −0 S32-list/roll.t
View
111 S02-builtin_data_types/range.t
@@ -2,7 +2,7 @@ use v6;
use Test;
-plan 61;
+plan 123;
# basic Range
# L<S02/Immutable types/A pair of Ordered endpoints>
@@ -149,4 +149,113 @@ is(+(6..8), 3, 'numification');
is((1..8)[1,3], [2,4], 'postcircumfix:<[ ]> on range works');
}
+{
+ my @b = pick(*, 1..100);
+ is @b.elems, 100, "pick(*, 1..100) returns the correct number of elements";
+ is ~@b.sort, ~(1..100), "pick(*, 1..100) returns the correct elements";
+ is ~@b.grep(Int).elems, 100, "pick(*, 1..100) returns Ints";
+
+ @b = (1..100).pick(*);
+ is @b.elems, 100, "pick(*, 1..100) returns the correct number of elements";
+ is ~@b.sort, ~(1..100), "pick(*, 1..100) returns the correct elements";
+ is ~@b.grep(Int).elems, 100, "pick(*, 1..100) returns Ints";
+
+ isa_ok (1..100).pick, Int, "picking a single element from an range of Ints produces an Int";
+ ok (1..100).pick ~~ 1..100, "picking a single element from an range of Ints produces one of them";
+
+ isa_ok (1..100).pick(1), Int, "picking 1 from an range of Ints produces an Int";
+ ok (1..100).pick(1) ~~ 1..100, "picking 1 from an range of Ints produces one of them";
+
+ my @c = (1..100).pick(2);
+ isa_ok @c[0], Int, "picking 2 from an range of Ints produces an Int...";
+ isa_ok @c[1], Int, "... and an Int";
+ ok (@c[0] ~~ 1..100) && (@c[1] ~~ 1..100), "picking 2 from an range of Ints produces two of them";
+ ok @c[0] != @c[1], "picking 2 from an range of Ints produces two distinct results";
+
+ is (1..100).pick("25").elems, 25, ".pick works Str arguments";
+ is pick("25", 1..100).elems, 25, "pick works Str arguments";
+}
+
+{
+ my @b = pick(*, 'b' .. 'y');
+ is @b.elems, 24, "pick(*, 'b' .. 'y') returns the correct number of elements";
+ is ~@b.sort, ~('b' .. 'y'), "pick(*, 'b' .. 'y') returns the correct elements";
+ is ~@b.grep(Str).elems, 24, "pick(*, 'b' .. 'y') returns Strs";
+
+ @b = ('b' .. 'y').pick(*);
+ is @b.elems, 24, "pick(*, 'b' .. 'y') returns the correct number of elements";
+ is ~@b.sort, ~('b' .. 'y'), "pick(*, 'b' .. 'y') returns the correct elements";
+ is ~@b.grep(Str).elems, 24, "pick(*, 'b' .. 'y') returns Strs";
+
+ isa_ok ('b' .. 'y').pick, Str, "picking a single element from an range of Strs produces an Str";
+ ok ('b' .. 'y').pick ~~ 'b' .. 'y', "picking a single element from an range of Strs produces one of them";
+
+ isa_ok ('b' .. 'y').pick(1), Str, "picking 1 from an range of Strs produces an Str";
+ ok ('b' .. 'y').pick(1) ~~ 'b' .. 'y', "picking 1 from an range of Strs produces one of them";
+
+ my @c = ('b' .. 'y').pick(2);
+ isa_ok @c[0], Str, "picking 2 from an range of Strs produces an Str...";
+ isa_ok @c[1], Str, "... and an Str";
+ ok (@c[0] ~~ 'b' .. 'y') && (@c[1] ~~ 'b' .. 'y'), "picking 2 from an range of Strs produces two of them";
+ ok @c[0] ne @c[1], "picking 2 from an range of Strs produces two distinct results";
+
+ is ('b' .. 'y').pick("10").elems, 10, ".pick works Str arguments";
+ is pick("10", 'b' .. 'y').elems, 10, "pick works Str arguments";
+}
+
+{
+ my @b = roll(100, 1..100);
+ is @b.elems, 100, "roll(100, 1..100) returns the correct number of elements";
+ is ~@b.grep(1..100).elems, 100, "roll(100, 1..100) returns elements from 1..100";
+ is ~@b.grep(Int).elems, 100, "roll(100, 1..100) returns Ints";
+
+ @b = (1..100).roll(100);
+ is @b.elems, 100, "roll(100, 1..100) returns the correct number of elements";
+ is ~@b.grep(1..100).elems, 100, "roll(100, 1..100) returns elements from 1..100";
+ is ~@b.grep(Int).elems, 100, "roll(100, 1..100) returns Ints";
+
+ isa_ok (1..100).roll, Int, "rolling a single element from an range of Ints produces an Int";
+ ok (1..100).roll ~~ 1..100, "rolling a single element from an range of Ints produces one of them";
+
+ #?rakudo todo 'roll(1) returning a one-element List at the moment'
+ isa_ok (1..100).roll(1), Int, "rolling 1 from an range of Ints produces an Int";
+ #?rakudo todo 'roll(1) returning a one-element List at the moment'
+ ok (1..100).roll(1) ~~ 1..100, "rolling 1 from an range of Ints produces one of them";
+
+ my @c = (1..100).roll(2);
+ isa_ok @c[0], Int, "rolling 2 from an range of Ints produces an Int...";
+ isa_ok @c[1], Int, "... and an Int";
+ ok (@c[0] ~~ 1..100) && (@c[1] ~~ 1..100), "rolling 2 from an range of Ints produces two of them";
+
+ is (1..100).roll("25").elems, 25, ".roll works Str arguments";
+ is roll("25", 1..100).elems, 25, "roll works Str arguments";
+}
+
+{
+ my @b = roll(100, 'b' .. 'y');
+ is @b.elems, 100, "roll(100, 'b' .. 'y') returns the correct number of elements";
+ is ~@b.grep('b' .. 'y').elems, 100, "roll(100, 'b' .. 'y') returns elements from b..y";
+ is ~@b.grep(Str).elems, 100, "roll(100, 'b' .. 'y') returns Strs";
+
+ @b = ('b' .. 'y').roll(100);
+ is @b.elems, 100, "roll(100, 'b' .. 'y') returns the correct number of elements";
+ is ~@b.grep('b' .. 'y').elems, 100, "roll(100, 'b' .. 'y') returns elements from b..y";
+ is ~@b.grep(Str).elems, 100, "roll(100, 'b' .. 'y') returns Strs";
+
+ isa_ok ('b' .. 'y').roll, Str, "rolling a single element from an range of Strs produces an Str";
+ ok ('b' .. 'y').roll ~~ 'b' .. 'y', "rolling a single element from an range of Strs produces one of them";
+
+ #?rakudo todo 'roll(1) returning a one-element List at the moment'
+ isa_ok ('b' .. 'y').roll(1), Str, "rolling 1 from an range of Strs produces an Str";
+ ok ('b' .. 'y').roll(1) ~~ 'b' .. 'y', "rolling 1 from an range of Strs produces one of them";
+
+ my @c = ('b' .. 'y').roll(2);
+ isa_ok @c[0], Str, "rolling 2 from an range of Strs produces an Str...";
+ isa_ok @c[1], Str, "... and an Str";
+ ok (@c[0] ~~ 'b' .. 'y') && (@c[1] ~~ 'b' .. 'y'), "rolling 2 from an range of Strs produces two of them";
+
+ is ('b' .. 'y').roll("10").elems, 10, ".roll works Str arguments";
+ is roll("10", 'b' .. 'y').elems, 10, "roll works Str arguments";
+}
+
# vim:set ft=perl6
View
30 S32-list/pick.t
@@ -2,7 +2,7 @@ use v6;
use Test;
-plan 15;
+plan 25;
=begin description
@@ -27,15 +27,12 @@ my @arr = <z z z>;
ok ~(@arr.pick(2)) eq 'z z', 'method pick with $num < +@values';
ok ~(@arr.pick(4)) eq 'z z z', 'method pick with $num > +@values';
-#?pugs todo 'feature'
-is ~(@arr.roll(4)), 'z z z z', 'method roll() with $num > +@values';
#?pugs 2 todo 'feature'
is pick(2, @arr), <z z>, 'sub pick with $num < +@values, implicit no-replace';
is pick(4, @arr), <z z z>, 'sub pick with $num > +@values';
#?rakudo skip "Calling values by name fails hard"
is pick(2, :values(@arr)), <z z>, 'sub pick with $num < +@values and named args';
-is ~(roll(4, @arr)), 'z z z z', 'sub roll() with $num > +@values';
is (<a b c d>.pick(*).sort).Str, 'a b c d', 'pick(*) returns all the items in the array (but maybe not in order)';
@@ -47,8 +44,6 @@ is (<a b c d>.pick(*).sort).Str, 'a b c d', 'pick(*) returns all the items in th
'pick(*) returned the items of the array in a random order');
}
-is (0, 1).roll(*).[^10].elems, 10, '.roll(*) returns an answer';
-
{
# Test that List.pick doesn't flatten array refs
ok ?([[1, 2], [3, 4]].pick.join('|') eq any('1|2', '3|4')), '[[1,2],[3,4]].pick does not flatten';
@@ -60,4 +55,27 @@ is (0, 1).roll(*).[^10].elems, 10, '.roll(*) returns an answer';
'.pick() returns something can be used as single scalar';
}
+{
+ my @a = 1..100;
+ my @b = pick(*, @a);
+ is @b.elems, 100, "pick(*, @a) returns the correct number of elements";
+ is ~@b.sort, ~(1..100), "pick(*, @a) returns the correct elements";
+ is ~@b.grep(Int).elems, 100, "pick(*, @a) returns Ints (if @a is Ints)";
+
+ isa_ok @a.pick, Int, "picking a single element from an array of Ints produces an Int";
+ ok @a.pick ~~ 1..100, "picking a single element from an array of Ints produces one of them";
+
+ isa_ok @a.pick(1), Int, "picking 1 from an array of Ints produces an Int";
+ ok @a.pick(1) ~~ 1..100, "picking 1 from an array of Ints produces one of them";
+
+ my @c = @a.pick(2);
+ isa_ok @c[0], Int, "picking 2 from an array of Ints produces an Int...";
+ isa_ok @c[1], Int, "... and an Int";
+ ok (@c[0] ~~ 1..100) && (@c[1] ~~ 1..100), "picking 2 from an array of Ints produces two of them";
+ ok @c[0] != @c[1], "picking 2 from an array of Ints produces two distinct results";
+
+ is @a.pick("25").elems, 25, ".pick works Str arguments";
+ is pick("25", @a).elems, 25, "pick works Str arguments";
+}
+
# vim: ft=perl6
View
77 S32-list/roll.t
@@ -0,0 +1,77 @@
+use v6;
+
+use Test;
+
+plan 24;
+
+=begin description
+
+This test tests the C<roll> builtin. See S32::Containers#roll.
+
+=end description
+
+# L<S32::Containers/List/=item roll>
+
+my @array = <a b c d>;
+ok ?(@array.roll eq any <a b c d>), "roll works on arrays";
+
+my $junc = (1|2|3);
+#?rakudo skip 'dubious: roll on Junctions (unspecced?)'
+ok ?(1|2|3 == $junc.roll), "roll works on junctions";
+
+my @arr = <z z z>;
+
+ok ~(@arr.roll(2)) eq 'z z', 'method roll with $num < +@values';
+ok ~(@arr.roll(4)) eq 'z z z z', 'method roll with $num > +@values';
+
+#?pugs 2 todo 'feature'
+is roll(2, @arr), <z z>, 'sub roll with $num < +@values, implicit no-replace';
+is roll(4, @arr), <z z z z>, 'sub roll with $num > +@values';
+#?rakudo skip "Calling values by name fails hard"
+is roll(2, :values(@arr)), <z z>, 'sub roll with $num < +@values and named args';
+
+is <a b c d>.roll(*)[^10].elems, 10, 'roll(*) generates at least ten elements';
+
+{
+ my @items = <1 2 3 4>;
+ my @shuffled_items_10;
+ push @shuffled_items_10, @items.roll(4) for ^10;
+ isnt(@shuffled_items_10, @items xx 10,
+ 'roll(4) returned the items of the array in a random order');
+}
+
+is (0, 1).roll(*).[^10].elems, 10, '.roll(*) returns at least ten elements';
+
+{
+ # Test that List.roll doesn't flatten array refs
+ ok ?([[1, 2], [3, 4]].roll.join('|') eq any('1|2', '3|4')), '[[1,2],[3,4]].roll does not flatten';
+}
+
+{
+ ok <5 5>.roll() == 5,
+ '.roll() returns something can be used as single scalar';
+}
+
+{
+ my @a = 1..100;
+ my @b = roll(100, @a);
+ is @b.elems, 100, "roll(100, @a) returns the correct number of elements";
+ is ~@b.grep(Int).elems, 100, "roll(100, @a) returns Ints (if @a is Ints)";
+ is ~@b.grep(1..100).elems, 100, "roll(100, @a) returns numbers in the correct range";
+
+ isa_ok @a.roll, Int, "rolling a single element from an array of Ints produces an Int";
+ ok @a.roll ~~ 1..100, "rolling a single element from an array of Ints produces one of them";
+
+ isa_ok @a.roll(1), Int, "rolling 1 from an array of Ints produces an Int";
+ ok @a.roll(1) ~~ 1..100, "rolling 1 from an array of Ints produces one of them";
+
+ my @c = @a.roll(2);
+ isa_ok @c[0], Int, "rolling 2 from an array of Ints produces an Int...";
+ isa_ok @c[1], Int, "... and an Int";
+ ok (@c[0] ~~ 1..100) && (@c[1] ~~ 1..100), "rolling 2 from an array of Ints produces two of them";
+
+ is @a.roll("25").elems, 25, ".roll works Str arguments";
+ is roll("25", @a).elems, 25, "roll works Str arguments";
+}
+
+# vim: ft=perl6
Please sign in to comment.
Something went wrong with that request. Please try again.