tcgenericdictionary.pp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. unit tcgenericdictionary;
  2. {$mode objfpc}
  3. interface
  4. uses
  5. fpcunit, testregistry, Classes, SysUtils, Generics.Defaults, Generics.Collections;
  6. Type
  7. TMySimpleDict = Class(Specialize TDictionary<Integer,String>);
  8. {$IFDEF FPC}
  9. EDictionary = EListError;
  10. TMyPair = specialize TPair<Integer,String>;
  11. {$ENDIF}
  12. { TTestSimpleDictionary }
  13. TTestSimpleDictionary = Class(TTestCase)
  14. Private
  15. FDict : TMySimpleDict;
  16. FnotifyMessage : String;
  17. FCurrentKeyNotify : Integer;
  18. FCurrentValueNotify : Integer;
  19. FExpectKeys : Array of Integer;
  20. FExpectValues : Array of String;
  21. FExpectValueAction,
  22. FExpectKeyAction: Array of TCollectionNotification;
  23. procedure DoAdd(aCount: Integer; aOffset: Integer=0);
  24. procedure DoAdd2;
  25. Procedure DoneExpectKeys;
  26. Procedure DoneExpectValues;
  27. procedure DoGetValue(aKey: Integer; Match: String; ExceptionClass: TClass=nil);
  28. procedure DoKeyNotify(ASender: TObject; {$ifdef fpc}constref{$else}const{$endif} AItem: Integer; AAction: TCollectionNotification);
  29. procedure DoValueNotify(ASender: TObject; {$ifdef fpc}constref{$else}const{$endif} AItem: String; AAction: TCollectionNotification);
  30. Public
  31. Procedure SetExpectKeys(aMessage : string; AKeys : Array of Integer; AActions : Array of TCollectionNotification; DoReverse : Boolean = False);
  32. Procedure SetExpectValues(aMessage : string; AKeys : Array of String; AActions : Array of TCollectionNotification; DoReverse : Boolean = False);
  33. Procedure SetUp; override;
  34. Procedure TearDown; override;
  35. Property Dict : TMySimpleDict Read FDict;
  36. Published
  37. Procedure TestEmpty;
  38. Procedure TestAdd;
  39. Procedure TestClear;
  40. Procedure TestTryGetValue;
  41. Procedure TestGetValue;
  42. Procedure TestSetValue;
  43. Procedure TestAddDuplicate;
  44. Procedure TestAddOrSet;
  45. Procedure TestContainsKey;
  46. Procedure TestContainsValue;
  47. Procedure TestDelete;
  48. Procedure TestToArray;
  49. procedure TestKeys;
  50. Procedure TestValues;
  51. Procedure TestEnumerator;
  52. Procedure TestNotification;
  53. procedure TestNotificationDelete;
  54. procedure TestValueNotification;
  55. procedure TestValueNotificationDelete;
  56. procedure TestKeyValueNotificationSet;
  57. end;
  58. { TMyObject }
  59. TMyObject = Class(TObject)
  60. Private
  61. fOnDestroy : TNotifyEvent;
  62. FID : Integer;
  63. public
  64. Constructor Create(aID : Integer; aOnDestroy : TNotifyEvent);
  65. destructor destroy; override;
  66. Property ID : Integer Read FID;
  67. end;
  68. TSingleObjectDict = Class(Specialize TObjectDictionary<Integer,TMyObject>);
  69. TDualObjectDict = Class(Specialize TObjectDictionary<TMyObject,TMyObject>);
  70. { TTestSingleObjectDict }
  71. TTestSingleObjectDict = Class(TTestCase)
  72. private
  73. FDict: TSingleObjectDict;
  74. FList : TFPList;
  75. procedure DoAdd(aID: Integer);
  76. procedure DoDestroy(Sender: TObject);
  77. Public
  78. Procedure SetUp; override;
  79. Procedure TearDown; override;
  80. Property Dict : TSingleObjectDict Read FDict;
  81. Published
  82. Procedure TestEmpty;
  83. Procedure TestFreeOnRemove;
  84. Procedure TestNoFreeOnRemove;
  85. end;
  86. TTestDualObjectDict = Class(TTestCase)
  87. private
  88. FDict: TDualObjectDict;
  89. FList : TFPList;
  90. procedure DoAdd(aID: Integer);
  91. procedure DoDestroy(Sender: TObject);
  92. Public
  93. Procedure SetUp; override;
  94. Procedure TearDown; override;
  95. Property Dict : TDualObjectDict Read FDict;
  96. Published
  97. Procedure TestEmpty;
  98. Procedure TestFreeOnRemove;
  99. Procedure TestNoFreeOnRemove;
  100. end;
  101. TMyStringDict = Class(Specialize TDictionary<string,string>);
  102. TMyStringComparer = Specialize TComparer<string>;
  103. { TTestComparerDictionary }
  104. TTestComparerDictionary = Class(TTestCase)
  105. private
  106. FDict: TMyStringDict;
  107. public
  108. Procedure Setup; override;
  109. Procedure TearDown; override;
  110. Procedure FillDict;
  111. Property Dict : TMyStringDict Read FDict;
  112. Published
  113. Procedure TestHasKey;
  114. Procedure TestTryGetValue;
  115. Procedure TestAddOrSet;
  116. Procedure TestRemove;
  117. end;
  118. implementation
  119. { TTestComparerDictionary }
  120. procedure TTestComparerDictionary.Setup;
  121. begin
  122. inherited Setup;
  123. FDict:=TMyStringDict.Create(TMyStringComparer.Construct(function (Const a,b : String) : integer
  124. begin
  125. Result:=CompareText(a,b);
  126. // writeln('Comparing ',a,' and ',b,' result: ',Result);
  127. end
  128. ));
  129. FillDict;
  130. end;
  131. procedure TTestComparerDictionary.TearDown;
  132. begin
  133. FreeAndNil(FDict);
  134. inherited TearDown;
  135. end;
  136. procedure TTestComparerDictionary.FillDict;
  137. begin
  138. With Dict do
  139. begin
  140. add('a','A');
  141. add('B','b');
  142. add('c','C');
  143. end;
  144. end;
  145. procedure TTestComparerDictionary.TestHasKey;
  146. begin
  147. AssertTrue('ContainsKey A',Dict.ContainsKey('A'));
  148. AssertTrue('ContainsKey b',Dict.ContainsKey('b'));
  149. AssertTrue('ContainsKey c',Dict.ContainsKey('c'));
  150. AssertFalse('ContainsKey D',Dict.ContainsKey('D'));
  151. end;
  152. procedure TTestComparerDictionary.TestTryGetValue;
  153. Var
  154. S : String;
  155. begin
  156. AssertTrue('A',Dict.TryGetValue('A',S));
  157. AssertEquals('Value A','A',S);
  158. AssertTrue('b',Dict.TryGetValue('b',S));
  159. AssertEquals('Value b','b',S);
  160. AssertTrue('c',Dict.TryGetValue('c',S));
  161. AssertEquals('Value C','C',S);
  162. AssertFalse('d',Dict.TryGetValue('D',S));
  163. end;
  164. procedure TTestComparerDictionary.TestAddOrSet;
  165. Var
  166. S : String;
  167. begin
  168. Dict.AddOrSetValue('d','E');
  169. AssertTrue('d',Dict.TryGetValue('d',S));
  170. AssertEquals('Value d','E',S);
  171. Dict.AddOrSetValue('D','D');
  172. AssertTrue('D',Dict.TryGetValue('D',S));
  173. AssertEquals('Value D','D',S);
  174. end;
  175. procedure TTestComparerDictionary.TestRemove;
  176. begin
  177. Dict.Remove('C');
  178. AssertFalse('ContainsKey C',Dict.ContainsKey('C'));
  179. AssertFalse('ContainsKey c',Dict.ContainsKey('c'));
  180. end;
  181. { TTestSingleObjectDict }
  182. procedure TTestSingleObjectDict.SetUp;
  183. begin
  184. FDict:=TSingleObjectDict.Create([doOwnsValues]);
  185. FList:=TFPList.Create;
  186. inherited SetUp;
  187. end;
  188. procedure TTestSingleObjectDict.TearDown;
  189. Var
  190. I : integer;
  191. A : TObject;
  192. begin
  193. FreeAndNil(FDict);
  194. for I:=0 to FList.Count-1 do
  195. begin
  196. A:=TObject(FList[i]);
  197. A.Free;
  198. end;
  199. FreeAndNil(FList);
  200. inherited TearDown;
  201. end;
  202. procedure TTestSingleObjectDict.TestEmpty;
  203. begin
  204. AssertNotNull('Have object',Dict);
  205. AssertEquals('Have empty object',0,Dict.Count);
  206. end;
  207. procedure TTestSingleObjectDict.DoAdd(aID : Integer);
  208. Var
  209. O : TMyObject;
  210. begin
  211. O:=TMyObject.Create(aID,@DoDestroy);
  212. FList.Add(O);
  213. FDict.Add(aID,O);
  214. end;
  215. procedure TTestSingleObjectDict.DoDestroy(Sender: TObject);
  216. Var
  217. I : Integer;
  218. begin
  219. I:=FList.IndexOf(Sender);
  220. AssertTrue('Have object in list',I<>-1);
  221. FList.Delete(I);
  222. end;
  223. procedure TTestSingleObjectDict.TestFreeOnRemove;
  224. begin
  225. DoAdd(1);
  226. AssertEquals('Have obj',1,FList.Count);
  227. Dict.Remove(1);
  228. AssertEquals('Have no obj',0,FList.Count);
  229. end;
  230. procedure TTestSingleObjectDict.TestNoFreeOnRemove;
  231. begin
  232. Dict.OwnerShips:=[];
  233. DoAdd(1);
  234. AssertEquals('Have obj',1,FList.Count);
  235. Dict.Remove(1);
  236. AssertEquals('Have obj',1,FList.Count);
  237. end;
  238. { TTestDualObjectDict }
  239. procedure TTestDualObjectDict.SetUp;
  240. begin
  241. FDict:=TDualObjectDict.Create([doOwnsKeys,doOwnsValues]);
  242. FList:=TFPList.Create;
  243. inherited SetUp;
  244. end;
  245. procedure TTestDualObjectDict.TearDown;
  246. Var
  247. I : integer;
  248. A : TObject;
  249. begin
  250. FreeAndNil(FDict);
  251. for I:=0 to FList.Count-1 do
  252. begin
  253. A:=TObject(FList[i]);
  254. A.Free;
  255. end;
  256. FreeAndNil(FList);
  257. inherited TearDown;
  258. end;
  259. procedure TTestDualObjectDict.TestEmpty;
  260. begin
  261. AssertNotNull('Have object',Dict);
  262. AssertEquals('Have empty object',0,Dict.Count);
  263. end;
  264. procedure TTestDualObjectDict.DoAdd(aID : Integer);
  265. Var
  266. O1,O10 : TMyObject;
  267. begin
  268. O1:=TMyObject.Create(aID,@DoDestroy);
  269. FList.Add(O1);
  270. O10:=TMyObject.Create(aID*10,@DoDestroy);
  271. FList.Add(O10);
  272. FDict.Add(O1,O10);
  273. end;
  274. procedure TTestDualObjectDict.DoDestroy(Sender: TObject);
  275. Var
  276. I : Integer;
  277. begin
  278. I:=FList.IndexOf(Sender);
  279. AssertTrue('Have object in list',I<>-1);
  280. FList.Delete(I);
  281. end;
  282. procedure TTestDualObjectDict.TestFreeOnRemove;
  283. begin
  284. DoAdd(1);
  285. AssertEquals('Have obj',2,FList.Count);
  286. Dict.Remove(TMyObject(FList[0]));
  287. AssertEquals('Have no obj',0,FList.Count);
  288. end;
  289. procedure TTestDualObjectDict.TestNoFreeOnRemove;
  290. begin
  291. Dict.OwnerShips:=[doOwnsValues];
  292. DoAdd(1);
  293. AssertEquals('Have obj',2,FList.Count);
  294. Dict.Remove(TMyObject(FList[0]));
  295. AssertEquals('Have obj',1,FList.Count);
  296. AssertEquals('Have key',1,TMyObject(Flist[0]).ID);
  297. end;
  298. { TMyObject }
  299. constructor TMyObject.Create(aID: Integer; aOnDestroy: TNotifyEvent);
  300. begin
  301. FOnDestroy:=aOnDestroy;
  302. FID:=AID;
  303. end;
  304. destructor TMyObject.destroy;
  305. begin
  306. if Assigned(FOnDestroy) then
  307. FOnDestroy(Self);
  308. inherited destroy;
  309. end;
  310. { TTestSimpleDictionary }
  311. procedure TTestSimpleDictionary.SetUp;
  312. begin
  313. inherited SetUp;
  314. FDict:=TMySimpleDict.Create;
  315. FCurrentKeyNotify:=0;
  316. FCurrentValueNotify:=0;
  317. FExpectKeys:=[];
  318. FExpectKeyAction:=[];
  319. FExpectValues:=[];
  320. FExpectValueAction:=[];
  321. end;
  322. procedure TTestSimpleDictionary.TearDown;
  323. begin
  324. // So we don't get clear messages
  325. FDict.OnKeyNotify:=Nil;
  326. FDict.OnValueNotify:=Nil;
  327. FreeAndNil(FDict);
  328. inherited TearDown;
  329. end;
  330. procedure TTestSimpleDictionary.TestEmpty;
  331. begin
  332. AssertNotNull('Have dictionary',Dict);
  333. AssertEquals('empty dictionary',0,Dict.Count);
  334. end;
  335. procedure TTestSimpleDictionary.DoAdd(aCount : Integer; aOffset : Integer=0);
  336. Var
  337. I : Integer;
  338. begin
  339. if aOffset=-1 then
  340. aOffset:=Dict.Count;
  341. For I:=aOffset+1 to aOffset+aCount do
  342. Dict.Add(I,IntToStr(i));
  343. end;
  344. procedure TTestSimpleDictionary.TestAdd;
  345. begin
  346. DoAdd(1);
  347. AssertEquals('Count OK',1,Dict.Count);
  348. AssertTrue('Has added value',Dict.ContainsKey(1));
  349. DoAdd(1,1);
  350. AssertEquals('Count OK',2,Dict.Count);
  351. AssertTrue('Has added value',Dict.ContainsKey(2));
  352. end;
  353. procedure TTestSimpleDictionary.TestClear;
  354. begin
  355. DoAdd(3);
  356. AssertEquals('Count OK',3,Dict.Count);
  357. Dict.Clear;
  358. AssertEquals('Count after clear OK',0,Dict.Count);
  359. end;
  360. procedure TTestSimpleDictionary.TestTryGetValue;
  361. Var
  362. I : integer;
  363. SI,A : string;
  364. begin
  365. DoAdd(3);
  366. For I:=1 to 3 do
  367. begin
  368. SI:=IntToStr(I);
  369. AssertTrue('Have value '+SI,Dict.TryGetValue(I,A));
  370. AssertEquals('Value is correct '+SI,SI,A);
  371. end;
  372. AssertFalse('Have no value 4',Dict.TryGetValue(4,A));
  373. end;
  374. procedure TTestSimpleDictionary.DoGetValue(aKey: Integer; Match: String; ExceptionClass: TClass);
  375. Var
  376. EC : TClass;
  377. A,EM : String;
  378. begin
  379. EC:=Nil;
  380. try
  381. A:=Dict.Items[aKey];
  382. except
  383. On E : Exception do
  384. begin
  385. EC:=E.ClassType;
  386. EM:=E.Message;
  387. end
  388. end;
  389. if ExceptionClass=Nil then
  390. begin
  391. if EC<>Nil then
  392. Fail('Got exception '+EC.ClassName+' with message: '+EM);
  393. AssertEquals('Value is correct for '+IntToStr(aKey),Match,A)
  394. end
  395. else
  396. begin
  397. if EC=Nil then
  398. Fail('Expected exception '+ExceptionClass.ClassName+' but got none');
  399. if EC<>ExceptionClass then
  400. Fail('Expected exception class '+ExceptionClass.ClassName+' but got '+EC.ClassName+' with message '+EM);
  401. end;
  402. end;
  403. procedure TTestSimpleDictionary.DoKeyNotify(ASender: TObject; {$ifdef fpc}constref{$else}const{$endif} AItem: Integer; AAction: TCollectionNotification);
  404. begin
  405. // Writeln(FnotifyMessage+' Notification',FCurrentKeyNotify);
  406. AssertSame(FnotifyMessage+' Correct sender', FDict,aSender);
  407. if (FCurrentKeyNotify>=Length(FExpectKeys)) then
  408. Fail(FnotifyMessage+' Too many notificiations');
  409. AssertEquals(FnotifyMessage+' Notification Key no '+IntToStr(FCurrentKeyNotify),FExpectKeys[FCurrentKeyNotify],aItem);
  410. Inc(FCurrentKeyNotify);
  411. end;
  412. procedure TTestSimpleDictionary.DoValueNotify(ASender: TObject; {$ifdef fpc}constref{$else}const{$endif} AItem: String; AAction: TCollectionNotification);
  413. begin
  414. // Writeln(FnotifyMessage+' value Notification',FCurrentValueNotify);
  415. AssertSame(FnotifyMessage+' value Correct sender', FDict,aSender);
  416. if (FCurrentValueNotify>=Length(FExpectValues)) then
  417. Fail(FnotifyMessage+' Too many value notificiations');
  418. AssertEquals(FnotifyMessage+' Notification value no '+IntToStr(FCurrentValueNotify),FExpectValues[FCurrentValueNotify],aItem);
  419. Inc(FCurrentValueNotify);
  420. end;
  421. procedure TTestSimpleDictionary.SetExpectKeys(aMessage: string; AKeys: array of Integer;
  422. AActions: array of TCollectionNotification; DoReverse: Boolean = False);
  423. Var
  424. I,L : integer;
  425. begin
  426. FnotifyMessage:=aMessage;
  427. FCurrentKeyNotify:=0;
  428. L:=Length(aKeys);
  429. AssertEquals('SetExpectkeys: Lengths arrays equal',l,Length(aActions));
  430. SetLength(FExpectKeys,L);
  431. SetLength(FExpectKeyAction,L);
  432. Dec(L);
  433. if DoReverse then
  434. For I:=0 to L do
  435. begin
  436. FExpectKeys[L-i]:=AKeys[i];
  437. FExpectKeyAction[L-i]:=AActions[I];
  438. end
  439. else
  440. For I:=0 to L do
  441. begin
  442. FExpectKeys[i]:=AKeys[i];
  443. FExpectKeyAction[i]:=AActions[I];
  444. end;
  445. end;
  446. procedure TTestSimpleDictionary.SetExpectValues(aMessage: string; AKeys: array of String;
  447. AActions: array of TCollectionNotification; DoReverse: Boolean);
  448. Var
  449. I,L : integer;
  450. begin
  451. FnotifyMessage:=aMessage;
  452. FCurrentValueNotify:=0;
  453. L:=Length(aKeys);
  454. AssertEquals('SetExpectValues: Lengths arrays equal',l,Length(aActions));
  455. SetLength(FExpectValues,L);
  456. SetLength(FExpectValueAction,L);
  457. Dec(L);
  458. if DoReverse then
  459. For I:=0 to L do
  460. begin
  461. FExpectValues[L-i]:=AKeys[i];
  462. FExpectValueAction[L-i]:=AActions[I];
  463. end
  464. else
  465. For I:=0 to L do
  466. begin
  467. FExpectValues[i]:=AKeys[i];
  468. FExpectValueAction[i]:=AActions[I];
  469. end;
  470. end;
  471. procedure TTestSimpleDictionary.TestGetValue;
  472. Var
  473. I : integer;
  474. begin
  475. DoAdd(3);
  476. For I:=1 to 3 do
  477. DoGetValue(I,IntToStr(I));
  478. DoGetValue(4,'4',EDictionary);
  479. end;
  480. procedure TTestSimpleDictionary.TestSetValue;
  481. begin
  482. TestGetValue;
  483. Dict.Items[3]:='Six';
  484. DoGetValue(3,'Six');
  485. end;
  486. procedure TTestSimpleDictionary.DoAdd2;
  487. begin
  488. Dict.Add(2,'A new 2');
  489. end;
  490. procedure TTestSimpleDictionary.DoneExpectKeys;
  491. begin
  492. AssertEquals(FnotifyMessage+' Expected number of keys seen',Length(FExpectKeys),FCurrentKeyNotify);
  493. end;
  494. procedure TTestSimpleDictionary.DoneExpectValues;
  495. begin
  496. AssertEquals(FnotifyMessage+' Expected number of values seen',Length(FExpectValues),FCurrentValueNotify);
  497. end;
  498. procedure TTestSimpleDictionary.TestAddDuplicate;
  499. begin
  500. DoAdd(3);
  501. AssertException('Cannot add duplicate',EDictionary,@DoAdd2);
  502. end;
  503. procedure TTestSimpleDictionary.TestAddOrSet;
  504. begin
  505. DoAdd(3);
  506. Dict.AddOrSetValue(2,'a new 2');
  507. DoGetValue(2,'a new 2');
  508. end;
  509. procedure TTestSimpleDictionary.TestContainsKey;
  510. Var
  511. I : Integer;
  512. begin
  513. DoAdd(3);
  514. For I:=1 to 3 do
  515. AssertTrue('Has '+IntToStr(i),Dict.ContainsKey(I));
  516. AssertFalse('Has not 4',Dict.ContainsKey(4));
  517. end;
  518. procedure TTestSimpleDictionary.TestContainsValue;
  519. Var
  520. I : Integer;
  521. begin
  522. DoAdd(3);
  523. For I:=1 to 3 do
  524. AssertTrue('Has '+IntToStr(i),Dict.ContainsValue(IntToStr(i)));
  525. AssertFalse('Has not 4',Dict.ContainsValue('4'));
  526. end;
  527. procedure TTestSimpleDictionary.TestDelete;
  528. begin
  529. DoAdd(3);
  530. Dict.Remove(2);
  531. AssertEquals('Count',2,Dict.Count);
  532. AssertFalse('Has not 2',Dict.ContainsKey(2));
  533. end;
  534. procedure TTestSimpleDictionary.TestToArray;
  535. Var
  536. {$ifdef fpc}
  537. A : specialize TArray<TMyPair>;
  538. {$else}
  539. A : specialize TArray<TMySimpleDict.TMyPair>;
  540. {$endif}
  541. I : Integer;
  542. SI : String;
  543. begin
  544. DoAdd(3);
  545. A:=Dict.ToArray;
  546. AssertEquals('Length Ok',3,Length(A));
  547. For I:=1 to 3 do
  548. begin
  549. SI:=IntToStr(I);
  550. AssertEquals('key '+SI,I,A[i-1].Key);
  551. AssertEquals('Value '+SI,SI,A[i-1].Value);
  552. end;
  553. end;
  554. procedure TTestSimpleDictionary.TestKeys;
  555. Var
  556. A : Array of Integer;
  557. I : Integer;
  558. SI : String;
  559. begin
  560. DoAdd(3);
  561. A:=Dict.Keys.ToArray;
  562. AssertEquals('Length Ok',3,Length(A));
  563. For I:=1 to 3 do
  564. begin
  565. SI:=IntToStr(I);
  566. AssertEquals('key '+SI,I,A[i-1]);
  567. end;
  568. end;
  569. procedure TTestSimpleDictionary.TestValues;
  570. Var
  571. A : Array of String;
  572. I : Integer;
  573. SI : String;
  574. begin
  575. DoAdd(3);
  576. A:=Dict.Values.ToArray;
  577. AssertEquals('Length Ok',3,Length(A));
  578. For I:=1 to 3 do
  579. begin
  580. SI:=IntToStr(I);
  581. AssertEquals('Value '+SI,SI,A[i-1]);
  582. end;
  583. end;
  584. procedure TTestSimpleDictionary.TestEnumerator;
  585. Var
  586. {$ifdef fpc}
  587. A : TMyPair;
  588. {$else}
  589. A : TMySimpleDict.TMyPair;
  590. {$endif}
  591. I : Integer;
  592. SI : String;
  593. begin
  594. DoAdd(3);
  595. I:=1;
  596. For A in Dict do
  597. begin
  598. SI:=IntToStr(I);
  599. AssertEquals('key '+SI,I,A.Key);
  600. AssertEquals('Value '+SI,SI,A.Value);
  601. Inc(I);
  602. end;
  603. end;
  604. procedure TTestSimpleDictionary.TestNotification;
  605. begin
  606. Dict.OnKeyNotify:=@DoKeyNotify;
  607. SetExpectKeys('Add',[1,2,3],[cnAdded,cnAdded,cnAdded]);
  608. DoAdd(3);
  609. DoneExpectKeys;
  610. end;
  611. procedure TTestSimpleDictionary.TestNotificationDelete;
  612. begin
  613. DoAdd(3);
  614. Dict.OnKeyNotify:=@DoKeyNotify;
  615. SetExpectKeys('Clear',[1,2,3],[cnRemoved,cnRemoved,cnRemoved],{$IFDEF FPC}true{$ELSE}False{$endif});
  616. Dict.Clear;
  617. DoneExpectKeys;
  618. end;
  619. procedure TTestSimpleDictionary.TestValueNotification;
  620. begin
  621. Dict.OnValueNotify:=@DoValueNotify;
  622. SetExpectValues('Add',['1','2','3'],[cnAdded,cnAdded,cnAdded]);
  623. DoAdd(3);
  624. DoneExpectValues;
  625. end;
  626. procedure TTestSimpleDictionary.TestValueNotificationDelete;
  627. begin
  628. DoAdd(3);
  629. Dict.OnValueNotify:=@DoValueNotify;
  630. SetExpectValues('Clear',['1','2','3'],[cnRemoved,cnRemoved,cnRemoved],{$IFDEF FPC}true{$ELSE}False{$endif});
  631. Dict.Clear;
  632. DoneExpectValues;
  633. end;
  634. procedure TTestSimpleDictionary.TestKeyValueNotificationSet;
  635. begin
  636. DoAdd(3);
  637. Dict.OnValueNotify:=@DoValueNotify;
  638. Dict.OnKeyNotify:=@DoKeyNotify;
  639. SetExpectValues('Set',['2','Six'],[cnRemoved,cnAdded]);
  640. SetExpectKeys('Set',[],[]);
  641. Dict[2]:='Six';
  642. DoneExpectKeys;
  643. DoneExpectValues;
  644. end;
  645. begin
  646. RegisterTests([{TTestSimpleDictionary,
  647. TTestSingleObjectDict,
  648. TTestDualObjectDict,}
  649. TTestComparerDictionary]);
  650. end.