tstring.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. { Program to test system unit string routines
  2. Tested against Delphi 3 and (where possible)
  3. against Borland Pascal v7.01
  4. }
  5. program tstring;
  6. {$R+}
  7. {$Q+}
  8. {$ifndef MACOS}
  9. {$APPTYPE CONSOLE}
  10. {$else}
  11. {$APPTYPE TOOL}
  12. {$endif}
  13. {$ifdef fpc}
  14. {$ifndef ver1_0}
  15. {$define haswidestring}
  16. {$endif}
  17. {$else}
  18. {$ifndef ver70}
  19. {$define haswidestring}
  20. {$endif}
  21. {$endif}
  22. var
  23. str1 : shortstring;
  24. str2 : ansistring;
  25. {$ifdef haswidestring}
  26. str3 : widestring;
  27. {$endif}
  28. procedure fail;
  29. begin
  30. WriteLn('Failed!');
  31. Halt(1);
  32. end;
  33. procedure test_stringofchar;
  34. var
  35. _result : boolean;
  36. i: integer;
  37. begin
  38. Write('StringOfChar tests...');
  39. _result := true;
  40. {************************* shortstring ************************}
  41. { try to fill a shortstring with a null character }
  42. str1:='';
  43. str1:=stringofchar(#0,0);
  44. if length(str1)<>0 then
  45. _result := false;
  46. str1:='';
  47. str1:='';
  48. str1:=stringofchar('a',-1);
  49. if length(str1)<>0 then
  50. _result := false;
  51. str1:='';
  52. { try to fill a shortstring with more chars than possible }
  53. str1:=stringofchar('c',300);
  54. if length(str1)<>255 then
  55. _result := false;
  56. { try to fill a shortstring with no chars }
  57. str1:='';
  58. str1:=stringofchar('c',0);
  59. if length(str1)<>0 then
  60. _result := false;
  61. { try to fill a shortstring chars }
  62. str1:='';
  63. str1:=stringofchar('a',255);
  64. for i:=1 to 255 do
  65. if str1[i] <> 'a' then
  66. _result := false;
  67. {************************* ansistring *************************}
  68. { try to fill a ansistring with a null character }
  69. str2:='';
  70. str2:=stringofchar(#0,0);
  71. if length(str2)<>0 then
  72. _result := false;
  73. str2:='';
  74. str2:=stringofchar('a',-1);
  75. if length(str2)<>0 then
  76. _result := false;
  77. { try to fill a ansistring with no chars }
  78. str2:='';
  79. str2:=stringofchar('c',0);
  80. if length(str2)<>0 then
  81. _result := false;
  82. { try to fill an ansistring chars }
  83. str2:='';
  84. str2:=stringofchar('a',1024);
  85. for i:=1 to 1024 do
  86. if str2[i] <> 'a' then
  87. _result := false;
  88. {************************* widestring *************************}
  89. {$ifdef haswidestring}
  90. { try to fill a widestring with a null character }
  91. str3:='';
  92. str3:=stringofchar(#0,0);
  93. if length(str3)<>0 then
  94. _result := false;
  95. str3:='';
  96. { try to fill a widestring with no chars }
  97. str3:='';
  98. str3:=stringofchar('c',0);
  99. if length(str3)<>0 then
  100. _result := false;
  101. { try to fill an widestring chars }
  102. str3:='';
  103. str3:=stringofchar('a',1024);
  104. for i:=1 to 1024 do
  105. if str3[i] <> 'a' then
  106. _result := false;
  107. str3:='';
  108. str3:=stringofchar('a',-1);
  109. if length(str3)<>0 then
  110. _result := false;
  111. {$endif}
  112. if not _result then
  113. fail
  114. else
  115. WriteLn('Success!');
  116. end;
  117. procedure test_delete;
  118. var
  119. _result : boolean;
  120. i: integer;
  121. begin
  122. Write('Delete tests...');
  123. _result := true;
  124. {************************* shortstring ************************}
  125. { try to delete from an empty string }
  126. str1:='';
  127. Delete(str1,0,12);
  128. if str1<>'' then
  129. _result := false;
  130. str1:='Hello';
  131. Delete(str1,0,12);
  132. if str1<>'Hello' then
  133. _result := false;
  134. str1:='Hello';
  135. Delete(str1,1,12);
  136. if str1<>'' then
  137. _result := false;
  138. str1:='Hello';
  139. Delete(str1,12,255);
  140. if str1<>'Hello' then
  141. _result := false;
  142. str1:='Hello';
  143. Delete(str1,-1,255);
  144. if str1<>'Hello' then
  145. _result := false;
  146. str1:='Hello';
  147. Delete(str1,1,-12);
  148. if str1<>'Hello' then
  149. _result := false;
  150. {************************* ansistring *************************}
  151. { try to delete from an empty string }
  152. str2:='';
  153. Delete(str2,0,12);
  154. if str2<>'' then
  155. _result := false;
  156. str2:='Hello';
  157. Delete(str2,0,12);
  158. if str2<>'Hello' then
  159. _result := false;
  160. str2:='Hello';
  161. Delete(str2,1,12);
  162. if str2<>'' then
  163. _result := false;
  164. str2:='Hello';
  165. Delete(str2,12,255);
  166. if str2<>'Hello' then
  167. _result := false;
  168. STR2:='Hello';
  169. Delete(STR2,-1,255);
  170. if STR2<>'Hello' then
  171. _result := false;
  172. STR2:='Hello';
  173. Delete(STR2,1,-12);
  174. if STR2<>'Hello' then
  175. _result := false;
  176. {************************* widestring *************************}
  177. {$ifdef haswidestring}
  178. { try to delete from an empty string }
  179. str3:='';
  180. Delete(str3,0,12);
  181. if str3<>'' then
  182. _result := false;
  183. str3:='Hello';
  184. Delete(str3,0,12);
  185. if str3<>'Hello' then
  186. _result := false;
  187. str3:='Hello';
  188. Delete(str3,1,12);
  189. if str3<>'' then
  190. _result := false;
  191. str3:='Hello';
  192. Delete(str3,12,255);
  193. if str3<>'Hello' then
  194. _result := false;
  195. str3:='Hello';
  196. Delete(str3,-1,255);
  197. if str3<>'Hello' then
  198. _result := false;
  199. str3:='Hello';
  200. Delete(str3,1,-12);
  201. if str3<>'Hello' then
  202. _result := false;
  203. {$endif}
  204. if not _result then
  205. fail
  206. else
  207. WriteLn('Success!');
  208. end;
  209. procedure test_copy;
  210. var
  211. _result : boolean;
  212. i: integer;
  213. begin
  214. Write('Copy tests...');
  215. _result := true;
  216. {************************* shortstring ************************}
  217. { try to copy from an empty string }
  218. str1:='';
  219. str1:=Copy(str1,1,12);
  220. if str1<>'' then
  221. _result := false;
  222. str1:='';
  223. str1:=Copy('Hello world',0,12);
  224. if str1<>'Hello world' then
  225. _result := false;
  226. str1:='';
  227. str1:=Copy('Hello world',1,12);
  228. if str1<>'Hello world' then
  229. _result := false;
  230. str1:='';
  231. str1:=Copy('Hello world',-12,12);
  232. if str1<>'Hello world' then
  233. _result := false;
  234. str1:='';
  235. str1:=Copy('Hello world',64,128);
  236. if str1<>'' then
  237. _result := false;
  238. str1:='';
  239. str1:=Copy('Hello world',1,-12);
  240. if str1<>'' then
  241. _result := false;
  242. str1:='';
  243. str1:=Copy('Hello world',-12,0);
  244. if str1<>'' then
  245. _result := false;
  246. str1:='';
  247. str1:=Copy('Hello world',7,11);
  248. if str1<>'world' then
  249. _result := false;
  250. str1:='';
  251. str1:=Copy('Hello world',1,11);
  252. if str1<>'Hello world' then
  253. _result := false;
  254. str1:='';
  255. str1:=Copy('',0,12);
  256. if str1<>'' then
  257. _result := false;
  258. {************************* ansistring *************************}
  259. { try to copy from an empty string }
  260. str2:='';
  261. str2:=Copy(str2,1,12);
  262. if str2<>'' then
  263. _result := false;
  264. str2:='';
  265. str2:=Copy('Hello world',0,12);
  266. if str2<>'Hello world' then
  267. _result := false;
  268. str2:='';
  269. str2:=Copy('Hello world',1,12);
  270. if str2<>'Hello world' then
  271. _result := false;
  272. str2:='';
  273. str2:=Copy('Hello world',-12,12);
  274. if str2<>'Hello world' then
  275. _result := false;
  276. str2:='';
  277. str2:=Copy('Hello world',64,128);
  278. if str2<>'' then
  279. _result := false;
  280. str2:='';
  281. str2:=Copy('Hello world',1,-12);
  282. if str2<>'' then
  283. _result := false;
  284. str2:='';
  285. str2:=Copy('Hello world',-12,0);
  286. if str2<>'' then
  287. _result := false;
  288. str2:='';
  289. str2:=Copy('Hello world',7,11);
  290. if str2<>'world' then
  291. _result := false;
  292. str2:='';
  293. str2:=Copy('Hello world',1,11);
  294. if str2<>'Hello world' then
  295. _result := false;
  296. str2:='';
  297. str2:=Copy('',0,12);
  298. if str2<>'' then
  299. _result := false;
  300. {************************* widestring *************************}
  301. {$ifdef haswidestring}
  302. { try to copy from an empty string }
  303. str3:='';
  304. str3:=Copy(str3,1,12);
  305. if str3<>'' then
  306. _result := false;
  307. str3:='';
  308. str3:=Copy('Hello world',0,12);
  309. if str3<>'Hello world' then
  310. _result := false;
  311. str3:='';
  312. str3:=Copy('Hello world',1,12);
  313. if str3<>'Hello world' then
  314. _result := false;
  315. str3:='';
  316. str3:=Copy('Hello world',-12,12);
  317. if str3<>'Hello world' then
  318. _result := false;
  319. str3:='';
  320. str3:=Copy('Hello world',64,128);
  321. if str3<>'' then
  322. _result := false;
  323. str3:='';
  324. str3:=Copy('Hello world',1,-12);
  325. if str3<>'' then
  326. _result := false;
  327. str3:='';
  328. str3:=Copy('Hello world',-12,0);
  329. if str3<>'' then
  330. _result := false;
  331. str3:='';
  332. str3:=Copy('Hello world',7,11);
  333. if str3<>'world' then
  334. _result := false;
  335. str3:='';
  336. str3:=Copy('Hello world',1,11);
  337. if str3<>'Hello world' then
  338. _result := false;
  339. str3:='';
  340. str3:=Copy('',0,12);
  341. if str3<>'' then
  342. _result := false;
  343. {$endif}
  344. if not _result then
  345. fail
  346. else
  347. WriteLn('Success!');
  348. end;
  349. procedure test_insert;
  350. var
  351. _result : boolean;
  352. i: integer;
  353. begin
  354. Write('Insert tests...');
  355. _result := true;
  356. {************************* shortstring ************************}
  357. str1:='Hello world';
  358. Insert(' this is my ',str1,-12);
  359. if str1<>' this is my Hello world' then
  360. _result := false;
  361. str1:='Hello world';
  362. Insert(' this is my ',str1,0);
  363. if str1<>' this is my Hello world' then
  364. _result := false;
  365. str1:='Hello world';
  366. Insert(' this is my ',str1,64);
  367. if str1<>'Hello world this is my ' then
  368. _result := false;
  369. str1:='Hello world';
  370. Insert(' this is my ',str1,300);
  371. if str1<>'Hello world this is my ' then
  372. _result := false;
  373. str1:='Hello world';
  374. Insert(' this is my ',str1,length(str1)+1);
  375. if str1<>'Hello world this is my ' then
  376. _result := false;
  377. str1:='Hello world';
  378. Insert('this is my ',str1,7);
  379. if str1<>'Hello this is my world' then
  380. _result := false;
  381. str1:='';
  382. Insert(' this is my ',str1,0);
  383. if str1<>' this is my ' then
  384. _result := false;
  385. str1:='';
  386. Insert(' this is my ',str1,length(str1));
  387. if str1<>' this is my ' then
  388. _result := false;
  389. str1:='';
  390. Insert(' this is my ',str1,32);
  391. if str1<>' this is my ' then
  392. _result := false;
  393. str1:='Hello world';
  394. Insert('',str1,0);
  395. if str1<>'Hello world' then
  396. _result := false;
  397. str1:='Hello world';
  398. Insert('',str1,7);
  399. if str1<>'Hello world' then
  400. _result := false;
  401. {************************* ansistring *************************}
  402. str2:='Hello world';
  403. Insert(' this is my ',str2,-12);
  404. if str2<>' this is my Hello world' then
  405. _result := false;
  406. str2:='Hello world';
  407. Insert(' this is my ',str2,0);
  408. if str2<>' this is my Hello world' then
  409. _result := false;
  410. str2:='Hello world';
  411. Insert(' this is my ',str2,64);
  412. if str2<>'Hello world this is my ' then
  413. _result := false;
  414. str2:='Hello world';
  415. Insert(' this is my ',str2,300);
  416. if str2<>'Hello world this is my ' then
  417. _result := false;
  418. str2:='Hello world';
  419. Insert(' this is my ',str2,length(str2)+1);
  420. if str2<>'Hello world this is my ' then
  421. _result := false;
  422. str2:='Hello world';
  423. Insert('this is my ',str2,7);
  424. if str2<>'Hello this is my world' then
  425. _result := false;
  426. str2:='';
  427. Insert(' this is my ',str2,0);
  428. if str2<>' this is my ' then
  429. _result := false;
  430. str2:='';
  431. Insert(' this is my ',str2,length(str2));
  432. if str2<>' this is my ' then
  433. _result := false;
  434. str2:='';
  435. Insert(' this is my ',str2,32);
  436. if str2<>' this is my ' then
  437. _result := false;
  438. str2:='Hello world';
  439. Insert('',str2,0);
  440. if str2<>'Hello world' then
  441. _result := false;
  442. str2:='Hello world';
  443. Insert('',str2,7);
  444. if str2<>'Hello world' then
  445. _result := false;
  446. {************************* widestring *************************}
  447. {$ifdef haswidestring}
  448. str3:='Hello world';
  449. Insert(' this is my ',str3,-12);
  450. if str3<>' this is my Hello world' then
  451. _result := false;
  452. str3:='Hello world';
  453. Insert(' this is my ',str3,0);
  454. if str3<>' this is my Hello world' then
  455. _result := false;
  456. str3:='Hello world';
  457. Insert(' this is my ',str3,64);
  458. if str3<>'Hello world this is my ' then
  459. _result := false;
  460. str3:='Hello world';
  461. Insert(' this is my ',str3,300);
  462. if str3<>'Hello world this is my ' then
  463. _result := false;
  464. str3:='Hello world';
  465. Insert(' this is my ',str3,length(str3)+1);
  466. if str3<>'Hello world this is my ' then
  467. _result := false;
  468. str3:='Hello world';
  469. Insert('this is my ',str3,7);
  470. if str3<>'Hello this is my world' then
  471. _result := false;
  472. str3:='';
  473. Insert(' this is my ',str3,0);
  474. if str3<>' this is my ' then
  475. _result := false;
  476. str3:='';
  477. Insert(' this is my ',str3,length(str3));
  478. if str3<>' this is my ' then
  479. _result := false;
  480. str3:='';
  481. Insert(' this is my ',str3,32);
  482. if str3<>' this is my ' then
  483. _result := false;
  484. str3:='Hello world';
  485. Insert('',str3,0);
  486. if str3<>'Hello world' then
  487. _result := false;
  488. str3:='Hello world';
  489. Insert('',str3,7);
  490. if str3<>'Hello world' then
  491. _result := false;
  492. {$endif}
  493. if not _result then
  494. fail
  495. else
  496. WriteLn('Success!');
  497. end;
  498. procedure test_pos;
  499. var
  500. _result : boolean;
  501. position: integer;
  502. begin
  503. Write('Pos tests...');
  504. _result := true;
  505. {************************* shortstring ************************}
  506. str1:='Hello world';
  507. position:=Pos('',str1);
  508. if position <> 0 then
  509. _result := false;
  510. str1:='';
  511. position:=Pos('',str1);
  512. if position <> 0 then
  513. _result := false;
  514. str1:='Hello world';
  515. position:=Pos('world',str1);
  516. if position <> 7 then
  517. _result := false;
  518. str1:='Hello world';
  519. position:=Pos('world',str1);
  520. if position <> 7 then
  521. _result := false;
  522. str1:='Hello world';
  523. position:=Pos('worldx',str1);
  524. if position <> 0 then
  525. _result := false;
  526. str1:='';
  527. position:=Pos('worldx',str1);
  528. if position <> 0 then
  529. _result := false;
  530. {************************* ansistring *************************}
  531. str2:='Hello world';
  532. position:=Pos('',str2);
  533. if position <> 0 then
  534. _result := false;
  535. str2:='';
  536. position:=Pos('',str2);
  537. if position <> 0 then
  538. _result := false;
  539. str2:='Hello world';
  540. position:=Pos('world',str2);
  541. if position <> 7 then
  542. _result := false;
  543. str2:='Hello world';
  544. position:=Pos('world',str2);
  545. if position <> 7 then
  546. _result := false;
  547. str2:='Hello world';
  548. position:=Pos('worldx',str2);
  549. if position <> 0 then
  550. _result := false;
  551. str2:='';
  552. position:=Pos('worldx',str2);
  553. if position <> 0 then
  554. _result := false;
  555. {************************* widestring *************************}
  556. {$ifdef haswidestring}
  557. str3:='Hello world';
  558. position:=Pos('',str3);
  559. if position <> 0 then
  560. _result := false;
  561. str3:='';
  562. position:=Pos('',str3);
  563. if position <> 0 then
  564. _result := false;
  565. str3:='Hello world';
  566. position:=Pos('world',str3);
  567. if position <> 7 then
  568. _result := false;
  569. str3:='Hello world';
  570. position:=Pos('world',str3);
  571. if position <> 7 then
  572. _result := false;
  573. str3:='Hello world';
  574. position:=Pos('worldx',str3);
  575. if position <> 0 then
  576. _result := false;
  577. str3:='';
  578. position:=Pos('worldx',str3);
  579. if position <> 0 then
  580. _result := false;
  581. {$endif}
  582. if not _result then
  583. fail
  584. else
  585. WriteLn('Success!');
  586. end;
  587. procedure test_chr;
  588. var
  589. c: char;
  590. _result : boolean;
  591. begin
  592. Write('Chr tests...');
  593. _result := true;
  594. { c:=chr($3074);
  595. if c<>'t' then
  596. _result := false;
  597. The above statement compile under Delphi, and it
  598. should not imho. Freepascal gives a range-check
  599. error, as it should.
  600. }
  601. if chr(76)<>'L' then
  602. _result := false;
  603. if _result = false then
  604. fail
  605. else
  606. WriteLn('Success!');
  607. end;
  608. procedure test_concat;
  609. var
  610. _result : boolean;
  611. i: integer;
  612. begin
  613. Write('Concat tests...');
  614. _result := true;
  615. if not _result then
  616. fail
  617. else
  618. WriteLn('Success!');
  619. end;
  620. Begin
  621. test_delete;
  622. test_stringofchar;
  623. test_copy;
  624. test_insert;
  625. test_pos;
  626. test_chr;
  627. end.