Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Tune and fudge bag, keybag, keyset and set tests for Rakudo and for G…
…reater Good
  • Loading branch information
Tadeusz Sośnierz committed Mar 1, 2012
1 parent fbfc883 commit 71f1532
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
4 changes: 3 additions & 1 deletion S02-types/bag.t
Expand Up @@ -43,6 +43,7 @@ sub showkv($x) {
is +$b, 8, '+$bag gives sum of values';
}

#?rakudo skip ':exists and :delete NYI'
{
my $s = bag <a a b foo>;
is $s<a>:exists, True, ':exists with existing element';
Expand All @@ -54,6 +55,7 @@ sub showkv($x) {
my $b = bag 'a', False, 2, 'a', False, False;
my @ks = $b.keys;
#?niecza 2 todo
#?rakudo 2 todo ''
is @ks.grep(Int)[0], 2, 'Int keys are left as Ints';
is @ks.grep(* eqv False).elems, 1, 'Bool keys are left as Bools';
is @ks.grep(Str)[0], 'a', 'And Str keys are permitted in the same set';
Expand Down Expand Up @@ -204,7 +206,7 @@ sub showkv($x) {
is +@a, 2, '.roll(2) returns the right number of items';
is @a.grep(* eq 'a').elems + @a.grep(* eq 'b').elems, 2, '.roll(2) returned "a"s and "b"s';

my @a = $b.roll: 100;
@a = $b.roll: 100;
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)';
Expand Down
16 changes: 15 additions & 1 deletion S02-types/keybag.t
Expand Up @@ -65,6 +65,7 @@ sub showkv($x) {
nok $b.exists("farve"), "... and everything is still okay";
}

#?rakudo skip ':exists and :delete NYI'
{
my $s = KeyBag.new(<a a b foo>);
is $s<a>:exists, True, ':exists with existing element';
Expand All @@ -77,6 +78,7 @@ sub showkv($x) {
my $b = KeyBag.new('a', False, 2, 'a', False, False);
my @ks = $b.keys;
#?niecza 2 todo
#?rakudo 2 todo ''
is @ks.grep(Int)[0], 2, 'Int keys are left as Ints';
is @ks.grep(* eqv False).elems, 1, 'Bool keys are left as Bools';
is @ks.grep(Str)[0], 'a', 'And Str keys are permitted in the same set';
Expand Down Expand Up @@ -221,7 +223,7 @@ sub showkv($x) {
is +@a, 2, '.roll(2) returns the right number of items';
is @a.grep(* eq 'a').elems + @a.grep(* eq 'b').elems, 2, '.roll(2) returned "a"s and "b"s';

my @a = $b.roll: 100;
@a = $b.roll: 100;
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)';
Expand Down Expand Up @@ -273,10 +275,14 @@ sub showkv($x) {
#?niecza skip "Trait name not available on variables"
{
my %h is KeyBag = a => 1, b => 0, c => 2;
#?rakudo todo 'todo'
nok %h.exists( 'b' ), '"b", initialized to zero, does not exist';
#?rakudo todo 'todo'
is +%h.keys, 2, 'Inititalization worked';
is %h.elems, 3, '.elems works';
#?rakudo todo 'todo'
isa_ok %h<nonexisting>, Int, '%h<nonexisting> is an Int';
#?rakudo todo 'todo'
is %h<nonexisting>, 0, '%h<nonexisting> is 0';
}

Expand All @@ -285,11 +291,15 @@ sub showkv($x) {
my %h is KeyBag = a => 1, b => 0, c => 2;

lives_ok { %h<c> = 0 }, 'can set an item to 0';
#?rakudo todo 'todo'
nok %h.exists( 'c' ), '"c", set to zero, does not exist';
#?rakudo todo 'todo'
is %h.elems, 1, 'one item left';
#?rakudo todo 'todo'
is %h.keys, ('a'), '... and the right one is gone';

lives_ok { %h<c>++ }, 'can add (++) an item that was removed';
#?rakudo todo 'todo'
is %h.keys.sort, <a c>, '++ on an item reinstates it';
}

Expand All @@ -302,12 +312,16 @@ sub showkv($x) {
is %h.keys.sort, <a c>, '++ on an existing item does not add a key';

lives_ok { %h<a>-- }, 'can remove an item with decrement (--)';
#?rakudo todo 'todo'
is %h.keys, ('c'), 'decrement (--) removes items';
#?rakudo todo 'todo'
nok %h.exists( 'a' ), 'item is gone according to .exists too';
is %h<a>, 0, 'removed item is zero';

lives_ok { %h<a>-- }, 'remove a missing item lives';
#?rakudo todo 'todo'
is %h.keys, ('c'), 'removing missing item does not change contents';
#?rakudo todo 'todo'
is %h<a>, 0, 'item removed again is still zero';
}

Expand Down
14 changes: 11 additions & 3 deletions S02-types/keyset.t
Expand Up @@ -67,6 +67,7 @@ sub showset($s) { $s.keys.sort.join(' ') }
is showset($s), 'a foo', '... but only if they were there to start with';
}

#?rakudo skip ':exists and :delete NYI'
{
my $s = KeySet.new(<a b foo>);
is $s<a>:exists, True, ':exists with existing element';
Expand Down Expand Up @@ -164,7 +165,6 @@ sub showset($s) { $s.keys.sort.join(' ') }

{
my $s = KeySet.new(<foo bar baz>);
my $s;
lives_ok { $s = $s.Str }, ".Str lives";
isa_ok $s, Str, "... and produces a string";
ok $s ~~ /foo/, "... which mentions foo";
Expand All @@ -174,7 +174,6 @@ sub showset($s) { $s.keys.sort.join(' ') }

{
my $s = KeySet.new(<foo bar baz>);
my $s;
lives_ok { $s = $s.gist }, ".gist lives";
isa_ok $s, Str, "... and produces a string";
ok $s ~~ /foo/, "... which mentions foo";
Expand Down Expand Up @@ -207,7 +206,7 @@ sub showset($s) { $s.keys.sort.join(' ') }
is +@a, 2, '.roll(2) returns the right number of items';
is @a.grep(* eq 'a' | 'b' | 'c').elems, 2, '.roll(2) returned "a"s, "b"s, and "c"s';

my @a = $s.roll: 100;
@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';
}
Expand Down Expand Up @@ -239,23 +238,32 @@ sub showset($s) { $s.keys.sort.join(' ') }
#?niecza skip "is KeySet doesn't work yet"
{
my %h is KeySet = a => True, b => False, c => True;
#?rakudo todo 'todo'
is +%h.elems, 2, 'Inititalization worked';

lives_ok { %h<c> = False }, 'can set an item to False';
#?rakudo todo 'todo'
is %h.elems, 1, '... and an item is gone';
#?rakudo todo 'todo'
is ~%h.keys, 'a', '... and the right one is gone';

%h<c>++;
#?rakudo todo 'todo'
is %h.keys.sort.join, 'ac', '++ on an item reinstates it';
%h<c>++;
#?rakudo todo 'todo'
is %h.keys.sort.join, 'ac', '++ on an existing item does nothing';

%h<a>--;
#?rakudo todo 'todo'
is ~%h.keys, 'c', '-- removes items';
%h<b>--;
#?rakudo todo 'todo'
is ~%h.keys, 'c', '... but only if they were there from the beginning';

#?rakudo todo 'todo'
lives_ok { %h = set <Q P R> }, 'Assigning a Set to a KeySet';
#?rakudo todo 'todo'
is %h.keys.sort.join, 'PQR', '... works as expected';
}

Expand Down
13 changes: 7 additions & 6 deletions S02-types/set.t
Expand Up @@ -41,15 +41,16 @@ sub showset($s) { $s.keys.sort.join(' ') }

{
my $s = set <a b foo>;
is $s<a>:exists, True, ':exists with existing element';
is $s<santa>:exists, False, ':exists with nonexistent element';
dies_ok { $s<a>:delete }, ':delete does not work on set';
is $s.exists(<a>), True, ':exists with existing element';
is $s.exists(<santa>), False, ':exists with nonexistent element';
dies_ok { $s.delete(<a>) }, ':delete does not work on set';
}

{
my $s = set 2, 'a', False;
my @ks = $s.keys;
#?niecza 3 todo
#?rakudo 3 todo ''
is @ks.grep(Int)[0], 2, 'Int keys are left as Ints';
is @ks.grep(* eqv False).elems, 1, 'Bool keys are left as Bools';
is @ks.grep(Str)[0], 'a', 'And Str keys are permitted in the same set';
Expand All @@ -58,6 +59,7 @@ sub showset($s) { $s.keys.sort.join(' ') }

# RT #77760
#?niecza skip "Unmatched key in Hash.LISTSTORE"
#?rakudo skip "Odd number of elements found where hash expected"
{
my %h = set <a b o p a p o o>;
ok %h ~~ Hash, 'A hash to which a Set has been assigned remains a hash';
Expand Down Expand Up @@ -132,7 +134,6 @@ sub showset($s) { $s.keys.sort.join(' ') }

{
my $s = set <foo bar baz>;
my $s;
lives_ok { $s = $s.Str }, ".Str lives";
isa_ok $s, Str, "... and produces a string";
ok $s ~~ /foo/, "... which mentions foo";
Expand All @@ -142,7 +143,6 @@ sub showset($s) { $s.keys.sort.join(' ') }

{
my $s = set <foo bar baz>;
my $s;
lives_ok { $s = $s.gist }, ".gist lives";
isa_ok $s, Str, "... and produces a string";
ok $s ~~ /foo/, "... which mentions foo";
Expand All @@ -166,6 +166,7 @@ sub showset($s) { $s.keys.sort.join(' ') }

# L<S03/Hyper operators/'unordered type'>
#?niecza skip "Hypers not yet Set compatible"
#?rakudo skip "Hypers not yet Set compatible"
{
is showset(set(1, 2, 3) »+» 6), '7 8 9', 'Set »+» Int';
is showset("a" «~« set(<pple bbot rmadillo>)), 'abbot apple armadillo', 'Str «~« Set';
Expand Down Expand Up @@ -193,7 +194,7 @@ sub showset($s) { $s.keys.sort.join(' ') }
is +@a, 2, '.roll(2) returns the right number of items';
is @a.grep(* eq 'a' | 'b' | 'c').elems, 2, '.roll(2) returned "a"s, "b"s, and "c"s';

my @a = $s.roll: 100;
@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';
}
Expand Down

0 comments on commit 71f1532

Please sign in to comment.