tcnvstr1.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. program tcnvstr1;
  2. {****************************************************************}
  3. { CODE GENERATOR TEST PROGRAM }
  4. { Copyright (c) 2002, Carl Eric Codere }
  5. {****************************************************************}
  6. { NODE TESTED : secondtypeconvert() -> second_string_string }
  7. {****************************************************************}
  8. { PRE-REQUISITES: secondload() }
  9. { secondassign() }
  10. { secondtypeconv() }
  11. {****************************************************************}
  12. { DEFINES: }
  13. { FPC = Target is FreePascal compiler }
  14. {****************************************************************}
  15. { REMARKS: Same type short conversion is not tested, except for }
  16. { shortstrings , since it requires special handling. }
  17. { }
  18. { }
  19. {****************************************************************}
  20. {$ifdef fpc}
  21. {$mode objfpc}
  22. {$ifndef ver1_0}
  23. {$define haswidestring}
  24. {$endif}
  25. {$else}
  26. {$ifndef ver70}
  27. {$define haswidestring}
  28. {$endif}
  29. {$endif}
  30. {$define hasshortstring}
  31. uses
  32. jdk15;
  33. {$H+}
  34. {$macro on}
  35. {$define writeln:=JLSystem.fout.println}
  36. {$define write:=JLSystem.fout.print}
  37. const
  38. { exactly 255 characters in length }
  39. BIG_STRING =
  40. ' This is a small text documentation to verify the validity of'+
  41. ' the string conversion routines. Of course the conversion routines'+
  42. ' should normally work like a charm, and this can only test that there'+
  43. ' aren''t any problems with maximum length strings. This fix!';
  44. { < 255 characters in length }
  45. SMALL_STRING = 'This is a small hello!';
  46. { > 255 characters in length }
  47. HUGE_STRING_END = ' the goal of this experiment';
  48. HUGE_STRING =
  49. ' This is a huge text documentation to verify the validity of'+
  50. ' the string conversion routines. Of course the conversion routines'+
  51. ' should normally work like a charm, and this can only test that there'+
  52. ' aren''t any problems with maximum length strings. I hope you understand'+
  53. HUGE_STRING_END;
  54. EMPTY_STRING = '';
  55. type
  56. shortstr = string[127];
  57. var
  58. {$ifdef hasshortstring}
  59. s2: shortstr;
  60. {$endif}
  61. str_ansi: ansistring;
  62. {$ifdef hasshortstring}
  63. str_short: shortstring;
  64. {$endif}
  65. {$ifdef haswidestring}
  66. str_wide : widestring;
  67. {$endif}
  68. procedure fail;
  69. begin
  70. Raise JLException.create('failure');
  71. end;
  72. {$ifdef hasshortstring}
  73. procedure test_ansi_to_short;
  74. var
  75. p: pchar;
  76. begin
  77. {************************************************************************}
  78. { ansistring -> shortstring }
  79. {************************************************************************}
  80. WriteLn('Test ansistring -> shortstring');
  81. { ansistring -> shortstring }
  82. str_short := '';
  83. str_ansi:='';
  84. str_ansi := SMALL_STRING;
  85. str_short:=str_ansi;
  86. Write('small ansistring -> shortstring...');
  87. if str_short = str_ansi then
  88. WriteLn('Success.')
  89. else
  90. fail;
  91. str_short := '';
  92. str_ansi:='';
  93. str_ansi := EMPTY_STRING;
  94. str_short:=str_ansi;
  95. Write('empty ansistring -> shortstring...');
  96. if str_short = str_ansi then
  97. WriteLn('Success.')
  98. else
  99. fail;
  100. str_short := '';
  101. str_ansi:='';
  102. str_ansi := BIG_STRING;
  103. str_short:=str_ansi;
  104. Write('big ansistring -> shortstring...');
  105. jlsystem.fout.println;
  106. jlsystem.fout.println('const: '+BIG_STRING);
  107. jlsystem.fout.println('ansi : '+unicodestring(str_ansi));
  108. jlsystem.fout.println('short: '+unicodestring(str_short));
  109. if str_short = str_ansi then
  110. WriteLn('Success.')
  111. else
  112. fail;
  113. Write('huge ansistring -> shortstring...');
  114. str_short := '';
  115. str_ansi:='';
  116. str_ansi := HUGE_STRING;
  117. str_short:=str_ansi;
  118. { Delphi 3/Delphi 6 does not consider these as the same string }
  119. if str_short <> str_ansi then
  120. WriteLn('Success.')
  121. else
  122. fail;
  123. {}
  124. s2 := '';
  125. str_ansi:='';
  126. str_ansi := SMALL_STRING;
  127. s2:=str_ansi;
  128. Write('small ansistring -> shortstring...');
  129. if s2 = str_ansi then
  130. WriteLn('Success.')
  131. else
  132. fail;
  133. s2 := '';
  134. str_ansi:='';
  135. str_ansi := EMPTY_STRING;
  136. s2:=str_ansi;
  137. Write('empty ansistring -> shortstring...');
  138. if s2 = str_ansi then
  139. WriteLn('Success.')
  140. else
  141. fail;
  142. str_ansi:='';
  143. p:=pchar(str_ansi);
  144. Write('empty ansistring -> pchar...');
  145. if p^<>#0 then
  146. fail;
  147. if p[0]<>#0 then
  148. fail
  149. else
  150. Writeln('Success');
  151. s2 := '';
  152. str_ansi:='';
  153. str_ansi := BIG_STRING;
  154. s2:=str_ansi;
  155. Write('big ansistring -> shortstring...');
  156. { Should fail, since comparing different string lengths }
  157. if s2 <> str_ansi then
  158. WriteLn('Success.')
  159. else
  160. fail;
  161. str_ansi := BIG_STRING;
  162. Write('big ansistring -> pchar...');
  163. p:=pchar(str_ansi);
  164. if p^<>' ' then
  165. fail;
  166. if p[0]<>' ' then
  167. fail;
  168. if length(p)<>length(BIG_STRING) then
  169. fail
  170. else
  171. Writeln('Success');
  172. s2 := '';
  173. str_ansi:='';
  174. str_ansi := HUGE_STRING;
  175. s2:=str_ansi;
  176. Write('huge ansistring -> shortstring...');
  177. { Should fail, since comparing different string lengths }
  178. if s2 <> str_ansi then
  179. WriteLn('Success.')
  180. else
  181. fail;
  182. end;
  183. procedure test_short_to_short;
  184. begin
  185. {************************************************************************}
  186. { shortstring -> shortstring }
  187. {************************************************************************}
  188. WriteLn('Test shortstring -> shortstring...');
  189. { shortstring -> shortstring }
  190. str_short := '';
  191. s2:='';
  192. s2 := SMALL_STRING;
  193. str_short:=s2;
  194. Write('small shortstring -> shortstring...');
  195. if str_short = s2 then
  196. WriteLn('Success.')
  197. else
  198. fail;
  199. str_short := '';
  200. s2:='';
  201. s2 := EMPTY_STRING;
  202. str_short:=s2;
  203. Write('empty shortstring -> shortstring...');
  204. if str_short = s2 then
  205. WriteLn('Success.')
  206. else
  207. fail;
  208. {$ifdef fpc}
  209. { Delphi does not compile these }
  210. str_short := '';
  211. s2:='';
  212. s2 := BIG_STRING;
  213. str_short:=s2;
  214. Write('big shortstring -> shortstring...');
  215. if str_short = s2 then
  216. WriteLn('Success.')
  217. else
  218. fail;
  219. str_short := '';
  220. s2:='';
  221. s2 := HUGE_STRING;
  222. str_short:=s2;
  223. Write('huge shortstring -> shortstring...');
  224. { Delphi 3/Delphi 6 does not consider these as the same string }
  225. if str_short = s2 then
  226. WriteLn('Success.')
  227. else
  228. fail;
  229. {$endif}
  230. s2 := '';
  231. str_short:='';
  232. str_short := SMALL_STRING;
  233. Write('small shortstring -> shortstring...');
  234. s2:=str_short;
  235. if s2 = str_short then
  236. WriteLn('Success.')
  237. else
  238. fail;
  239. s2 := '';
  240. str_short:='';
  241. str_short := EMPTY_STRING;
  242. Write('empty shortstring -> shortstring...');
  243. s2:=str_short;
  244. if s2 = str_short then
  245. WriteLn('Success.')
  246. else
  247. fail;
  248. s2 := '';
  249. str_short:='';
  250. str_short := BIG_STRING;
  251. Write('big shortstring -> shortstring...');
  252. s2:=str_short;
  253. { Should fail, since comparing different string lengths }
  254. if s2 <> str_short then
  255. WriteLn('Success.')
  256. else
  257. fail;
  258. {$ifdef fpc}
  259. s2 := '';
  260. str_short:='';
  261. writeln(length(ShortstringClass(@str_short).fdata));
  262. writeln(length(str_short));
  263. str_short := HUGE_STRING;
  264. writeln(length(ShortstringClass(@str_short).fdata));
  265. writeln(length(str_short));
  266. Write('huge shortstring -> shortstring...');
  267. s2:=str_short;
  268. writeln(unicodestring(s2));
  269. writeln(unicodestring(str_short));
  270. { Should fail, since comparing different string lengths }
  271. if s2 <> str_short then
  272. WriteLn('Success.')
  273. else
  274. fail;
  275. {$endif}
  276. end;
  277. procedure test_short_to_ansi;
  278. begin
  279. {************************************************************************}
  280. { shortstring -> ansistring }
  281. {************************************************************************}
  282. WriteLn('Test shortstring -> ansistring');
  283. Write('small shortstring -> ansistring...');
  284. { shortstring -> ansistring }
  285. str_short := SMALL_STRING;
  286. str_ansi:=str_short;
  287. if str_short = str_ansi then
  288. WriteLn('Success.')
  289. else
  290. fail;
  291. Write('empty shortstring -> ansistring...');
  292. str_short := EMPTY_STRING;
  293. str_ansi:=str_short;
  294. if str_short = str_ansi then
  295. WriteLn('Success.')
  296. else
  297. fail;
  298. Write('big shortstring -> ansistring...');
  299. str_short := BIG_STRING;
  300. str_ansi:=str_short;
  301. if str_short = str_ansi then
  302. WriteLn('Success.')
  303. else
  304. fail;
  305. Write('small shortstring -> ansistring...');
  306. { shortstring -> ansistring }
  307. s2 := SMALL_STRING;
  308. str_ansi:=s2;
  309. if s2 = str_ansi then
  310. WriteLn('Success.')
  311. else
  312. fail;
  313. Write('empty shortstring -> ansistring...');
  314. s2 := EMPTY_STRING;
  315. str_ansi:=s2;
  316. if s2 = str_ansi then
  317. WriteLn('Success.')
  318. else
  319. fail;
  320. end;
  321. {$endif}
  322. {$ifdef haswidestring}
  323. procedure test_wide_to_ansi;
  324. begin
  325. {************************************************************************}
  326. { widestring -> ansistring }
  327. {************************************************************************}
  328. WriteLn('Test widestring -> ansistring');
  329. Write('small widestring -> ansistring...');
  330. { widestring -> ansistring }
  331. str_wide := SMALL_STRING;
  332. str_ansi:=str_wide;
  333. if str_wide = str_ansi then
  334. WriteLn('Success.')
  335. else
  336. fail;
  337. Write('empty widestring -> ansistring...');
  338. str_wide := EMPTY_STRING;
  339. str_ansi:=str_wide;
  340. if str_wide = str_ansi then
  341. WriteLn('Success.')
  342. else
  343. fail;
  344. Write('big widestring -> ansistring...');
  345. str_wide := BIG_STRING;
  346. str_ansi:=str_wide;
  347. if str_wide = str_ansi then
  348. WriteLn('Success.')
  349. else
  350. fail;
  351. Write('huge widestring -> ansistring...');
  352. str_wide := HUGE_STRING;
  353. str_ansi:=str_wide;
  354. if str_wide = str_ansi then
  355. WriteLn('Success.')
  356. else
  357. fail;
  358. end;
  359. {$ifdef hasshortstring}
  360. procedure test_short_to_wide;
  361. begin
  362. {************************************************************************}
  363. { shortstring -> widestring }
  364. {************************************************************************}
  365. WriteLn('Test shortstring -> widestring');
  366. Write('small shortstring -> widestring...');
  367. { shortstring -> widestring }
  368. str_short := SMALL_STRING;
  369. str_wide:=str_short;
  370. if str_short = str_wide then
  371. WriteLn('Success.')
  372. else
  373. fail;
  374. Write('empty shortstring -> widestring...');
  375. str_short := EMPTY_STRING;
  376. str_wide:=str_short;
  377. if str_short = str_wide then
  378. WriteLn('Success.')
  379. else
  380. fail;
  381. Write('big shortstring -> widestring...');
  382. str_short := BIG_STRING;
  383. str_wide:=str_short;
  384. if str_short = str_wide then
  385. WriteLn('Success.')
  386. else
  387. fail;
  388. {$ifdef hasshortstring}
  389. Write('small shortstring -> widestring...');
  390. { shortstring -> widestring }
  391. s2 := SMALL_STRING;
  392. str_wide:=s2;
  393. if s2 = str_wide then
  394. WriteLn('Success.')
  395. else
  396. fail;
  397. Write('empty shortstring -> widestring...');
  398. s2 := EMPTY_STRING;
  399. str_wide:=s2;
  400. if s2 = str_wide then
  401. WriteLn('Success.')
  402. else
  403. fail;
  404. {$endif}
  405. end;
  406. {$endif}
  407. procedure test_ansi_to_wide;
  408. begin
  409. {************************************************************************}
  410. { ansistring -> widestring }
  411. {************************************************************************}
  412. WriteLn('Test ansistring -> widestring');
  413. Write('small ansistring -> widestring...');
  414. { ansistring -> widestring }
  415. str_ansi := SMALL_STRING;
  416. str_wide:=str_ansi;
  417. if str_ansi = str_wide then
  418. WriteLn('Success.')
  419. else
  420. fail;
  421. Write('empty ansistring -> widestring...');
  422. str_ansi := EMPTY_STRING;
  423. str_wide:=str_ansi;
  424. if str_ansi = str_wide then
  425. WriteLn('Success.')
  426. else
  427. fail;
  428. Write('big ansistring -> widestring...');
  429. str_ansi := BIG_STRING;
  430. str_wide:=str_ansi;
  431. if str_ansi = str_wide then
  432. WriteLn('Success.')
  433. else
  434. fail;
  435. {$ifdef hasshortstring}
  436. Write('small ansistring -> widestring...');
  437. { ansistring -> widestring }
  438. s2 := SMALL_STRING;
  439. str_wide:=s2;
  440. if s2 = str_wide then
  441. WriteLn('Success.')
  442. else
  443. fail;
  444. Write('empty ansistring -> widestring...');
  445. s2 := EMPTY_STRING;
  446. str_wide:=s2;
  447. if s2 = str_wide then
  448. WriteLn('Success.')
  449. else
  450. fail;
  451. {$endif hasshortstring}
  452. end;
  453. {$ifdef hasshortstring}
  454. procedure test_wide_to_short;
  455. begin
  456. {************************************************************************}
  457. { widestring -> shortstring }
  458. {************************************************************************}
  459. WriteLn('Test widestring -> shortstring');
  460. { widestring -> shortstring }
  461. str_short := '';
  462. str_wide:='';
  463. str_wide := SMALL_STRING;
  464. Write('small widestring -> shortstring...');
  465. str_short:=str_wide;
  466. if str_short = str_wide then
  467. WriteLn('Success.')
  468. else
  469. fail;
  470. str_short := '';
  471. str_wide:='';
  472. str_wide := EMPTY_STRING;
  473. Write('empty widestring -> shortstring...');
  474. str_short:=str_wide;
  475. if str_short = str_wide then
  476. WriteLn('Success.')
  477. else
  478. fail;
  479. Write('big widestring -> shortstring...');
  480. str_short := '';
  481. str_wide:='';
  482. str_wide := BIG_STRING;
  483. str_short:=str_wide;
  484. if str_short = str_wide then
  485. WriteLn('Success.')
  486. else
  487. fail;
  488. Write('huge widestring -> shortstring...');
  489. str_wide := HUGE_STRING;
  490. str_short:=str_wide;
  491. if str_short <> str_wide then
  492. WriteLn('Success.')
  493. else
  494. fail;
  495. {}
  496. Write('small widestring -> shortstring...');
  497. s2 := '';
  498. str_wide:='';
  499. str_wide := SMALL_STRING;
  500. s2:=str_wide;
  501. if s2 = str_wide then
  502. WriteLn('Success.')
  503. else
  504. fail;
  505. Write('empty widestring -> shortstring...');
  506. s2 := '';
  507. str_wide:='';
  508. str_wide := EMPTY_STRING;
  509. s2:=str_wide;
  510. if s2 = str_wide then
  511. WriteLn('Success.')
  512. else
  513. fail;
  514. Write('big widestring -> shortstring...');
  515. s2 := '';
  516. str_wide:='';
  517. str_wide := BIG_STRING;
  518. s2:=str_wide;
  519. if s2 <> str_wide then
  520. WriteLn('Success.')
  521. else
  522. fail;
  523. Write('huge widestring -> shortstring...');
  524. s2 := '';
  525. str_wide:='';
  526. str_wide := HUGE_STRING;
  527. s2:=str_wide;
  528. if s2 <> str_wide then
  529. WriteLn('Success.')
  530. else
  531. fail;
  532. end;
  533. {$endif}
  534. {$endif}
  535. Begin
  536. {$ifdef hasshortstring}
  537. test_ansi_to_short;
  538. test_short_to_short;
  539. test_short_to_ansi;
  540. {$endif}
  541. { requires widestring support }
  542. {$ifdef haswidestring}
  543. {$ifdef hasshortstring}
  544. test_short_to_wide;
  545. {$endif}
  546. test_ansi_to_wide;
  547. {$ifdef hasshortstring}
  548. test_wide_to_short;
  549. {$endif}
  550. test_wide_to_ansi;
  551. {$endif}
  552. End.