25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

431 lines
12 KiB

  1. unit uutlListTest;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, TestFramework, contnrs,
  6. uTestHelper, uutlGenerics;
  7. type
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TIntList = specialize TutlList<Integer>;
  10. TIntfList = specialize TutlList<IUnknown>;
  11. TObjList = specialize TutlList<TObject>;
  12. TutlListTest = class(TIntfObjOwner)
  13. private
  14. fIntList: TIntList;
  15. fIntfList: TIntfList;
  16. fObjList: TObjList;
  17. protected
  18. procedure SetUp; override;
  19. procedure TearDown; override;
  20. published
  21. procedure Prop_Count;
  22. procedure Prop_First;
  23. procedure Prop_last;
  24. procedure Prop_Items;
  25. procedure Meth_Add;
  26. procedure Meth_Insert;
  27. procedure Meth_Exchange;
  28. procedure Meth_Move;
  29. procedure Meth_Delete;
  30. procedure Meth_Extract;
  31. procedure Meth_PushFirst;
  32. procedure Meth_PopFirst;
  33. procedure Meth_PushLast;
  34. procedure Meth_PopLast;
  35. procedure Dtor_FreesAllItems;
  36. procedure Meth_IndexOf;
  37. procedure Meth_Extract_WithDefault;
  38. procedure Meth_Remove;
  39. procedure AddRemoveInterfaces;
  40. procedure AddRemoveObject_OwnedByList;
  41. procedure AddRemoveObject_NotOwnedByList;
  42. end;
  43. implementation
  44. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45. //TutlListTest//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  46. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. procedure TutlListTest.SetUp;
  48. begin
  49. inherited SetUp;
  50. fIntList := TIntList.Create(true);
  51. fIntfList := TIntfList.Create(true);
  52. fObjList := TObjList.Create(true);
  53. end;
  54. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  55. procedure TutlListTest.TearDown;
  56. begin
  57. FreeAndNil(fIntList);
  58. FreeAndNil(fIntfList);
  59. FreeAndNil(fObjList);
  60. inherited TearDown;
  61. end;
  62. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. procedure TutlListTest.Prop_Count;
  64. begin
  65. AssertEquals(0, fIntList.Count);
  66. fIntList.Add(123);
  67. AssertEquals(1, fIntList.Count);
  68. fIntList.Add(234);
  69. AssertEquals(2, fIntList.Count);
  70. fIntList.Add(345);
  71. AssertEquals(3, fIntList.Count);
  72. end;
  73. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. procedure TutlListTest.Prop_First;
  75. begin
  76. fIntList.Add(123);
  77. AssertEquals(123, fIntList.First);
  78. fIntList.Add(456);
  79. AssertEquals(123, fIntList.First);
  80. end;
  81. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  82. procedure TutlListTest.Prop_last;
  83. begin
  84. fIntList.Add(123);
  85. AssertEquals(123, fIntList.Last);
  86. fIntList.Add(456);
  87. AssertEquals(456, fIntList.Last);
  88. end;
  89. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  90. procedure TutlListTest.Prop_Items;
  91. begin
  92. fIntList.Add(123);
  93. fIntList.Add(234);
  94. fIntList.Add(345);
  95. fIntList.Add(456);
  96. AssertEquals(123, fIntList[0]);
  97. AssertEquals(234, fIntList[1]);
  98. AssertEquals(345, fIntList[2]);
  99. AssertEquals(456, fIntList[3]);
  100. end;
  101. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  102. procedure TutlListTest.Meth_Add;
  103. begin
  104. fIntList.Add(123);
  105. AssertEquals(fIntList.Count, 1);
  106. AssertEquals(123, fIntList[0]);
  107. end;
  108. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  109. procedure TutlListTest.Meth_Insert;
  110. begin
  111. fIntList.Insert(0, 123);
  112. fIntList.Insert(0, 456);
  113. AssertEquals(fIntList.Count, 2);
  114. AssertEquals(123, fIntList[1]);
  115. AssertEquals(456, fIntList[0]);
  116. end;
  117. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  118. procedure TutlListTest.Meth_Exchange;
  119. begin
  120. fIntList.Add(123);
  121. fIntList.Add(234);
  122. fIntList.Add(345);
  123. fIntList.Add(456);
  124. fIntList.Add(567);
  125. fIntList.Exchange(1, 3);
  126. AssertEquals(123, fIntList[0]);
  127. AssertEquals(456, fIntList[1]);
  128. AssertEquals(345, fIntList[2]);
  129. AssertEquals(234, fIntList[3]);
  130. AssertEquals(567, fIntList[4]);
  131. end;
  132. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. procedure TutlListTest.Meth_Move;
  134. begin
  135. fIntList.Add(123);
  136. fIntList.Add(234);
  137. fIntList.Add(345);
  138. fIntList.Add(456);
  139. fIntList.Add(567);
  140. fIntList.Move(1, 3);
  141. AssertEquals(123, fIntList[0]);
  142. AssertEquals(345, fIntList[1]);
  143. AssertEquals(456, fIntList[2]);
  144. AssertEquals(234, fIntList[3]);
  145. AssertEquals(567, fIntList[4]);
  146. end;
  147. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  148. procedure TutlListTest.Meth_Delete;
  149. begin
  150. fIntList.Add(123);
  151. fIntList.Add(234);
  152. fIntList.Add(345);
  153. fIntList.Add(456);
  154. fIntList.Add(567);
  155. fIntList.Delete(2);
  156. AssertEquals(4, fIntList.Count);
  157. AssertEquals(456, fIntList[2]);
  158. end;
  159. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  160. procedure TutlListTest.Meth_Extract;
  161. begin
  162. fIntList.Add(123);
  163. fIntList.Add(234);
  164. fIntList.Add(345);
  165. fIntList.Add(456);
  166. fIntList.Add(567);
  167. AssertEquals(234, fIntList.Extract(1));
  168. AssertEquals(4, fIntList.Count);
  169. end;
  170. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  171. procedure TutlListTest.Meth_PushFirst;
  172. begin
  173. fIntList.PushFirst(123);
  174. fIntList.PushFirst(234);
  175. AssertEquals(2, fIntList.Count);
  176. AssertEquals(123, fIntList[1]);
  177. AssertEquals(234, fIntList[0]);
  178. end;
  179. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  180. procedure TutlListTest.Meth_PopFirst;
  181. begin
  182. fIntList.Add(123);
  183. fIntList.Add(234);
  184. fIntList.Add(345);
  185. fIntList.Add(456);
  186. fIntList.Add(567);
  187. AssertEquals(123, fIntList.PopFirst(false));
  188. AssertEquals(234, fIntList.PopFirst(false));
  189. AssertEquals(3, fIntList.Count);
  190. end;
  191. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  192. procedure TutlListTest.Meth_PushLast;
  193. begin
  194. fIntList.PushLast(123);
  195. fIntList.PushLast(234);
  196. fIntList.PushLast(345);
  197. AssertEquals(3, fIntList.Count);
  198. AssertEquals(123, fIntList[0]);
  199. AssertEquals(234, fIntList[1]);
  200. AssertEquals(345, fIntList[2]);
  201. end;
  202. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  203. procedure TutlListTest.Meth_PopLast;
  204. begin
  205. fIntList.Add(123);
  206. fIntList.Add(234);
  207. fIntList.Add(345);
  208. fIntList.Add(456);
  209. fIntList.Add(567);
  210. AssertEquals(567, fIntList.PopLast(false));
  211. AssertEquals(456, fIntList.PopLast(false));
  212. AssertEquals(3, fIntList.Count);
  213. end;
  214. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  215. procedure TutlListTest.Dtor_FreesAllItems;
  216. begin
  217. fObjList.Add(TIntfObj.Create(self));
  218. fObjList.Add(TIntfObj.Create(self));
  219. FreeAndNil(fObjList);
  220. AssertEquals(0, IntfObjCounter);
  221. end;
  222. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  223. procedure TutlListTest.Meth_IndexOf;
  224. begin
  225. fIntList.Add(123);
  226. fIntList.Add(234);
  227. fIntList.Add(345);
  228. fIntList.Add(456);
  229. fIntList.Add(567);
  230. AssertEquals( 1, fIntList.IndexOf(234));
  231. AssertEquals( 3, fIntList.IndexOf(456));
  232. AssertEquals(-1, fIntList.IndexOf(999));
  233. end;
  234. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  235. procedure TutlListTest.Meth_Extract_WithDefault;
  236. begin
  237. fIntList.Add(123);
  238. fIntList.Add(234);
  239. fIntList.Add(345);
  240. fIntList.Add(456);
  241. fIntList.Add(567);
  242. AssertEquals(234, fIntList.Extract(234, 999));
  243. AssertEquals(999, fIntList.Extract(234, 999));
  244. end;
  245. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  246. procedure TutlListTest.Meth_Remove;
  247. begin
  248. fIntList.Add(123);
  249. fIntList.Add(234);
  250. fIntList.Add(345);
  251. fIntList.Add(456);
  252. fIntList.Add(567);
  253. fIntList.Remove(234);
  254. AssertEquals(4, fIntList.Count);
  255. AssertEquals(345, fIntList[1]);
  256. end;
  257. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  258. procedure TutlListTest.AddRemoveInterfaces;
  259. var
  260. i1: IUnknown;
  261. begin
  262. fIntfList.Add(TIntfObj.Create(self));
  263. fIntfList.Add(TIntfObj.Create(self));
  264. fIntfList.Add(TIntfObj.Create(self));
  265. fIntfList.Exchange(0, 2);
  266. fIntfList.Move(0, 2);
  267. fIntfList.Delete(0);
  268. fIntfList.Extract(0);
  269. fIntfList.Clear;
  270. fIntfList.Insert(0, TIntfObj.Create(self));
  271. fIntfList.PopLast(true);
  272. fIntfList.Insert(0, TIntfObj.Create(self));
  273. fIntfList.PopLast(false);
  274. fIntfList.Insert(0, TIntfObj.Create(self));
  275. fIntfList.PopFirst(true);
  276. fIntfList.Insert(0, TIntfObj.Create(self));
  277. fIntfList.PopFirst(false);
  278. i1 := TIntfObj.Create(self);
  279. fIntfList.Insert(0, i1);
  280. fIntfList.Extract(i1, nil);
  281. i1 := nil;
  282. i1 := TIntfObj.Create(self);
  283. fIntfList.Insert(0, i1);
  284. fIntfList.Remove(i1);
  285. i1 := nil;
  286. fIntfList.Add(TIntfObj.Create(self));
  287. fIntfList[0] := TIntfObj.Create(self);
  288. fIntfList.Clear;
  289. AssertEquals(0, IntfObjCounter);
  290. end;
  291. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  292. procedure TutlListTest.AddRemoveObject_OwnedByList;
  293. function CreateObj: TObject;
  294. begin
  295. result := TIntfObj.Create(self);
  296. end;
  297. begin
  298. fObjList.Add(CreateObj);
  299. fObjList.Add(CreateObj);
  300. fObjList.Add(CreateObj);
  301. fObjList.Exchange(0, 2);
  302. fObjList.Move(0, 2);
  303. fObjList.Delete(0);
  304. fObjList.Extract(0).Free;
  305. fObjList.Clear;
  306. fObjList.Add(CreateObj);
  307. fObjList.PopLast(true);
  308. fObjList.Add(CreateObj);
  309. fObjList.PopLast(false).Free;
  310. fObjList.Add(CreateObj);
  311. fObjList.PopFirst(true);
  312. fObjList.Add(CreateObj);
  313. fObjList.PopFirst(false).Free;
  314. fObjList.Add(CreateObj);
  315. fObjList.Extract(fObjList[0], nil).Free;
  316. fObjList.Add(CreateObj);
  317. fObjList.Remove(fObjList[0]);
  318. fObjList.Add(CreateObj);
  319. fObjList[0] := CreateObj;
  320. fObjList.Clear;
  321. AssertEquals(0, IntfObjCounter);
  322. end;
  323. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  324. procedure TutlListTest.AddRemoveObject_NotOwnedByList;
  325. var
  326. lst: TObjectList;
  327. function CreateObj: TObject;
  328. begin
  329. result := TIntfObj.Create(self);
  330. lst.Add(result);
  331. end;
  332. begin
  333. lst := TObjectList.Create(true);
  334. try
  335. fObjList.OwnsItems := false;
  336. fObjList.Add(CreateObj);
  337. fObjList.Add(CreateObj);
  338. fObjList.Add(CreateObj);
  339. fObjList.Exchange(0, 2);
  340. fObjList.Move(0, 2);
  341. fObjList.Delete(0);
  342. fObjList.Extract(0);
  343. fObjList.Clear;
  344. fObjList.Add(CreateObj);
  345. fObjList.PopLast(true);
  346. fObjList.Add(CreateObj);
  347. fObjList.PopLast(false);
  348. fObjList.Add(CreateObj);
  349. fObjList.PopFirst(true);
  350. fObjList.Add(CreateObj);
  351. fObjList.PopFirst(false);
  352. fObjList.Add(CreateObj);
  353. fObjList.Extract(fObjList[0], nil);
  354. fObjList.Add(CreateObj);
  355. fObjList.Remove(fObjList[0]);
  356. fObjList.Add(CreateObj);
  357. fObjList[0] := CreateObj;
  358. fObjList.Clear;
  359. finally
  360. FreeAndNil(lst);
  361. end;
  362. AssertEquals(0, IntfObjCounter);
  363. end;
  364. initialization
  365. RegisterTest(TutlListTest.Suite);
  366. end.