123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- unit tclinkedlist;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, fpcunit, testutils, testregistry, fplists;
- type
- { TTestLinkedList }
- TTestLinkedList= class(TTestCase)
- published
- procedure TestCreate;
- procedure TestAdd;
- procedure TestAdd2;
- procedure TestClear;
- procedure TestRemove;
- procedure TestRemove2;
- procedure TestRemove3;
- Procedure TestVisit;
- end;
- implementation
- procedure TTestLinkedList.TestCreate;
- Var
- LL : TLinkedList;
- begin
- LL:=TLinkedList.Create(TLinkedListItem);
- Try
- AssertEquals('Item class is TLinkedListItem.',TLinkedListItem,LL.ItemClass);
- AssertEquals('Item count is 0',0,LL.Count);
- If (LL.Root<>Nil) then
- Fail('Root is not nil')
- Finally
- LL.Free;
- end;
- end;
- procedure TTestLinkedList.TestAdd;
- Var
- LL : TLinkedList;
- I : TLinkedListItem;
-
- begin
- LL:=TLinkedList.Create(TLinkedListItem);
- Try
- I:=LL.Add;
- AssertEquals('Add result is TLinkedListItem.',TLinkedListItem,I.ClassType);
- AssertEquals('Item count is 1',1,LL.Count);
- If (I<>LL.Root) then
- Fail('Root item is not added item');
- Finally
- LL.Free;
- end;
- end;
- procedure TTestLinkedList.TestClear;
- Var
- LL : TLinkedList;
- I : Integer;
- begin
- LL:=TLinkedList.Create(TLinkedListItem);
- Try
- For I:=1 to 3 do
- LL.Add;
- LL.Clear;
- AssertEquals('Item count after clear is 0',0,LL.Count);
- Finally
- LL.Free;
- end;
- end;
- procedure TTestLinkedList.TestAdd2;
- Var
- LL : TLinkedList;
- I1,I2 : TLinkedListItem;
-
- begin
- LL:=TLinkedList.Create(TLinkedListItem);
- Try
- I1:=LL.Add;
- I2:=LL.Add;
- If (I2<>LL.Root) then
- Fail('Root item is not last added item');
- If (I2.Next<>I1) then
- Fail('Items ordered in the wrong way');
- Finally
- LL.Free;
- end;
- end;
- procedure TTestLinkedList.TestRemove;
- Var
- LL : TLinkedList;
- I : TLinkedListItem;
- begin
- LL:=TLinkedList.Create(TLinkedListItem);
- Try
- I:=LL.Add;
- Try
- LL.RemoveItem(I);
- AssertEquals('After remove Item count is 0',0,LL.Count);
- If (Nil<>LL.Root) then
- Fail('Root item is not nil after last removed item');
- Finally
- I.Free;
- end;
- Finally
- LL.Free;
- end;
- end;
- procedure TTestLinkedList.TestRemove2;
- Var
- LL : TLinkedList;
- I1,I2 : TLinkedListItem;
- begin
- LL:=TLinkedList.Create(TLinkedListItem);
- Try
- I1:=LL.Add;
- Try
- I2:=LL.Add;
- LL.RemoveItem(I1);
- AssertEquals('After remove first Item count is 1',1,LL.Count);
- If (I2<>LL.Root) then
- Fail('Root item is not I2 after remove of I1');
- Finally
- I1.Free;
- end;
- Finally
- LL.Free;
- end;
- end;
- procedure TTestLinkedList.TestRemove3;
- Var
- LL : TLinkedList;
- I1,I2, I3 : TLinkedListItem;
- begin
- LL:=TLinkedList.Create(TLinkedListItem);
- Try
- I1:=LL.Add;
- I2:=LL.Add;
- I3:=LL.Add;
- LL.RemoveItem(I2);
- Try
- AssertEquals('After remove I2 Item count is 2',2,LL.Count);
- If (I3.Next<>I1) then
- Fail('After Remove of I2, I3.Next<>I1');
- Finally
- I2.Free;
- end;
- Finally
- LL.Free;
- end;
- end;
- Type
- { TCountVisitor }
- TCountVisitor = Class(TLinkedListVisitor)
- FCount : integer;
- FMax : integer;
- Function Visit(Item : TLinkedListItem) : Boolean; override;
- Constructor Create(AMax : integer);
- end;
- { TCountVisitor }
- function TCountVisitor.Visit(Item: TLinkedListItem): Boolean;
- begin
- Inc(FCount);
- Result:=(FMax=-1) or (FCount<FMax);
- end;
- constructor TCountVisitor.Create(AMax: integer);
- begin
- FMax:=AMax;
- end;
- procedure TTestLinkedList.TestVisit;
- Var
- I : Integer;
- V : TCountVisitor;
- LL : TLinkedList;
- begin
- LL:=TLinkedList.Create(TLinkedListItem);
- Try
- For I:=1 to 5 do
- LL.Add;
- V:=TCountVisitor.Create(-1);
- Try
- LL.Foreach(V);
- AssertEquals('Counter visited all items',5,V.FCount);
- Finally
- V.Free;
- end;
- V:=TCountVisitor.Create(3);
- Try
- LL.Foreach(V);
- AssertEquals('Counter visited 3 items',3,V.FCount);
- Finally
- V.Free;
- end;
- Finally
- LL.Free;
- end;
- end;
- initialization
- RegisterTest(TTestLinkedList);
- end.
|