-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbinaryheap.tests.inc
More file actions
475 lines (416 loc) · 14.2 KB
/
binaryheap.tests.inc
File metadata and controls
475 lines (416 loc) · 14.2 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// THIS FILE WAS GENERATED BY CLAUDE with a variety of modifications to make it work
type
{ Utils for integer min-heap: root = smallest }
TIntMinUtils = record
class function IsOrdered(const A, B: Integer): Boolean; static; inline;
end;
{ Utils for integer max-heap: root = largest }
TIntMaxUtils = record
class function IsOrdered(const A, B: Integer): Boolean; static; inline;
end;
TIntArray = array of Integer;
TIntMinHeap = specialize THeap<Integer, TIntMinUtils>;
TIntMaxHeap = specialize THeap<Integer, TIntMaxUtils>;
class function TIntMinUtils.IsOrdered(const A, B: Integer): Boolean;
begin
Result := A <= B;
end;
class function TIntMaxUtils.IsOrdered(const A, B: Integer): Boolean;
begin
Result := A >= B;
end;
{ ──────────────────────────────────────────────────────────── }
{ Helpers }
{ ──────────────────────────────────────────────────────────── }
var
PassCount, FailCount: Integer;
procedure Check(const Condition: Boolean; const TestName: string);
begin
if Condition then
begin
Inc(PassCount);
end
else
begin
Inc(FailCount);
WriteLn(' FAIL ', TestName);
end;
end;
{ Drain the heap and return extracted values in extraction order. }
function DrainMin(var H: TIntMinHeap): TIntArray;
var
I: Integer;
begin
SetLength(Result, H.Count); // {BOGUS Warning: Function result variable of a managed type does not seem to be initialized}
for I := 0 to High(Result) do // $R-
Result[I] := H.Extract();
end;
function DrainMax(var H: TIntMaxHeap): TIntArray;
var
I: Integer;
begin
SetLength(Result, H.Count); // {BOGUS Warning: Function result variable of a managed type does not seem to be initialized}
for I := 0 to High(Result) do // $R-
Result[I] := H.Extract();
end;
{ Returns True when Arr is non-decreasing. }
function IsSorted(const Arr: TIntArray): Boolean;
var
I: Integer;
begin
Result := True;
for I := 1 to High(Arr) do // $R-
if Arr[I] < Arr[I - 1] then
begin
Result := False;
Exit;
end;
end;
{ Returns True when Arr is non-increasing. }
function IsReverseSorted(const Arr: TIntArray): Boolean;
var
I: Integer;
begin
Result := True;
for I := 1 to High(Arr) do // $R-
if Arr[I] > Arr[I - 1] then
begin
Result := False;
Exit;
end;
end;
function ArrEqual(const A, B: TIntArray): Boolean;
var
I: Integer;
begin
if Length(A) <> Length(B) then Exit(False);
for I := 0 to High(A) do // $R-
if A[I] <> B[I] then Exit(False);
Result := True;
end;
{ ──────────────────────────────────────────────────────────── }
{ Test groups }
{ ──────────────────────────────────────────────────────────── }
{ 1. Basic min-heap property after sequential inserts }
procedure TestMinHeapOrdering;
var
H: TIntMinHeap;
Got: TIntArray;
begin
{ Insert in ascending order }
H.Insert(1); H.Insert(2); H.Insert(3); H.Insert(4); H.Insert(5);
Got := DrainMin(H);
Check(IsSorted(Got), 'ascending insert → extracts ascending');
{ Insert in descending order }
H.Insert(5); H.Insert(4); H.Insert(3); H.Insert(2); H.Insert(1);
Got := DrainMin(H);
Check(IsSorted(Got), 'descending insert → extracts ascending');
Check(H.Count = 0, 'extracts did not empty heap');
{ Insert in random order }
H.Insert(3); H.Insert(1); H.Insert(4); H.Insert(1); H.Insert(5);
H.Insert(9); H.Insert(2); H.Insert(6);
Got := DrainMin(H);
Check(IsSorted(Got), 'random insert → extracts ascending');
{ Minimum is always at the root before extraction }
H.Insert(7); H.Insert(2); H.Insert(9); H.Insert(1); H.Insert(5);
Check(H.Extract() = 1, 'first Extract returns global minimum');
Check(H.Extract() = 2, 'second Extract returns next minimum');
end;
{ 2. Basic max-heap property }
procedure TestMaxHeapOrdering;
var
H: TIntMaxHeap;
Got: TIntArray;
begin
H.Insert(3); H.Insert(1); H.Insert(4); H.Insert(1); H.Insert(5);
H.Insert(9); H.Insert(2); H.Insert(6);
Got := DrainMax(H);
Check(IsReverseSorted(Got), 'random insert → extracts descending');
H.Insert(10); H.Insert(20); H.Insert(5);
Check(H.Extract() = 20, 'max-heap Extract returns maximum');
end;
{ 3. AdoptInit (heapify) — drawn from CPython heapq._heapify_max tests }
procedure TestAdoptInit;
var
H: TIntMinHeap;
Src: TIntArray;
Got: TIntArray;
Expected: TIntArray;
begin
{ Sorted input }
Src := TIntArray.Create(1, 2, 3, 4, 5, 6, 7);
H.AdoptInit(Src);
Got := DrainMin(H);
Expected := TIntArray.Create(1, 2, 3, 4, 5, 6, 7);
Check(ArrEqual(Got, Expected), 'heapify sorted array → correct order');
{ Reverse-sorted input }
Src := TIntArray.Create(7, 6, 5, 4, 3, 2, 1);
H.AdoptInit(Src);
Got := DrainMin(H);
Check(ArrEqual(Got, Expected), 'heapify reverse-sorted array → correct order');
{ Single element }
Src := TIntArray.Create(42);
H.AdoptInit(Src);
Check(H.Count = 1, 'heapify single element → Count = 1');
Check(H.Extract() = 42, 'heapify single element → Extract returns it');
{ Empty array }
Src := nil;
H.AdoptInit(Src);
Check(H.Count = 0, 'heapify empty array → Count = 0');
{ Power-of-two minus 1 (complete tree boundary) }
Src := TIntArray.Create(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1);
H.AdoptInit(Src);
Got := DrainMin(H);
Check(IsSorted(Got), 'heapify 15-element reverse array → sorted extraction');
end;
{ 4. Count property }
procedure TestCount;
var
H: TIntMinHeap;
begin
Check(H.Count = 0, 'empty heap Count = 0'); // {BOGUS Warning: Local variable "H" of a managed type does not seem to be initialized}
H.Insert(1);
Check(H.Count = 1, 'Count = 1 after one Insert');
H.Insert(2); H.Insert(3);
Check(H.Count = 3, 'Count = 3 after three inserts');
H.Extract();
Check(H.Count = 2, 'Count decrements after Extract');
H.Extract(); H.Extract();
Check(H.Count = 0, 'Count = 0 after all extracted');
end;
{ 5. Duplicate values — from Python heapq test suite }
procedure TestDuplicates;
var
H: TIntMinHeap;
Got: TIntArray;
var
I: Integer;
begin
H.Insert(3); H.Insert(3); H.Insert(3);
Got := DrainMin(H);
Check((Got[0] = 3) and (Got[1] = 3) and (Got[2] = 3),
'all-duplicate heap extracts all copies');
H.Insert(1); H.Insert(1); H.Insert(2); H.Insert(1);
Check(H.Count = 4, 'duplicates all counted');
Got := DrainMin(H);
Check(IsSorted(Got), 'duplicates sorted correctly on extraction');
{ Large number of duplicates }
for I := 1 to 100 do
H.Insert(7);
Check(H.Count = 100, '100 duplicate inserts → Count = 100');
for I := 1 to 100 do
Check(H.Extract() = 7, 'every extracted duplicate = 7');
end;
{ 6. InsertThenExtract and ExtractThenInsert }
{ Semantics verified against Python heapq.heappushpop / heapreplace }
procedure TestMergeOps;
var
H: TIntMinHeap;
R: Integer;
begin
{ InsertThenExtract: equivalent to Insert followed by Extract,
but must never return a value larger than NewValue
when NewValue is already ≤ all heap elements. }
H.Insert(3); H.Insert(5); H.Insert(7);
{ Push 1 (new min), pop should return 1 }
R := H.InsertThenExtract(1);
Check(R = 1, 'InsertThenExtract(1) on [3,5,7] = 1 (new value is smallest)');
Check(H.Count = 3, 'InsertThenExtract does not change Count');
{ Push 4, pop should return 3 (existing min) }
H.Clear();
H.Insert(3); H.Insert(5); H.Insert(7);
R := H.InsertThenExtract(4);
Check(R = 3, 'InsertThenExtract(4) on [3,5,7] = 3');
{ Push 10 (new max), pop should return 3 }
H.Clear();
H.Insert(3); H.Insert(5); H.Insert(7);
R := H.InsertThenExtract(10);
Check(R = 3, 'InsertThenExtract(10) on [3,5,7] = 3');
{ ExtractThenInsert: equivalent to Extract followed by Insert;
the previous minimum is returned and NewValue takes its place. }
H.Clear();
H.Insert(3); H.Insert(5); H.Insert(7);
Check(H.Count = 3, 'Count is correct after Insert');
R := H.ExtractThenInsert(2);
Check(R = 3, 'ExtractThenInsert(2) returns old min = 3');
Check(H.Count = 3, 'ExtractThenInsert does not change Count');
{ Heap should now contain [2, 5, 7] }
R := H.Extract();
Check(R = 2, 'after ExtractThenInsert(2), new min = 2');
H.Clear();
H.Insert(3); H.Insert(5); H.Insert(7);
R := H.ExtractThenInsert(9);
Check(R = 3, 'ExtractThenInsert(9) returns old min = 3');
{ Heap should now contain [5, 7, 9] }
Check(H.Extract() = 5, 'after ExtractThenInsert(9), next min = 5');
{ InsertThenExtract on single-element heap }
H.Clear();
H.Insert(10);
R := H.InsertThenExtract(5);
Check(R = 5, 'InsertThenExtract(5) on [10] = 5');
H.Clear();
H.Insert(10);
R := H.InsertThenExtract(20);
Check(R = 10, 'InsertThenExtract(20) on [10] = 10');
end;
{ 7. Heap sort equivalence — from Go container/heap tests }
{ Repeated Extract from a min-heap must produce a fully sorted sequence. }
procedure TestHeapSort;
var
H: TIntMinHeap;
Src: TIntArray;
Got: TIntArray;
I, N: Integer;
begin
{ Small fixed input }
Src := TIntArray.Create(5, 1, 3, 2, 4);
for I := 0 to High(Src) do H.Insert(Src[I]); // $R-
Got := DrainMin(H);
Check(IsSorted(Got), 'heap-sort: small array');
{ Already sorted }
N := 20;
SetLength(Src, N);
for I := 0 to N - 1 do Src[I] := I + 1; // $R-
for I := 0 to High(Src) do H.Insert(Src[I]); // $R-
Got := DrainMin(H);
Check(IsSorted(Got), 'heap-sort: already sorted input');
{ Reverse sorted }
for I := 0 to N - 1 do Src[I] := N - I; // $R-
for I := 0 to High(Src) do H.Insert(Src[I]); // $R-
Got := DrainMin(H);
Check(IsSorted(Got), 'heap-sort: reverse sorted input');
{ Interleaved inserts and extracts still maintain sort order }
H.Insert(5); H.Insert(2); H.Insert(8);
Check(H.Extract() = 2, 'interleaved: Extract after 3 inserts = min');
H.Insert(1); H.Insert(6);
Check(H.Extract() = 1, 'interleaved: Extract after more inserts = new min');
H.Insert(3);
Got := DrainMin(H);
Check(IsSorted(Got), 'interleaved: remaining elements extracted in order');
end;
{ 8. Large / stress test — from Java PriorityQueue tests }
procedure TestStress;
const
N = 10000;
var
H: TIntMinHeap;
Prev, Curr: Integer;
I: Integer;
var
LCG: Int64 = 12345;
begin
{ Insert 0..N-1 in sequential order }
for I := 0 to N - 1 do
H.Insert(I);
Check(H.Count = N, 'stress sequential: Count = N');
Prev := H.Extract();
for I := 1 to N - 1 do
begin
Curr := H.Extract();
if Curr < Prev then
begin
Check(False, 'stress sequential: order violation at index ' + IntToStr(I));
Exit;
end;
Prev := Curr;
end;
Check(True, 'stress sequential: all ' + IntToStr(N) + ' elements in order');
{ Insert N-1..0 in reverse order }
for I := N - 1 downto 0 do
H.Insert(I);
Check(H.Count = N, 'stress reverse: Count = N');
Prev := H.Extract();
for I := 1 to N - 1 do
begin
Curr := H.Extract();
if Curr < Prev then
begin
Check(False, 'stress reverse: order violation at index ' + IntToStr(I));
Exit;
end;
Prev := Curr;
end;
Check(True, 'stress reverse: all ' + IntToStr(N) + ' elements in order');
{ Pseudo-random via LCG (reproducible, no RTL dependency) }
for I := 0 to N - 1 do
begin
LCG := (LCG * 6364136223846793005 + 1442695040888963407) and $7FFFFFFF;
H.Insert(Integer(LCG mod 100000));
end;
Check(H.Count = N, 'stress random: Count = N');
Prev := H.Extract();
for I := 1 to N - 1 do
begin
Curr := H.Extract();
if Curr < Prev then
begin
Check(False, 'stress random: order violation at index ' + IntToStr(I));
Exit;
end;
Prev := Curr;
end;
Check(True, 'stress random: ' + IntToStr(N) + ' random elements in order');
end;
{ 9. AdoptInit with ExpectedInsertionCount hint }
{ The hint must not affect correctness, only (optionally) performance. }
procedure TestAdoptInitHint;
var
H: TIntMinHeap;
Src: TIntArray;
Got: TIntArray;
I: Integer;
begin
Src := TIntArray.Create(5, 3, 8, 1, 9, 2);
H.AdoptInit(Src, 100); { hint: expect 100 more inserts }
for I := 10 to 15 do
H.Insert(I);
Got := DrainMin(H);
Check(IsSorted(Got), 'hint does not affect ordering correctness');
Check(Length(Got) = 12, 'hint does not affect final element count');
{ Zero hint }
Src := TIntArray.Create(4, 2, 6);
H.AdoptInit(Src, 0);
Got := DrainMin(H);
Check(IsSorted(Got), 'zero hint: correct ordering');
end;
{ 10. Single-element edge cases }
procedure TestSingleElement;
var
H: TIntMinHeap;
begin
H.Insert(42);
Check(H.Count = 1, 'single element: Count = 1');
Check(H.Extract() = 42, 'single element: Extract returns it');
Check(H.Count = 0, 'single element: Count = 0 after Extract');
{ Negative values }
H.Insert(-1); H.Insert(-5); H.Insert(-3);
Check(H.Extract() = -5, 'negatives: min-heap returns most negative first');
{ Low(Integer) / High(Integer) }
H.Clear();
H.Insert(High(Integer));
H.Insert(Low(Integer));
H.Insert(0);
Check(H.Extract() = Low(Integer), 'boundary: Low(Integer) extracted first');
Check(H.Extract() = 0, 'boundary: 0 extracted second');
Check(H.Extract() = High(Integer), 'boundary: High(Integer) extracted last');
end;
{ ──────────────────────────────────────────────────────────── }
{ Entry point }
{ ──────────────────────────────────────────────────────────── }
procedure RunTests;
begin
PassCount := 0;
FailCount := 0;
TestMinHeapOrdering;
TestMaxHeapOrdering;
TestAdoptInit;
TestCount;
TestDuplicates;
TestMergeOps;
TestHeapSort;
TestStress;
TestAdoptInitHint;
TestSingleElement;
if (FailCount > 0) then
WriteLn('THeap test results: ', PassCount, ' passed, ', FailCount, ' failed');
end;