2
0

Test_StringMap.dpr 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. program Test_StringMap;
  26. {$I Tests.inc}
  27. {$IFDEF FPC}
  28. {$CODEPAGE UTF8}
  29. {$ENDIF}
  30. uses
  31. SysUtils,
  32. libsagui,
  33. BrookLibraryLoader,
  34. BrookStringMap,
  35. Test;
  36. type
  37. TLocalStringMap = class(TBrookStringMap)
  38. private
  39. FOperation: TBrookStringMapOperation;
  40. protected
  41. procedure DoChange(AOperation: TBrookStringMapOperation); override;
  42. public
  43. procedure LocalDestroy;
  44. property Operation: TBrookStringMapOperation read FOperation;
  45. end;
  46. procedure TLocalStringMap.DoChange(AOperation: TBrookStringMapOperation);
  47. begin
  48. FOperation := AOperation;
  49. inherited DoChange(AOperation);
  50. end;
  51. procedure TLocalStringMap.LocalDestroy;
  52. begin
  53. inherited Destroy;
  54. SgLib.Check;
  55. { checks if the handle was really freed and 'nilified'. }
  56. Assert(not Assigned(Handle));
  57. sg_strmap_cleanup(Handle);
  58. end;
  59. procedure Test_StringMapNameValue;
  60. var
  61. VPair: TBrookStringPair;
  62. begin
  63. VPair := TBrookStringPair.Create('', '');
  64. Assert(VPair.Name.IsEmpty);
  65. Assert(VPair.Value.IsEmpty);
  66. VPair := TBrookStringPair.Create('abc', '123');
  67. Assert(VPair.Name.Equals('abc'));
  68. Assert(VPair.Value.Equals('123'));
  69. end;
  70. procedure Test_StringMapClearOnDestroy;
  71. var
  72. VMapHandle: Pointer;
  73. VMap: TBrookStringMap;
  74. begin
  75. VMapHandle := nil;
  76. VMap := TBrookStringMap.Create(@VMapHandle);
  77. try
  78. Assert(VMap.ClearOnDestroy);
  79. VMap.ClearOnDestroy := False;
  80. VMap.Add('abc', '123');
  81. VMap.Add('def', '456');
  82. SgLib.Check;
  83. Assert(sg_strmap_count(VMapHandle) = 2);
  84. finally
  85. VMap.Free;
  86. end;
  87. Assert(sg_strmap_count(VMapHandle) = 2);
  88. sg_strmap_cleanup(@VMapHandle);
  89. VMapHandle := nil;
  90. VMap := TLocalStringMap.Create(@VMapHandle);
  91. try
  92. VMap.Add('abc', '123');
  93. VMap.Add('def', '456');
  94. SgLib.Check;
  95. Assert(sg_strmap_count(VMapHandle) = 2);
  96. finally
  97. VMap.Free;
  98. end;
  99. Assert(sg_strmap_count(VMapHandle) = 0);
  100. end;
  101. procedure Test_StringMapOnChange;
  102. var
  103. VMap: TLocalStringMap;
  104. begin
  105. VMap := TLocalStringMap.Create(nil);
  106. try
  107. Assert(VMap.Operation = sgmoNone);
  108. VMap.Add('abc', '123');
  109. Assert(VMap.Operation = sgmoAdd);
  110. VMap.AddOrSet('def', '456');
  111. Assert(VMap.Operation = sgmoAddOrSet);
  112. VMap.Remove('abc');
  113. Assert(VMap.Operation = sgmoRemove);
  114. VMap.Clear;
  115. Assert(VMap.Operation = sgmoNone);
  116. finally
  117. VMap.Free;
  118. end;
  119. end;
  120. procedure Test_StringMapHandle(AMap: TBrookStringMap);
  121. var
  122. VMapHandle: Pointer;
  123. VMap: TBrookStringMap;
  124. begin
  125. AMap.Clear;
  126. AMap.Add('abc', '123');
  127. Assert(Assigned(AMap.Handle));
  128. VMap := TBrookStringMap.Create(AMap.Handle);
  129. try
  130. Assert(VMap.Handle = AMap.Handle);
  131. finally
  132. VMap.Free;
  133. end;
  134. VMapHandle := nil;
  135. VMap := TBrookStringMap.Create(@VMapHandle);
  136. try
  137. VMap.Add('abc', '123');
  138. Assert(Assigned(VMap.Handle));
  139. Assert(VMap.Handle <> AMap.Handle);
  140. finally
  141. VMap.Free;
  142. end;
  143. end;
  144. procedure Test_StringMapAdd(AMap: TBrookStringMap; const AName, AValue: string);
  145. begin
  146. AMap.Clear;
  147. Assert(AMap.Count = 0);
  148. AMap.Add('', AValue);
  149. Assert(AMap.Count = 1);
  150. AMap.Clear;
  151. AMap.Add(AName, '');
  152. Assert(AMap.Count = 1);
  153. AMap.Clear;
  154. AMap.Add(AName, AValue);
  155. AMap.Add(AName, AValue);
  156. Assert(AMap.Count = 2);
  157. end;
  158. procedure Test_StringMapAddOrSet(AMap: TBrookStringMap; const AName,
  159. AValue: string);
  160. begin
  161. AMap.Clear;
  162. Assert(AMap.Count = 0);
  163. AMap.AddOrSet('', AValue);
  164. Assert(AMap.Count = 1);
  165. AMap.Clear;
  166. AMap.AddOrSet(AName, '');
  167. Assert(AMap.Count = 1);
  168. AMap.Clear;
  169. AMap.AddOrSet(AName, AValue);
  170. AMap.AddOrSet(AName, AValue);
  171. Assert(AMap.Count = 1);
  172. end;
  173. procedure Test_StringMapFind(AMap: TBrookStringMap; const AName,
  174. AValue: string);
  175. var
  176. VPair: TBrookStringPair;
  177. begin
  178. AMap.Clear;
  179. Assert(AMap.Count = 0);
  180. AMap.Add(AName, AValue);
  181. Assert(AMap.Count = 1);
  182. Assert(not AMap.Find('', VPair));
  183. Assert(VPair.Name.IsEmpty);
  184. Assert(VPair.Value.IsEmpty);
  185. Assert(not AMap.Find('xxx', VPair));
  186. Assert(VPair.Name.IsEmpty);
  187. Assert(VPair.Value.IsEmpty);
  188. Assert(not AMap.Find('yyy', VPair));
  189. Assert(VPair.Name.IsEmpty);
  190. Assert(VPair.Value.IsEmpty);
  191. AMap.Add('', '');
  192. AMap.Add('xxx', 'yyy');
  193. AMap.Add('yyy', 'xxx');
  194. Assert(AMap.Count = 4);
  195. Assert(AMap.Find(AName, VPair));
  196. Assert((VPair.Name = AName) and (VPair.Value = AValue));
  197. Assert(AMap.Find('', VPair));
  198. Assert((VPair.Name.IsEmpty) and (VPair.Value.IsEmpty));
  199. Assert(AMap.Find('xxx', VPair));
  200. Assert(VPair.Name.Equals('xxx') and VPair.Value.Equals('yyy'));
  201. Assert(AMap.Find('yyy', VPair));
  202. Assert(VPair.Name.Equals('yyy') and VPair.Value.Equals('xxx'));
  203. end;
  204. procedure Test_StringMapGet(AMap: TBrookStringMap; const AName,
  205. AValue: string);
  206. begin
  207. AMap.Clear;
  208. Assert(AMap.Count = 0);
  209. AMap.Add(AName, AValue);
  210. Assert(AMap.Count = 1);
  211. Assert(AMap.Get('').IsEmpty);
  212. Assert(AMap.Get('xxx').IsEmpty);
  213. Assert(AMap.Get('yyy').IsEmpty);
  214. AMap.Add('', '');
  215. AMap.Add('xxx', 'yyy');
  216. AMap.Add('yyy', 'xxx');
  217. Assert(AMap.Count = 4);
  218. Assert(AMap.Get(AName).Equals(AValue));
  219. Assert(AMap.Get('').IsEmpty);
  220. Assert(AMap.Get('xxx').Equals('yyy'));
  221. Assert(AMap.Get('yyy').Equals('xxx'));
  222. end;
  223. procedure Test_StringMapRemove(AMap: TBrookStringMap; const AName,
  224. AValue: string);
  225. var
  226. VPair: TBrookStringPair;
  227. begin
  228. AMap.Clear;
  229. Assert(AMap.Count = 0);
  230. AMap.Add(AName, AValue);
  231. Assert(AMap.Count = 1);
  232. AMap.Remove('');
  233. Assert(AMap.Count = 1);
  234. AMap.Remove('xxx');
  235. Assert(AMap.Count = 1);
  236. AMap.Remove('yyy');
  237. Assert(AMap.Count = 1);
  238. AMap.Add('', '');
  239. AMap.Add('xxx', 'yyy');
  240. AMap.Add('yyy', 'xxx');
  241. Assert(AMap.Count = 4);
  242. Assert(AMap.Find(AName, VPair));
  243. AMap.Remove(AName);
  244. Assert(AMap.Count = 3);
  245. Assert(not AMap.Find(AName, VPair));
  246. Assert(AMap.Find('', VPair));
  247. AMap.Remove('');
  248. Assert(AMap.Count = 2);
  249. Assert(not AMap.Find('', VPair));
  250. Assert(AMap.Find('xxx', VPair));
  251. AMap.Remove('xxx');
  252. Assert(AMap.Count = 1);
  253. Assert(not AMap.Find('xxx', VPair));
  254. Assert(AMap.Find('yyy', VPair));
  255. AMap.Remove('yyy');
  256. Assert(AMap.Count = 0);
  257. Assert(not AMap.Find('yyy', VPair));
  258. end;
  259. function StringMapIterateEmpty(AData: Pointer;
  260. APair: TBrookStringPair): Integer;
  261. begin
  262. Result := 0;
  263. end;
  264. function StringMapIterate123(AData: Pointer; APair: TBrookStringPair): Integer;
  265. begin
  266. Result := 123;
  267. end;
  268. function StringMapIterateConcat(AData: Pointer;
  269. APair: TBrookStringPair): Integer;
  270. var
  271. S: PString absolute AData;
  272. begin
  273. S^ := Concat(S^, APair.Name, APair.Value);
  274. Result := 0;
  275. end;
  276. procedure DoStringMapIterate(const AArgs: array of const);
  277. begin
  278. TBrookStringMap(AArgs[0].VObject).Iterate(StringMapIterate123, nil);
  279. end;
  280. procedure Test_StringMapIterate(AMap: TBrookStringMap);
  281. var
  282. S: string;
  283. begin
  284. AMap.Clear;
  285. AMap.Add('abc', '123');
  286. AMap.Add('def', '456');
  287. AMap.Iterate(StringMapIterateEmpty, nil);
  288. AssertOSExcept(DoStringMapIterate, 123, [AMap]);
  289. S := '';
  290. AMap.Iterate(StringMapIterateConcat, @S);
  291. Assert(S.Equals('abc123def456'));
  292. end;
  293. function StringMapSortEmpty(AData: Pointer; APairA,
  294. APairB: TBrookStringPair): Integer;
  295. var
  296. S: PString absolute AData;
  297. begin
  298. S^ := Concat(S^, S^);
  299. Result := 0;
  300. end;
  301. function StringMapSortNameDesc(AData: Pointer; APairA,
  302. APairB: TBrookStringPair): Integer;
  303. begin
  304. Result := CompareStr(APairB.Name, APairA.Name);
  305. end;
  306. function StringMapSortNameAsc(AData: Pointer; APairA,
  307. APairB: TBrookStringPair): Integer;
  308. begin
  309. Result := CompareStr(APairA.Name, APairB.Name);
  310. end;
  311. function StringMapSortValueDesc(AData: Pointer; APairA,
  312. APairB: TBrookStringPair): Integer;
  313. begin
  314. Result := CompareStr(APairB.Value, APairA.Value);
  315. end;
  316. function StringMapSortValueAsc(AData: Pointer; APairA,
  317. APairB: TBrookStringPair): Integer;
  318. begin
  319. Result := CompareStr(APairA.Value, APairB.Value);
  320. end;
  321. procedure Test_StringMapSort(AMap: TBrookStringMap);
  322. var
  323. S: string;
  324. begin
  325. AMap.Clear;
  326. AMap.Add('abc', '123');
  327. AMap.Add('def', '456');
  328. S := 'abc';
  329. AMap.Sort(StringMapSortEmpty, @S);
  330. Assert(S.Equals('abcabc'));
  331. S := '';
  332. AMap.Iterate(StringMapIterateConcat, @S);
  333. Assert(S.Equals('abc123def456'));
  334. AMap.Sort(StringMapSortNameDesc, nil);
  335. S := '';
  336. AMap.Iterate(StringMapIterateConcat, @S);
  337. Assert(S.Equals('def456abc123'));
  338. AMap.Sort(StringMapSortNameAsc, nil);
  339. S := '';
  340. AMap.Iterate(StringMapIterateConcat, @S);
  341. Assert(S.Equals('abc123def456'));
  342. AMap.Sort(StringMapSortValueDesc, nil);
  343. S := '';
  344. AMap.Iterate(StringMapIterateConcat, @S);
  345. Assert(S.Equals('def456abc123'));
  346. AMap.Sort(StringMapSortValueAsc, nil);
  347. S := '';
  348. AMap.Iterate(StringMapIterateConcat, @S);
  349. Assert(S.Equals('abc123def456'));
  350. end;
  351. procedure Test_StringMapCount(AMap: TBrookStringMap; const AName,
  352. AValue: string);
  353. begin
  354. AMap.Clear;
  355. Assert(AMap.Count = 0);
  356. AMap.Add(AName, AValue);
  357. Assert(AMap.Count = 1);
  358. AMap.Add('xxx', 'yyy');
  359. Assert(AMap.Count = 2);
  360. AMap.Add('yyy', 'xxx');
  361. Assert(AMap.Count = 3);
  362. AMap.Add(AName, AValue);
  363. Assert(AMap.Count = 4);
  364. AMap.Remove(AName);
  365. Assert(AMap.Count = 3);
  366. AMap.Clear;
  367. Assert(AMap.Count = 0);
  368. end;
  369. procedure Test_StringMapTryValue(AMap: TBrookStringMap; const AName,
  370. AValue: string);
  371. var
  372. S: string;
  373. begin
  374. AMap.Clear;
  375. Assert(AMap.Count = 0);
  376. AMap.Add(AName, AValue);
  377. Assert(AMap.Count = 1);
  378. Assert(not AMap.TryValue('', S));
  379. Assert(S.IsEmpty);
  380. Assert(not AMap.TryValue('xxx', S));
  381. Assert(S.IsEmpty);
  382. Assert(not AMap.TryValue('yyy', S));
  383. Assert(S.IsEmpty);
  384. AMap.Add('', '');
  385. AMap.Add('xxx', 'yyy');
  386. AMap.Add('yyy', 'xxx');
  387. Assert(AMap.Count = 4);
  388. Assert(AMap.TryValue(AName, S));
  389. Assert(S = AValue);
  390. Assert(AMap.TryValue('', S));
  391. Assert(S.IsEmpty);
  392. Assert(AMap.TryValue('xxx', S));
  393. Assert(S.Equals('yyy'));
  394. Assert(AMap.TryValue('yyy', S));
  395. Assert(S.Equals('xxx'));
  396. end;
  397. procedure Test_StringMapFirst(AMap: TBrookStringMap);
  398. var
  399. VPair: TBrookStringPair;
  400. begin
  401. AMap.Clear;
  402. AMap.Add('abc', '123');
  403. AMap.Add('def', '456');
  404. AMap.Add('xxx', 'yyy');
  405. AMap.First(VPair);
  406. Assert(VPair.Name.Equals('abc') and VPair.Value.Equals('123'));
  407. AMap.Next(VPair);
  408. AMap.Next(VPair);
  409. Assert(VPair.Name.Equals('xxx') and VPair.Value.Equals('yyy'));
  410. AMap.First(VPair);
  411. Assert(VPair.Name.Equals('abc') and VPair.Value.Equals('123'));
  412. end;
  413. procedure Test_StringMapValues(AMap: TBrookStringMap);
  414. begin
  415. AMap.Clear;
  416. Assert(AMap.Values['abc'].IsEmpty);
  417. AMap.Values['abc'] := '123';
  418. Assert(AMap.Values['abc'].Equals('123'));
  419. Assert(AMap.Values['def'].IsEmpty);
  420. AMap.Values['def'] := '456';
  421. Assert(AMap.Values['def'].Equals('456'));
  422. Assert(AMap.Values['xxx'].IsEmpty);
  423. AMap.Values['xxx'] := 'yyy';
  424. Assert(AMap.Values['xxx'].Equals('yyy'));
  425. Assert(AMap.Count = 3);
  426. AMap.Values['xxx'] := 'yyy';
  427. Assert(AMap.Count = 3);
  428. end;
  429. procedure Test_StringMapEOF(AMap: TBrookStringMap);
  430. var
  431. VPair: TBrookStringPair;
  432. begin
  433. AMap.Clear;
  434. AMap.Add('abc', '123');
  435. AMap.Add('def', '456');
  436. AMap.Add('xxx', 'yyy');
  437. AMap.First(VPair);
  438. Assert(not AMap.EOF);
  439. AMap.Next(VPair);
  440. Assert(not AMap.EOF);
  441. AMap.Next(VPair);
  442. Assert(not AMap.EOF);
  443. AMap.Next(VPair);
  444. Assert(AMap.EOF);
  445. AMap.Next(VPair);
  446. Assert(AMap.EOF);
  447. end;
  448. procedure Test_StringMapNext(AMap: TBrookStringMap);
  449. var
  450. S: string;
  451. VPair: TBrookStringPair;
  452. begin
  453. AMap.Clear;
  454. VPair := Default(TBrookStringPair);
  455. Assert(not AMap.Next(VPair));
  456. AMap.Add('abc', '123');
  457. AMap.Add('def', '456');
  458. AMap.Add('xxx', 'yyy');
  459. S := '';
  460. AMap.First(VPair);
  461. while not AMap.EOF do
  462. begin
  463. S := Concat(S, VPair.Name, VPair.Value);
  464. AMap.Next(VPair);
  465. end;
  466. Assert(S.Equals('abc123def456xxxyyy'));
  467. end;
  468. procedure Test_StringMapEnumerator(AMap: TBrookStringMap);
  469. var
  470. I: Byte;
  471. S: string;
  472. P: TBrookStringPair;
  473. begin
  474. AMap.Clear;
  475. for I := 1 to 3 do
  476. begin
  477. S := I.ToString;
  478. AMap.Add(Concat('name', S), Concat('value', S));
  479. end;
  480. I := 0;
  481. for P in AMap do
  482. begin
  483. S := Succ(I).ToString;
  484. Assert(P.Name.Equals(Concat('name', S)) and
  485. P.Value.Equals(Concat('value', S)));
  486. Inc(I);
  487. end;
  488. Assert(I = 3);
  489. end;
  490. procedure Test_StringMapToString(AMap: TBrookStringMap; const AName,
  491. AValue: string);
  492. begin
  493. AMap.Clear;
  494. Assert(AMap.ToString.IsEmpty);
  495. AMap.Add(AName, AValue);
  496. Assert(AMap.ToString = Concat(AName, '=', AValue, sLineBreak));
  497. AMap.Add('xxx', 'yyy');
  498. AMap.Add('yyy', 'xxx');
  499. Assert(AMap.ToString.Equals(Concat(AName, '=', AValue, sLineBreak,
  500. 'xxx=yyy', sLineBreak, 'yyy=xxx', sLineBreak)));
  501. end;
  502. procedure Test_StringMapClear(AMap: TBrookStringMap);
  503. begin
  504. AMap.Clear;
  505. Assert(AMap.Count = 0);
  506. AMap.Add('abc', '123');
  507. AMap.Add('def', '456');
  508. AMap.Add('xxx', 'yyy');
  509. Assert(AMap.Count = 3);
  510. AMap.Clear;
  511. Assert(AMap.Count = 0);
  512. AMap.Clear;
  513. Assert(AMap.Count = 0);
  514. end;
  515. const
  516. NAME = 'abç';
  517. VAL = 'déf';
  518. var
  519. VMap: TBrookStringMap;
  520. begin
  521. {$IF (NOT DEFINED(FPC)) AND DEFINED(DEBUG)}
  522. ReportMemoryLeaksOnShutdown := True;
  523. {$ENDIF}
  524. TBrookLibraryLoader.Load;
  525. Test_StringMapNameValue;
  526. Test_StringMapClearOnDestroy;
  527. Test_StringMapOnChange;
  528. VMap := TBrookStringMap.Create(nil);
  529. try
  530. Test_StringMapHandle(VMap);
  531. Test_StringMapAdd(VMap, NAME, VAL);
  532. Test_StringMapAddOrSet(VMap, NAME, VAL);
  533. Test_StringMapFind(VMap, NAME, VAL);
  534. Test_StringMapGet(VMap, NAME, VAL);
  535. Test_StringMapRemove(VMap, NAME, VAL);
  536. Test_StringMapIterate(VMap);
  537. Test_StringMapSort(VMap);
  538. Test_StringMapCount(VMap, NAME, VAL);
  539. Test_StringMapTryValue(VMap, NAME, VAL);
  540. Test_StringMapFirst(VMap);
  541. Test_StringMapValues(VMap);
  542. Test_StringMapEOF(VMap);
  543. Test_StringMapNext(VMap);
  544. Test_StringMapEnumerator(VMap);
  545. Test_StringMapToString(VMap, NAME, VAL);
  546. Test_StringMapClear(VMap);
  547. finally
  548. VMap.Free;
  549. end;
  550. end.