Skip to content

Commit

Permalink
Additions, corrections, and reformatting for KeySet, KeyBag, and KeyW…
Browse files Browse the repository at this point in the history
…eight.
  • Loading branch information
Kodi Arfer authored and Kodi Arfer committed Sep 16, 2010
1 parent a25207b commit 264f008
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 33 deletions.
33 changes: 19 additions & 14 deletions S02-builtin_data_types/keybag.t
@@ -1,27 +1,26 @@
use v6;
use Test;
plan 20;

plan 23;

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

# A KeyBag is a KeyHash of UInt, i.e. the values are positive Int

{
my %h is KeyBag;

%h = (a => 1, b => 0, c => 2);
ok ! %h.exists( 'b' ), '"b", initialized to zero, does not exist';
is %h.elems, 2, 'Inititalization worked';
ok %h<nonexisting> ~~ Int, '%h<nonexisting> is 0 (Int)';
ok %h<nonexisting> == 0, '%h<nonexisting> is 0 (Int)';
my %h is KeyBag = a => 1, b => 0, c => 2;
nok %h.exists( 'b' ), '"b", initialized to zero, does not exist';
is +%h.keys, 2, 'Inititalization worked';
is %h.elems, 3, '.elems works';
isa_ok %h<nonexisting>, Int, '%h<nonexisting> is an Int';
is %h<nonexisting>, 0, '%h<nonexisting> is 0';
}

{
my %h is KeyBag;
%h = (a => 1, b => 0, c => 2);
my %h is KeyBag = a => 1, b => 0, c => 2;

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

Expand All @@ -30,21 +29,27 @@ plan 20;
}

{
my %h is KeyBag;
%h = (a => 1, c => 1);
my %h is KeyBag = a => 1, c => 1;

lives_ok { %h<c>++ }, 'can "add" (++) an existing item';
is %h<c>, 2, '++ on an existing item increments the counter';
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 (--)';
is %h.keys, ('c'), 'decrement (--) removes items';
ok ! %h.exists( 'a' ), 'item is gone according to .exists too';
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';
is %h.keys, ('c'), 'removing missing item does not change contents';
is %h<a>, 0, 'item removed again is still zero';
}

{
my %h is KeyBag;
lives_ok { %h = bag <a b c d c b> }, 'Assigning a Bag to a KeyBag';
is %h.keys.sort.map({ $^k ~ ':' ~ %h{$^k} }).join(' '),
'a:1 b:2 c:2 d:1', '... works as expected';
}

# vim: ft=perl6
17 changes: 11 additions & 6 deletions S02-builtin_data_types/keyset.t
@@ -1,27 +1,32 @@
use v6;
use Test;
plan 8;

plan 10;

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

# A KeySet is a KeyHash of Bool, i.e. the values are Bool

{
my %h is KeySet;

%h = (a => True, b => False, c => True);
my %h is KeySet = a => True, b => False, c => True;
is +%h.elems, 2, 'Inititalization worked';
lives_ok { %h<c> = 0 }, 'can set an item to 0';

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

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

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

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

# vim: ft=perl6
33 changes: 20 additions & 13 deletions S02-builtin_data_types/keyweight.t
@@ -1,27 +1,34 @@
use v6;
use Test;
plan 4;
plan 8;

# L<S02/Mutable types>
#?rakudo emit class FatRat { method new($x, $y) { Rat.new($x, $y) } }; # FatRat NYI, so we fake it with Rat

# L<S02/Mutable types/KeyWeight>

{
my %h is KeyWeight;
my %h is KeyWeight = a => FatRat.new(1,2), b => FatRat.new(3,4);
is +%h.keys, 2, 'Inititalization worked';

is +%h, (FatRat.new(1,2) + FatRat.new(3,4)), '+%h works';

%h = (a => FatRat.new(1,2), b => FatRat.new(3,4));
is +%h.elems, 2, 'Inititalization worked';
%h<a> = 0;
is %h.elems, 1, '... and an item is gone';
is %h.keys.join, 'a', '... and the right one is gone';
%h<a> = FatRat.new(0, 1);
is +%h.keys, 1, 'After setting an item to FatRat.new(0, 1), an item is gone';
is ~%h.keys, 'b', '... and the right one is gone';
is +%h, FatRat.new(3,4), '... and +%h has changed appropriately';
}

# L<S32::Containers/KeyWeight>

{
my %h is KeyWeight;
my %h is KeyWeight = a => FatRat.new(1,2), b => FatRat.new(3,4);
%h<a> = FatRat.new(-1,1); # negative key
is +%h.keys, 2, 'No deletion of negative keys'; # may warn

%h = (a => FatRat.new(1,2), b => FatRat.new(3,4));
%h<a> = FatRat(-1,1); # negative key
is +%h.elems, 2, 'No deletion of negative keys'; # may warn
%h = x => FatRat.new(2,3), y => FatRat.new(1,3);
my @a = %h.roll: 25;
ok 2 < @a.grep(* eq 'y') < 25, 'KeyWeight.roll(25) (1)';
ok @a.grep(* eq 'y') < @a.grep({* eq 'x'}), 'KeyWeight.roll(25) (2)';
}


# vim: ft=perl6

0 comments on commit 264f008

Please sign in to comment.