Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add tests for where .grabpairs *should* work
  • Loading branch information
lizmat committed Oct 6, 2013
1 parent af7277f commit 8379851
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 6 deletions.
43 changes: 42 additions & 1 deletion S02-types/baghash.t
@@ -1,7 +1,7 @@
use v6;
use Test;

plan 190;
plan 212;

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

Expand Down Expand Up @@ -46,6 +46,7 @@ sub showkv($x) {
#?pugs skip '.total NYI'
#?niecza skip '.total NYI'
is $b.total, 8, '.total gives sum of values';
is $b.elems, 3, '.elems gives number of elements';
is +$b, 8, '+$bag gives sum of values';

lives_ok { $b<a> = 42 }, "Can assign to an existing element";
Expand Down Expand Up @@ -269,6 +270,7 @@ sub showkv($x) {
#?pugs skip '.total NYI'
#?niecza skip '.total NYI'
is $b.total, 3, '.roll should not change BagHash';
is $b.elems, 2, '.roll should not change BagHash';
}

{
Expand All @@ -284,6 +286,7 @@ sub showkv($x) {
#?pugs skip '.total NYI'
#?niecza skip '.total NYI'
is $b.total, 100000000001, '.roll should not change BagHash';
is $b.elems, 2, '.roll should not change BagHash';
}

# L<S32::Containers/BagHash/pick>
Expand All @@ -306,6 +309,7 @@ sub showkv($x) {
#?pugs skip '.total NYI'
#?niecza skip '.total NYI'
is $b.total, 3, '.pick should not change BagHash';
is $b.elems, 2, '.pick should not change BagHash';
}

{
Expand All @@ -321,6 +325,7 @@ sub showkv($x) {
#?pugs skip '.total NYI'
#?niecza skip '.total NYI'
is $b.total, 100000000001, '.pick should not change BagHash';
is $b.elems, 2, '.pick should not change BagHash';
}

# L<S32::Containers/BagHash/grab>
Expand All @@ -338,6 +343,7 @@ sub showkv($x) {
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';
is $b.elems, 0, '.grab *should* change BagHash';
}

#?pugs skip '.grab NYI'
Expand All @@ -349,6 +355,7 @@ sub showkv($x) {
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';
is $b.elems, 0, '.grab *should* change BagHash';
}

#?pugs skip '.grab NYI'
Expand All @@ -364,6 +371,40 @@ sub showkv($x) {
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';
ok 0 <= $b.elems <= 2, '.grab *should* change BagHash';
}

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

#?pugs skip '.grabpairs NYI'
#?niecza skip '.grabpairs NYI'
{
my $b = BagHash.new("a", "b", "b");

my $a = $b.grabpairs[0];
isa_ok $a, Pair, 'did we get a Pair';
ok $a.key eq "a" || $a.key eq "b", "We got one of the two choices";

my @a = $b.grabpairs(2);
is +@a, 1, '.grabpairs(2) returns the right number of items';
is @a.grep( {.isa(Pair)} ).Num, 1, 'are they all Pairs';
ok @a[0].key eq "a" || @a[0].key eq "b", "We got one of the two choices";
is $b.total, 0, '.grabpairs *should* change BagHash';
is $b.elems, 0, '.grabpairs *should* change BagHash';
}

#?pugs skip '.grabpairs NYI'
#?niecza skip '.grabpairs NYI'
{
my $b = BagHash.new(<a a b b c c d d e e f f g g h h>);
my @a = $b.grabpairs: *;
is +@a, 8, '.grabpairs(*) returns the right number of items';
is @a.grep( {.isa(Pair)} ).Num, 8, 'are they all Pairs';
is @a.grep( {.value == 2} ).Num, 8, 'and they all have an expected value';
is @a.sort.map({.key}).join, "abcdefgh", 'SetHash.grabpairs(*) gets all elements';
isnt @a.map({.key}).join, "abcdefgh", 'SetHash.grabpairs(*) returns elements in a random order';
is $b.total, 0, '.grabpairs *should* change BagHash';
is $b.elems, 0, '.grabpairs *should* change BagHash';
}

#?rakudo skip "'is ObjectType' NYI"
Expand Down
47 changes: 43 additions & 4 deletions S02-types/mixhash.t
@@ -1,7 +1,7 @@
use v6;
use Test;

plan 168;
plan 188;

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

Expand Down Expand Up @@ -46,6 +46,7 @@ sub showkv($x) {
#?pugs skip '.total NYI'
#?niecza skip '.total NYI'
is $m.total, 8, '.total gives sum of values';
is $m.elems, 3, '.total gives sum of values';
is +$m, 8, '+$mix gives sum of values';

lives_ok { $m<a> = 42 }, "Can assign to an existing element";
Expand Down Expand Up @@ -137,6 +138,7 @@ sub showkv($x) {
{
my $m = MixHash.new([ foo => 10.1, bar => 17.2, baz => 42.3, santa => 0 ]);
is $m.total, 1, 'make sure .total is ok';
is $m.elems, 1, 'make sure .elems is ok';
isa_ok $m, MixHash, '&MixHash.new given an array of pairs produces a MixHash';
is +$m, 1, "... with one element";
}
Expand Down Expand Up @@ -164,8 +166,8 @@ sub showkv($x) {
}

{
my $m = MixHash.new(SetHash.new(<foo bar foo bar baz foo>));
isa_ok $m, MixHash, '&MixHash.new given a SetHash produces a MixHash';
my $m = MixHash.new(MixHash.new(<foo bar foo bar baz foo>));
isa_ok $m, MixHash, '&MixHash.new given a MixHash produces a MixHash';
is +$m, 1, "... with one element";
}

Expand All @@ -190,6 +192,7 @@ sub showkv($x) {
{
my $m = { foo => 10.1, bar => 1.2, baz => 2.3}.MixHash;
is $m.total, 13.6, 'make sure .total is ok';
is $m.elems, 3, 'make sure .elems is ok';

# .list is just the keys, as per TimToady:
# http://irclog.perlgeek.de/perl6/2012-02-07#i_5112706
Expand Down Expand Up @@ -271,11 +274,13 @@ sub showkv($x) {
#?pugs skip '.total NYI'
#?niecza skip '.total NYI'
is $m.total, 3, '.roll should not change MixHash';
is $m.elems, 2, '.roll should not change MixHash';
}

{
my $m = {a => 100000000000, b => 1, c => -100000000000}.MixHash;
is $m.total, 1, 'make sure total is ok';
is $m.elems, 3, '.roll should not change MixHash';

my $a = $m.roll;
ok $a eq "a" || $a eq "b", "We got one of the two choices (and this was pretty quick, we hope!)";
Expand All @@ -287,9 +292,9 @@ sub showkv($x) {
#?pugs skip '.total NYI'
#?niecza skip '.total NYI'
is $m.total, 1, '.roll should not change MixHash';
is $m.elems, 3, '.roll should not change MixHash';
}


# L<S32::Containers/MixHash/pick>

{
Expand All @@ -306,6 +311,40 @@ sub showkv($x) {
dies_ok { $m.grab }, 'cannot call .grab on a MixHash';
}


# L<S32::Containers/MixHash/grabpairs>

#?pugs skip '.grabpairs NYI'
#?niecza skip '.grabpairs NYI'
{
my $m = MixHash.new("a", "b", "b");

my $a = $m.grabpairs[0];
isa_ok $a, Pair, 'did we get a Pair';
ok $a.key eq "a" || $a.key eq "b", "We got one of the two choices";

my @a = $m.grabpairs(2);
is +@a, 1, '.grabpairs(2) returns the right number of items';
is @a.grep( {.isa(Pair)} ).Num, 1, 'are they all Pairs';
ok @a[0].key eq "a" || @a[0].key eq "b", "We got one of the two choices";
is $m.total, 0, '.grabpairs *should* change MixHash';
is $m.elems, 0, '.grabpairs *should* change MixHash';
}

#?pugs skip '.grabpairs NYI'
#?niecza skip '.grabpairs NYI'
{
my $m = (a=>1.1,b=>2.2,c=>3.3,d=>4.4,e=>5.5,f=>6.6,g=>7.7,h=>8.8).MixHash;
my @a = $m.grabpairs: *;
is +@a, 8, '.grabpairs(*) returns the right number of items';
is @a.grep( {.isa(Pair)} ).Num, 8, 'are they all Pairs';
is @a.grep( {1.1 <= .value <= 8.8} ).Num, 8, 'and they all have an expected value';
is @a.sort.map({.key}).join, "abcdefgh", 'MixHash.grabpairs(*) gets all elements';
isnt @a.map({.key}).join, "abcdefgh", 'MixHash.grabpairs(*) returns elements in a random order';
is $m.total, 0, '.grabpairs *should* change MixHash';
is $m.elems, 0, '.grabpairs *should* change MixHash';
}

#?rakudo skip "'is ObjectType' NYI"
#?niecza skip "Trait name not available on variables"
{
Expand Down
46 changes: 45 additions & 1 deletion S02-types/sethash.t
@@ -1,7 +1,7 @@
use v6;
use Test;

plan 158;
plan 182;

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

Expand Down Expand Up @@ -247,6 +247,7 @@ sub showset($s) { $s.keys.sort.join(' ') }
#?pugs skip '.total NYI'
#?niecza skip '.total NYI'
is $s.total, 3, '.roll should not change the SetHash';
is $s.elems, 3, '.roll should not change the SetHash';
}

# L<S32::Containers/SetHash/pick>
Expand All @@ -260,6 +261,7 @@ sub showset($s) { $s.keys.sort.join(' ') }
#?pugs skip '.total NYI'
#?niecza skip '.total NYI'
is $s.total, 8, '.pick should not change the SetHash';
is $s.elems, 8, '.pick should not change the SetHash';
}

{
Expand All @@ -277,6 +279,7 @@ sub showset($s) { $s.keys.sort.join(' ') }
#?pugs skip '.total NYI'
#?niecza skip '.total NYI'
is $s.total, 3, '.pick should not change the SetHash';
is $s.elems, 3, '.pick should not change the SetHash';
}

# L<S32::Containers/SetHash/grab>
Expand All @@ -290,6 +293,7 @@ sub showset($s) { $s.keys.sort.join(' ') }
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';
is $s.elems, 0, '.grab *should* change the SetHash';
}

#?pugs skip '.grab NYI'
Expand All @@ -300,6 +304,7 @@ sub showset($s) { $s.keys.sort.join(' ') }
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';
is $s.elems, 2, '.grab *should* change the SetHash';

my @a = $s.grab(2);
is +@a, 2, '.grab(2) returns the right number of items';
Expand All @@ -308,6 +313,45 @@ sub showset($s) { $s.keys.sort.join(' ') }
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';
is $s.elems, 0, '.grab *should* change the SetHash';
}

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

#?pugs skip '.grabpairs NYI'
#?niecza skip '.grabpairs NYI'
{
my $s = SetHash.new(<a b c d e f g h>);
my @a = $s.grabpairs: *;
is @a.grep( {.isa(Pair)} ).Num, 8, 'are they all Pairs';
is @a.grep( {.value === True} ).Num, 8, 'and they all have a True value';
is @a.sort.map({.key}).join, "abcdefgh", 'SetHash.grabpairs(*) gets all elements';
isnt @a.map({.key}).join, "abcdefgh", 'SetHash.grabpairs(*) 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, '.grabpairs *should* change the SetHash';
is $s.elems, 0, '.grabpairs *should* change the SetHash';
}

#?pugs skip '.grabpairs NYI'
#?niecza skip '.grabpairs NYI'
{
my $s = SetHash.new(<a b c>);

my $a = $s.grabpairs[0];
isa_ok $a, Pair, 'and is it a Pair';
ok $a.key eq "a" || $a.key eq "b" || $a.key eq "c", "We got one of the three choices";
is $s.total, 2, '.grabpairs *should* change the SetHash';
is $s.elems, 2, '.grabpairs *should* change the SetHash';

my @a = $s.grabpairs(2);
is @a.grep( {.isa(Pair)} ).Num, 2, 'are they all Pairs';
is +@a, 2, '.grabpairs(2) returns the right number of items';
is @a.grep(*.key eq 'a' | 'b' | 'c').elems, 2, '.grabpairs(2) returned "a"s, "b"s, and "c"s';
ok @a.grep(*.key eq 'a').elems <= 1, '.grabpairs(2) returned at most one "a"';
ok @a.grep(*.key eq 'b').elems <= 1, '.grabpairs(2) returned at most one "b"';
ok @a.grep(*.key eq 'c').elems <= 1, '.grabpairs(2) returned at most one "c"';
is $s.total, 0, '.grabpairs *should* change the SetHash';
is $s.elems, 0, '.grabpairs *should* change the SetHash';
}

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

0 comments on commit 8379851

Please sign in to comment.