|
- unit uutlStackTests;
-
- {$mode objfpc}{$H+}
-
- interface
-
- uses
- Classes, SysUtils, TestFramework, Contnrs,
- uTestHelper, uutlGenerics;
-
- type
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- TIntStack = specialize TutlStack<Integer>;
- TIntfStack = specialize TutlStack<IUnknown>;
- TObjStack = specialize TutlStack<TObject>;
- TutlStackTests = class(TIntfObjOwner)
- private
- fIntStack: TIntStack;
- fIntfStack: TIntfStack;
- fObjStack: TObjStack;
-
- protected
- procedure SetUp; override;
- procedure TearDown; override;
-
- published
- procedure Prop_Count;
- procedure Prop_Empty;
- procedure Meth_Peek;
- procedure Meth_PushPop1000Times;
- procedure Meth_PushPop1000Items;
- procedure Meth_PushPop1000Items1000Times;
- procedure Meth_PushPop100Interfaces;
- procedure Meth_PushPop100_ObjectOwned_WithFree;
- procedure Meth_PushPop100_ObjectOwnedByStack_WithoutFree;
- procedure Meth_PushPop100_ObjectNotOwned_WithFree;
- procedure Meth_PushPop100_ObjectNotOwned_WithoutFree;
- procedure Dtor_FreesAllItems;
- end;
-
- implementation
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //TutlStackTests////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.SetUp;
- begin
- inherited SetUp;
- fIntStack := TIntStack.Create(true);
- fIntfStack := TIntfStack.Create(true);
- fObjStack := TObjStack.Create(true);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.TearDown;
- begin
- FreeAndNil(fIntStack);
- FreeAndNil(fIntfStack);
- FreeAndNil(fObjStack);
- inherited TearDown;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.Prop_Count;
- begin
- AssertEquals(0, fIntStack.Count);
- fIntStack.Push(123);
- AssertEquals(1, fIntStack.Count);
- fIntStack.Push(234);
- AssertEquals(2, fIntStack.Count);
- fIntStack.Push(345);
- AssertEquals(3, fIntStack.Count);
- fIntStack.Pop;
- AssertEquals(2, fIntStack.Count);
- fIntStack.Pop;
- AssertEquals(1, fIntStack.Count);
- fIntStack.Pop;
- AssertEquals(0, fIntStack.Count);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.Prop_Empty;
- begin
- AssertEquals(true, fIntStack.IsEmpty);
- fIntStack.Push(345);
- AssertEquals(false, fIntStack.IsEmpty);
- fIntStack.Pop;
- AssertEquals(true, fIntStack.IsEmpty);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.Meth_Peek;
- begin
- fIntStack.Push(123);
- AssertEquals(123, fIntStack.Peek);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.Meth_PushPop1000Times;
- var
- i, tmp: Integer;
- begin
- for i := 0 to 1000 do begin
- fIntStack.Push(i);
- tmp := fIntStack.Pop;
- AssertEquals(i, tmp);
- end;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.Meth_PushPop1000Items;
- var
- i, tmp: Integer;
- begin
- for i := 0 to 1000 do
- fIntStack.Push(i);
- for i := 0 to 1000 do begin
- tmp := fIntStack.Pop;
- AssertEquals(1000-i, tmp);
- end;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.Meth_PushPop1000Items1000Times;
- var
- i, j, tmp: Integer;
- begin
- for j := 0 to 1000 do begin
- for i := 0 to 1000 do
- fIntStack.Push(i);
- for i := 0 to 1000 do begin
- tmp := fIntStack.Pop;
- AssertEquals(1000-i, tmp);
- end;
- end;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.Meth_PushPop100Interfaces;
- var
- i: Integer;
- begin
- for i := 0 to 100 do begin
- fIntfStack.Push(TIntfObj.Create(self));
- AssertEquals(i+1, IntfObjCounter);
- end;
- for i := 0 to 100 do begin
- fIntfStack.Pop;
- AssertEquals(100 - i, IntfObjCounter);
- end;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.Meth_PushPop100_ObjectOwned_WithFree;
- var
- i: Integer;
- begin
- for i := 0 to 100 do begin
- fObjStack.Push(TIntfObj.Create(self));
- AssertEquals(i+1, IntfObjCounter);
- end;
- for i := 0 to 100 do begin
- AssertNull('Pop returned non-zero item', fObjStack.Pop(true));
- AssertEquals(100 - i, IntfObjCounter);
- end;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.Meth_PushPop100_ObjectOwnedByStack_WithoutFree;
- var
- i: Integer;
- lst: TObjectList;
- obj: TObject;
- begin
- for i := 0 to 100 do begin
- fObjStack.Push(TIntfObj.Create(self));
- AssertEquals(i+1, IntfObjCounter);
- end;
- for i := 0 to 100 do begin
- fObjStack.Pop(true);
- AssertEquals(100 - i, IntfObjCounter);
- end;
-
- // free on Pop
- for i := 0 to 100 do begin
- fObjStack.Push(TIntfObj.Create(self));
- AssertEquals(i+1, IntfObjCounter);
- end;
- lst := TObjectList.Create(true);
- try
- for i := 0 to 100 do begin
- obj := fObjStack.Pop(false);
- AssertNotNull('Pop returned zero item', obj);
- lst.Add(obj);
- AssertEquals(101, IntfObjCounter);
- end;
- finally
- FreeAndNil(lst);
- end;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.Meth_PushPop100_ObjectNotOwned_WithFree;
- var
- lst: TObjectList;
- obj: TObject;
- i: Integer;
- begin
- lst := TObjectList.Create(true);
- try
- for i := 0 to 100 do begin
- obj := TIntfObj.Create(self);
- lst.Add(obj);
- fObjStack.Push(obj);
- AssertEquals(i+1, IntfObjCounter);
- end;
- fObjStack.OwnsItems := false;
- for i := 0 to 100 do begin
- AssertNull('Pop returned non-zero item', fObjStack.Pop(true));
- AssertEquals(101, IntfObjCounter);
- end;
- finally
- FreeAndNil(lst);
- end;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.Meth_PushPop100_ObjectNotOwned_WithoutFree;
- var
- lst: TObjectList;
- obj: TObject;
- i: Integer;
- begin
- lst := TObjectList.Create(true);
- try
- for i := 0 to 100 do begin
- obj := TIntfObj.Create(self);
- lst.Add(obj);
- fObjStack.Push(obj);
- AssertEquals(i+1, IntfObjCounter);
- end;
- fObjStack.OwnsItems := false;
- for i := 0 to 100 do begin
- AssertNotNull('Pop returned zero item', fObjStack.Pop(false));
- AssertEquals(101, IntfObjCounter);
- end;
- finally
- FreeAndNil(lst);
- end;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlStackTests.Dtor_FreesAllItems;
- begin
- fObjStack.Push(TIntfObj.Create(self));
- FreeAndNil(fObjStack);
- AssertEquals(0, IntfObjCounter);
- end;
-
- initialization
- RegisterTest(TutlStackTests.Suite);
-
- end.
|