tstring.pp 16 KB

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