Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additions, corrections, and reformatting for KeySet, KeyBag, and KeyW…
…eight.
- Loading branch information
Kodi Arfer
authored and
Kodi Arfer
committed
Sep 16, 2010
1 parent
a25207b
commit 264f008
Showing
3 changed files
with
50 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |