Files
CentrED/tests/UTestLinkedList.pas
aksdb 99749a5168 ✅ Add FPCUnit test suites for shared code and server
Introduces two headless FPCUnit runners under tests/ with 119 tests
covering the shared stream, cache, and packet layers, the MUL file
library, and the server config, region, account, and landscape
bounds logic. Each runner uses its own object output directory so
client and server PPUs with colliding unit names cannot pollute
each other's builds. Drone now builds and runs both suites before
packaging. Found on the way: two product defects fixed in the
previous commits.
2026-11-15 22:08:32 +01:00

174 lines
3.7 KiB
ObjectPascal

unit UTestLinkedList;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fpcunit, testregistry, ULinkedList;
type
TTestLinkedList = class(TTestCase)
protected
procedure SetUp; override;
procedure TearDown; override;
published
procedure AddAndIterate;
procedure DeleteOnlyRemovesFirstMatch;
procedure DeleteHead;
procedure DeleteTail;
procedure DeleteMiddle;
procedure DeleteByID;
procedure DeleteMissingIsHarmless;
procedure ClearEmptiesList;
end;
implementation
var
items: array[0..2] of Pointer;
list: TLinkedList;
procedure TTestLinkedList.SetUp;
var
i: Integer;
begin
for i := 0 to High(items) do
items[i] := Pointer(PtrInt(100 + i));
list := TLinkedList.Create;
end;
procedure TTestLinkedList.TearDown;
begin
list.Free;
end;
procedure TTestLinkedList.AddAndIterate;
var
item: PLinkedItem;
count: Integer;
begin
list.Add(0, items[0]);
list.Add(0, items[1]);
list.Add(0, items[2]);
count := 0;
item := nil;
while list.Iterate(item) do
begin
case count of
0: AssertSame('order 1', items[0], item^.Data);
1: AssertSame('order 2', items[1], item^.Data);
2: AssertSame('order 3', items[2], item^.Data);
end;
Inc(count);
end;
AssertEquals(3, count);
end;
procedure TTestLinkedList.DeleteOnlyRemovesFirstMatch;
var
item: PLinkedItem;
count, firstMatches: Integer;
begin
list.Add(0, items[0]);
list.Add(0, items[0]); //duplicate
list.Add(0, items[1]);
list.Delete(items[0]);
count := 0;
firstMatches := 0;
item := nil;
while list.Iterate(item) do
begin
if item^.Data = items[0] then Inc(firstMatches);
Inc(count);
end;
AssertEquals('duplicate survives', 1, firstMatches);
AssertEquals('two entries remain', 2, count);
end;
procedure TTestLinkedList.DeleteHead;
var
item: PLinkedItem;
begin
list.Add(0, items[0]);
list.Add(0, items[1]);
list.Delete(items[0]);
item := nil;
AssertTrue(list.Iterate(item));
AssertSame('second item becomes head', items[1], item^.Data);
AssertFalse('no third entry', list.Iterate(item));
end;
procedure TTestLinkedList.DeleteTail;
var
item: PLinkedItem;
begin
list.Add(0, items[0]);
list.Add(0, items[1]);
list.Delete(items[1]);
list.Add(0, items[2]); //append after tail deletion
item := nil;
AssertTrue(list.Iterate(item));
AssertSame(items[0], item^.Data);
AssertTrue(list.Iterate(item));
AssertSame('append after tail delete stays intact', items[2], item^.Data);
AssertFalse(list.Iterate(item));
end;
procedure TTestLinkedList.DeleteMiddle;
var
item: PLinkedItem;
begin
list.Add(0, items[0]);
list.Add(0, items[1]);
list.Add(0, items[2]);
list.Delete(items[1]);
item := nil;
AssertTrue(list.Iterate(item));
AssertSame(items[0], item^.Data);
AssertTrue(list.Iterate(item));
AssertSame('chain must stay linked', items[2], item^.Data);
AssertFalse(list.Iterate(item));
end;
procedure TTestLinkedList.DeleteByID;
var
item: PLinkedItem;
begin
list.Add(42, items[0]);
list.Add(43, items[1]);
list.Delete(42);
item := nil;
AssertTrue(list.Iterate(item));
AssertSame(items[1], item^.Data);
AssertFalse(list.Iterate(item));
end;
procedure TTestLinkedList.DeleteMissingIsHarmless;
var
item: PLinkedItem;
begin
list.Add(0, items[0]);
list.Delete(Pointer(PtrInt(999))); //no match, no crash
item := nil;
AssertTrue('entry untouched', list.Iterate(item));
AssertSame(items[0], item^.Data);
AssertFalse('no extra entries', list.Iterate(item));
end;
procedure TTestLinkedList.ClearEmptiesList;
var
item: PLinkedItem;
begin
list.Add(0, items[0]);
list.Add(0, items[1]);
list.Clear;
item := nil;
AssertFalse('empty after clear', list.Iterate(item));
end;
initialization
RegisterTests([TTestLinkedList]);
end.