tcgenericlist.pp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. unit tcgenericlist;
  2. {$mode objfpc}
  3. interface
  4. uses
  5. fpcunit, testregistry, Classes, SysUtils, Generics.Defaults, Generics.Collections;
  6. Type
  7. TMySimpleList = Class(Specialize TList<String>);
  8. {$IFDEF FPC}
  9. EList = EListError;
  10. {$ENDIF}
  11. { TTestSimpleList }
  12. TTestSimpleList = Class(TTestCase)
  13. Private
  14. FList : TMySimpleList;
  15. FnotifyMessage : String;
  16. FCurrentValueNotify : Integer;
  17. FExpectValues : Array of String;
  18. FExpectValueAction: Array of TCollectionNotification;
  19. procedure DoAdd(aCount: Integer; aOffset: Integer=0);
  20. procedure DoAdd2;
  21. Procedure DoneExpectValues;
  22. procedure DoGetValue(aKey: Integer; Match: String; ExceptionClass: TClass=nil);
  23. procedure DoValueNotify(ASender: TObject; {$ifdef fpc}constref{$else}const{$endif} AItem: String; AAction: TCollectionNotification);
  24. Public
  25. Procedure SetExpectValues(aMessage : string; AKeys : Array of String; AActions : Array of TCollectionNotification; DoReverse : Boolean = False);
  26. Procedure SetUp; override;
  27. Procedure TearDown; override;
  28. Property List : TMySimpleList Read FList;
  29. Published
  30. Procedure TestEmpty;
  31. Procedure TestAdd;
  32. Procedure TestClear;
  33. Procedure TestGetValue;
  34. Procedure TestSetValue;
  35. Procedure TestContainsValue;
  36. Procedure TestDelete;
  37. Procedure TestToArray;
  38. Procedure TestEnumerator;
  39. procedure TestValueNotification;
  40. procedure TestValueNotificationDelete;
  41. procedure TestValueNotificationSet;
  42. end;
  43. { TMyObject }
  44. TMyObject = Class(TObject)
  45. Private
  46. fOnDestroy : TNotifyEvent;
  47. FID : Integer;
  48. public
  49. Constructor Create(aID : Integer; aOnDestroy : TNotifyEvent);
  50. destructor destroy; override;
  51. Property ID : Integer Read FID;
  52. end;
  53. TSingleObjectList = Class(Specialize TObjectList<TMyObject>);
  54. { TTestSingleObjectList }
  55. TTestSingleObjectList = Class(TTestCase)
  56. private
  57. FOList: TSingleObjectList;
  58. FList : TFPList;
  59. procedure DoAdd(aID: Integer);
  60. procedure DoDestroy(Sender: TObject);
  61. Public
  62. Procedure SetUp; override;
  63. Procedure TearDown; override;
  64. Property List : TSingleObjectList Read FOList;
  65. Published
  66. Procedure TestEmpty;
  67. Procedure TestFreeOnRemove;
  68. Procedure TestNoFreeOnRemove;
  69. Procedure TestFreeOnDelete;
  70. Procedure TestNoFreeDelete;
  71. end;
  72. implementation
  73. { TTestSingleObjectList }
  74. procedure TTestSingleObjectList.SetUp;
  75. begin
  76. FOList:=TSingleObjectList.Create(True);
  77. FList:=TFPList.Create;
  78. inherited SetUp;
  79. end;
  80. procedure TTestSingleObjectList.TearDown;
  81. Var
  82. I : Integer;
  83. A : TObject;
  84. begin
  85. for I:=0 to FList.Count-1 do
  86. begin
  87. A:=TObject(FList[i]);
  88. A.Free;
  89. end;
  90. FreeAndNil(FList);
  91. FreeAndNil(FOList);
  92. inherited TearDown;
  93. end;
  94. procedure TTestSingleObjectList.TestEmpty;
  95. begin
  96. AssertNotNull('Have object',List);
  97. AssertEquals('Have empty object',0,List.Count);
  98. end;
  99. procedure TTestSingleObjectList.DoAdd(aID : Integer);
  100. Var
  101. O : TMyObject;
  102. begin
  103. O:=TMyObject.Create(aID,@DoDestroy);
  104. FOList.Add(O);
  105. FList.Add(O);
  106. end;
  107. procedure TTestSingleObjectList.DoDestroy(Sender: TObject);
  108. Var
  109. I : Integer;
  110. begin
  111. I:=FList.IndexOf(Sender);
  112. AssertTrue('Have object in list',I<>-1);
  113. FList.Delete(I);
  114. end;
  115. procedure TTestSingleObjectList.TestFreeOnRemove;
  116. begin
  117. DoAdd(1);
  118. AssertEquals('Have obj',1,FList.Count);
  119. List.Remove(TMyObject(FList[0]));
  120. AssertEquals('Have no obj',0,FList.Count);
  121. end;
  122. procedure TTestSingleObjectList.TestNoFreeOnRemove;
  123. begin
  124. List.OwnsObjects:=False;
  125. DoAdd(1);
  126. AssertEquals('Have obj',1,FList.Count);
  127. List.Remove(TMyObject(FList[0]));
  128. AssertEquals('Have obj',1,FList.Count);
  129. end;
  130. procedure TTestSingleObjectList.TestFreeOnDelete;
  131. begin
  132. DoAdd(1);
  133. AssertEquals('Have obj',1,FList.Count);
  134. List.Delete(0);
  135. AssertEquals('Have no obj',0,FList.Count);
  136. end;
  137. procedure TTestSingleObjectList.TestNoFreeDelete;
  138. begin
  139. List.OwnsObjects:=False;
  140. DoAdd(1);
  141. AssertEquals('Have obj',1,FList.Count);
  142. List.Delete(0);
  143. AssertEquals('Have obj',1,FList.Count);
  144. end;
  145. { TMyObject }
  146. constructor TMyObject.Create(aID: Integer; aOnDestroy: TNotifyEvent);
  147. begin
  148. FOnDestroy:=aOnDestroy;
  149. FID:=AID;
  150. end;
  151. destructor TMyObject.destroy;
  152. begin
  153. if Assigned(FOnDestroy) then
  154. FOnDestroy(Self);
  155. inherited destroy;
  156. end;
  157. { TTestSimpleList }
  158. procedure TTestSimpleList.SetUp;
  159. begin
  160. inherited SetUp;
  161. FList:=TMySimpleList.Create;
  162. FCurrentValueNotify:=0;
  163. FExpectValues:=[];
  164. FExpectValueAction:=[];
  165. end;
  166. procedure TTestSimpleList.TearDown;
  167. begin
  168. // So we don't get clear messages
  169. FList.OnNotify:=Nil;
  170. FreeAndNil(FList);
  171. inherited TearDown;
  172. end;
  173. procedure TTestSimpleList.TestEmpty;
  174. begin
  175. AssertNotNull('Have dictionary',List);
  176. AssertEquals('empty dictionary',0,List.Count);
  177. end;
  178. procedure TTestSimpleList.DoAdd(aCount : Integer; aOffset : Integer=0);
  179. Var
  180. I : Integer;
  181. begin
  182. if aOffset=-1 then
  183. aOffset:=List.Count;
  184. For I:=aOffset+1 to aOffset+aCount do
  185. List.Add(IntToStr(i));
  186. end;
  187. procedure TTestSimpleList.TestAdd;
  188. begin
  189. DoAdd(1);
  190. AssertEquals('Count OK',1,List.Count);
  191. AssertTrue('Has added value',List.Contains('1'));
  192. DoAdd(1,1);
  193. AssertEquals('Count OK',2,List.Count);
  194. AssertTrue('Has added value',List.Contains('2'));
  195. end;
  196. procedure TTestSimpleList.TestClear;
  197. begin
  198. DoAdd(3);
  199. AssertEquals('Count OK',3,List.Count);
  200. List.Clear;
  201. AssertEquals('Count after clear OK',0,List.Count);
  202. end;
  203. procedure TTestSimpleList.DoGetValue(aKey: Integer; Match: String; ExceptionClass: TClass);
  204. Var
  205. EC : TClass;
  206. A,EM : String;
  207. begin
  208. EC:=Nil;
  209. try
  210. A:=List.Items[aKey];
  211. except
  212. On E : Exception do
  213. begin
  214. EC:=E.ClassType;
  215. EM:=E.Message;
  216. end
  217. end;
  218. if ExceptionClass=Nil then
  219. begin
  220. if EC<>Nil then
  221. Fail('Got exception '+EC.ClassName+' with message: '+EM);
  222. AssertEquals('Value is correct for '+IntToStr(aKey),Match,A)
  223. end
  224. else
  225. begin
  226. if EC=Nil then
  227. Fail('Expected exception '+ExceptionClass.ClassName+' but got none');
  228. if EC<>ExceptionClass then
  229. Fail('Expected exception class '+ExceptionClass.ClassName+' but got '+EC.ClassName+' with message '+EM);
  230. end;
  231. end;
  232. procedure TTestSimpleList.DoValueNotify(ASender: TObject; {$ifdef fpc}constref{$else}const{$endif} AItem: String; AAction: TCollectionNotification);
  233. begin
  234. // Writeln(FnotifyMessage+' value Notification',FCurrentValueNotify);
  235. AssertSame(FnotifyMessage+' value Correct sender', FList,aSender);
  236. if (FCurrentValueNotify>=Length(FExpectValues)) then
  237. Fail(FnotifyMessage+' Too many value notificiations');
  238. AssertEquals(FnotifyMessage+' Notification value no '+IntToStr(FCurrentValueNotify),FExpectValues[FCurrentValueNotify],aItem);
  239. Inc(FCurrentValueNotify);
  240. end;
  241. procedure TTestSimpleList.SetExpectValues(aMessage: string; AKeys: array of String;
  242. AActions: array of TCollectionNotification; DoReverse: Boolean);
  243. Var
  244. I,L : integer;
  245. begin
  246. FnotifyMessage:=aMessage;
  247. FCurrentValueNotify:=0;
  248. L:=Length(aKeys);
  249. AssertEquals('SetExpectValues: Lengths arrays equal',l,Length(aActions));
  250. SetLength(FExpectValues,L);
  251. SetLength(FExpectValueAction,L);
  252. Dec(L);
  253. if DoReverse then
  254. For I:=0 to L do
  255. begin
  256. FExpectValues[L-i]:=AKeys[i];
  257. FExpectValueAction[L-i]:=AActions[I];
  258. end
  259. else
  260. For I:=0 to L do
  261. begin
  262. FExpectValues[i]:=AKeys[i];
  263. FExpectValueAction[i]:=AActions[I];
  264. end;
  265. end;
  266. procedure TTestSimpleList.TestGetValue;
  267. Var
  268. I : integer;
  269. begin
  270. DoAdd(3);
  271. For I:=1 to 3 do
  272. DoGetValue(i-1,IntToStr(I));
  273. DoGetValue(3,'4',EArgumentOutOfRangeException);
  274. end;
  275. procedure TTestSimpleList.TestSetValue;
  276. begin
  277. TestGetValue;
  278. List.Items[1]:='Six';
  279. DoGetValue(1,'Six');
  280. end;
  281. procedure TTestSimpleList.DoAdd2;
  282. begin
  283. List.Add('A new 2');
  284. end;
  285. procedure TTestSimpleList.DoneExpectValues;
  286. begin
  287. AssertEquals(FnotifyMessage+' Expected number of values seen',Length(FExpectValues),FCurrentValueNotify);
  288. end;
  289. procedure TTestSimpleList.TestContainsValue;
  290. Var
  291. I : Integer;
  292. begin
  293. DoAdd(3);
  294. For I:=1 to 3 do
  295. AssertTrue('Has '+IntToStr(i),List.Contains(IntToStr(i)));
  296. AssertFalse('Has not 4',List.Contains('4'));
  297. end;
  298. procedure TTestSimpleList.TestDelete;
  299. begin
  300. DoAdd(3);
  301. List.Remove('2');
  302. AssertEquals('Count',2,List.Count);
  303. AssertFalse('Has not 2',List.Contains('2'));
  304. end;
  305. procedure TTestSimpleList.TestToArray;
  306. Var
  307. A : specialize TArray<String>;
  308. I : Integer;
  309. SI : String;
  310. begin
  311. DoAdd(3);
  312. A:=List.ToArray;
  313. AssertEquals('Length Ok',3,Length(A));
  314. For I:=1 to 3 do
  315. begin
  316. SI:=IntToStr(I);
  317. AssertEquals('Value '+SI,SI,A[i-1]);
  318. end;
  319. end;
  320. procedure TTestSimpleList.TestEnumerator;
  321. Var
  322. A : String;
  323. I : Integer;
  324. SI : String;
  325. begin
  326. DoAdd(3);
  327. I:=1;
  328. For A in List do
  329. begin
  330. SI:=IntToStr(I);
  331. AssertEquals('Value '+SI,SI,A);
  332. Inc(I);
  333. end;
  334. end;
  335. procedure TTestSimpleList.TestValueNotification;
  336. begin
  337. List.OnNotify:=@DoValueNotify;
  338. SetExpectValues('Add',['1','2','3'],[cnAdded,cnAdded,cnAdded]);
  339. DoAdd(3);
  340. DoneExpectValues;
  341. end;
  342. procedure TTestSimpleList.TestValueNotificationDelete;
  343. begin
  344. DoAdd(3);
  345. List.OnNotify:=@DoValueNotify;
  346. SetExpectValues('Clear',['1','2','3'],[cnRemoved,cnRemoved,cnRemoved],{$IFDEF FPC}true{$ELSE}False{$endif});
  347. List.Clear;
  348. DoneExpectValues;
  349. end;
  350. procedure TTestSimpleList.TestValueNotificationSet;
  351. begin
  352. DoAdd(3);
  353. List.OnNotify:=@DoValueNotify;
  354. SetExpectValues('Set',['2','Six'],[cnRemoved,cnAdded]);
  355. List[1]:='Six';
  356. DoneExpectValues;
  357. end;
  358. begin
  359. RegisterTests([ TTestSimpleList,TTestSingleObjectList]);
  360. end.