Repository navigation
Expand file tree
/
Copy pathplasticarrays.tests.inc
More file actions
1344 lines (1177 loc) · 45.1 KB
/
Copy pathplasticarrays.tests.inc
File metadata and controls
1344 lines (1177 loc) · 45.1 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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// THIS FILE WAS GENERATED BY CLAUDE with minor hand-written modifications
// =========================================================================
// Utils implementations used for testing
// =========================================================================
type
TIntUtils = record
class function Equals(const A, B: Integer): Boolean; static; inline;
class function LessThan(const A, B: Integer): Boolean; static; inline;
class function GreaterThan(const A, B: Integer): Boolean; static; inline;
class function Compare(const A, B: Integer): Int64; static; inline;
end;
TStrUtils = record
class function Equals(const A, B: AnsiString): Boolean; static; inline;
class function LessThan(const A, B: AnsiString): Boolean; static; inline;
class function GreaterThan(const A, B: AnsiString): Boolean; static; inline;
class function Compare(const A, B: AnsiString): Int64; static; inline;
end;
TIntArray = specialize PlasticArray<Integer, TIntUtils>;
TStrArray = specialize PlasticArray<AnsiString, TStrUtils>;
class function TIntUtils.Equals(const A, B: Integer): Boolean;
begin Result := A = B; end;
class function TIntUtils.LessThan(const A, B: Integer): Boolean;
begin Result := A < B; end;
class function TIntUtils.GreaterThan(const A, B: Integer): Boolean;
begin Result := A > B; end;
class function TIntUtils.Compare(const A, B: Integer): Int64;
begin
if A < B then Result := -1
else if A > B then Result := 1
else Result := 0;
end;
class function TStrUtils.Equals(const A, B: AnsiString): Boolean;
begin Result := A = B; end;
class function TStrUtils.LessThan(const A, B: AnsiString): Boolean;
begin Result := A < B; end;
class function TStrUtils.GreaterThan(const A, B: AnsiString): Boolean;
begin Result := A > B; end;
class function TStrUtils.Compare(const A, B: AnsiString): Int64;
begin
if A < B then Result := -1
else if A > B then Result := 1
else Result := 0;
end;
// =========================================================================
// Lifecycle tracking type for managed-operator tests
// =========================================================================
type
TLifecycleEvent = (leInitialize, leFinalize, leCopy, leAddRef);
TLifecycleLog = array of TLifecycleEvent;
var
GLifecycleLog: TLifecycleLog;
GLifecycleLogCount: Integer;
procedure LogEvent(Event: TLifecycleEvent);
begin
if GLifecycleLogCount >= System.Length(GLifecycleLog) then
SetLength(GLifecycleLog, GLifecycleLogCount * 2 + 8);
GLifecycleLog[GLifecycleLogCount] := Event;
Inc(GLifecycleLogCount);
end;
procedure ResetLog();
begin
GLifecycleLogCount := 0;
end;
type
TTracked = record
Value: Integer;
class operator Initialize(var T: TTracked);
class operator Finalize(var T: TTracked);
class operator Copy(constref Src: TTracked; var Dst: TTracked);
class operator AddRef(var T: TTracked);
end;
TTrackedUtils = record
class function Equals(const A, B: TTracked): Boolean; static; inline;
class function LessThan(const A, B: TTracked): Boolean; static; inline;
class function GreaterThan(const A, B: TTracked): Boolean; static; inline;
class function Compare(const A, B: TTracked): Int64; static; inline;
end;
TTrackedArray = specialize PlasticArray<TTracked, TTrackedUtils>;
class operator TTracked.Initialize(var T: TTracked);
begin
T.Value := 0;
LogEvent(leInitialize);
end;
class operator TTracked.Finalize(var T: TTracked);
begin
LogEvent(leFinalize);
end;
class operator TTracked.Copy(constref Src: TTracked; var Dst: TTracked);
begin
Dst.Value := Src.Value;
LogEvent(leCopy);
end;
class operator TTracked.AddRef(var T: TTracked);
begin
LogEvent(leAddRef);
end;
class function TTrackedUtils.Equals(const A, B: TTracked): Boolean;
begin Result := A.Value = B.Value; end;
class function TTrackedUtils.LessThan(const A, B: TTracked): Boolean;
begin Result := A.Value < B.Value; end;
class function TTrackedUtils.GreaterThan(const A, B: TTracked): Boolean;
begin Result := A.Value > B.Value; end;
class function TTrackedUtils.Compare(const A, B: TTracked): Int64;
begin
if A.Value < B.Value then Result := -1
else if A.Value > B.Value then Result := 1
else Result := 0;
end;
// =========================================================================
// Comparison helpers for Sort calls
// =========================================================================
function IntAscending(const A, B: Integer): Integer;
begin
if A < B then Result := -1
else if A > B then Result := 1
else Result := 0;
end;
function IntDescending(const A, B: Integer): Integer;
begin
if A > B then Result := -1
else if A < B then Result := 1
else Result := 0;
end;
// =========================================================================
// RunTests
// =========================================================================
procedure RunTests();
procedure Fail(const Msg: AnsiString);
begin
WriteLn('FAILURE: ', Msg);
Halt(1);
end;
procedure AssertTrue(const Condition: Boolean; const Msg: AnsiString);
begin
if not Condition then
Fail(Msg);
end;
procedure AssertFalse(const Condition: Boolean; const Msg: AnsiString);
begin
if Condition then
Fail(Msg);
end;
procedure AssertEquals(const Got, Expected: Integer; const Msg: AnsiString); overload;
begin
if Got <> Expected then
Fail(Msg + ' (got ' + IntToStr(Got) + ', expected ' + IntToStr(Expected) + ')');
end;
procedure AssertEquals(const Got, Expected: AnsiString; const Msg: AnsiString); overload;
begin
if Got <> Expected then
Fail(Msg + ' (got "' + Got + '", expected "' + Expected + '")');
end;
function EventName(E: TLifecycleEvent): AnsiString;
begin
case E of
leInitialize: Result := 'Initialize';
leFinalize: Result := 'Finalize';
leCopy: Result := 'Copy';
leAddRef: Result := 'AddRef';
end;
end;
procedure AssertNextEvent(var Cursor: Integer; Expected: TLifecycleEvent; const Msg: AnsiString);
begin
if Cursor >= GLifecycleLogCount then
Fail(Msg + ': expected ' + EventName(Expected) + ' but log ended at position ' + IntToStr(Cursor));
if GLifecycleLog[Cursor] <> Expected then
Fail(Msg + ': at position ' + IntToStr(Cursor) +
' expected ' + EventName(Expected) +
' but got ' + EventName(GLifecycleLog[Cursor]));
Inc(Cursor);
end;
procedure AssertNoMoreEvents(const Cursor: Integer; const Msg: AnsiString);
var
I: Integer;
Remaining: AnsiString;
begin
if Cursor <> GLifecycleLogCount then
begin
Remaining := '';
for I := Cursor to GLifecycleLogCount - 1 do // $R-
begin
if Remaining <> '' then
Remaining := Remaining + ', ';
Remaining := Remaining + IntToStr(I) + ':' + EventName(GLifecycleLog[I]);
end;
Fail(Msg + ': expected no more events at position ' + IntToStr(Cursor) +
' but ' + IntToStr(GLifecycleLogCount - Cursor) + ' remain: [' + Remaining + ']');
end;
end;
function CountEvents(const Event: TLifecycleEvent): Integer;
var
I: Integer;
begin
Result := 0;
for I := 0 to GLifecycleLogCount - 1 do // $R-
if GLifecycleLog[I] = Event then
Inc(Result);
end;
function MakeIntArray(const Values: array of Integer): TIntArray;
var
I: Integer;
begin
for I in Values do
Result.Push(I);
end;
// -------------------------------------------------------------------------
procedure TestEmptyState();
var
A: TIntArray;
begin
AssertTrue(A.IsEmpty, 'TestEmptyState: new array should be empty');
AssertFalse(A.IsNotEmpty, 'TestEmptyState: new array IsNotEmpty should be false');
AssertEquals(Integer(A.Length), 0, 'TestEmptyState: new array Length should be 0');
end;
// -------------------------------------------------------------------------
procedure TestPushAndLength();
var
A: TIntArray;
I: Integer;
begin
for I := 1 to 10 do
begin
A.Push(I);
AssertEquals(Integer(A.Length), I, 'TestPushAndLength: length after push ' + IntToStr(I));
end;
AssertFalse(A.IsEmpty, 'TestPushAndLength: should not be empty after pushes');
AssertTrue(A.IsNotEmpty, 'TestPushAndLength: IsNotEmpty should be true after pushes');
end;
// -------------------------------------------------------------------------
procedure TestPopBasic();
var
A: TIntArray;
V: Integer;
begin
A.Push(42);
A.Push(99);
V := A.Pop();
AssertEquals(V, 99, 'TestPopBasic: popped wrong value');
AssertEquals(Integer(A.Length), 1, 'TestPopBasic: length after pop');
V := A.Pop();
AssertEquals(V, 42, 'TestPopBasic: popped wrong value (second pop)');
AssertTrue(A.IsEmpty, 'TestPopBasic: should be empty after popping all');
end;
// -------------------------------------------------------------------------
procedure TestEmptyProcedure();
var
A: TIntArray;
begin
A.Push(1);
A.Push(2);
A.Push(3);
A.Empty();
AssertTrue(A.IsEmpty, 'TestEmptyProcedure: should be empty after Empty()');
AssertEquals(Integer(A.Length), 0, 'TestEmptyProcedure: Length should be 0 after Empty()');
end;
// -------------------------------------------------------------------------
procedure TestItemsReadWrite();
var
A: TIntArray;
I: Integer;
begin
for I := 0 to 9 do
A.Push(I * 10); // $R-
for I := 0 to 9 do
AssertEquals(A[Cardinal(I)], I * 10, 'TestItemsReadWrite: read item[' + IntToStr(I) + ']'); // $R-
for I := 0 to 9 do
A[Cardinal(I)] := I * 100; // $R-
for I := 0 to 9 do
AssertEquals(A[Cardinal(I)], I * 100, 'TestItemsReadWrite: read after write item[' + IntToStr(I) + ']'); // $R-
end;
// -------------------------------------------------------------------------
procedure TestLastProperty();
var
A: TIntArray;
begin
A.Push(1);
A.Push(2);
A.Push(3);
AssertEquals(A.Last, 3, 'TestLastProperty: Last should be 3');
A.Last := 99;
AssertEquals(A.Last, 99, 'TestLastProperty: Last after write should be 99');
AssertEquals(Integer(A.Length), 3, 'TestLastProperty: Length should not change after Last write');
end;
// -------------------------------------------------------------------------
procedure TestSetLength();
var
A: TIntArray;
I: Integer;
begin
A.Length := 5;
AssertEquals(Integer(A.Length), 5, 'TestSetLength: length after set');
A.Length := 10;
AssertEquals(Integer(A.Length), 10, 'TestSetLength: length after grow');
A.Length := 3;
AssertEquals(Integer(A.Length), 3, 'TestSetLength: length after shrink');
for I := 1 to 5 do
A.Push(I);
AssertEquals(Integer(A.Length), 8, 'TestSetLength: length after push following shrink');
end;
// -------------------------------------------------------------------------
procedure TestPrepareAndGrowFor();
var
A: TIntArray;
I, V, Count: Integer;
Before, After: TFPCHeapStatus;
begin
// Verify Prepare allows subsequent pushes without reallocation
A.Prepare(20);
Before := GetFPCHeapStatus();
for I := 1 to 20 do
A.Push(I);
After := GetFPCHeapStatus();
AssertTrue(After.CurrHeapUsed = Before.CurrHeapUsed,
'TestPrepareAndGrowFor: Prepare(20) should prevent reallocation during 20 pushes');
AssertEquals(Integer(A.Length), 20, 'TestPrepareAndGrowFor: length after pushing 20 items following Prepare(20)');
Count := 0;
for V in A do
begin
Inc(Count);
AssertEquals(V, Count, 'TestPrepareAndGrowFor: value at position ' + IntToStr(Count));
end;
AssertEquals(Count, 20, 'TestPrepareAndGrowFor: enumerated 20 items after Prepare');
// Verify GrowFor allows subsequent pushes without reallocation
A.Empty();
A.GrowFor(15);
Before := GetFPCHeapStatus();
for I := 1 to 15 do
A.Push(I);
After := GetFPCHeapStatus();
AssertTrue(After.CurrHeapUsed = Before.CurrHeapUsed,
'TestPrepareAndGrowFor: GrowFor(15) should prevent reallocation during 15 pushes');
AssertEquals(Integer(A.Length), 15, 'TestPrepareAndGrowFor: length after pushing 15 items following GrowFor(15)');
Count := 0;
for V in A do
begin
Inc(Count);
AssertEquals(V, Count, 'TestPrepareAndGrowFor: value at position ' + IntToStr(Count));
end;
AssertEquals(Count, 15, 'TestPrepareAndGrowFor: enumerated 15 items after GrowFor');
// Verify GrowFor is additive: existing items plus new ones fit without reallocation
A.Empty();
for I := 1 to 5 do
A.Push(I);
A.GrowFor(10);
Before := GetFPCHeapStatus();
for I := 6 to 15 do
A.Push(I);
After := GetFPCHeapStatus();
AssertTrue(After.CurrHeapUsed = Before.CurrHeapUsed,
'TestPrepareAndGrowFor: GrowFor(10) with 5 existing items should prevent reallocation during 10 more pushes');
AssertEquals(Integer(A.Length), 15, 'TestPrepareAndGrowFor: length after GrowFor with existing items');
// Verify Prepare is idempotent: calling it again with a smaller value does not shrink
A.Empty();
A.Prepare(20);
A.Prepare(5);
Before := GetFPCHeapStatus();
for I := 1 to 20 do
A.Push(I);
After := GetFPCHeapStatus();
AssertTrue(After.CurrHeapUsed = Before.CurrHeapUsed,
'TestPrepareAndGrowFor: Prepare(5) after Prepare(20) should not shrink capacity');
AssertEquals(Integer(A.Length), 20, 'TestPrepareAndGrowFor: length after idempotent Prepare');
// Verify Squeeze followed by Prepare re-establishes the no-reallocation guarantee
A.Empty();
A.Squeeze();
A.Prepare(10);
Before := GetFPCHeapStatus();
for I := 1 to 10 do
A.Push(I);
After := GetFPCHeapStatus();
AssertTrue(After.CurrHeapUsed = Before.CurrHeapUsed,
'TestPrepareAndGrowFor: Prepare(10) after Squeeze should prevent reallocation during 10 pushes');
AssertEquals(Integer(A.Length), 10, 'TestPrepareAndGrowFor: length after Squeeze+Prepare');
end;
// -------------------------------------------------------------------------
procedure TestSqueeze();
var
A: TIntArray;
I: Integer;
begin
for I := 1 to 100 do
A.Push(I);
A.Empty();
A.Squeeze();
AssertTrue(A.IsEmpty, 'TestSqueeze: should still be empty after Squeeze');
A.Push(7);
AssertEquals(Integer(A.Length), 1, 'TestSqueeze: can push after squeeze');
AssertEquals(A[0], 7, 'TestSqueeze: value after squeeze+push');
end;
// -------------------------------------------------------------------------
procedure TestInsertAt();
var
A: TIntArray;
begin
A := MakeIntArray([1, 2, 3, 4, 5]);
A.InsertAt(2, 99);
AssertEquals(Integer(A.Length), 6, 'TestInsertAt: length after insert');
AssertEquals(A[0], 1, 'TestInsertAt: [0]');
AssertEquals(A[1], 2, 'TestInsertAt: [1]');
AssertEquals(A[2], 99, 'TestInsertAt: [2] should be inserted value');
AssertEquals(A[3], 3, 'TestInsertAt: [3] shifted');
AssertEquals(A[4], 4, 'TestInsertAt: [4] shifted');
AssertEquals(A[5], 5, 'TestInsertAt: [5] shifted');
A.InsertAt(0, 0);
AssertEquals(A[0], 0, 'TestInsertAt: insert at [0]');
AssertEquals(Integer(A.Length), 7, 'TestInsertAt: length after insert at 0');
A.InsertAt(Cardinal(A.Length), 100);
AssertEquals(A[Cardinal(A.Length - 1)], 100, 'TestInsertAt: insert at end');
end;
// -------------------------------------------------------------------------
procedure TestInsertAtBoundaries();
var
A: TIntArray;
begin
A.Push(5);
A.InsertAt(0, 1);
AssertEquals(Integer(A.Length), 2, 'TestInsertAtBoundaries: length');
AssertEquals(A[0], 1, 'TestInsertAtBoundaries: new first');
AssertEquals(A[1], 5, 'TestInsertAtBoundaries: shifted');
end;
// -------------------------------------------------------------------------
procedure TestRemoveAt();
var
A: TIntArray;
begin
A := MakeIntArray([10, 20, 30, 40, 50]);
A.RemoveAt(2);
AssertEquals(Integer(A.Length), 4, 'TestRemoveAt: length after remove');
AssertEquals(A[0], 10, 'TestRemoveAt: [0]');
AssertEquals(A[1], 20, 'TestRemoveAt: [1]');
AssertEquals(A[2], 40, 'TestRemoveAt: [2] shifted');
AssertEquals(A[3], 50, 'TestRemoveAt: [3] shifted');
A.RemoveAt(0);
AssertEquals(A[0], 20, 'TestRemoveAt: remove first');
AssertEquals(Integer(A.Length), 3, 'TestRemoveAt: length after remove first');
A.RemoveAt(Cardinal(A.Length - 1));
AssertEquals(Integer(A.Length), 2, 'TestRemoveAt: length after remove last');
AssertEquals(A[Cardinal(A.Length - 1)], 40, 'TestRemoveAt: last after remove last');
end;
// -------------------------------------------------------------------------
procedure TestRemoveAtAllElements();
var
A: TIntArray;
begin
A := MakeIntArray([42]);
A.RemoveAt(0);
AssertTrue(A.IsEmpty, 'TestRemoveAtAllElements: empty after removing sole element');
end;
// -------------------------------------------------------------------------
procedure TestRemove();
var
A: TIntArray;
begin
A := MakeIntArray([1, 2, 3, 2, 4]);
A.Remove(2);
AssertEquals(Integer(A.Length), 4, 'TestRemove: length after remove');
AssertEquals(A[0], 1, 'TestRemove: [0]');
AssertEquals(A[1], 2, 'TestRemove: [1] should still be present (first occurrence)');
AssertEquals(A[2], 3, 'TestRemove: [2]');
AssertEquals(A[3], 4, 'TestRemove: [3]');
A.Remove(99);
AssertEquals(Integer(A.Length), 4, 'TestRemove: length unchanged for absent value');
end;
// -------------------------------------------------------------------------
procedure TestRemoveAll();
var
A: TIntArray;
begin
A := MakeIntArray([1, 2, 1, 2, 1]);
A.RemoveAll(1);
AssertEquals(Integer(A.Length), 2, 'TestRemoveAll: length after RemoveAll');
AssertEquals(A[0], 2, 'TestRemoveAll: [0]');
AssertEquals(A[1], 2, 'TestRemoveAll: [1]');
A.RemoveAll(99);
AssertEquals(Integer(A.Length), 2, 'TestRemoveAll: unchanged for absent value');
A.RemoveAll(2);
AssertTrue(A.IsEmpty, 'TestRemoveAll: empty after removing all items');
end;
// -------------------------------------------------------------------------
procedure TestRemoveAllTrailing();
var
A: TIntArray;
begin
A := MakeIntArray([1, 2, 3, 0, 0, 0]);
A.RemoveAllTrailing(0);
AssertEquals(Integer(A.Length), 3, 'TestRemoveAllTrailing: length after removing trailing zeros');
AssertEquals(A[0], 1, 'TestRemoveAllTrailing: [0]');
AssertEquals(A[1], 2, 'TestRemoveAllTrailing: [1]');
AssertEquals(A[2], 3, 'TestRemoveAllTrailing: [2]');
A.RemoveAllTrailing(99);
AssertEquals(Integer(A.Length), 3, 'TestRemoveAllTrailing: no trailing match');
A := MakeIntArray([0, 0, 0]);
A.RemoveAllTrailing(0);
AssertTrue(A.IsEmpty, 'TestRemoveAllTrailing: all trailing removed');
A := MakeIntArray([0, 1, 0, 0]);
A.RemoveAllTrailing(0);
AssertEquals(Integer(A.Length), 2, 'TestRemoveAllTrailing: stops at non-matching');
AssertEquals(A[0], 0, 'TestRemoveAllTrailing: leading zero preserved');
AssertEquals(A[1], 1, 'TestRemoveAllTrailing: middle preserved');
end;
// -------------------------------------------------------------------------
procedure TestReplace();
var
A: TIntArray;
begin
A := MakeIntArray([1, 2, 3, 2, 4]);
A.Replace(2, 99);
AssertEquals(Integer(A.Length), 5, 'TestReplace: length unchanged');
AssertEquals(A[0], 1, 'TestReplace: [0]');
AssertEquals(A[1], 2, 'TestReplace: [1] first occurrence still present');
AssertEquals(A[2], 3, 'TestReplace: [2]');
AssertEquals(A[3], 99, 'TestReplace: [3] last occurrence replaced');
AssertEquals(A[4], 4, 'TestReplace: [4]');
A.Replace(77, 88);
AssertEquals(Integer(A.Length), 5, 'TestReplace: length unchanged for absent value');
end;
// -------------------------------------------------------------------------
procedure TestContains();
var
A: TIntArray;
Idx: Cardinal;
begin
A := MakeIntArray([10, 20, 30, 40, 50]);
AssertTrue(A.Contains(30), 'TestContains: should contain 30');
AssertFalse(A.Contains(99), 'TestContains: should not contain 99');
AssertTrue(A.Contains(30, Idx), 'TestContains: Contains with index should return true for 30');
AssertEquals(Integer(Idx), 2, 'TestContains: index of 30 should be 2');
AssertFalse(A.Contains(99, Idx), 'TestContains: Contains with index should return false for 99');
end;
// -------------------------------------------------------------------------
procedure TestRemoveShiftLeftInsert();
var
A: TIntArray;
begin
A := MakeIntArray([1, 2, 3, 4, 5]);
A.RemoveShiftLeftInsert(1, 3, 99);
AssertEquals(Integer(A.Length), 5, 'TestRemoveShiftLeftInsert: length unchanged');
AssertEquals(A[0], 1, 'TestRemoveShiftLeftInsert: [0]');
AssertEquals(A[1], 3, 'TestRemoveShiftLeftInsert: [1]');
AssertEquals(A[2], 4, 'TestRemoveShiftLeftInsert: [2]');
AssertEquals(A[3], 99, 'TestRemoveShiftLeftInsert: [3]');
AssertEquals(A[4], 5, 'TestRemoveShiftLeftInsert: [4]');
A := MakeIntArray([1, 2, 3]);
A.RemoveShiftLeftInsert(1, 1, 99);
AssertEquals(Integer(A.Length), 3, 'TestRemoveShiftLeftInsert: identity length');
AssertEquals(A[0], 1, 'TestRemoveShiftLeftInsert: identity [0]');
AssertEquals(A[1], 99, 'TestRemoveShiftLeftInsert: identity [1]');
AssertEquals(A[2], 3, 'TestRemoveShiftLeftInsert: identity [2]');
end;
// -------------------------------------------------------------------------
procedure TestDistill();
var
A: TIntArray;
D: array of Integer;
I, V: Integer;
begin
for I := 1 to 5 do
A.Push(I * 11); // $R-
D := A.Distill();
AssertTrue(A.IsEmpty, 'TestDistill: PlasticArray should be empty after Distill');
AssertEquals(System.Length(D), 5, 'TestDistill: distilled array length'); // $R-
I := 0;
for V in D do
begin
AssertEquals(V, (I + 1) * 11, 'TestDistill: distilled[' + IntToStr(I) + ']'); // $R-
Inc(I);
end;
end;
// -------------------------------------------------------------------------
procedure TestCopy();
var
A: TIntArray;
C: array of Integer;
I, V: Integer;
begin
for I := 1 to 5 do
A.Push(I);
C := A.Copy();
AssertEquals(Integer(A.Length), 5, 'TestCopy: original length unchanged');
AssertEquals(System.Length(C), 5, 'TestCopy: copied array length'); // $R-
I := 0;
for V in C do
begin
AssertEquals(V, I + 1, 'TestCopy: copied[' + IntToStr(I) + ']'); // $R-
Inc(I);
end;
end;
// -------------------------------------------------------------------------
procedure TestSortWithCompareFunc();
var
A: TIntArray;
Prev, V: Integer;
begin
A := MakeIntArray([5, 3, 1, 4, 2]);
A.Sort(@IntAscending);
AssertEquals(Integer(A.Length), 5, 'TestSortWithCompareFunc: length preserved');
Prev := A[0];
for V in A do
begin
AssertTrue(Prev <= V, 'TestSortWithCompareFunc: not sorted ascending');
Prev := V;
end;
A.Sort(@IntDescending);
Prev := A[0];
for V in A do
begin
AssertTrue(Prev >= V, 'TestSortWithCompareFunc: not sorted descending');
Prev := V;
end;
end;
// -------------------------------------------------------------------------
procedure TestSortDefault();
var
A: TIntArray;
Prev, V: Integer;
begin
A := MakeIntArray([9, 1, 5, 3, 7, 2, 8, 4, 6, 0]);
A.Sort();
AssertEquals(Integer(A.Length), 10, 'TestSortDefault: length preserved');
Prev := A[0];
for V in A do
begin
AssertTrue(Prev <= V, 'TestSortDefault: not sorted');
Prev := V;
end;
end;
// -------------------------------------------------------------------------
procedure TestSortAlreadySorted();
var
A: TIntArray;
Prev, V: Integer;
begin
A := MakeIntArray([1, 2, 3, 4, 5]);
A.Sort();
Prev := A[0];
for V in A do
begin
AssertTrue(Prev <= V, 'TestSortAlreadySorted: not sorted');
Prev := V;
end;
end;
// -------------------------------------------------------------------------
procedure TestSortReversed();
var
A: TIntArray;
Prev, V: Integer;
begin
A := MakeIntArray([5, 4, 3, 2, 1]);
A.Sort();
Prev := A[0];
for V in A do
begin
AssertTrue(Prev <= V, 'TestSortReversed: not sorted');
Prev := V;
end;
end;
// -------------------------------------------------------------------------
procedure TestSortDuplicates();
var
A: TIntArray;
Prev, V: Integer;
begin
A := MakeIntArray([3, 1, 2, 1, 3, 2]);
A.Sort();
AssertEquals(Integer(A.Length), 6, 'TestSortDuplicates: length preserved');
Prev := A[0];
for V in A do
begin
AssertTrue(Prev <= V, 'TestSortDuplicates: not sorted');
Prev := V;
end;
end;
// -------------------------------------------------------------------------
procedure TestSortSingleElement();
var
A: TIntArray;
begin
A.Push(42);
A.Sort();
AssertEquals(Integer(A.Length), 1, 'TestSortSingleElement: length');
AssertEquals(A[0], 42, 'TestSortSingleElement: value preserved');
end;
// -------------------------------------------------------------------------
procedure TestSortSubrange();
var
A: TIntArray;
begin
A := MakeIntArray([5, 3, 1, 4, 2]);
A.SortSubrange(1, 3);
AssertEquals(A[0], 5, 'TestSortSubrange: [0] outside range unchanged');
AssertEquals(A[4], 2, 'TestSortSubrange: [4] outside range unchanged');
AssertTrue(A[1] <= A[2], 'TestSortSubrange: [1]<=[2]');
AssertTrue(A[2] <= A[3], 'TestSortSubrange: [2]<=[3]');
end;
// -------------------------------------------------------------------------
procedure TestSortSubrangeWithFunc();
var
A: TIntArray;
begin
A := MakeIntArray([5, 3, 1, 4, 2]);
A.SortSubrange(1, 3, @IntAscending);
AssertEquals(A[0], 5, 'TestSortSubrangeWithFunc: [0] outside range unchanged');
AssertEquals(A[4], 2, 'TestSortSubrangeWithFunc: [4] outside range unchanged');
AssertTrue(A[1] <= A[2], 'TestSortSubrangeWithFunc: [1]<=[2]');
AssertTrue(A[2] <= A[3], 'TestSortSubrangeWithFunc: [2]<=[3]');
end;
// -------------------------------------------------------------------------
procedure TestSortStability();
var
A: TIntArray;
Ones, Twos, Threes, V: Integer;
begin
A := MakeIntArray([2, 1, 3, 1, 2, 3, 1, 2, 3]);
A.Sort();
Ones := 0; Twos := 0; Threes := 0;
for V in A do
case V of
1: Inc(Ones);
2: Inc(Twos);
3: Inc(Threes);
else
Fail('TestSortStability: unexpected value ' + IntToStr(V));
end;
AssertEquals(Ones, 3, 'TestSortStability: three 1s');
AssertEquals(Twos, 3, 'TestSortStability: three 2s');
AssertEquals(Threes, 3, 'TestSortStability: three 3s');
end;
// -------------------------------------------------------------------------
procedure TestShuffle();
var
A: TIntArray;
I, Sum, Prev, V: Integer;
begin
for I := 0 to 99 do
A.Push(I);
A.Shuffle();
AssertEquals(Integer(A.Length), 100, 'TestShuffle: length preserved');
Sum := 0;
for V in A do
Sum := Sum + V; // $R-
AssertEquals(Sum, 4950, 'TestShuffle: sum of shuffled elements must equal sum of original');
A.Sort();
Prev := A[0];
for V in A do
begin
AssertTrue(Prev <= V, 'TestShuffle: not sorted after sort post-shuffle');
Prev := V;
end;
end;
// -------------------------------------------------------------------------
procedure TestFind();
var
A: TIntArray;
Idx: Cardinal;
begin
A := MakeIntArray([1, 3, 5, 7, 9, 11, 13]);
Idx := A.Find(1);
AssertEquals(Integer(Idx), 0, 'TestFind: Find(1)');
Idx := A.Find(7);
AssertEquals(Integer(Idx), 3, 'TestFind: Find(7)');
Idx := A.Find(13);
AssertEquals(Integer(Idx), 6, 'TestFind: Find(13)');
A.Empty();
A.Push(42);
Idx := A.Find(42);
AssertEquals(Integer(Idx), 0, 'TestFind: Find in single-element array');
end;
// -------------------------------------------------------------------------
procedure TestEnumerator();
var
A: TIntArray;
I, Sum: Integer;
begin
for I := 1 to 5 do
A.Push(I);
Sum := 0;
for I in A do
Sum := Sum + I; // $R-
AssertEquals(Sum, 15, 'TestEnumerator: sum via for-in');
A.Empty();
Sum := 0;
for I in A do
Sum := Sum + I; // $R-
AssertEquals(Sum, 0, 'TestEnumerator: sum over empty array');
end;
// -------------------------------------------------------------------------
procedure TestEnumeratorLiveMutationAppend();
var
A: TIntArray;
V: Integer;
begin
A.Push(1);
A.Push(2);
A.Push(3);
// While enumerating, append new items beyond the current position.
// The spec guarantees this is safe. Each value <= 6 causes its double
// to be appended, so the sequence grows:
// Visit 1 (<=6): append 2 -> [1,2,3,2]
// Visit 2 (<=6): append 4 -> [1,2,3,2,4]
// Visit 3 (<=6): append 6 -> [1,2,3,2,4,6]
// Visit 2 (<=6): append 4 -> [1,2,3,2,4,6,4]
// Visit 4 (<=6): append 8 -> [1,2,3,2,4,6,4,8]
// Visit 6 (<=6): append 12 -> [1,2,3,2,4,6,4,8,12]
// Visit 4 (<=6): append 8 -> [1,2,3,2,4,6,4,8,12,8]
// Visit 8 (>6): no append
// Visit 12 (>6): no append
// Visit 8 (>6): no append
for V in A do
if V <= 6 then
A.Push(V * 2); // $R-
AssertEquals(Integer(A.Length), 10, 'TestEnumeratorLiveMutationAppend: length after live appends');
AssertEquals(A[0], 1, 'TestEnumeratorLiveMutationAppend: [0]');
AssertEquals(A[1], 2, 'TestEnumeratorLiveMutationAppend: [1]');
AssertEquals(A[2], 3, 'TestEnumeratorLiveMutationAppend: [2]');
AssertEquals(A[3], 2, 'TestEnumeratorLiveMutationAppend: [3] appended from 1');
AssertEquals(A[4], 4, 'TestEnumeratorLiveMutationAppend: [4] appended from 2');
AssertEquals(A[5], 6, 'TestEnumeratorLiveMutationAppend: [5] appended from 3');
AssertEquals(A[6], 4, 'TestEnumeratorLiveMutationAppend: [6] appended from 2');
AssertEquals(A[7], 8, 'TestEnumeratorLiveMutationAppend: [7] appended from 4');
AssertEquals(A[8], 12, 'TestEnumeratorLiveMutationAppend: [8] appended from 6');
AssertEquals(A[9], 8, 'TestEnumeratorLiveMutationAppend: [9] appended from 4');
end;
// -------------------------------------------------------------------------
procedure TestCopyOnWrite();
var
A, B: TIntArray;
I: Integer;
begin
for I := 1 to 5 do
A.Push(I);
B := A;
B[0] := 99;
AssertEquals(A[0], 1, 'TestCopyOnWrite: A[0] must not be affected by write to B');
AssertEquals(B[0], 99, 'TestCopyOnWrite: B[0] should be 99');
AssertEquals(Integer(A.Length), 5, 'TestCopyOnWrite: A length unchanged');
AssertEquals(Integer(B.Length), 5, 'TestCopyOnWrite: B length unchanged');
end;
// -------------------------------------------------------------------------
procedure TestCopyOnWritePush();
var
A, B: TIntArray;
begin
A.Push(1);
A.Push(2);
B := A;
B.Push(3);
AssertEquals(Integer(A.Length), 2, 'TestCopyOnWritePush: A length should remain 2');
AssertEquals(Integer(B.Length), 3, 'TestCopyOnWritePush: B length should be 3');
end;
// -------------------------------------------------------------------------
procedure TestCopyOnWriteMultipleRefs();
var
A, B, C: TIntArray;
begin
A.Push(10);
A.Push(20);
B := A;
C := A;
C[0] := 77;
AssertEquals(A[0], 10, 'TestCopyOnWriteMultipleRefs: A[0] unchanged');
AssertEquals(B[0], 10, 'TestCopyOnWriteMultipleRefs: B[0] unchanged');
AssertEquals(C[0], 77, 'TestCopyOnWriteMultipleRefs: C[0] is 77');
end;
// -------------------------------------------------------------------------
procedure TestPushPopRoundTrip();
var
A: TIntArray;
I, V: Integer;
begin
for I := 1 to 20 do
A.Push(I);
for I := 20 downto 1 do
begin
V := A.Pop();
AssertEquals(V, I, 'TestPushPopRoundTrip: pop at step ' + IntToStr(21 - I));
end;