Skip to content

Commit d20b4aa

Browse files
committed
Merge pull request #8 from titsuki/fix-type-problem
Fix a type problem
2 parents 57f99c9 + 22b6e81 commit d20b4aa

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

lib/Algorithm/MinMaxHeap.pm6

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@ use Algorithm::MinMaxHeap::CmpOperator;
77
has @.nodes;
88
has Mu $.type;
99

10-
multi submethod BUILD() {
11-
$!type = Int;
12-
}
13-
14-
multi submethod BUILD(Mu:U :$!type) {
15-
if (Algorithm::MinMaxHeap::Comparable|Cool|Str|Rat|Int|Num ~~ $!type) {
16-
# nothing to do
17-
} else {
18-
die "ERROR: Not compatible type is specified";
19-
}
20-
}
10+
multi submethod BUILD(Int:U :$!type) { Int === $!type or die "ERROR: Not compatible type is specified"; } # when no parameters are specified $!type === Int
11+
multi submethod BUILD(Algorithm::MinMaxHeap::Comparable:U :$!type) { Algorithm::MinMaxHeap::Comparable === $!type or die "ERROR: Not compatible type is specified"; }
12+
multi submethod BUILD(Str:U :$!type) { Str === $!type or die "ERROR: Not compatible type is specified"; }
13+
multi submethod BUILD(Rat:U :$!type) { Rat === $!type or die "ERROR: Not compatible type is specified"; }
14+
multi submethod BUILD(Num:U :$!type) { Num === $!type or die "ERROR: Not compatible type is specified"; }
15+
multi submethod BUILD(Cool:U :$!type) { Cool === $!type or die "ERROR: Not compatible type is specified"; }
2116

2217
method insert($value) {
2318
if (not $value.WHAT ~~ $!type) {

t/01-basic.t

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,44 @@ use Algorithm::MinMaxHeap;
55
lives-ok { my $heap = Algorithm::MinMaxHeap.new(); }
66
lives-ok { my $heap = Algorithm::MinMaxHeap.new(type => Cool); }
77
lives-ok { my $heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable); }
8-
98
lives-ok { my $heap = Algorithm::MinMaxHeap.new(type => Str); }
109
lives-ok { my $heap = Algorithm::MinMaxHeap.new(type => Rat); }
1110
lives-ok { my $heap = Algorithm::MinMaxHeap.new(type => Int); }
1211
lives-ok { my $heap = Algorithm::MinMaxHeap.new(type => Num); }
13-
dies-ok { my $heap = Algorithm::MinMaxHeap.new(type => Range); }
12+
dies-ok { my $heap = Algorithm::MinMaxHeap.new(type => Range); }, "It shouldn't set Range";
13+
dies-ok { my $heap = Algorithm::MinMaxHeap.new(type => Any); }, "It shouldn't set Any";
14+
dies-ok { my $heap = Algorithm::MinMaxHeap.new(type => Mu); }, "It shouldn't set Mu";
15+
dies-ok { my $heap = Algorithm::MinMaxHeap.new(type => Numeric); }, "It shouldn't set Numeric";
16+
dies-ok { my $heap = Algorithm::MinMaxHeap.new(type => Real); }, "It shouldn't set Real";
17+
18+
{
19+
my $heap = Algorithm::MinMaxHeap.new();
20+
is $heap.type, Int, "It should be Int";
21+
}
22+
23+
{
24+
my $heap = Algorithm::MinMaxHeap.new(type => Cool);
25+
is $heap.type, Cool, "It should be Cool";
26+
}
27+
28+
{
29+
my $heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable);
30+
is $heap.type, Algorithm::MinMaxHeap::Comparable, "It should be Comparable";
31+
}
32+
33+
{
34+
my $heap = Algorithm::MinMaxHeap.new(type => Str);
35+
is $heap.type, Str, "It should be Str";
36+
}
37+
38+
{
39+
my $heap = Algorithm::MinMaxHeap.new(type => Rat);
40+
is $heap.type, Rat, "It should be Rat";
41+
}
42+
43+
{
44+
my $heap = Algorithm::MinMaxHeap.new(type => Num);
45+
is $heap.type, Num, "It should be Num";
46+
}
1447

1548
done-testing;

0 commit comments

Comments
 (0)