unit uutlListTest; {$mode objfpc}{$H+} interface uses Classes, SysUtils, TestFramework, contnrs, uTestHelper, uutlGenerics; type //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TIntList = specialize TutlList; TIntfList = specialize TutlList; TObjList = specialize TutlList; TutlListTest = class(TIntfObjOwner) private fIntList: TIntList; fIntfList: TIntfList; fObjList: TObjList; protected procedure SetUp; override; procedure TearDown; override; published procedure Prop_Count; procedure Prop_First; procedure Prop_last; procedure Prop_Items; procedure Meth_Add; procedure Meth_Insert; procedure Meth_Exchange; procedure Meth_Move; procedure Meth_Delete; procedure Meth_Extract; procedure Meth_PushFirst; procedure Meth_PopFirst; procedure Meth_PushLast; procedure Meth_PopLast; procedure Dtor_FreesAllItems; procedure Meth_IndexOf; procedure Meth_Extract_WithDefault; procedure Meth_Remove; procedure AddRemoveInterfaces; procedure AddRemoveObject_OwnedByList; procedure AddRemoveObject_NotOwnedByList; end; implementation //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TutlListTest////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.SetUp; begin inherited SetUp; fIntList := TIntList.Create(true); fIntfList := TIntfList.Create(true); fObjList := TObjList.Create(true); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.TearDown; begin FreeAndNil(fIntList); FreeAndNil(fIntfList); FreeAndNil(fObjList); inherited TearDown; end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Prop_Count; begin AssertEquals(0, fIntList.Count); fIntList.Add(123); AssertEquals(1, fIntList.Count); fIntList.Add(234); AssertEquals(2, fIntList.Count); fIntList.Add(345); AssertEquals(3, fIntList.Count); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Prop_First; begin fIntList.Add(123); AssertEquals(123, fIntList.First); fIntList.Add(456); AssertEquals(123, fIntList.First); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Prop_last; begin fIntList.Add(123); AssertEquals(123, fIntList.Last); fIntList.Add(456); AssertEquals(456, fIntList.Last); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Prop_Items; begin fIntList.Add(123); fIntList.Add(234); fIntList.Add(345); fIntList.Add(456); AssertEquals(123, fIntList[0]); AssertEquals(234, fIntList[1]); AssertEquals(345, fIntList[2]); AssertEquals(456, fIntList[3]); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Meth_Add; begin fIntList.Add(123); AssertEquals(fIntList.Count, 1); AssertEquals(123, fIntList[0]); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Meth_Insert; begin fIntList.Insert(0, 123); fIntList.Insert(0, 456); AssertEquals(fIntList.Count, 2); AssertEquals(123, fIntList[1]); AssertEquals(456, fIntList[0]); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Meth_Exchange; begin fIntList.Add(123); fIntList.Add(234); fIntList.Add(345); fIntList.Add(456); fIntList.Add(567); fIntList.Exchange(1, 3); AssertEquals(123, fIntList[0]); AssertEquals(456, fIntList[1]); AssertEquals(345, fIntList[2]); AssertEquals(234, fIntList[3]); AssertEquals(567, fIntList[4]); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Meth_Move; begin fIntList.Add(123); fIntList.Add(234); fIntList.Add(345); fIntList.Add(456); fIntList.Add(567); fIntList.Move(1, 3); AssertEquals(123, fIntList[0]); AssertEquals(345, fIntList[1]); AssertEquals(456, fIntList[2]); AssertEquals(234, fIntList[3]); AssertEquals(567, fIntList[4]); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Meth_Delete; begin fIntList.Add(123); fIntList.Add(234); fIntList.Add(345); fIntList.Add(456); fIntList.Add(567); fIntList.Delete(2); AssertEquals(4, fIntList.Count); AssertEquals(456, fIntList[2]); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Meth_Extract; begin fIntList.Add(123); fIntList.Add(234); fIntList.Add(345); fIntList.Add(456); fIntList.Add(567); AssertEquals(234, fIntList.Extract(1)); AssertEquals(4, fIntList.Count); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Meth_PushFirst; begin fIntList.PushFirst(123); fIntList.PushFirst(234); AssertEquals(2, fIntList.Count); AssertEquals(123, fIntList[1]); AssertEquals(234, fIntList[0]); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Meth_PopFirst; begin fIntList.Add(123); fIntList.Add(234); fIntList.Add(345); fIntList.Add(456); fIntList.Add(567); AssertEquals(123, fIntList.PopFirst(false)); AssertEquals(234, fIntList.PopFirst(false)); AssertEquals(3, fIntList.Count); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Meth_PushLast; begin fIntList.PushLast(123); fIntList.PushLast(234); fIntList.PushLast(345); AssertEquals(3, fIntList.Count); AssertEquals(123, fIntList[0]); AssertEquals(234, fIntList[1]); AssertEquals(345, fIntList[2]); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Meth_PopLast; begin fIntList.Add(123); fIntList.Add(234); fIntList.Add(345); fIntList.Add(456); fIntList.Add(567); AssertEquals(567, fIntList.PopLast(false)); AssertEquals(456, fIntList.PopLast(false)); AssertEquals(3, fIntList.Count); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Dtor_FreesAllItems; begin fObjList.Add(TIntfObj.Create(self)); fObjList.Add(TIntfObj.Create(self)); FreeAndNil(fObjList); AssertEquals(0, IntfObjCounter); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Meth_IndexOf; begin fIntList.Add(123); fIntList.Add(234); fIntList.Add(345); fIntList.Add(456); fIntList.Add(567); AssertEquals( 1, fIntList.IndexOf(234)); AssertEquals( 3, fIntList.IndexOf(456)); AssertEquals(-1, fIntList.IndexOf(999)); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Meth_Extract_WithDefault; begin fIntList.Add(123); fIntList.Add(234); fIntList.Add(345); fIntList.Add(456); fIntList.Add(567); AssertEquals(234, fIntList.Extract(234, 999)); AssertEquals(999, fIntList.Extract(234, 999)); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.Meth_Remove; begin fIntList.Add(123); fIntList.Add(234); fIntList.Add(345); fIntList.Add(456); fIntList.Add(567); fIntList.Remove(234); AssertEquals(4, fIntList.Count); AssertEquals(345, fIntList[1]); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.AddRemoveInterfaces; var i1: IUnknown; begin fIntfList.Add(TIntfObj.Create(self)); fIntfList.Add(TIntfObj.Create(self)); fIntfList.Add(TIntfObj.Create(self)); fIntfList.Exchange(0, 2); fIntfList.Move(0, 2); fIntfList.Delete(0); fIntfList.Extract(0); fIntfList.Clear; fIntfList.Insert(0, TIntfObj.Create(self)); fIntfList.PopLast(true); fIntfList.Insert(0, TIntfObj.Create(self)); fIntfList.PopLast(false); fIntfList.Insert(0, TIntfObj.Create(self)); fIntfList.PopFirst(true); fIntfList.Insert(0, TIntfObj.Create(self)); fIntfList.PopFirst(false); i1 := TIntfObj.Create(self); fIntfList.Insert(0, i1); fIntfList.Extract(i1, nil); i1 := nil; i1 := TIntfObj.Create(self); fIntfList.Insert(0, i1); fIntfList.Remove(i1); i1 := nil; fIntfList.Add(TIntfObj.Create(self)); fIntfList[0] := TIntfObj.Create(self); fIntfList.Clear; AssertEquals(0, IntfObjCounter); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.AddRemoveObject_OwnedByList; function CreateObj: TObject; begin result := TIntfObj.Create(self); end; begin fObjList.Add(CreateObj); fObjList.Add(CreateObj); fObjList.Add(CreateObj); fObjList.Exchange(0, 2); fObjList.Move(0, 2); fObjList.Delete(0); fObjList.Extract(0).Free; fObjList.Clear; fObjList.Add(CreateObj); fObjList.PopLast(true); fObjList.Add(CreateObj); fObjList.PopLast(false).Free; fObjList.Add(CreateObj); fObjList.PopFirst(true); fObjList.Add(CreateObj); fObjList.PopFirst(false).Free; fObjList.Add(CreateObj); fObjList.Extract(fObjList[0], nil).Free; fObjList.Add(CreateObj); fObjList.Remove(fObjList[0]); fObjList.Add(CreateObj); fObjList[0] := CreateObj; fObjList.Clear; AssertEquals(0, IntfObjCounter); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListTest.AddRemoveObject_NotOwnedByList; var lst: TObjectList; function CreateObj: TObject; begin result := TIntfObj.Create(self); lst.Add(result); end; begin lst := TObjectList.Create(true); try fObjList.OwnsItems := false; fObjList.Add(CreateObj); fObjList.Add(CreateObj); fObjList.Add(CreateObj); fObjList.Exchange(0, 2); fObjList.Move(0, 2); fObjList.Delete(0); fObjList.Extract(0); fObjList.Clear; fObjList.Add(CreateObj); fObjList.PopLast(true); fObjList.Add(CreateObj); fObjList.PopLast(false); fObjList.Add(CreateObj); fObjList.PopFirst(true); fObjList.Add(CreateObj); fObjList.PopFirst(false); fObjList.Add(CreateObj); fObjList.Extract(fObjList[0], nil); fObjList.Add(CreateObj); fObjList.Remove(fObjList[0]); fObjList.Add(CreateObj); fObjList[0] := CreateObj; fObjList.Clear; finally FreeAndNil(lst); end; AssertEquals(0, IntfObjCounter); end; initialization RegisterTest(TutlListTest.Suite); end.