|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- unit uutlLinkedListTests;
-
- {$mode objfpc}{$H+}
-
- interface
-
- uses
- Classes, SysUtils, TestFramework,
- uTestHelper, uutlGenerics, uutlExceptions;
-
- type
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- TIntList = specialize TutlLinkedList<Integer>;
- TutlLinkedListTests = class(TTestCase)
- private
- fIntList: TIntList;
-
- procedure AccessPropFirst;
- procedure AccessPropLast;
-
- protected
- procedure SetUp; override;
- procedure TearDown; override;
-
- published
- procedure Prop_Count;
- procedure Prop_IsEmpty;
- procedure Prop_First;
- procedure Prop_Last;
-
- procedure Meth_PushFirst_PopFirst;
- procedure Meth_PushLast_PopLast;
- procedure Meth_InsertBefore;
- procedure Meth_InsertAfter;
- procedure Meth_Remove;
- procedure Meth_Clear;
-
- procedure Iterator;
- end;
-
- implementation
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //TutlLinkedListTests///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.AccessPropFirst;
- begin
- fIntList.First;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.AccessPropLast;
- begin
- fIntList.Last;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.SetUp;
- begin
- inherited SetUp;
- fIntList := TIntList.Create(true);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.TearDown;
- begin
- FreeAndNil(fIntList);
- inherited TearDown;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.Prop_Count;
- begin
- AssertEquals(0, fIntList.Count);
- fIntList.PushFirst(123);
- AssertEquals(1, fIntList.Count);
- fIntList.PushFirst(234);
- AssertEquals(2, fIntList.Count);
- fIntList.PopFirst(true);
- AssertEquals(1, fIntList.Count);
- fIntList.PopFirst(true);
- AssertEquals(0, fIntList.Count);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.Prop_IsEmpty;
- begin
- AssertEquals(true, fIntList.IsEmpty);
- fIntList.PushFirst(123);
- AssertEquals(false, fIntList.IsEmpty);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.Prop_First;
- var
- i: TIntList.Iterator;
- begin
- AssertException('empty list does not raise exception when accessing First property', EutlInvalidOperation, @AccessPropFirst);
- fIntList.PushLast(123);
- fIntList.PushLast(234);
- i := fIntList.First;
- AssertEquals(123, i.Value);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.Prop_Last;
- var
- i: TIntList.Iterator;
- begin
- AssertException('empty list does not raise exception when accessing First property', EutlInvalidOperation, @AccessPropLast);
- fIntList.PushLast(123);
- fIntList.PushLast(234);
- i := fIntList.Last;
- AssertEquals(234, i.Value);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.Meth_PushFirst_PopFirst;
- begin
- fIntList.PushFirst(123);
- AssertEquals(123, fIntList.First.Value);
- fIntList.PushFirst(234);
- AssertEquals(234, fIntList.First.Value);
- fIntList.PushFirst(345);
- AssertEquals(345, fIntList.First.Value);
- fIntList.PushFirst(456);
- AssertEquals(456, fIntList.First.Value);
-
- AssertEquals(456, fIntList.PopFirst(false));
- AssertEquals(345, fIntList.First.Value);
- AssertEquals( 0, fIntList.PopFirst(true));
- AssertEquals(234, fIntList.First.Value);
- AssertEquals(234, fIntList.PopFirst(false));
- AssertEquals(123, fIntList.First.Value);
- AssertEquals( 0, fIntList.PopFirst(true));
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.Meth_PushLast_PopLast;
- begin
- fIntList.PushLast(123);
- AssertEquals(123, fIntList.Last.Value);
- fIntList.PushLast(234);
- AssertEquals(234, fIntList.Last.Value);
- fIntList.PushLast(345);
- AssertEquals(345, fIntList.Last.Value);
- fIntList.PushLast(456);
- AssertEquals(456, fIntList.Last.Value);
-
- AssertEquals(456, fIntList.PopLast(false));
- AssertEquals(345, fIntList.Last.Value);
- AssertEquals( 0, fIntList.PopLast(true));
- AssertEquals(234, fIntList.Last.Value);
- AssertEquals(234, fIntList.PopLast(false));
- AssertEquals(123, fIntList.Last.Value);
- AssertEquals( 0, fIntList.PopLast(true));
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.Meth_InsertBefore;
- var
- it: TIntList.Iterator;
- begin
- fIntList.PushLast(123);
- fIntList.PushLast(234);
- fIntList.PushLast(345);
- fIntList.PushLast(456);
-
- it := fIntList.First;
- fIntList.InsertBefore(it, 999);
- AssertTrue(it.MovePrev);
- AssertEquals(999, it.Value);
- AssertEquals(5, fIntList.Count);
-
- it := fIntList.Last;
- fIntList.InsertBefore(it, 888);
- AssertTrue(it.MovePrev);
- AssertEquals(888, it.Value);
- AssertEquals(6, fIntList.Count);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.Meth_InsertAfter;
- var
- it: TIntList.Iterator;
- begin
- fIntList.PushLast(123);
- fIntList.PushLast(234);
- fIntList.PushLast(345);
- fIntList.PushLast(456);
-
- it := fIntList.First;
- fIntList.InsertAfter(it, 999);
- AssertTrue(it.MoveNext);
- AssertEquals(999, it.Value);
- AssertEquals(5, fIntList.Count);
-
- it := fIntList.Last;
- fIntList.InsertAfter(it, 888);
- AssertTrue(it.MoveNext);
- AssertEquals(888, it.Value);
- AssertEquals(6, fIntList.Count);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.Meth_Remove;
- var
- it: TIntList.Iterator;
- begin
- fIntList.PushLast(123);
- fIntList.PushLast(234);
- fIntList.PushLast(345);
- fIntList.PushLast(456);
-
- it := fIntList.First;
- it.MoveNext;
- fIntList.Remove(it);
-
- AssertEquals(3, fIntList.Count);
- AssertEquals(123, fIntList.First.Value);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.Meth_Clear;
- begin
- fIntList.PushLast(123);
- fIntList.PushLast(234);
- fIntList.PushLast(345);
- fIntList.PushLast(456);
-
- AssertEquals(4, fIntList.Count);
- fIntList.Clear;
-
- AssertEquals(0, fIntList.Count);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TutlLinkedListTests.Iterator;
- var
- it1: TIntList.Iterator;
- begin
- fIntList.PushLast(123);
- fIntList.PushLast(234);
- fIntList.PushLast(345);
- fIntList.PushLast(456);
-
- it1 := fIntList.First;
- AssertEquals(123, it1.Value);
- AssertTrue (it1.IsValid);
- AssertTrue (it1.Equals(fIntList.First));
- AssertTrue (it1.MoveNext);
- AssertEquals(234, it1.Value);
- AssertTrue (it1.MoveNext);
- AssertEquals(345, it1.Value);
- AssertTrue (it1.MoveNext);
- AssertEquals(456, it1.Value);
- AssertTrue (it1.Equals(fIntList.Last));
- AssertFalse (it1.MoveNext);
- fIntList.PopLast;
- AssertFalse (it1.IsValid);
-
- it1 := fIntList.Last;
- AssertEquals(345, it1.Value);
- AssertTrue (it1.IsValid);
- AssertTrue (it1.Equals(fIntList.Last));
- AssertTrue (it1.MovePrev);
- AssertEquals(234, it1.Value);
- AssertTrue (it1.MovePrev);
- AssertEquals(123, it1.Value);
- AssertTrue (it1.Equals(fIntList.First));
- AssertFalse (it1.MovePrev);
- fIntList.PopFirst;
- AssertFalse (it1.IsValid);
- end;
-
- initialization
- RegisterTest(TutlLinkedListTests.Suite);
-
- end.
-
|