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.

265 lines
8.1 KiB

  1. unit uutlStackTests;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, TestFramework, Contnrs,
  6. uTestHelper, uutlGenerics;
  7. type
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TIntStack = specialize TutlStack<Integer>;
  10. TIntfStack = specialize TutlStack<IUnknown>;
  11. TObjStack = specialize TutlStack<TObject>;
  12. TutlStackTests = class(TIntfObjOwner)
  13. private
  14. fIntStack: TIntStack;
  15. fIntfStack: TIntfStack;
  16. fObjStack: TObjStack;
  17. protected
  18. procedure SetUp; override;
  19. procedure TearDown; override;
  20. published
  21. procedure Prop_Count;
  22. procedure Prop_Empty;
  23. procedure Meth_Peek;
  24. procedure Meth_PushPop1000Times;
  25. procedure Meth_PushPop1000Items;
  26. procedure Meth_PushPop1000Items1000Times;
  27. procedure Meth_PushPop100Interfaces;
  28. procedure Meth_PushPop100_ObjectOwned_WithFree;
  29. procedure Meth_PushPop100_ObjectOwnedByStack_WithoutFree;
  30. procedure Meth_PushPop100_ObjectNotOwned_WithFree;
  31. procedure Meth_PushPop100_ObjectNotOwned_WithoutFree;
  32. procedure Dtor_FreesAllItems;
  33. end;
  34. implementation
  35. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. //TutlStackTests////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. procedure TutlStackTests.SetUp;
  39. begin
  40. inherited SetUp;
  41. fIntStack := TIntStack.Create(true);
  42. fIntfStack := TIntfStack.Create(true);
  43. fObjStack := TObjStack.Create(true);
  44. end;
  45. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  46. procedure TutlStackTests.TearDown;
  47. begin
  48. FreeAndNil(fIntStack);
  49. FreeAndNil(fIntfStack);
  50. FreeAndNil(fObjStack);
  51. inherited TearDown;
  52. end;
  53. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. procedure TutlStackTests.Prop_Count;
  55. begin
  56. AssertEquals(0, fIntStack.Count);
  57. fIntStack.Push(123);
  58. AssertEquals(1, fIntStack.Count);
  59. fIntStack.Push(234);
  60. AssertEquals(2, fIntStack.Count);
  61. fIntStack.Push(345);
  62. AssertEquals(3, fIntStack.Count);
  63. fIntStack.Pop;
  64. AssertEquals(2, fIntStack.Count);
  65. fIntStack.Pop;
  66. AssertEquals(1, fIntStack.Count);
  67. fIntStack.Pop;
  68. AssertEquals(0, fIntStack.Count);
  69. end;
  70. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  71. procedure TutlStackTests.Prop_Empty;
  72. begin
  73. AssertEquals(true, fIntStack.IsEmpty);
  74. fIntStack.Push(345);
  75. AssertEquals(false, fIntStack.IsEmpty);
  76. fIntStack.Pop;
  77. AssertEquals(true, fIntStack.IsEmpty);
  78. end;
  79. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  80. procedure TutlStackTests.Meth_Peek;
  81. begin
  82. fIntStack.Push(123);
  83. AssertEquals(123, fIntStack.Peek);
  84. end;
  85. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  86. procedure TutlStackTests.Meth_PushPop1000Times;
  87. var
  88. i, tmp: Integer;
  89. begin
  90. for i := 0 to 1000 do begin
  91. fIntStack.Push(i);
  92. tmp := fIntStack.Pop;
  93. AssertEquals(i, tmp);
  94. end;
  95. end;
  96. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  97. procedure TutlStackTests.Meth_PushPop1000Items;
  98. var
  99. i, tmp: Integer;
  100. begin
  101. for i := 0 to 1000 do
  102. fIntStack.Push(i);
  103. for i := 0 to 1000 do begin
  104. tmp := fIntStack.Pop;
  105. AssertEquals(1000-i, tmp);
  106. end;
  107. end;
  108. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  109. procedure TutlStackTests.Meth_PushPop1000Items1000Times;
  110. var
  111. i, j, tmp: Integer;
  112. begin
  113. for j := 0 to 1000 do begin
  114. for i := 0 to 1000 do
  115. fIntStack.Push(i);
  116. for i := 0 to 1000 do begin
  117. tmp := fIntStack.Pop;
  118. AssertEquals(1000-i, tmp);
  119. end;
  120. end;
  121. end;
  122. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  123. procedure TutlStackTests.Meth_PushPop100Interfaces;
  124. var
  125. i: Integer;
  126. begin
  127. for i := 0 to 100 do begin
  128. fIntfStack.Push(TIntfObj.Create(self));
  129. AssertEquals(i+1, IntfObjCounter);
  130. end;
  131. for i := 0 to 100 do begin
  132. fIntfStack.Pop;
  133. AssertEquals(100 - i, IntfObjCounter);
  134. end;
  135. end;
  136. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  137. procedure TutlStackTests.Meth_PushPop100_ObjectOwned_WithFree;
  138. var
  139. i: Integer;
  140. begin
  141. for i := 0 to 100 do begin
  142. fObjStack.Push(TIntfObj.Create(self));
  143. AssertEquals(i+1, IntfObjCounter);
  144. end;
  145. for i := 0 to 100 do begin
  146. AssertNull('Pop returned non-zero item', fObjStack.Pop(true));
  147. AssertEquals(100 - i, IntfObjCounter);
  148. end;
  149. end;
  150. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  151. procedure TutlStackTests.Meth_PushPop100_ObjectOwnedByStack_WithoutFree;
  152. var
  153. i: Integer;
  154. lst: TObjectList;
  155. obj: TObject;
  156. begin
  157. for i := 0 to 100 do begin
  158. fObjStack.Push(TIntfObj.Create(self));
  159. AssertEquals(i+1, IntfObjCounter);
  160. end;
  161. for i := 0 to 100 do begin
  162. fObjStack.Pop(true);
  163. AssertEquals(100 - i, IntfObjCounter);
  164. end;
  165. // free on Pop
  166. for i := 0 to 100 do begin
  167. fObjStack.Push(TIntfObj.Create(self));
  168. AssertEquals(i+1, IntfObjCounter);
  169. end;
  170. lst := TObjectList.Create(true);
  171. try
  172. for i := 0 to 100 do begin
  173. obj := fObjStack.Pop(false);
  174. AssertNotNull('Pop returned zero item', obj);
  175. lst.Add(obj);
  176. AssertEquals(101, IntfObjCounter);
  177. end;
  178. finally
  179. FreeAndNil(lst);
  180. end;
  181. end;
  182. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  183. procedure TutlStackTests.Meth_PushPop100_ObjectNotOwned_WithFree;
  184. var
  185. lst: TObjectList;
  186. obj: TObject;
  187. i: Integer;
  188. begin
  189. lst := TObjectList.Create(true);
  190. try
  191. for i := 0 to 100 do begin
  192. obj := TIntfObj.Create(self);
  193. lst.Add(obj);
  194. fObjStack.Push(obj);
  195. AssertEquals(i+1, IntfObjCounter);
  196. end;
  197. fObjStack.OwnsItems := false;
  198. for i := 0 to 100 do begin
  199. AssertNull('Pop returned non-zero item', fObjStack.Pop(true));
  200. AssertEquals(101, IntfObjCounter);
  201. end;
  202. finally
  203. FreeAndNil(lst);
  204. end;
  205. end;
  206. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  207. procedure TutlStackTests.Meth_PushPop100_ObjectNotOwned_WithoutFree;
  208. var
  209. lst: TObjectList;
  210. obj: TObject;
  211. i: Integer;
  212. begin
  213. lst := TObjectList.Create(true);
  214. try
  215. for i := 0 to 100 do begin
  216. obj := TIntfObj.Create(self);
  217. lst.Add(obj);
  218. fObjStack.Push(obj);
  219. AssertEquals(i+1, IntfObjCounter);
  220. end;
  221. fObjStack.OwnsItems := false;
  222. for i := 0 to 100 do begin
  223. AssertNotNull('Pop returned zero item', fObjStack.Pop(false));
  224. AssertEquals(101, IntfObjCounter);
  225. end;
  226. finally
  227. FreeAndNil(lst);
  228. end;
  229. end;
  230. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  231. procedure TutlStackTests.Dtor_FreesAllItems;
  232. begin
  233. fObjStack.Push(TIntfObj.Create(self));
  234. FreeAndNil(fObjStack);
  235. AssertEquals(0, IntfObjCounter);
  236. end;
  237. initialization
  238. RegisterTest(TutlStackTests.Suite);
  239. end.