utcfpobjecthashtable.pp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. unit utcFPObjectHashTable;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, contnrs, punit;
  6. procedure RegisterTests;
  7. implementation
  8. type
  9. TMyObject = class(TObject)
  10. IsFreed: ^Boolean;
  11. destructor Destroy; override;
  12. end;
  13. destructor TMyObject.Destroy;
  14. begin
  15. if Assigned(IsFreed) then
  16. IsFreed^ := True;
  17. inherited Destroy;
  18. end;
  19. Function TFPObjectHashTable_TestCreate : TTestString;
  20. var
  21. HT: TFPObjectHashTable;
  22. begin
  23. Result:='';
  24. HT := TFPObjectHashTable.Create;
  25. try
  26. AssertNotNull('Hash table should be created', HT);
  27. AssertEquals('Count should be 0 on creation', 0, HT.Count);
  28. AssertTrue('IsEmpty should be true on creation', HT.IsEmpty);
  29. AssertTrue('OwnsObjects should be true by default', HT.OwnsObjects);
  30. finally
  31. HT.Free;
  32. end;
  33. end;
  34. Function TFPObjectHashTable_TestAdd : TTestString;
  35. var
  36. HT: TFPObjectHashTable;
  37. O1, O2: TObject;
  38. begin
  39. Result:='';
  40. HT := TFPObjectHashTable.Create(False);
  41. try
  42. O1 := TObject.Create;
  43. O2 := TObject.Create;
  44. HT.Add('Key1', O1);
  45. AssertEquals('Count should be 1 after adding one item', 1, HT.Count);
  46. AssertFalse('IsEmpty should be false after adding an item', HT.IsEmpty);
  47. AssertSame('Items property should return correct value', O1, HT.Items['Key1']);
  48. HT.Add('Key2', O2);
  49. AssertEquals('Count should be 2 after adding a second item', 2, HT.Count);
  50. AssertSame('Items property should return correct value for second item', O2, HT.Items['Key2']);
  51. finally
  52. HT.Free;
  53. O1.Free;
  54. O2.Free;
  55. end;
  56. end;
  57. Function TFPObjectHashTable_TestDelete : TTestString;
  58. var
  59. HT: TFPObjectHashTable;
  60. O1, O2: TObject;
  61. begin
  62. Result:='';
  63. HT := TFPObjectHashTable.Create(False);
  64. try
  65. O1 := TObject.Create;
  66. O2 := TObject.Create;
  67. HT.Add('Key1', O1);
  68. HT.Add('Key2', O2);
  69. HT.Delete('Key1');
  70. AssertEquals('Count should be 1 after deleting an item', 1, HT.Count);
  71. AssertNull('Accessing deleted key should return nil', HT.Items['Key1']);
  72. AssertSame('Other item should still exist', O2, HT.Items['Key2']);
  73. finally
  74. HT.Free;
  75. O1.Free;
  76. O2.Free;
  77. end;
  78. end;
  79. Function TFPObjectHashTable_TestClear : TTestString;
  80. var
  81. HT: TFPObjectHashTable;
  82. O1, O2: TObject;
  83. begin
  84. Result:='';
  85. HT := TFPObjectHashTable.Create(False);
  86. try
  87. O1 := TObject.Create;
  88. O2 := TObject.Create;
  89. HT.Add('Key1', O1);
  90. HT.Add('Key2', O2);
  91. HT.Clear;
  92. AssertEquals('Count should be 0 after clearing', 0, HT.Count);
  93. AssertTrue('IsEmpty should be true after clearing', HT.IsEmpty);
  94. finally
  95. HT.Free;
  96. O1.Free;
  97. O2.Free;
  98. end;
  99. end;
  100. Function TFPObjectHashTable_TestItemsProperty : TTestString;
  101. var
  102. HT: TFPObjectHashTable;
  103. O1, O2: TObject;
  104. begin
  105. Result:='';
  106. HT := TFPObjectHashTable.Create(False);
  107. try
  108. O1 := TObject.Create;
  109. O2 := TObject.Create;
  110. HT.Items['Key1'] := O1;
  111. AssertEquals('Count should be 1 after setting item', 1, HT.Count);
  112. AssertSame('Items property should return correct value', O1, HT.Items['Key1']);
  113. HT.Items['Key1'] := O2;
  114. AssertEquals('Count should still be 1 after updating item', 1, HT.Count);
  115. AssertSame('Items property should return updated value', O2, HT.Items['Key1']);
  116. finally
  117. HT.Free;
  118. O1.Free;
  119. O2.Free;
  120. end;
  121. end;
  122. Function TFPObjectHashTable_TestFind : TTestString;
  123. var
  124. HT: TFPObjectHashTable;
  125. O1: TObject;
  126. Node: THTCustomNode;
  127. begin
  128. Result:='';
  129. HT := TFPObjectHashTable.Create(False);
  130. try
  131. O1 := TObject.Create;
  132. HT.Add('Key1', O1);
  133. Node := HT.Find('Key1');
  134. AssertNotNull('Find should return a node for an existing key', Node);
  135. if Node <> nil then
  136. begin
  137. AssertEquals('Node should have the correct key', 'Key1', Node.Key);
  138. AssertSame('Node data should be correct', O1, THTObjectNode(Node).Data);
  139. end;
  140. Node := HT.Find('NonExistentKey');
  141. AssertNull('Find should return nil for a non-existent key', Node);
  142. finally
  143. HT.Free;
  144. O1.Free;
  145. end;
  146. end;
  147. Function TFPObjectHashTable_TestOwnsObjects : TTestString;
  148. var
  149. HT: TFPObjectHashTable;
  150. O1: TMyObject;
  151. Freed: Boolean;
  152. begin
  153. Result:='';
  154. HT := TFPObjectHashTable.Create(True);
  155. Freed := False;
  156. O1 := TMyObject.Create;
  157. O1.IsFreed := @Freed;
  158. HT.Add('Key1', O1);
  159. HT.Free; // This should free O1 as well
  160. AssertTrue('Object should be freed when OwnsObjects is true and hash table is freed', Freed);
  161. end;
  162. procedure RegisterTests;
  163. begin
  164. AddSuite('TFPObjectHashTableTests');
  165. AddTest('TestCreate', @TFPObjectHashTable_TestCreate, 'TFPObjectHashTableTests');
  166. AddTest('TestAdd', @TFPObjectHashTable_TestAdd, 'TFPObjectHashTableTests');
  167. AddTest('TestDelete', @TFPObjectHashTable_TestDelete, 'TFPObjectHashTableTests');
  168. AddTest('TestClear', @TFPObjectHashTable_TestClear, 'TFPObjectHashTableTests');
  169. AddTest('TestItemsProperty', @TFPObjectHashTable_TestItemsProperty, 'TFPObjectHashTableTests');
  170. AddTest('TestFind', @TFPObjectHashTable_TestFind, 'TFPObjectHashTableTests');
  171. AddTest('TestOwnsObjects', @TFPObjectHashTable_TestOwnsObjects, 'TFPObjectHashTableTests');
  172. end;
  173. end.