2
0

tclinkedlist.pp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. unit tclinkedlist;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testutils, testregistry, fplists;
  6. type
  7. { TTestLinkedList }
  8. TTestLinkedList= class(TTestCase)
  9. published
  10. procedure TestCreate;
  11. procedure TestAdd;
  12. procedure TestAdd2;
  13. procedure TestClear;
  14. procedure TestRemove;
  15. procedure TestRemove2;
  16. procedure TestRemove3;
  17. Procedure TestVisit;
  18. end;
  19. implementation
  20. procedure TTestLinkedList.TestCreate;
  21. Var
  22. LL : TLinkedList;
  23. begin
  24. LL:=TLinkedList.Create(TLinkedListItem);
  25. Try
  26. AssertEquals('Item class is TLinkedListItem.',TLinkedListItem,LL.ItemClass);
  27. AssertEquals('Item count is 0',0,LL.Count);
  28. If (LL.Root<>Nil) then
  29. Fail('Root is not nil')
  30. Finally
  31. LL.Free;
  32. end;
  33. end;
  34. procedure TTestLinkedList.TestAdd;
  35. Var
  36. LL : TLinkedList;
  37. I : TLinkedListItem;
  38. begin
  39. LL:=TLinkedList.Create(TLinkedListItem);
  40. Try
  41. I:=LL.Add;
  42. AssertEquals('Add result is TLinkedListItem.',TLinkedListItem,I.ClassType);
  43. AssertEquals('Item count is 1',1,LL.Count);
  44. If (I<>LL.Root) then
  45. Fail('Root item is not added item');
  46. Finally
  47. LL.Free;
  48. end;
  49. end;
  50. procedure TTestLinkedList.TestClear;
  51. Var
  52. LL : TLinkedList;
  53. I : Integer;
  54. begin
  55. LL:=TLinkedList.Create(TLinkedListItem);
  56. Try
  57. For I:=1 to 3 do
  58. LL.Add;
  59. LL.Clear;
  60. AssertEquals('Item count after clear is 0',0,LL.Count);
  61. Finally
  62. LL.Free;
  63. end;
  64. end;
  65. procedure TTestLinkedList.TestAdd2;
  66. Var
  67. LL : TLinkedList;
  68. I1,I2 : TLinkedListItem;
  69. begin
  70. LL:=TLinkedList.Create(TLinkedListItem);
  71. Try
  72. I1:=LL.Add;
  73. I2:=LL.Add;
  74. If (I2<>LL.Root) then
  75. Fail('Root item is not last added item');
  76. If (I2.Next<>I1) then
  77. Fail('Items ordered in the wrong way');
  78. Finally
  79. LL.Free;
  80. end;
  81. end;
  82. procedure TTestLinkedList.TestRemove;
  83. Var
  84. LL : TLinkedList;
  85. I : TLinkedListItem;
  86. begin
  87. LL:=TLinkedList.Create(TLinkedListItem);
  88. Try
  89. I:=LL.Add;
  90. Try
  91. LL.RemoveItem(I);
  92. AssertEquals('After remove Item count is 0',0,LL.Count);
  93. If (Nil<>LL.Root) then
  94. Fail('Root item is not nil after last removed item');
  95. Finally
  96. I.Free;
  97. end;
  98. Finally
  99. LL.Free;
  100. end;
  101. end;
  102. procedure TTestLinkedList.TestRemove2;
  103. Var
  104. LL : TLinkedList;
  105. I1,I2 : TLinkedListItem;
  106. begin
  107. LL:=TLinkedList.Create(TLinkedListItem);
  108. Try
  109. I1:=LL.Add;
  110. Try
  111. I2:=LL.Add;
  112. LL.RemoveItem(I1);
  113. AssertEquals('After remove first Item count is 1',1,LL.Count);
  114. If (I2<>LL.Root) then
  115. Fail('Root item is not I2 after remove of I1');
  116. Finally
  117. I1.Free;
  118. end;
  119. Finally
  120. LL.Free;
  121. end;
  122. end;
  123. procedure TTestLinkedList.TestRemove3;
  124. Var
  125. LL : TLinkedList;
  126. I1,I2, I3 : TLinkedListItem;
  127. begin
  128. LL:=TLinkedList.Create(TLinkedListItem);
  129. Try
  130. I1:=LL.Add;
  131. I2:=LL.Add;
  132. I3:=LL.Add;
  133. LL.RemoveItem(I2);
  134. Try
  135. AssertEquals('After remove I2 Item count is 2',2,LL.Count);
  136. If (I3.Next<>I1) then
  137. Fail('After Remove of I2, I3.Next<>I1');
  138. Finally
  139. I2.Free;
  140. end;
  141. Finally
  142. LL.Free;
  143. end;
  144. end;
  145. Type
  146. { TCountVisitor }
  147. TCountVisitor = Class(TLinkedListVisitor)
  148. FCount : integer;
  149. FMax : integer;
  150. Function Visit(Item : TLinkedListItem) : Boolean; override;
  151. Constructor Create(AMax : integer);
  152. end;
  153. { TCountVisitor }
  154. function TCountVisitor.Visit(Item: TLinkedListItem): Boolean;
  155. begin
  156. Inc(FCount);
  157. Result:=(FMax=-1) or (FCount<FMax);
  158. end;
  159. constructor TCountVisitor.Create(AMax: integer);
  160. begin
  161. FMax:=AMax;
  162. end;
  163. procedure TTestLinkedList.TestVisit;
  164. Var
  165. I : Integer;
  166. V : TCountVisitor;
  167. LL : TLinkedList;
  168. begin
  169. LL:=TLinkedList.Create(TLinkedListItem);
  170. Try
  171. For I:=1 to 5 do
  172. LL.Add;
  173. V:=TCountVisitor.Create(-1);
  174. Try
  175. LL.Foreach(V);
  176. AssertEquals('Counter visited all items',5,V.FCount);
  177. Finally
  178. V.Free;
  179. end;
  180. V:=TCountVisitor.Create(3);
  181. Try
  182. LL.Foreach(V);
  183. AssertEquals('Counter visited 3 items',3,V.FCount);
  184. Finally
  185. V.Free;
  186. end;
  187. Finally
  188. LL.Free;
  189. end;
  190. end;
  191. initialization
  192. RegisterTest(TTestLinkedList);
  193. end.