utcfpstringhashtable.pp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. unit utcFPStringHashTable;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, contnrs, punit;
  6. procedure RegisterTests;
  7. implementation
  8. Function TFPStringHashTable_TestCreate : TTestString;
  9. var
  10. HT: TFPStringHashTable;
  11. begin
  12. Result:='';
  13. HT := TFPStringHashTable.Create;
  14. try
  15. AssertNotNull('Hash table should be created', HT);
  16. AssertEquals('Count should be 0 on creation', 0, HT.Count);
  17. AssertTrue('IsEmpty should be true on creation', HT.IsEmpty);
  18. finally
  19. HT.Free;
  20. end;
  21. end;
  22. Function TFPStringHashTable_TestAdd : TTestString;
  23. var
  24. HT: TFPStringHashTable;
  25. begin
  26. Result:='';
  27. HT := TFPStringHashTable.Create;
  28. try
  29. HT.Add('Key1', 'Value1');
  30. AssertEquals('Count should be 1 after adding one item', 1, HT.Count);
  31. AssertFalse('IsEmpty should be false after adding an item', HT.IsEmpty);
  32. AssertEquals('Items property should return correct value', 'Value1', HT.Items['Key1']);
  33. HT.Add('Key2', 'Value2');
  34. AssertEquals('Count should be 2 after adding a second item', 2, HT.Count);
  35. AssertEquals('Items property should return correct value for second item', 'Value2', HT.Items['Key2']);
  36. finally
  37. HT.Free;
  38. end;
  39. end;
  40. Function TFPStringHashTable_TestDelete : TTestString;
  41. var
  42. HT: TFPStringHashTable;
  43. begin
  44. Result:='';
  45. HT := TFPStringHashTable.Create;
  46. try
  47. HT.Add('Key1', 'Value1');
  48. HT.Add('Key2', 'Value2');
  49. HT.Delete('Key1');
  50. AssertEquals('Count should be 1 after deleting an item', 1, HT.Count);
  51. AssertEquals('Accessing deleted key should return empty string', '', HT.Items['Key1']);
  52. AssertEquals('Other item should still exist', 'Value2', HT.Items['Key2']);
  53. finally
  54. HT.Free;
  55. end;
  56. end;
  57. Function TFPStringHashTable_TestClear : TTestString;
  58. var
  59. HT: TFPStringHashTable;
  60. begin
  61. Result:='';
  62. HT := TFPStringHashTable.Create;
  63. try
  64. HT.Add('Key1', 'Value1');
  65. HT.Add('Key2', 'Value2');
  66. HT.Clear;
  67. AssertEquals('Count should be 0 after clearing', 0, HT.Count);
  68. AssertTrue('IsEmpty should be true after clearing', HT.IsEmpty);
  69. finally
  70. HT.Free;
  71. end;
  72. end;
  73. Function TFPStringHashTable_TestItemsProperty : TTestString;
  74. var
  75. HT: TFPStringHashTable;
  76. begin
  77. Result:='';
  78. HT := TFPStringHashTable.Create;
  79. try
  80. HT.Items['Key1'] := 'Value1';
  81. AssertEquals('Count should be 1 after setting item', 1, HT.Count);
  82. AssertEquals('Items property should return correct value', 'Value1', HT.Items['Key1']);
  83. HT.Items['Key1'] := 'NewValue1';
  84. AssertEquals('Count should still be 1 after updating item', 1, HT.Count);
  85. AssertEquals('Items property should return updated value', 'NewValue1', HT.Items['Key1']);
  86. finally
  87. HT.Free;
  88. end;
  89. end;
  90. Function TFPStringHashTable_TestFind : TTestString;
  91. var
  92. HT: TFPStringHashTable;
  93. Node: THTCustomNode;
  94. begin
  95. Result:='';
  96. HT := TFPStringHashTable.Create;
  97. try
  98. HT.Add('Key1', 'Value1');
  99. Node := HT.Find('Key1');
  100. AssertNotNull('Find should return a node for an existing key', Node);
  101. if Node <> nil then
  102. AssertEquals('Node should have the correct key', 'Key1', Node.Key);
  103. Node := HT.Find('NonExistentKey');
  104. AssertNull('Find should return nil for a non-existent key', Node);
  105. finally
  106. HT.Free;
  107. end;
  108. end;
  109. procedure RegisterTests;
  110. begin
  111. AddSuite('TFPStringHashTableTests');
  112. AddTest('TestCreate', @TFPStringHashTable_TestCreate, 'TFPStringHashTableTests');
  113. AddTest('TestAdd', @TFPStringHashTable_TestAdd, 'TFPStringHashTableTests');
  114. AddTest('TestDelete', @TFPStringHashTable_TestDelete, 'TFPStringHashTableTests');
  115. AddTest('TestClear', @TFPStringHashTable_TestClear, 'TFPStringHashTableTests');
  116. AddTest('TestItemsProperty', @TFPStringHashTable_TestItemsProperty, 'TFPStringHashTableTests');
  117. AddTest('TestFind', @TFPStringHashTable_TestFind, 'TFPStringHashTableTests');
  118. end;
  119. end.