forked from gap-packages/datastructures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench-heap.g
117 lines (95 loc) · 3.05 KB
/
bench-heap.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
LoadPackage("datastructures");
# Tests for the heap implementation.
# TODO: Rewrite using the UnitTests package
PCQL_Heap_Create:=function(vals)
local h, x;
h := PairingHeap();
for x in vals do
# We use -x as key because we want find/remove MAX but
# datastructures provides find/remove MIN.
PairingHeapPush(h,-x,x);
od;
return h;
end;
PCQL_Heap_IsValid := ReturnTrue;
PCQL_Heap_FindMax := heap -> PairingHeapPeek(heap)[2];
PCQL_Heap_RemoveMax := heap -> PairingHeapPop(heap)[2];
PCQL_Heap_IsEmpty := heap -> heap![1] = 0;
PCQL_Heap_Length := heap -> heap![1];
fails := 0;
successes := 0;
TestHeap := function(vals)
local heap, x, i;
Print("Testing heap code with ", Length(vals), " elements\n");
heap := PCQL_Heap_Create(vals);
if not PCQL_Heap_IsValid(heap) then
fails := fails + 1;
Print(" FAILURE: newly created heap is invalid\n");
return;
fi;
Sort(vals, function(a,b) return a>b; end);
for i in [1..Length(vals)] do
x := PCQL_Heap_FindMax(heap);
if x <> vals[i] then
fails := fails + 1;
Print(" FAILURE in FindMax at sorted position ", i,
" (expected ", vals[i], " but got ", x, ")\n");
return;
fi;
x := PCQL_Heap_RemoveMax(heap);
if x <> vals[i] then
fails := fails + 1;
Print(" FAILURE in RemoveMax at sorted position ", i,
" (expected ", vals[i], " but got ", x, ")\n");
return;
fi;
if not PCQL_Heap_IsValid(heap) then
fails := fails + 1;
Print(" FAILURE: heap invalid\n");
return;
fi;
od;
if not PCQL_Heap_IsEmpty(heap) then
fails := fails + 1;
Print(" FAILURE: heap should be empty but isn't\n");
return;
fi;
successes := successes + 1;
end;
#TestHeap( [ 17, 3, 5 ] );
TestHeap( [] );
for n in [1..20] do
vals := List([1..n], i -> Random([1..50]));
TestHeap(vals);
od;
Print("Of ", fails + successes, " tests, ", fails, " failed and ", successes, " succeeded.\n");
GASMAN("collect");
GASMAN("collect");
RunHeapTests := function(len)
local vals, t, heap, i, x;
vals := List([1..len], i -> Random([1..2^27]));;
t := Runtime();;
heap := PCQL_Heap_Create(vals);;
Print("Creating heap: ", (Runtime() - t)/1000.0, " seconds\n");
if PCQL_Heap_Length(heap) <> Length(vals) then
Error("wrong length");
fi;
t := Runtime();;
Sort(vals, function(a,b) return a>b; end);
Print("Sorting plist: ", (Runtime() - t)/1000.0, " seconds\n");
t := Runtime();;
for i in [1..Length(vals)] do
x := PCQL_Heap_RemoveMax(heap);
if x <> vals[i] then
Error("oops");
fi;
od;
Print("Reading heap: ", (Runtime() - t)/1000.0, " seconds\n");
end;
#if false then
#PCQL_Heap_Insert:=_PCQL_Heap_Insert_GAP;
#PCQL_Heap_ReplaceMax:=_PCQL_Heap_ReplaceMax_GAP;
#PCQL_Heap_Insert:=_PCQL_Heap_Insert_C;
#PCQL_Heap_ReplaceMax:=_PCQL_Heap_ReplaceMax_C;
RunHeapTests(2^20);
#fi;