1# Before `make install' is performed this script should be runnable with
2# `make test'. After `make install' it should work as `perl test.pl'
3
4######################### We start with some black magic to print on failure.
5
6# Change 1..1 below to 1..last_test_to_print .
7# (It may become useful if the test is moved to ./t subdirectory.)
8
9BEGIN { $| = 1; print "1..7\n"; }
10use Heap;
11use Heap::Elem::NumRev;
12
13######################### End of black magic.
14
15# Insert your test code below (better if it prints "ok 13"
16# (correspondingly "not ok 13") depending on the success of chunk 13
17# of the test code):
18
19my @test_seq =
20	(
21	    [ test_empty => ],
22	    [ add    => 1, 100 ],
23	    [ test   => 100 ],
24	    [ remove => 50, 100, 51 ],
25	    [ test   => 50 ],
26	    [ remove => 50, 50, 1 ],
27	    [ test_empty => ],
28	    [ repeat => 0, 2 ],
29	    [ mem_test => ],
30	    [ repeat => 1, 50 ],
31	    [ last => ],
32	);
33my $test_index = 0;
34my @repeat_count = ( 0, 0, 0, 0 );
35
36my $heap = new Heap::Fibonacci;
37my $test_num = 0;
38my $still_testing = 1;
39my $not;
40
41while (1) {
42    my $step = $test_seq[$test_index++];
43    my $op = $step->[0];
44    my $scratch;
45    $not = 'not ';
46    if( $op eq 'test_empty' ) {
47	defined($heap->top) or $not = '';
48    } elsif( $op eq 'test' ) {
49	defined($scratch = $heap->top) and $scratch->val == $step->[1] and $not = '';
50    } elsif( $op eq 'add' ) {
51	my( $base, $limit, $incr ) = (@$step)[1..3];
52	defined $incr or $incr = 1;
53	while(1) {
54	    my $elem = new Heap::Elem::NumRev($base);
55	    $heap->add( $elem );
56	    last if $base == $limit;
57	    $base += $incr;
58	}
59	$not = 'skip';
60    } elsif( $op eq 'remove' ) {
61	my( $count, $base, $limit, $incr ) = (@$step)[1..4];
62	defined $incr or $incr = -1;
63	$not = '';
64	while($count--) {
65	    my $elem = $heap->extract_top;
66	    defined($elem) && $elem->val == $base
67		or $not = 'not ';
68	    $base += $incr;
69	}
70	$not = 'not '
71	    if $base != $limit + $incr;
72    } elsif( $op eq 'repeat' ) {
73	my( $index, $limit ) = (@$step)[1..2];
74	if( $still_testing ) {
75	    $still_testing = 0;
76	}
77	if( ++$repeat_count[$index] == $limit ) {
78	    $repeat_count[$index] = 0;
79	} else {
80	    $test_index = 0;
81	}
82	$not = '';
83    } elsif( $op eq 'mem_test' ) {
84	$not = '';
85	print `ps -lp$$`;
86    } elsif( $op eq 'last' ) {
87	$not = '';
88	last;
89    }
90    if( $not ne 'skip' ) {
91	if( $still_testing ) {
92	    ++$test_num;
93	    print $not, "ok $test_num\n";
94	} else {
95	    last if $not;
96	}
97    }
98}
99
100++$test_num;
101print $not, "ok $test_num\n";
102