Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add Set/Bag tests for .grab and .total
  • Loading branch information
lizmat committed Oct 4, 2013
1 parent cf33793 commit adcc1c7
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
13 changes: 12 additions & 1 deletion S02-types/bag.t
@@ -1,7 +1,7 @@
use v6;
use Test;

plan 154;
plan 159;

sub showkv($x) {
$x.keys.sort.map({ $^k ~ ':' ~ $x{$k} }).join(' ')
Expand Down Expand Up @@ -256,6 +256,7 @@ sub showkv($x) {
is +@a, 100, '.roll(100) returns 100 items';
ok 2 < @a.grep(* eq 'a') < 75, '.roll(100) (1)';
ok @a.grep(* eq 'a') + 2 < @a.grep(* eq 'b'), '.roll(100) (2)';
is $b.total, 3, '.roll should not change Bag';
}

{
Expand All @@ -268,6 +269,7 @@ sub showkv($x) {
is +@a, 100, '.roll(100) returns 100 items';
ok @a.grep(* eq 'a') > 97, '.roll(100) (1)';
ok @a.grep(* eq 'b') < 3, '.roll(100) (2)';
is $b.total, 100000000001, '.roll should not change Bag';
}

# L<S32::Containers/Bag/pick>
Expand All @@ -287,6 +289,7 @@ sub showkv($x) {
is +@a, 3, '.pick(*) returns the right number of items';
is @a.grep(* eq 'a').elems, 1, '.pick(*) (1)';
is @a.grep(* eq 'b').elems, 2, '.pick(*) (2)';
is $b.total, 3, '.pick should not change Bag';
}

{
Expand All @@ -299,6 +302,14 @@ sub showkv($x) {
is +@a, 100, '.pick(100) returns 100 items';
ok @a.grep(* eq 'a') > 98, '.pick(100) (1)';
ok @a.grep(* eq 'b') < 2, '.pick(100) (2)';
is $b.total, 100000000001, '.pick should not change Bag';
}

# L<S32::Containers/Bag/grab>

{
my $b = bag <a b b c c c>;
dies_ok { $b.grab }, 'cannot call .grab on a Bag';
}

{
Expand Down
43 changes: 42 additions & 1 deletion S02-types/baghash.t
@@ -1,7 +1,7 @@
use v6;
use Test;

plan 172;
plan 190;

# L<S02/Mutable types/QuantHash of UInt>

Expand Down Expand Up @@ -264,6 +264,7 @@ sub showkv($x) {
is +@a, 100, '.roll(100) returns 100 items';
ok 2 < @a.grep(* eq 'a') < 75, '.roll(100) (1)';
ok @a.grep(* eq 'a') + 2 < @a.grep(* eq 'b'), '.roll(100) (2)';
is $b.total, 3, '.roll should not change BagHash';
}

{
Expand All @@ -276,6 +277,7 @@ sub showkv($x) {
is +@a, 100, '.roll(100) returns 100 items';
ok @a.grep(* eq 'a') > 97, '.roll(100) (1)';
ok @a.grep(* eq 'b') < 3, '.roll(100) (2)';
is $b.total, 100000000001, '.roll should not change BagHash';
}

# L<S32::Containers/BagHash/pick>
Expand All @@ -295,6 +297,7 @@ sub showkv($x) {
is +@a, 3, '.pick(*) returns the right number of items';
is @a.grep(* eq 'a').elems, 1, '.pick(*) (1)';
is @a.grep(* eq 'b').elems, 2, '.pick(*) (2)';
is $b.total, 3, '.pick should not change BagHash';
}

{
Expand All @@ -307,6 +310,44 @@ sub showkv($x) {
is +@a, 100, '.pick(100) returns 100 items';
ok @a.grep(* eq 'a') > 98, '.pick(100) (1)';
ok @a.grep(* eq 'b') < 2, '.pick(100) (2)';
is $b.total, 100000000001, '.pick should not change BagHash';
}

# L<S32::Containers/BagHash/grab>

{
my $b = BagHash.new("a", "b", "b");

my $a = $b.grab;
ok $a eq "a" || $a eq "b", "We got one of the two choices";

my @a = $b.grab(2);
is +@a, 2, '.grab(2) returns the right number of items';
ok @a.grep(* eq 'a').elems <= 1, '.grab(2) returned at most one "a"';
is @a.grep(* eq 'b').elems, 2 - @a.grep(* eq 'a').elems, '.grab(2) and the rest are "b"';
is $b.total, 0, '.grab *should* change BagHash';
}

{
my $b = BagHash.new("a", "b", "b");
my @a = $b.grab: *;
is +@a, 3, '.grab(*) returns the right number of items';
is @a.grep(* eq 'a').elems, 1, '.grab(*) (1)';
is @a.grep(* eq 'b').elems, 2, '.grab(*) (2)';
is $b.total, 0, '.grab *should* change BagHash';
}

{
my $b = {"a" => 100000000000, "b" => 1}.BagHash;

my $a = $b.grab;
ok $a eq "a" || $a eq "b", "We got one of the two choices (and this was pretty quick, we hope!)";

my @a = $b.grab: 100;
is +@a, 100, '.grab(100) returns 100 items';
ok @a.grep(* eq 'a') > 98, '.grab(100) (1)';
ok @a.grep(* eq 'b') < 2, '.grab(100) (2)';
is $b.total, 99999999900, '.grab *should* change BagHash';
}

#?rakudo skip "'is ObjectType' NYI"
Expand Down
12 changes: 11 additions & 1 deletion S02-types/set.t
@@ -1,7 +1,7 @@
use v6;
use Test;

plan 146;
plan 150;

sub showset($s) { $s.keys.sort.join(' ') }

Expand Down Expand Up @@ -248,6 +248,7 @@ sub showset($s) { $s.keys.sort.join(' ') }
@a = $s.roll: 100;
is +@a, 100, '.roll(100) returns 100 items';
is @a.grep(* eq 'a' | 'b' | 'c').elems, 100, '.roll(100) returned "a"s, "b"s, and "c"s';
is $s.total, 3, '.roll should not change Set';
}

# L<S32::Containers/Set/pick>
Expand All @@ -258,6 +259,7 @@ sub showset($s) { $s.keys.sort.join(' ') }
is @a.sort.join, 'abcdefgh', 'Set.pick(*) gets all elements';
isnt @a.join, 'abcdefgh', 'Set.pick(*) returns elements in a random order';
# There's only a 1/40_320 chance of that test failing by chance alone.
is $s.total, 8, '.pick should not change Set';
}

{
Expand All @@ -272,6 +274,14 @@ sub showset($s) { $s.keys.sort.join(' ') }
ok @a.grep(* eq 'a').elems <= 1, '.pick(2) returned at most one "a"';
ok @a.grep(* eq 'b').elems <= 1, '.pick(2) returned at most one "b"';
ok @a.grep(* eq 'c').elems <= 1, '.pick(2) returned at most one "c"';
is $s.total, 3, '.pick should not change Set';
}

# L<S32::Containers/Set/grab>

{
my $s = set <a b c>;
dies_ok { $s.grab }, 'cannot call .grab on a Set';
}

# RT 107022
Expand Down
32 changes: 31 additions & 1 deletion S02-types/sethash.t
@@ -1,7 +1,7 @@
use v6;
use Test;

plan 144;
plan 158;

# L<S02/Mutable types/"QuantHash of Bool">

Expand Down Expand Up @@ -244,6 +244,7 @@ sub showset($s) { $s.keys.sort.join(' ') }
@a = $s.roll: 100;
is +@a, 100, '.roll(100) returns 100 items';
is @a.grep(* eq 'a' | 'b' | 'c').elems, 100, '.roll(100) returned "a"s, "b"s, and "c"s';
is $s.total, 3, '.roll should not change the SetHash';
}

# L<S32::Containers/SetHash/pick>
Expand All @@ -254,6 +255,7 @@ sub showset($s) { $s.keys.sort.join(' ') }
is @a.sort.join, 'abcdefgh', 'SetHash.pick(*) gets all elements';
isnt @a.join, 'abcdefgh', 'SetHash.pick(*) returns elements in a random order';
# There's only a 1/40_320 chance of that test failing by chance alone.
is $s.total, 8, '.pick should not change the SetHash';
}

{
Expand All @@ -268,6 +270,34 @@ sub showset($s) { $s.keys.sort.join(' ') }
ok @a.grep(* eq 'a').elems <= 1, '.pick(2) returned at most one "a"';
ok @a.grep(* eq 'b').elems <= 1, '.pick(2) returned at most one "b"';
ok @a.grep(* eq 'c').elems <= 1, '.pick(2) returned at most one "c"';
is $s.total, 3, '.pick should not change the SetHash';
}

# L<S32::Containers/SetHash/grab>

{
my $s = SetHash.new(<a b c d e f g h>);
my @a = $s.grab: *;
is @a.sort.join, 'abcdefgh', 'SetHash.grab(*) gets all elements';
isnt @a.join, 'abcdefgh', 'SetHash.grab(*) returns elements in a random order';
# There's only a 1/40_320 chance of that test failing by chance alone.
is $s.total, 0, '.grab *should* change the SetHash';
}

{
my $s = SetHash.new(<a b c>);

my $a = $s.grab;
ok $a eq "a" || $a eq "b" || $a eq "c", "We got one of the three choices";
is $s.total, 2, '.grab *should* change the SetHash';

my @a = $s.grab(2);
is +@a, 2, '.grab(2) returns the right number of items';
is @a.grep(* eq 'a' | 'b' | 'c').elems, 2, '.grab(2) returned "a"s, "b"s, and "c"s';
ok @a.grep(* eq 'a').elems <= 1, '.grab(2) returned at most one "a"';
ok @a.grep(* eq 'b').elems <= 1, '.grab(2) returned at most one "b"';
ok @a.grep(* eq 'c').elems <= 1, '.grab(2) returned at most one "c"';
is $s.total, 0, '.grab *should* change the SetHash';
}

#?rakudo skip "'is ObjectType' NYI"
Expand Down

0 comments on commit adcc1c7

Please sign in to comment.