Skip to content

Commit 5ed954c

Browse files
committed
Add tests for class insertion feature
1 parent e0b396c commit 5ed954c

File tree

6 files changed

+357
-5
lines changed

6 files changed

+357
-5
lines changed

t/01-basic.t

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,13 @@ use Test;
33
use Algorithm::MinMaxHeap;
44

55
lives-ok { my $heap = Algorithm::MinMaxHeap.new(); }
6+
lives-ok { my $heap = Algorithm::MinMaxHeap.new(type => Cool); }
7+
lives-ok { my $heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable); }
8+
9+
lives-ok { my $heap = Algorithm::MinMaxHeap.new(type => Str); }
10+
lives-ok { my $heap = Algorithm::MinMaxHeap.new(type => Rat); }
11+
lives-ok { my $heap = Algorithm::MinMaxHeap.new(type => Int); }
12+
lives-ok { my $heap = Algorithm::MinMaxHeap.new(type => Num); }
13+
dies-ok { my $heap = Algorithm::MinMaxHeap.new(type => Range); }
614

715
done-testing;

t/02-insert.t

Lines changed: 187 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use v6;
22
use Test;
33
use Algorithm::MinMaxHeap;
4+
use Algorithm::MinMaxHeap::Comparable;
45

5-
{
6+
subtest {
67
my $heap = Algorithm::MinMaxHeap.new();
78
$heap.insert(0);
89
$heap.insert(1);
@@ -23,6 +24,190 @@ use Algorithm::MinMaxHeap;
2324
is $heap.nodes[1] > $heap.nodes[8], True;
2425
is $heap.nodes[0], 0;
2526
is max($heap.nodes[1], $heap.nodes[2]), 8;
26-
}
27+
}, "Given a constructor with no parameters, It should insert Int items";
28+
29+
subtest {
30+
my $heap = Algorithm::MinMaxHeap.new(type => Cool);
31+
$heap.insert(0);
32+
$heap.insert(1.1);
33+
$heap.insert(2);
34+
$heap.insert(3.1);
35+
$heap.insert(4);
36+
$heap.insert(5);
37+
$heap.insert(6);
38+
$heap.insert(7);
39+
$heap.insert(8);
40+
41+
is $heap.nodes[0] < $heap.nodes[3], True;
42+
is $heap.nodes[0] < $heap.nodes[4], True;
43+
is $heap.nodes[0] < $heap.nodes[5], True;
44+
is $heap.nodes[0] < $heap.nodes[6], True;
45+
46+
is $heap.nodes[1] > $heap.nodes[7], True;
47+
is $heap.nodes[1] > $heap.nodes[8], True;
48+
is $heap.nodes[0], 0;
49+
is max($heap.nodes[1], $heap.nodes[2]), 8;
50+
}, "Given a constructor with no parameters, It should insert Int items";
51+
52+
subtest {
53+
my $heap = Algorithm::MinMaxHeap.new(type => Cool);
54+
$heap.insert(0);
55+
$heap.insert(1.1e0);
56+
$heap.insert(2);
57+
$heap.insert(3.1e0);
58+
$heap.insert(4);
59+
$heap.insert(5);
60+
$heap.insert(6);
61+
$heap.insert(7);
62+
$heap.insert(8);
63+
64+
is $heap.nodes[0] < $heap.nodes[3], True;
65+
is $heap.nodes[0] < $heap.nodes[4], True;
66+
is $heap.nodes[0] < $heap.nodes[5], True;
67+
is $heap.nodes[0] < $heap.nodes[6], True;
68+
69+
is $heap.nodes[1] > $heap.nodes[7], True;
70+
is $heap.nodes[1] > $heap.nodes[8], True;
71+
is $heap.nodes[0], 0;
72+
is max($heap.nodes[1], $heap.nodes[2]), 8;
73+
}, "Given a constructor with \"type => Cool\" option, It should insert Int/Num items";
74+
75+
subtest {
76+
my $heap = Algorithm::MinMaxHeap.new(type => Cool);
77+
$heap.insert(0);
78+
$heap.insert(1.1e0);
79+
$heap.insert(2);
80+
$heap.insert(3.1e0);
81+
$heap.insert(4);
82+
$heap.insert(5);
83+
$heap.insert(6);
84+
$heap.insert(7.1);
85+
$heap.insert(8);
86+
87+
is $heap.nodes[0] < $heap.nodes[3], True;
88+
is $heap.nodes[0] < $heap.nodes[4], True;
89+
is $heap.nodes[0] < $heap.nodes[5], True;
90+
is $heap.nodes[0] < $heap.nodes[6], True;
91+
92+
is $heap.nodes[1] > $heap.nodes[7], True;
93+
is $heap.nodes[1] > $heap.nodes[8], True;
94+
is $heap.nodes[0], 0;
95+
is max($heap.nodes[1], $heap.nodes[2]), 8;
96+
}, "Given a constructor with \"type => Cool\" option, It should insert Int/Num/Rat items";
97+
98+
subtest {
99+
my class State {
100+
also does Algorithm::MinMaxHeap::Comparable[State];
101+
has Int $.value;
102+
submethod BUILD(:$!value) { }
103+
method compare-to(State $s) {
104+
if (self.value == $s.value) {
105+
return Order::Same;
106+
}
107+
if (self.value > $s.value) {
108+
return Order::More;
109+
}
110+
if (self.value < $s.value) {
111+
return Order::Less;
112+
}
113+
}
114+
}
115+
my $heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable);
116+
117+
$heap.insert(State.new(value => 8));
118+
$heap.insert(State.new(value => 0));
119+
$heap.insert(State.new(value => 1));
120+
$heap.insert(State.new(value => 2));
121+
$heap.insert(State.new(value => 3));
122+
$heap.insert(State.new(value => 4));
123+
$heap.insert(State.new(value => 5));
124+
$heap.insert(State.new(value => 6));
125+
$heap.insert(State.new(value => 7));
126+
127+
is $heap.nodes[0].compare-to($heap.nodes[3]) == Order::Less, True;
128+
is $heap.nodes[0].compare-to($heap.nodes[4]) == Order::Less, True;
129+
is $heap.nodes[0].compare-to($heap.nodes[5]) == Order::Less, True;
130+
is $heap.nodes[0].compare-to($heap.nodes[6]) == Order::Less, True;
131+
132+
is $heap.nodes[1].compare-to($heap.nodes[7]) == Order::More, True;
133+
is $heap.nodes[1].compare-to($heap.nodes[8]) == Order::More, True;
134+
is $heap.nodes[0].value, 0;
135+
is max($heap.nodes[1].value, $heap.nodes[2].value), 8;
136+
}, "Given a constructor with \"type => Algorithm::MinMaxHeap::Comparable\" option, It should insert Algorithm::MinMaxHeap::Comparable items";
137+
138+
subtest {
139+
my class State {
140+
also does Algorithm::MinMaxHeap::Comparable[State];
141+
has Int $.value;
142+
submethod BUILD(:$!value) { }
143+
method compare-to(State $s) {
144+
if (self.value == $s.value) {
145+
return Order::Same;
146+
}
147+
if (self.value > $s.value) {
148+
return Order::More;
149+
}
150+
if (self.value < $s.value) {
151+
return Order::Less;
152+
}
153+
}
154+
}
155+
my $heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable);
156+
157+
$heap.insert(State.new(value => 8));
158+
$heap.insert(State.new(value => 0));
159+
$heap.insert(State.new(value => 1));
160+
dies-ok { $heap.insert(1); }
161+
}, "Given a constructor with \"type => Algorithm::MinMaxHeap::Comparable\" option, It shouldn't insert Algorithm::MinMaxHeap::Comparable/Int items";
162+
163+
subtest {
164+
my class State {
165+
also does Algorithm::MinMaxHeap::Comparable[State];
166+
has Int $.value;
167+
submethod BUILD(:$!value) { }
168+
method compare-to(State $s) {
169+
if (self.value == $s.value) {
170+
return Order::Same;
171+
}
172+
if (self.value > $s.value) {
173+
return Order::More;
174+
}
175+
if (self.value < $s.value) {
176+
return Order::Less;
177+
}
178+
}
179+
}
180+
my $heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable);
181+
182+
$heap.insert(State.new(value => 8));
183+
$heap.insert(State.new(value => 0));
184+
$heap.insert(State.new(value => 1));
185+
dies-ok { $heap.insert(1.1e0); }
186+
}, "Given a constructor with \"type => Algorithm::MinMaxHeap::Comparable\" option, It shouldn't insert Algorithm::MinMaxHeap::Comparable/Num items";
187+
188+
subtest {
189+
my class State {
190+
also does Algorithm::MinMaxHeap::Comparable[State];
191+
has Int $.value;
192+
submethod BUILD(:$!value) { }
193+
method compare-to(State $s) {
194+
if (self.value == $s.value) {
195+
return Order::Same;
196+
}
197+
if (self.value > $s.value) {
198+
return Order::More;
199+
}
200+
if (self.value < $s.value) {
201+
return Order::Less;
202+
}
203+
}
204+
}
205+
my $heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable);
206+
207+
$heap.insert(State.new(value => 8));
208+
$heap.insert(State.new(value => 0));
209+
$heap.insert(State.new(value => 1));
210+
dies-ok { $heap.insert(1.5); }
211+
}, "Given a constructor with \"type => Algorithm::MinMaxHeap::Comparable\" option, It shouldn't insert Algorithm::MinMaxHeap::Comparable/Rat items";
27212

