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.

281 lines
8.9 KiB

  1. unit uutlLinkedListTests;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, TestFramework,
  6. uTestHelper, uutlGenerics, uutlExceptions;
  7. type
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TIntList = specialize TutlLinkedList<Integer>;
  10. TutlLinkedListTests = class(TTestCase)
  11. private
  12. fIntList: TIntList;
  13. procedure AccessPropFirst;
  14. procedure AccessPropLast;
  15. protected
  16. procedure SetUp; override;
  17. procedure TearDown; override;
  18. published
  19. procedure Prop_Count;
  20. procedure Prop_IsEmpty;
  21. procedure Prop_First;
  22. procedure Prop_Last;
  23. procedure Meth_PushFirst_PopFirst;
  24. procedure Meth_PushLast_PopLast;
  25. procedure Meth_InsertBefore;
  26. procedure Meth_InsertAfter;
  27. procedure Meth_Remove;
  28. procedure Meth_Clear;
  29. procedure Iterator;
  30. end;
  31. implementation
  32. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  33. //TutlLinkedListTests///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. procedure TutlLinkedListTests.AccessPropFirst;
  36. begin
  37. fIntList.First;
  38. end;
  39. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. procedure TutlLinkedListTests.AccessPropLast;
  41. begin
  42. fIntList.Last;
  43. end;
  44. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45. procedure TutlLinkedListTests.SetUp;
  46. begin
  47. inherited SetUp;
  48. fIntList := TIntList.Create(true);
  49. end;
  50. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  51. procedure TutlLinkedListTests.TearDown;
  52. begin
  53. FreeAndNil(fIntList);
  54. inherited TearDown;
  55. end;
  56. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. procedure TutlLinkedListTests.Prop_Count;
  58. begin
  59. AssertEquals(0, fIntList.Count);
  60. fIntList.PushFirst(123);
  61. AssertEquals(1, fIntList.Count);
  62. fIntList.PushFirst(234);
  63. AssertEquals(2, fIntList.Count);
  64. fIntList.PopFirst(true);
  65. AssertEquals(1, fIntList.Count);
  66. fIntList.PopFirst(true);
  67. AssertEquals(0, fIntList.Count);
  68. end;
  69. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  70. procedure TutlLinkedListTests.Prop_IsEmpty;
  71. begin
  72. AssertEquals(true, fIntList.IsEmpty);
  73. fIntList.PushFirst(123);
  74. AssertEquals(false, fIntList.IsEmpty);
  75. end;
  76. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. procedure TutlLinkedListTests.Prop_First;
  78. var
  79. i: TIntList.Iterator;
  80. begin
  81. AssertException('empty list does not raise exception when accessing First property', EutlInvalidOperation, @AccessPropFirst);
  82. fIntList.PushLast(123);
  83. fIntList.PushLast(234);
  84. i := fIntList.First;
  85. AssertEquals(123, i.Value);
  86. end;
  87. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. procedure TutlLinkedListTests.Prop_Last;
  89. var
  90. i: TIntList.Iterator;
  91. begin
  92. AssertException('empty list does not raise exception when accessing First property', EutlInvalidOperation, @AccessPropLast);
  93. fIntList.PushLast(123);
  94. fIntList.PushLast(234);
  95. i := fIntList.Last;
  96. AssertEquals(234, i.Value);
  97. end;
  98. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  99. procedure TutlLinkedListTests.Meth_PushFirst_PopFirst;
  100. begin
  101. fIntList.PushFirst(123);
  102. AssertEquals(123, fIntList.First.Value);
  103. fIntList.PushFirst(234);
  104. AssertEquals(234, fIntList.First.Value);
  105. fIntList.PushFirst(345);
  106. AssertEquals(345, fIntList.First.Value);
  107. fIntList.PushFirst(456);
  108. AssertEquals(456, fIntList.First.Value);
  109. AssertEquals(456, fIntList.PopFirst(false));
  110. AssertEquals(345, fIntList.First.Value);
  111. AssertEquals( 0, fIntList.PopFirst(true));
  112. AssertEquals(234, fIntList.First.Value);
  113. AssertEquals(234, fIntList.PopFirst(false));
  114. AssertEquals(123, fIntList.First.Value);
  115. AssertEquals( 0, fIntList.PopFirst(true));
  116. end;
  117. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  118. procedure TutlLinkedListTests.Meth_PushLast_PopLast;
  119. begin
  120. fIntList.PushLast(123);
  121. AssertEquals(123, fIntList.Last.Value);
  122. fIntList.PushLast(234);
  123. AssertEquals(234, fIntList.Last.Value);
  124. fIntList.PushLast(345);
  125. AssertEquals(345, fIntList.Last.Value);
  126. fIntList.PushLast(456);
  127. AssertEquals(456, fIntList.Last.Value);
  128. AssertEquals(456, fIntList.PopLast(false));
  129. AssertEquals(345, fIntList.Last.Value);
  130. AssertEquals( 0, fIntList.PopLast(true));
  131. AssertEquals(234, fIntList.Last.Value);
  132. AssertEquals(234, fIntList.PopLast(false));
  133. AssertEquals(123, fIntList.Last.Value);
  134. AssertEquals( 0, fIntList.PopLast(true));
  135. end;
  136. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  137. procedure TutlLinkedListTests.Meth_InsertBefore;
  138. var
  139. it: TIntList.Iterator;
  140. begin
  141. fIntList.PushLast(123);
  142. fIntList.PushLast(234);
  143. fIntList.PushLast(345);
  144. fIntList.PushLast(456);
  145. it := fIntList.First;
  146. fIntList.InsertBefore(it, 999);
  147. AssertTrue(it.MovePrev);
  148. AssertEquals(999, it.Value);
  149. AssertEquals(5, fIntList.Count);
  150. it := fIntList.Last;
  151. fIntList.InsertBefore(it, 888);
  152. AssertTrue(it.MovePrev);
  153. AssertEquals(888, it.Value);
  154. AssertEquals(6, fIntList.Count);
  155. end;
  156. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  157. procedure TutlLinkedListTests.Meth_InsertAfter;
  158. var
  159. it: TIntList.Iterator;
  160. begin
  161. fIntList.PushLast(123);
  162. fIntList.PushLast(234);
  163. fIntList.PushLast(345);
  164. fIntList.PushLast(456);
  165. it := fIntList.First;
  166. fIntList.InsertAfter(it, 999);
  167. AssertTrue(it.MoveNext);
  168. AssertEquals(999, it.Value);
  169. AssertEquals(5, fIntList.Count);
  170. it := fIntList.Last;
  171. fIntList.InsertAfter(it, 888);
  172. AssertTrue(it.MoveNext);
  173. AssertEquals(888, it.Value);
  174. AssertEquals(6, fIntList.Count);
  175. end;
  176. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  177. procedure TutlLinkedListTests.Meth_Remove;
  178. var
  179. it: TIntList.Iterator;
  180. begin
  181. fIntList.PushLast(123);
  182. fIntList.PushLast(234);
  183. fIntList.PushLast(345);
  184. fIntList.PushLast(456);
  185. it := fIntList.First;
  186. it.MoveNext;
  187. fIntList.Remove(it);
  188. AssertEquals(3, fIntList.Count);
  189. AssertEquals(123, fIntList.First.Value);
  190. end;
  191. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  192. procedure TutlLinkedListTests.Meth_Clear;
  193. begin
  194. fIntList.PushLast(123);
  195. fIntList.PushLast(234);
  196. fIntList.PushLast(345);
  197. fIntList.PushLast(456);
  198. AssertEquals(4, fIntList.Count);
  199. fIntList.Clear;
  200. AssertEquals(0, fIntList.Count);
  201. end;
  202. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  203. procedure TutlLinkedListTests.Iterator;
  204. var
  205. it1: TIntList.Iterator;
  206. begin
  207. fIntList.PushLast(123);
  208. fIntList.PushLast(234);
  209. fIntList.PushLast(345);
  210. fIntList.PushLast(456);
  211. it1 := fIntList.First;
  212. AssertEquals(123, it1.Value);
  213. AssertTrue (it1.IsValid);
  214. AssertTrue (it1.Equals(fIntList.First));
  215. AssertTrue (it1.MoveNext);
  216. AssertEquals(234, it1.Value);
  217. AssertTrue (it1.MoveNext);
  218. AssertEquals(345, it1.Value);
  219. AssertTrue (it1.MoveNext);
  220. AssertEquals(456, it1.Value);
  221. AssertTrue (it1.Equals(fIntList.Last));
  222. AssertFalse (it1.MoveNext);
  223. fIntList.PopLast;
  224. AssertFalse (it1.IsValid);
  225. it1 := fIntList.Last;
  226. AssertEquals(345, it1.Value);
  227. AssertTrue (it1.IsValid);
  228. AssertTrue (it1.Equals(fIntList.Last));
  229. AssertTrue (it1.MovePrev);
  230. AssertEquals(234, it1.Value);
  231. AssertTrue (it1.MovePrev);
  232. AssertEquals(123, it1.Value);
  233. AssertTrue (it1.Equals(fIntList.First));
  234. AssertFalse (it1.MovePrev);
  235. fIntList.PopFirst;
  236. AssertFalse (it1.IsValid);
  237. end;
  238. initialization
  239. RegisterTest(TutlLinkedListTests.Suite);
  240. end.