unit uutlLinkedListTests; {$mode objfpc}{$H+} interface uses Classes, SysUtils, TestFramework, uTestHelper, uutlGenerics, uutlExceptions; type //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TIntList = specialize TutlLinkedList; 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.