11use v6 ;
22use Test ;
33use 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
28213done-testing ;
0 commit comments