28213
done-testing;

t/03-pop-max.t

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use Algorithm::MinMaxHeap;
1818
while (not $heap.is-empty()) {
1919
@actual.push($heap.pop-max);
2020
}
21-
is @actual, [8,7,6,5,4,3,2,1,0];
21+
is @actual, [8,7,6,5,4,3,2,1,0], "It should return descending array";
2222
}
2323

2424
{
@@ -37,7 +37,44 @@ use Algorithm::MinMaxHeap;
3737
while (not $heap.is-empty()) {
3838
@actual.push($heap.pop-max);
3939
}
40-
is @actual, [8,7,6,5,4,3,1,1,0];
40+
is @actual, [8,7,6,5,4,3,1,1,0], "Given a input including duplicated items. It should return descending array";
41+
}
42+
43+
{
44+
my class State {
45+
also does Algorithm::MinMaxHeap::Comparable[State];
46+
has Int $.value;
47+
submethod BUILD(:$!value) { }
48+
method compare-to(State $s) {
49+
if (self.value == $s.value) {
50+
return Order::Same;
51+
}
52+
if (self.value > $s.value) {
53+
return Order::More;
54+
}
55+
if (self.value < $s.value) {
56+
return Order::Less;
57+
}
58+
}
59+
}
60+
61+
my $heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable);
62+
63+
$heap.insert(State.new(value => 0));
64+
$heap.insert(State.new(value => 1));
65+
$heap.insert(State.new(value => 2));
66+
$heap.insert(State.new(value => 3));
67+
$heap.insert(State.new(value => 4));
68+
$heap.insert(State.new(value => 5));
69+
$heap.insert(State.new(value => 6));
70+
$heap.insert(State.new(value => 7));
71+
$heap.insert(State.new(value => 8));
72+
73+
my @actual;
74+
while (not $heap.is-empty()) {
75+
@actual.push($heap.pop-max.value);
76+
}
77+
is @actual, [8,7,6,5,4,3,2,1,0], "It should return descending array";
4178
}
4279

