tstring6.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. { %VERSION=1.1 }
  2. Program widetest;
  3. uses
  4. erroru;
  5. { -------------------------------------------------------------------
  6. General stuff
  7. ------------------------------------------------------------------- }
  8. Procedure DoRef (P : Pointer);
  9. Type PLongint = ^Longint;
  10. begin
  11. If P=Nil then
  12. Writeln ('(Ref : Empty string)')
  13. else
  14. begin
  15. {$ifdef fpc}
  16. {$if defined(ver1_0) or defined(ver1_9_4)}
  17. Writeln (' (Ref: ',Plongint(Longint(P)-4)^,',Len: ',PLongint(Longint(P)-8)^,')');
  18. {$else}
  19. Writeln (' (Ref: ',PSizeInt(PtrInt(P)-sizeof(SizeInt)*2)^,',Len: ',PSizeInt(PtrInt(P)-sizeof(SizeInt))^,')');
  20. {$endif}
  21. {$else}
  22. Writeln (' (Ref: ',Plongint(Longint(P)-8)^,',Len: ',PLongint(Longint(P)-4)^ div sizeof(WideChar),')');
  23. {$endif}
  24. end;
  25. end;
  26. { -------------------------------------------------------------------
  27. Initialize/Finalize test
  28. ------------------------------------------------------------------- }
  29. Procedure TestInitFinal;
  30. Type ARec = record
  31. FirstName, LastName : WideString;
  32. end;
  33. AnArray = Array [1..10] of WideString;
  34. Var
  35. S : WideString;
  36. AR : Arec;
  37. AAR : AnArray;
  38. I : longint;
  39. Begin
  40. S:='This is an WideString!';
  41. If Pointer(AR.FirstNAme)<>Nil then
  42. Writeln ('AR.FirstName not OK');
  43. If Pointer(AR.LastName)<>Nil then
  44. Writeln ('AR.LastName not OK');
  45. for I:=1 to 10 do
  46. If Pointer(AAR[I])<>Nil then
  47. Writeln ('Array (',I,') NOT ok');
  48. AR.FirstName:='Napoleon';
  49. AR.LastName:='Bonaparte';
  50. For I:=1 to 10 do
  51. AAR[I]:='Yet another WideString';
  52. Writeln ('S : ',S);
  53. Writeln (AR.FirstName, ' ', AR.LastName);
  54. For I:=1 to 10 do
  55. Writeln (I:2,' : ',AAR[i]);
  56. end;
  57. { -------------------------------------------------------------------
  58. Parameter passing test
  59. ------------------------------------------------------------------- }
  60. Procedure TestVarParam (Var Sv : WideString);
  61. Var LS : WideString;
  62. begin
  63. Write ('TestVarParam : Got S="',Sv,'"');
  64. DoRef(Pointer(Sv));
  65. Sv:='This is a var parameter WideString';
  66. Write ('S Changed to : ',Sv);
  67. DoRef (Pointer(Sv));
  68. Ls:=Sv;
  69. Write ('Assigned to local var: "',ls,'"');
  70. DoRef (Pointer(Sv));
  71. end;
  72. Procedure TestValParam (S : WideString);
  73. Var LS : WideString;
  74. begin
  75. Write ('TestValParam : Got S="',S,'"');
  76. S:='This is a value parameter WideString';
  77. Write ('S Changed to : ',S);
  78. DoRef(Pointer(S));
  79. Ls:=S;
  80. Write ('Assigned to local var: "',ls,'"');
  81. DoRef(Pointer(S));
  82. end;
  83. Procedure TestConstParam (Const Sc : WideString);
  84. Var LS : WideString;
  85. begin
  86. Write ('TestConstParam : Got S="',Sc,'"');
  87. DoRef(Pointer(Sc));
  88. Ls:=Sc;
  89. Write ('Assigned to local var: "',ls,'"');
  90. DoRef(Pointer(Sc));
  91. end;
  92. Procedure TestParams;
  93. Var S : WideString;
  94. Mem : SizeUInt;
  95. begin
  96. Mem:=0;
  97. DoMem(Mem);
  98. S :='This is another WideString';
  99. Writeln ('Calling testvalparam with "',s,'"');
  100. testvalparam (s);
  101. DoMem(Mem);
  102. Writeln ('Calling testConstparam with "',s,'"');
  103. testconstparam (s);
  104. DoMem(Mem);
  105. Writeln ('Calling testvarparam with "',s,'"');
  106. testvarparam (s);
  107. Writeln ('TestVarParam returned with "',S,'"');
  108. DoMem(Mem);
  109. end;
  110. { -------------------------------------------------------------------
  111. Comparision operators test
  112. ------------------------------------------------------------------- }
  113. Procedure TestCompare;
  114. Const S1 : WideString = 'Teststring 1';
  115. S2 : WideString = 'Teststring 1';
  116. S3 : WideString = 'Teststring 2';
  117. S4 : WideString = '';
  118. PC : Pchar = 'Teststring 1';
  119. Var S,T : WideString;
  120. ss : Shortstring;
  121. begin
  122. If S1=S2 then writeln ('S1 and S2 are the same');
  123. If S4='' then Writeln ('S4 is empty. OK');
  124. If Not(S4='Non-empty') then writeln ('S4 is not non-empty');
  125. if S3='Teststring 2' then writeln('S3 equals "Teststring 2". OK.');
  126. Write ('S3<>S2 : ');
  127. If S2<>S3 Then writeln ('OK') else writeln ('NOT OK');
  128. Write ('S3>S2 : ');
  129. If (S3>S2) Then Writeln ('OK') else writeln ('NOT OK');
  130. Write ('S1<S3 : ');
  131. if (S1<S3) Then writeln ('OK') else writeln ('NOT OK');
  132. S:=S2;
  133. T:=S;
  134. Write ('Assigned S to T. ');Doref(Pointer(T));
  135. If S=T then Writeln ('S=T, OK');
  136. SS:='Teststring 1';
  137. If SS=S then
  138. Writeln ('Shortstring and WideString are the same. OK')
  139. else
  140. Writeln ('Shortstring and WideString NOT equal. PROBLEM !');
  141. If S=PC then
  142. Writeln ('Pchar and WideString are the same. OK')
  143. else
  144. Writeln ('Pchar and WideString NOT equal. PROBLEM !');
  145. end;
  146. { -------------------------------------------------------------------
  147. Type conversion test
  148. ------------------------------------------------------------------- }
  149. Procedure DoPchar (P : Pchar);
  150. begin
  151. Writeln ('DoPchar : Got : "',P,'"');
  152. end;
  153. Procedure TestConversion;
  154. Var Pstr : Pchar;
  155. Sstr : String[40];
  156. Astr : WideString;
  157. Const PC : Pchar = 'A PCHAR constant string';
  158. begin
  159. Writeln ('Astr empty : "',Astr,'"');
  160. Pstr:=PChar(Astr);
  161. Writeln ('WideString Assigned to Pchar : "',Pstr,'"');
  162. DoPchar(Pchar(Astr));
  163. Astr:='An WideString';
  164. Writeln ('Astr: "',Astr,'"');
  165. Pstr:=PChar(Astr);
  166. Writeln ('WideString Assigned to Pchar : "',Pstr,'"');
  167. DoPchar(Pchar(Astr));
  168. SStr:='A ShortString';
  169. Writeln ('Shortstring : "',Sstr,'"');
  170. Astr:=Sstr;
  171. Write ('ShortString assigned to WideString : "',Astr,'"');
  172. DoRef(Pointer(Astr));
  173. Astr:=PC;
  174. Write ('PChar assigned to WideString : "',Astr,'"');
  175. DoRef(Pointer(Astr));
  176. end;
  177. { -------------------------------------------------------------------
  178. Adding of strings test.
  179. ------------------------------------------------------------------- }
  180. Procedure TestAdd;
  181. Const S1 : WideString = 'This is WideString 1 ';
  182. S2 : WideString = 'This is WideString 2 ';
  183. S3 : WideString = 'This is WideString 3';
  184. Var S : WideString;
  185. S4 : String;
  186. begin
  187. S:=S1+S2;
  188. //!! Reference count is 2, should be 1...
  189. Write ('Adding S1+S2 : ',S,' '); DoRef(Pointer(S));
  190. S:=S1+S2+S3;
  191. Write ('Adding S1+S2+S3 : ',S,' '); DoRef(Pointer(S));
  192. S:=S+'...Added tail';
  193. Write ('Added tail to S ! : ',S);DoRef(Pointer(S));
  194. S4:=' This is a shortstring';
  195. //!! This crashes the program...
  196. S:=S1+S4;
  197. Write ('Adding S1+S4 : ',S,' '); DoRef(Pointer(S));
  198. S:=S1+'@';
  199. Write ('Adding S1+''@'' : ',S,' '); DoRef(Pointer(S));
  200. end;
  201. { -------------------------------------------------------------------
  202. SetLength test.
  203. ------------------------------------------------------------------- }
  204. Procedure TestSetlength;
  205. Const S1 : WideString = 'This is WideString 1';
  206. S2 : WideString = 'This is WideString 2 and it is longer';
  207. Var S : WideString;
  208. begin
  209. Setlength(S,length(S1));
  210. Write ('Set length of s to ',length(s1));Doref(pointer(s));
  211. Move (Pointer(S1)^,Pointer(S)^,(Length(S1)+1)*sizeof(WideChar));
  212. Write ('S = "',S,'" '); DoRef(Pointer(S));
  213. Setlength(S,length(S2));
  214. Write ('Set length of s to ',length(s2));Doref(pointer(s));
  215. Move (Pointer(S2)^,Pointer(S)^,(Length(S2)+1)*sizeof(WideChar));
  216. Write ('S = "',S,'" '); DoRef(Pointer(S));
  217. SetLength(S,10);
  218. Write ('Set length of s to 10 ');Doref(pointer(s));
  219. Write ('S = "',S,'" '); DoRef(Pointer(S));
  220. SetLength(S,0);
  221. Write ('Set length of S to 0 ');Doref(Pointer(S));
  222. Write ('S = "',S,'" ');Doref(Pointer(s));
  223. end;
  224. { -------------------------------------------------------------------
  225. Index test.
  226. ------------------------------------------------------------------- }
  227. Procedure testIndex;
  228. Var S,T : WideString;
  229. I,Len : longint;
  230. begin
  231. S:='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  232. Write ('S = "',S,'" ');doref(pointer(S));
  233. Write ('S = "');
  234. Len:=Length(S);
  235. For I:=1 to Len do
  236. Write(S[i]);
  237. write ('" ');Doref(pointer(S));
  238. Write ('Inverting S, ');
  239. For I:=1 to Len do
  240. S[i]:='A';
  241. // Asc(Ord('Z')+1-i);
  242. Write ('S = "',S,'" ');doref(pointer(S));
  243. T:=S;
  244. Write ('Assigned S to T '); Doref(Pointer(S));
  245. Write ('Again inverting S. ');
  246. For I:=1 to Len do
  247. S[i]:='B';
  248. Write ('S = "',S,'" ');doref(pointer(S));
  249. Write ('T = "',T,'" ');doref(pointer(T));
  250. end;
  251. { -------------------------------------------------------------------
  252. Adding in expressions test.
  253. ------------------------------------------------------------------- }
  254. Procedure TestAddExpr;
  255. Const S1 : WideString = 'ABC';
  256. S2 : WideString = 'DEF';
  257. OK = 'OK';
  258. NOK = 'NOK';
  259. Var I : Integer;
  260. S3 : WideString;
  261. mem : SizeUInt;
  262. begin
  263. mem:=0;
  264. DoMem(Mem);
  265. S3 := 'ABCDEF';
  266. Write ('S1+S2=S3 :');
  267. If S1+S2=S3 then writeln (ok) else writeln (nok);
  268. Write ('S1+S2=ABCDEF');
  269. If S1+S2='ABCDEF' then writeln (ok) else writeln (nok);
  270. Write ('Testing repeat');
  271. I:=0;
  272. S3:='';
  273. Repeat
  274. Inc(i);
  275. If I=10 then s3:='ABCDEF';
  276. until S1+S2=S3;
  277. Writeln (' Done.');
  278. I:=2;
  279. S3:='';
  280. Write ('Testing While');
  281. While S1+S2<>S3 do
  282. begin
  283. INc(i);
  284. If I=10 then s3:='ABCDEF';
  285. end;
  286. Writeln (' Done');
  287. end;
  288. Procedure TestStdFunc;
  289. Var S,T : WideString;
  290. SS : ShortString;
  291. C : Char;
  292. Ca : Cardinal;
  293. L : longint;
  294. I : Integer;
  295. W : Word;
  296. B : Byte;
  297. R : Real;
  298. D : Double;
  299. E : Extended;
  300. Si : Single;
  301. Co : Comp;
  302. TempMem:SizeUInt;
  303. begin
  304. TempMem:=0;
  305. DoMem(TempMem);
  306. S:='ABCDEF';
  307. Write ('S = "',S,'"');Doref(Pointer(S));
  308. T:=Copy(S,1,3);
  309. Write ('T : "',T,'"');DoRef(Pointer(T));
  310. T:=Copy(S,3,3);
  311. Write ('T : "',T,'"');DoRef(Pointer(T));
  312. T:=Copy(S,3,6);
  313. Write ('T : "',T,'"');DoRef(Pointer(T));
  314. Writeln ('Inserting "123" in S at pos 4');
  315. Insert ('123',S,4);
  316. Write ('S = "',S,'"');DoRef(Pointer(S));
  317. Writeln ('Deleting 3 characters From S starting Pos 4');
  318. Delete (S,4,3);
  319. Write ('S = "',S,'"');Doref(Pointer(S));
  320. Writeln ('Pos ''DE'' in S is : ',Pos('DE',S));
  321. Write ('S = "',S,'"');Doref(Pointer(S));
  322. Writeln ('Setting T to ''DE''.');
  323. T:='DE';
  324. //!! Here something weird is happening ? S is lost ???
  325. Writeln('***');
  326. Writeln ('Pos T in S is : ',Pos(T,S));
  327. Write ('S = "',S,'"');Doref(Pointer(S));
  328. Writeln ('Setting T to ''D''.');
  329. T:='D';
  330. Writeln ('Pos T in S is : ',Pos(T,S));
  331. Write ('S = "',S,'"');Doref(Pointer(S));
  332. Writeln ('Setting T to ''DA''.');
  333. T:='DA';
  334. Writeln ('Pos T in S is : ',Pos(T,S));
  335. Write ('S = "',S,'"');Doref(Pointer(S));
  336. Writeln ('SS:=''DE''');
  337. Writeln('***');
  338. SS:='DE';
  339. Writeln ('Pos SS in S is : ',Pos(SS,S));
  340. Write ('S = "',S,'"');Doref(Pointer(S));
  341. Writeln ('C:=''D''');
  342. C:='D';
  343. Writeln ('Pos C in S is : ',Pos(C,S));
  344. Write ('S = "',S,'"');Doref(Pointer(S));
  345. Writeln ('Pos ''D'' in S is : ',Pos('D',S));
  346. Write ('S = "',S,'"');Doref(Pointer(S));
  347. Write ('str(Ca,S)= ');
  348. ca:=1;
  349. str(Ca,S);
  350. Writeln (S);
  351. Write ('str(L,S)= ');
  352. L:=2;
  353. str(L,S);
  354. Writeln (S);
  355. Write ('str(I,S)= ');
  356. I:=3;
  357. str(I,S);
  358. Writeln (S);
  359. Write ('str(W,S)= ');
  360. W:=4;
  361. str(W,S);
  362. Writeln (S);
  363. Write ('str(R,S)= ');
  364. R:=1.0;
  365. str(R,S);
  366. Writeln (S);
  367. Write ('str(D,S)= ');
  368. D:=2.0;
  369. str(D,S);
  370. Writeln (S);
  371. Write ('str(E,S)= ');
  372. E:=3.0;
  373. str(E,S);
  374. Writeln (S);
  375. Write ('str(Co,S)= ');
  376. {$ifdef cpui386}
  377. Co:=4.0;
  378. {$else cpui386}
  379. Co := 4;
  380. {$endif cpui386}
  381. str(Co,S);
  382. Writeln (S);
  383. Write ('str(Si,S)= ');
  384. Si:=5.0;
  385. str(Si,S);
  386. Writeln (S);
  387. end;
  388. Var GlobalStartMem,StartMem : SizeUInt;
  389. begin
  390. GlobalStartMem:=0;
  391. StartMem:=0;
  392. DoMem(GlobalStartMem);
  393. DoMem(StartMem);
  394. Writeln ('Testing Initialize/Finalize.');
  395. TestInitFinal;
  396. Write ('End of Initialize/finalize test : ');DoMem(StartMem);
  397. Writeln;Writeln ('Testing parameter passing.');
  398. TestParams;
  399. Write ('End of Parameter passing test : ');DoMem(StartMem);
  400. Writeln;Writeln ('Testing comparision operators');
  401. TestCompare;
  402. Write ('End of compare test : ');DoMem(StartMem);
  403. Writeln;Writeln ('Testing setlength of WideStrings');
  404. TestSetLength;
  405. Write ('End of setlength test : ');DoMem(StartMem);
  406. Writeln;Writeln ('Testing Adding of WideStrings');
  407. TestAdd;
  408. Write ('End of adding test : ');DoMem(StartMem);
  409. Writeln;Writeln ('Testing Adding of WideStrings in expressions');
  410. TestAddExpr;
  411. Write ('End of adding in expressions test : ');DoMem(StartMem);
  412. Writeln;Writeln ('Testing type conversion.');
  413. // TestConversion;
  414. Write ('End of typeconversion test : ');DoMem(StartMem);
  415. Writeln;Writeln ('Testing indexed access.');
  416. TestIndex;
  417. Write ('End of index access test : ');DoMem(StartMem);
  418. Writeln;Writeln ('Testing standard functions.');
  419. TestStdfunc;
  420. Write ('End of standard functions: ');DoMem(StartMem);
  421. Write ('For the whole program ');DoMem(GlobalStartMem);
  422. end.