unit uutlMapTests; {$mode objfpc}{$H+} interface uses Classes, SysUtils, TestFramework, uTestHelper, uutlGenerics; type //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TIntMap = specialize TutlMap; TObjMap = specialize TutlMap; TutlMapTests = class(TIntfObjOwner) private fIntMap: TIntMap; fObjMap: TObjMap; procedure AssignNonExistsingItem; protected procedure SetUp; override; procedure TearDown; override; published procedure Prop_Values; procedure Prop_ValuesAt; procedure Prop_Keys; procedure Prop_KeyValuePairs; procedure Prop_Count; procedure Prop_IsEmpty; procedure Prop_Capacity; procedure Prop_CanShrink; procedure Prop_CanExpand; procedure Prop_OwnsKeys; procedure Prop_OwnsValues; procedure Prop_AutoCreate; procedure Meth_Add; procedure Meth_TryGetValue; procedure Meth_IndexOf; procedure Meth_Contains; procedure Meth_Delete; procedure Meth_DeleteAt; procedure Meth_Clear; end; implementation //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TutlMapTests////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.AssignNonExistsingItem; begin fIntMap[999] := 123; end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.SetUp; begin inherited SetUp; fIntMap := TIntMap.Create(true, true); fObjMap := TObjMap.Create(true, true); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.TearDown; begin FreeAndNil(fIntMap); FreeAndNil(fObjMap); inherited TearDown; end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Prop_Values; begin fIntMap.Add(123, 987); fIntMap.Add(234, 876); AssertEquals(987, fIntMap[123]); AssertEquals(876, fIntMap[234]); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Prop_ValuesAt; begin fIntMap.Add(123, 987); fIntMap.Add(234, 876); AssertEquals(987, fIntMap.ValueAt[0]); AssertEquals(876, fIntMap.ValueAt[1]); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Prop_Keys; begin fIntMap.Add(123, 987); fIntMap.Add(234, 876); AssertEquals(2, fIntMap.Keys.Count); AssertEquals(123, fIntMap.Keys[0]); AssertEquals(234, fIntMap.Keys[1]); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Prop_KeyValuePairs; begin fIntMap.Add(123, 987); fIntMap.Add(234, 876); AssertEquals(2, fIntMap.KeyValuePairs.Count); AssertEquals(123, fIntMap.KeyValuePairs[0].Key); AssertEquals(987, fIntMap.KeyValuePairs[0].Value); AssertEquals(234, fIntMap.KeyValuePairs[1].Key); AssertEquals(876, fIntMap.KeyValuePairs[1].Value); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Prop_Count; begin AssertEquals(0, fIntMap.Count); fIntMap.Add(123, 987); AssertEquals(1, fIntMap.Count); fIntMap.Add(234, 876); AssertEquals(2, fIntMap.Count); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Prop_IsEmpty; begin AssertTrue(fIntMap.IsEmpty); fIntMap.Add(123, 987); AssertFalse(fIntMap.IsEmpty); fIntMap.Add(234, 876); AssertFalse(fIntMap.IsEmpty); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Prop_Capacity; begin AssertEquals(0, fIntMap.Capacity); fIntMap.Capacity := 10; AssertEquals(10, fIntMap.Capacity); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Prop_CanShrink; begin AssertTrue(fIntMap.CanShrink); fIntMap.CanShrink := false; AssertFalse(fIntMap.CanShrink); fIntMap.CanShrink := true; AssertTrue(fIntMap.CanShrink); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Prop_CanExpand; begin AssertTrue(fIntMap.CanExpand); fIntMap.CanExpand := false; AssertFalse(fIntMap.CanExpand); fIntMap.CanExpand := true; AssertTrue(fIntMap.CanExpand); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Prop_OwnsKeys; var obj: TIntfObj; begin AssertTrue(fObjMap.OwnsKeys); fObjMap.OwnsKeys := false; AssertFalse(fObjMap.OwnsKeys); obj := TIntfObj.Create(self); fObjMap.Add(obj, nil); fObjMap.Delete(obj); AssertEquals(1, IntfObjCounter); FreeAndNil(obj); AssertEquals(0, IntfObjCounter); fObjMap.OwnsKeys := true; AssertTrue(fObjMap.OwnsKeys); obj := TIntfObj.Create(self); fObjMap.Add(obj, nil); fObjMap.Delete(obj); AssertEquals(0, IntfObjCounter); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Prop_OwnsValues; var key: TIntfObj; obj: TIntfObj; begin key := TIntfObj.Create(self); AssertTrue(fObjMap.OwnsValues); fObjMap.OwnsValues := false; AssertFalse(fObjMap.OwnsValues); obj := TIntfObj.Create(self); fObjMap.Add(key, obj); fObjMap.Delete(key); AssertEquals(1, IntfObjCounter); FreeAndNil(obj); AssertEquals(0, IntfObjCounter); fObjMap.OwnsValues := true; AssertTrue(fObjMap.OwnsValues); key := TIntfObj.Create(self); obj := TIntfObj.Create(self); fObjMap.Add(key, obj); fObjMap.Delete(key); AssertEquals(0, IntfObjCounter); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Prop_AutoCreate; begin AssertException('autocreate false does not throw exception', EInvalidOperation, @AssignNonExistsingItem); fIntMap.AutoCreate := true; AssignNonExistsingItem; end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Meth_Add; begin fIntMap.Add(123, 987); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Meth_TryGetValue; var i: Integer; begin fIntMap.Add(123, 987); fIntMap.Add(234, 876); fIntMap.Add(345, 765); AssertFalse (fIntMap.TryGetValue(999, i)); AssertTrue (fIntMap.TryGetValue(234, i)); AssertEquals(876, i); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Meth_IndexOf; begin fIntMap.Add(123, 987); fIntMap.Add(234, 876); fIntMap.Add(345, 765); AssertEquals( 0, fIntMap.IndexOf(123)); AssertEquals( 1, fIntMap.IndexOf(234)); AssertEquals( 2, fIntMap.IndexOf(345)); AssertEquals(-1, fIntMap.IndexOf(999)); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Meth_Contains; begin fIntMap.Add(123, 987); fIntMap.Add(234, 876); fIntMap.Add(345, 765); AssertTrue (fIntMap.Contains(123)); AssertTrue (fIntMap.Contains(234)); AssertTrue (fIntMap.Contains(345)); AssertFalse(fIntMap.Contains(999)); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Meth_Delete; begin fIntMap.Add(123, 987); fIntMap.Add(234, 876); fIntMap.Add(345, 765); AssertEquals(3, fIntMap.Count); fIntMap.Delete(123); AssertEquals(2, fIntMap.Count); fIntMap.Delete(234); AssertEquals(1, fIntMap.Count); fIntMap.Delete(345); AssertEquals(0, fIntMap.Count); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Meth_DeleteAt; begin fIntMap.Add(123, 987); fIntMap.Add(234, 876); fIntMap.Add(345, 765); AssertEquals(3, fIntMap.Count); fIntMap.DeleteAt(2); AssertEquals(2, fIntMap.Count); fIntMap.DeleteAt(1); AssertEquals(1, fIntMap.Count); fIntMap.DeleteAt(0); AssertEquals(0, fIntMap.Count); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlMapTests.Meth_Clear; begin fIntMap.Add(123, 987); fIntMap.Add(234, 876); fIntMap.Add(345, 765); fIntMap.Clear; AssertEquals(0, fIntMap.Count); end; initialization RegisterTest(TutlMapTests.Suite); end.