4380
done-testing;

t/04-pop-min.t

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,63 @@ use Algorithm::MinMaxHeap;
1818
while (not $heap.is-empty()) {
1919
@actual.push($heap.pop-min);
2020
}
21-
is @actual, [0,1,2,3,4,5,6,7,8];
21+
is @actual, [0,1,2,3,4,5,6,7,8], "It should return ascending array";
22+
}
23+
24+
{
25+
my $heap = Algorithm::MinMaxHeap.new();
26+
$heap.insert(0);
27+
$heap.insert(1);
28+
$heap.insert(1);
29+
$heap.insert(3);
30+
$heap.insert(4);
31+
$heap.insert(5);
32+
$heap.insert(6);
33+
$heap.insert(7);
34+
$heap.insert(8);
35+
36+
my @actual;
37+
while (not $heap.is-empty()) {
38+
@actual.push($heap.pop-min);
39+
}
40+
is @actual, [0,1,1,3,4,5,6,7,8], "Given a input including duplicated items. It should return ascending array";
41+
}
42+
43+
{
44+
my class State {
45+
also does Algorithm::MinMaxHeap::Comparable[State];
46+
has Int $.value;
47+
submethod BUILD(:$!value) { }
48+
method compare-to(State $s) {
49+
if (self.value == $s.value) {
50+
return Order::Same;
51+
}
52+
if (self.value > $s.value) {
53+
return Order::More;
54+
}
55+
if (self.value < $s.value) {
56+
return Order::Less;
57+
}
58+
}
59+
}
60+
61+
my $heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable);
62+
63+
$heap.insert(State.new(value => 0));
64+
$heap.insert(State.new(value => 1));
65+
$heap.insert(State.new(value => 2));
66+
$heap.insert(State.new(value => 3));
67+
$heap.insert(State.new(value => 4));
68+
$heap.insert(State.new(value => 5));
69+
$heap.insert(State.new(value => 6));
70+
$heap.insert(State.new(value => 7));
71+
$heap.insert(State.new(value => 8));
72+
73+
my @actual;
74+
while (not $heap.is-empty()) {
75+
@actual.push($heap.pop-min.value);
76+
}
77+
is @actual, [0,1,2,3,4,5,6,7,8], "It should return ascending array";
2278
}
2379

2480
done-testing;

t/05-find-max.t

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,37 @@ use Algorithm::MinMaxHeap;
1717
is $heap.find-max, 8;
1818
}
1919

20+
{
21+
my class State {
22+
also does Algorithm::MinMaxHeap::Comparable[State];
23+
has Int $.value;
24+
submethod BUILD(:$!value) { }
25+
method compare-to(State $s) {
26+
if (self.value == $s.value) {
27+
return Order::Same;
28+
}
29+
if (self.value > $s.value) {
30+
return Order::More;
31+
}
32+
if (self.value < $s.value) {
33+
return Order::Less;
34+
}
35+
}
36+
}
37+
38+
my $heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable);
39+
40+
$heap.insert(State.new(value => 0));
41+
$heap.insert(State.new(value => 1));
42+
$heap.insert(State.new(value => 2));
43+
$heap.insert(State.new(value => 3));
44+
$heap.insert(State.new(value => 4));
45+
$heap.insert(State.new(value => 5));
46+
$heap.insert(State.new(value => 6));
47+
$heap.insert(State.new(value => 7));
48+
$heap.insert(State.new(value => 8));
49+
50+
is $heap.find-max.value, 8;
51+
}
52+
2053
done-testing;

0 commit comments

Comments
 (0)