|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- unit uutlMapTests;
-
- {$mode objfpc}{$H+}
-
- interface
-
- uses
- Classes, SysUtils, TestFramework,
- uTestHelper, uutlGenerics;
-
- type
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- TIntMap = specialize TutlMap<Integer, Integer>;
- TObjMap = specialize TutlMap<TIntfObj, TIntfObj>;
- 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.
-
|