| 1 |
root |
1.1 |
BEGIN { $| = 1; print "1..16\n"; } |
| 2 |
|
|
END {print "not ok 1\n" unless $loaded;} |
| 3 |
|
|
use Array::Heap; |
| 4 |
|
|
$loaded = 1; |
| 5 |
|
|
print "ok 1\n"; |
| 6 |
|
|
|
| 7 |
|
|
@h = (9,8,7,4,3,5,6,1,2); |
| 8 |
|
|
make_heap @h; |
| 9 |
root |
1.2 |
print "@h" eq "1 2 5 3 7 8 6 9 4" ? "ok 2\n" : "not ok 2 # @h\n"; |
| 10 |
root |
1.1 |
|
| 11 |
|
|
@g = map +(pop_heap @h), 1..9; |
| 12 |
root |
1.2 |
print "@g" eq "1 2 3 4 5 6 7 8 9" ? "ok 3\n" : "not ok 3 # @g\n"; |
| 13 |
root |
1.1 |
|
| 14 |
|
|
push_heap @i, 1,2,3,5,6,7,9,8,4; |
| 15 |
root |
1.2 |
print "@i" eq "1 2 3 4 6 7 9 8 5" ? "ok 4\n" : "not ok 4 # @i\n"; |
| 16 |
root |
1.1 |
|
| 17 |
|
|
@g = map +(pop_heap @i), 1..9; |
| 18 |
root |
1.2 |
print "@g" eq "1 2 3 4 5 6 7 8 9" ? "ok 5\n" : "not ok 5 # @g\n"; |
| 19 |
root |
1.1 |
|
| 20 |
|
|
@h = (9,8,7,4,3,5,6,1,2); |
| 21 |
|
|
make_heap_cmp { $b <=> $a } @h; |
| 22 |
root |
1.2 |
print "@h" eq "9 8 7 4 3 5 6 1 2" ? "ok 6\n" : "not ok 6 # @h\n"; |
| 23 |
root |
1.1 |
|
| 24 |
|
|
@g = map +(pop_heap_cmp { $b <=> $a } @h), 1..9; |
| 25 |
root |
1.2 |
print "@g" eq "9 8 7 6 5 4 3 2 1" ? "ok 7\n" : "not ok 7 # @g\n"; |
| 26 |
root |
1.1 |
|
| 27 |
|
|
@h = (2,1,6,5,3,4,7,8,9); |
| 28 |
|
|
make_heap_cmp { $a <=> $b } @h; |
| 29 |
root |
1.2 |
print "@h" eq "1 2 4 5 3 6 7 8 9" ? "ok 8\n" : "not ok 8 # @h\n"; |
| 30 |
root |
1.1 |
|
| 31 |
|
|
@h = ([4, "hi"], 3, [7], [9,5], [0]); |
| 32 |
|
|
make_heap @h; |
| 33 |
|
|
print $h[0][0] == 0 ? "ok 9\n" : "not ok 9\n"; |
| 34 |
|
|
print $h[1] == 3 ? "ok 10\n" : "not ok 10\n"; |
| 35 |
|
|
print $h[2][0] == 7 ? "ok 11\n" : "not ok 11\n"; |
| 36 |
|
|
print $h[3][0] == 9 ? "ok 12\n" : "not ok 12\n"; |
| 37 |
|
|
print $h[4][0] == 4 ? "ok 13\n" : "not ok 13\n"; |
| 38 |
|
|
|
| 39 |
|
|
print @g == 9 ? "ok 14\n" : "not ok 14\n"; |
| 40 |
|
|
print @i == 0 ? "ok 15\n" : "not ok 15\n"; |
| 41 |
|
|
print @h == 5 ? "ok 16\n" : "not ok 16\n"; |
| 42 |
|
|
|