1 |
root |
1.1 |
$| = 1; print "1..3000\n"; |
2 |
|
|
|
3 |
|
|
no warnings; |
4 |
|
|
use Array::Heap; |
5 |
|
|
|
6 |
|
|
srand 0; |
7 |
|
|
|
8 |
|
|
my @x = map [$_], 1..10, map rand, 1..100; |
9 |
|
|
my $err; |
10 |
|
|
|
11 |
|
|
my @test = ( |
12 |
|
|
sub { push_heap_idx @x, [rand] }, |
13 |
|
|
sub { push_heap_idx @x, [1 + rand], [3 + rand] }, |
14 |
|
|
sub { pop_heap_idx @x }, |
15 |
|
|
sub { splice_heap_idx @x, int rand @x }, |
16 |
|
|
); |
17 |
|
|
|
18 |
|
|
sub chk { |
19 |
|
|
for (1 .. $#x) { |
20 |
|
|
if (!($x[$_][0] > $x[($_ - 1) >> 1][0])) { |
21 |
|
|
$err = "cmp \$x[$_] ($x[$_]) !> \$x[$_ >> 1] ($x[($_ - 1) >> 1])"; |
22 |
|
|
make_heap_idx @x; |
23 |
|
|
} |
24 |
|
|
} |
25 |
|
|
for (0 .. $#x) { |
26 |
|
|
if ($x[$_][1] != $_) { |
27 |
|
|
$err = "idx $_ != $x[$_][1]"; |
28 |
|
|
$x[$_][1] = $_; |
29 |
|
|
} |
30 |
|
|
} |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
make_heap_idx @x; |
34 |
|
|
chk; |
35 |
|
|
|
36 |
|
|
for (1..3000) { |
37 |
|
|
undef $err; |
38 |
|
|
my $t = int rand @test; |
39 |
|
|
$test[$t]->(); |
40 |
|
|
chk; |
41 |
|
|
print defined $err ? "not " : "", "ok $_ # $t,$err\n"; |
42 |
|
|
} |