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.

313 line
10 KiB

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