You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
10 KiB

  1. unit uutlMapTests;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, TestFramework,
  6. uTestHelper, uutlGenerics;
  7. type
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TIntMap = specialize TutlMap<Integer, Integer>;
  10. TObjMap = specialize TutlMap<TIntfObj, TIntfObj>;
  11. TutlMapTests = class(TIntfObjOwner)
  12. private
  13. fIntMap: TIntMap;
  14. fObjMap: TObjMap;
  15. procedure AssignNonExistsingItem;
  16. protected
  17. procedure SetUp; override;
  18. procedure TearDown; override;
  19. published
  20. procedure Prop_Values;
  21. procedure Prop_ValuesAt;
  22. procedure Prop_Keys;
  23. procedure Prop_KeyValuePairs;
  24. procedure Prop_Count;
  25. procedure Prop_IsEmpty;
  26. procedure Prop_Capacity;
  27. procedure Prop_CanShrink;
  28. procedure Prop_CanExpand;
  29. procedure Prop_OwnsKeys;
  30. procedure Prop_OwnsValues;
  31. procedure Prop_AutoCreate;
  32. procedure Meth_Add;
  33. procedure Meth_TryGetValue;
  34. procedure Meth_IndexOf;
  35. procedure Meth_Contains;
  36. procedure Meth_Delete;
  37. procedure Meth_DeleteAt;
  38. procedure Meth_Clear;
  39. end;
  40. implementation
  41. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. //TutlMapTests//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  43. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  44. procedure TutlMapTests.AssignNonExistsingItem;
  45. begin
  46. fIntMap[999] := 123;
  47. end;
  48. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. procedure TutlMapTests.SetUp;
  50. begin
  51. inherited SetUp;
  52. fIntMap := TIntMap.Create(true, true);
  53. fObjMap := TObjMap.Create(true, true);
  54. end;
  55. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. procedure TutlMapTests.TearDown;
  57. begin
  58. FreeAndNil(fIntMap);
  59. FreeAndNil(fObjMap);
  60. inherited TearDown;
  61. end;
  62. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. procedure TutlMapTests.Prop_Values;
  64. begin
  65. fIntMap.Add(123, 987);
  66. fIntMap.Add(234, 876);
  67. AssertEquals(987, fIntMap[123]);
  68. AssertEquals(876, fIntMap[234]);
  69. end;
  70. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  71. procedure TutlMapTests.Prop_ValuesAt;
  72. begin
  73. fIntMap.Add(123, 987);
  74. fIntMap.Add(234, 876);
  75. AssertEquals(987, fIntMap.ValueAt[0]);
  76. AssertEquals(876, fIntMap.ValueAt[1]);
  77. end;
  78. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  79. procedure TutlMapTests.Prop_Keys;
  80. begin
  81. fIntMap.Add(123, 987);
  82. fIntMap.Add(234, 876);
  83. AssertEquals(2, fIntMap.Keys.Count);
  84. AssertEquals(123, fIntMap.Keys[0]);
  85. AssertEquals(234, fIntMap.Keys[1]);
  86. end;
  87. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. procedure TutlMapTests.Prop_KeyValuePairs;
  89. begin
  90. fIntMap.Add(123, 987);
  91. fIntMap.Add(234, 876);
  92. AssertEquals(2, fIntMap.KeyValuePairs.Count);
  93. AssertEquals(123, fIntMap.KeyValuePairs[0].Key);
  94. AssertEquals(987, fIntMap.KeyValuePairs[0].Value);
  95. AssertEquals(234, fIntMap.KeyValuePairs[1].Key);
  96. AssertEquals(876, fIntMap.KeyValuePairs[1].Value);
  97. end;
  98. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  99. procedure TutlMapTests.Prop_Count;
  100. begin
  101. AssertEquals(0, fIntMap.Count);
  102. fIntMap.Add(123, 987);
  103. AssertEquals(1, fIntMap.Count);
  104. fIntMap.Add(234, 876);
  105. AssertEquals(2, fIntMap.Count);
  106. end;
  107. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  108. procedure TutlMapTests.Prop_IsEmpty;
  109. begin
  110. AssertTrue(fIntMap.IsEmpty);
  111. fIntMap.Add(123, 987);
  112. AssertFalse(fIntMap.IsEmpty);
  113. fIntMap.Add(234, 876);
  114. AssertFalse(fIntMap.IsEmpty);
  115. end;
  116. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  117. procedure TutlMapTests.Prop_Capacity;
  118. begin
  119. AssertEquals(0, fIntMap.Capacity);
  120. fIntMap.Capacity := 10;
  121. AssertEquals(10, fIntMap.Capacity);
  122. end;
  123. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  124. procedure TutlMapTests.Prop_CanShrink;
  125. begin
  126. AssertTrue(fIntMap.CanShrink);
  127. fIntMap.CanShrink := false;
  128. AssertFalse(fIntMap.CanShrink);
  129. fIntMap.CanShrink := true;
  130. AssertTrue(fIntMap.CanShrink);
  131. end;
  132. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. procedure TutlMapTests.Prop_CanExpand;
  134. begin
  135. AssertTrue(fIntMap.CanExpand);
  136. fIntMap.CanExpand := false;
  137. AssertFalse(fIntMap.CanExpand);
  138. fIntMap.CanExpand := true;
  139. AssertTrue(fIntMap.CanExpand);
  140. end;
  141. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  142. procedure TutlMapTests.Prop_OwnsKeys;
  143. var
  144. obj: TIntfObj;
  145. begin
  146. AssertTrue(fObjMap.OwnsKeys);
  147. fObjMap.OwnsKeys := false;
  148. AssertFalse(fObjMap.OwnsKeys);
  149. obj := TIntfObj.Create(self);
  150. fObjMap.Add(obj, nil);
  151. fObjMap.Delete(obj);
  152. AssertEquals(1, IntfObjCounter);
  153. FreeAndNil(obj);
  154. AssertEquals(0, IntfObjCounter);
  155. fObjMap.OwnsKeys := true;
  156. AssertTrue(fObjMap.OwnsKeys);
  157. obj := TIntfObj.Create(self);
  158. fObjMap.Add(obj, nil);
  159. fObjMap.Delete(obj);
  160. AssertEquals(0, IntfObjCounter);
  161. end;
  162. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  163. procedure TutlMapTests.Prop_OwnsValues;
  164. var
  165. key: TIntfObj;
  166. obj: TIntfObj;
  167. begin
  168. key := TIntfObj.Create(self);
  169. AssertTrue(fObjMap.OwnsValues);
  170. fObjMap.OwnsValues := false;
  171. AssertFalse(fObjMap.OwnsValues);
  172. obj := TIntfObj.Create(self);
  173. fObjMap.Add(key, obj);
  174. fObjMap.Delete(key);
  175. AssertEquals(1, IntfObjCounter);
  176. FreeAndNil(obj);
  177. AssertEquals(0, IntfObjCounter);
  178. fObjMap.OwnsValues := true;
  179. AssertTrue(fObjMap.OwnsValues);
  180. key := TIntfObj.Create(self);
  181. obj := TIntfObj.Create(self);
  182. fObjMap.Add(key, obj);
  183. fObjMap.Delete(key);
  184. AssertEquals(0, IntfObjCounter);
  185. end;
  186. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  187. procedure TutlMapTests.Prop_AutoCreate;
  188. begin
  189. AssertException('autocreate false does not throw exception', EInvalidOperation, @AssignNonExistsingItem);
  190. fIntMap.AutoCreate := true;
  191. AssignNonExistsingItem;
  192. end;
  193. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  194. procedure TutlMapTests.Meth_Add;
  195. begin
  196. fIntMap.Add(123, 987);
  197. end;
  198. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  199. procedure TutlMapTests.Meth_TryGetValue;
  200. var
  201. i: Integer;
  202. begin
  203. fIntMap.Add(123, 987);
  204. fIntMap.Add(234, 876);
  205. fIntMap.Add(345, 765);
  206. AssertFalse (fIntMap.TryGetValue(999, i));
  207. AssertTrue (fIntMap.TryGetValue(234, i));
  208. AssertEquals(876, i);
  209. end;
  210. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  211. procedure TutlMapTests.Meth_IndexOf;
  212. begin
  213. fIntMap.Add(123, 987);
  214. fIntMap.Add(234, 876);
  215. fIntMap.Add(345, 765);
  216. AssertEquals( 0, fIntMap.IndexOf(123));
  217. AssertEquals( 1, fIntMap.IndexOf(234));
  218. AssertEquals( 2, fIntMap.IndexOf(345));
  219. AssertEquals(-1, fIntMap.IndexOf(999));
  220. end;
  221. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  222. procedure TutlMapTests.Meth_Contains;
  223. begin
  224. fIntMap.Add(123, 987);
  225. fIntMap.Add(234, 876);
  226. fIntMap.Add(345, 765);
  227. AssertTrue (fIntMap.Contains(123));
  228. AssertTrue (fIntMap.Contains(234));
  229. AssertTrue (fIntMap.Contains(345));
  230. AssertFalse(fIntMap.Contains(999));
  231. end;
  232. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  233. procedure TutlMapTests.Meth_Delete;
  234. begin
  235. fIntMap.Add(123, 987);
  236. fIntMap.Add(234, 876);
  237. fIntMap.Add(345, 765);
  238. AssertEquals(3, fIntMap.Count);
  239. fIntMap.Delete(123);
  240. AssertEquals(2, fIntMap.Count);
  241. fIntMap.Delete(234);
  242. AssertEquals(1, fIntMap.Count);
  243. fIntMap.Delete(345);
  244. AssertEquals(0, fIntMap.Count);
  245. end;
  246. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  247. procedure TutlMapTests.Meth_DeleteAt;
  248. begin
  249. fIntMap.Add(123, 987);
  250. fIntMap.Add(234, 876);
  251. fIntMap.Add(345, 765);
  252. AssertEquals(3, fIntMap.Count);
  253. fIntMap.DeleteAt(2);
  254. AssertEquals(2, fIntMap.Count);
  255. fIntMap.DeleteAt(1);
  256. AssertEquals(1, fIntMap.Count);
  257. fIntMap.DeleteAt(0);
  258. AssertEquals(0, fIntMap.Count);
  259. end;
  260. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  261. procedure TutlMapTests.Meth_Clear;
  262. begin
  263. fIntMap.Add(123, 987);
  264. fIntMap.Add(234, 876);
  265. fIntMap.Add(345, 765);
  266. fIntMap.Clear;
  267. AssertEquals(0, fIntMap.Count);
  268. end;
  269. initialization
  270. RegisterTest(TutlMapTests.Suite);
  271. end.