tstr.pp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. program tstr;
  2. uses
  3. jdk15;
  4. {$macro on}
  5. {$define write:=JLSystem.fout.print}
  6. {$define writeln:=JLSystem.fout.println}
  7. {$ifdef fpc}
  8. {$ifndef ver1_0}
  9. {$define haswidestring}
  10. {$endif}
  11. {$else}
  12. {$ifndef ver70}
  13. {$define haswidestring}
  14. {$endif}
  15. {$endif}
  16. procedure test_shortstr;
  17. type
  18. tlocalstring = shortstring;
  19. var
  20. l: longint;
  21. c: cardinal;
  22. f: real;
  23. i: int64;
  24. q: qword;
  25. s: tlocalstring;
  26. len: byte;
  27. frac: word;
  28. longval : longint;
  29. procedure check(const ss: tlocalstring);
  30. begin
  31. if s <> ss then
  32. begin
  33. writeln('error!');
  34. halt(1);
  35. end;
  36. end;
  37. begin
  38. writeln('testing str(<value>,shortstring)...');
  39. l := -1;
  40. str(l,s);
  41. check('-1');
  42. str(l:0,s);
  43. check('-1');
  44. str(l:1,s);
  45. check('-1');
  46. str(l:2,s);
  47. check('-1');
  48. str(l:3,s);
  49. check(' -1');
  50. len := 4;
  51. str(l:len,s);
  52. check(' -1');
  53. c := 10;
  54. str(c,s);
  55. check('10');
  56. str(c:0,s);
  57. check('10');
  58. str(c:1,s);
  59. check('10');
  60. str(c:2,s);
  61. check('10');
  62. str(c:3,s);
  63. check(' 10');
  64. { for more in-depth tests of str_real, see ../tstreal[1,2].pp }
  65. f := -1.12345;
  66. {$IFOPT E-}
  67. str(f,s);
  68. if (sizeof(extended) = 10) or
  69. (sizeof(extended) = 12) then
  70. check('-1.12345000000000E+000')
  71. else if sizeof(extended) = 8 then
  72. check('-1.12345000000000E+000')
  73. else
  74. check('error, not yet implemented!!!!');
  75. {$endif}
  76. { the number of exponents depends on the maaping of the real type }
  77. if sizeof(real) = 8 then
  78. begin
  79. str(f:0,s);
  80. check('-1.1E+000');
  81. str(f:1,s);
  82. check('-1.1E+000');
  83. str(f:2,s);
  84. check('-1.1E+000');
  85. str(f:3,s);
  86. check('-1.1E+000');
  87. str(f:4,s);
  88. check('-1.1E+000');
  89. end
  90. else
  91. begin
  92. str(f:0,s);
  93. check('-1.1E+00');
  94. str(f:1,s);
  95. check('-1.1E+00');
  96. str(f:2,s);
  97. check('-1.1E+00');
  98. str(f:3,s);
  99. check('-1.1E+00');
  100. str(f:4,s);
  101. check('-1.1E+00');
  102. end;
  103. str(f:0:0,s);
  104. check('-1');
  105. str(f:0:1,s);
  106. check('-1.1');
  107. str(f:0:2,s);
  108. check('-1.12');
  109. str(f:1:0,s);
  110. check('-1');
  111. str(f:1:1,s);
  112. check('-1.1');
  113. str(f:5:0,s);
  114. check(' -1');
  115. str(f:5:1,s);
  116. check(' -1.1');
  117. str(f:5:2,s);
  118. check('-1.12');
  119. len := 6;
  120. frac := 2;
  121. str(f:len:frac,s);
  122. check(' -1.12');
  123. i := -1;
  124. str(i,s);
  125. check('-1');
  126. str(i:0,s);
  127. check('-1');
  128. str(i:1,s);
  129. check('-1');
  130. str(i:2,s);
  131. check('-1');
  132. str(i:3,s);
  133. check(' -1');
  134. i:=655536;
  135. str(i,s);
  136. check('655536');
  137. str(i:0,s);
  138. check('655536');
  139. str(i:1,s);
  140. check('655536');
  141. str(i:2,s);
  142. check('655536');
  143. str(i:3,s);
  144. check('655536');
  145. longval:=1;
  146. i:=int64(longval) shl 33;
  147. str(i,s);
  148. check('8589934592');
  149. str(i:0,s);
  150. check('8589934592');
  151. str(i:1,s);
  152. check('8589934592');
  153. str(i:2,s);
  154. check('8589934592');
  155. str(i:3,s);
  156. check('8589934592');
  157. q := 10;
  158. str(q,s);
  159. check('10');
  160. str(q:0,s);
  161. check('10');
  162. str(q:1,s);
  163. check('10');
  164. str(q:2,s);
  165. check('10');
  166. str(q:3,s);
  167. check(' 10');
  168. q:=655536;
  169. str(q,s);
  170. check('655536');
  171. str(q:0,s);
  172. check('655536');
  173. str(q:1,s);
  174. check('655536');
  175. str(q:2,s);
  176. check('655536');
  177. str(q:3,s);
  178. check('655536');
  179. longval:=1;
  180. q:=qword(longval) shl 33;
  181. str(q,s);
  182. check('8589934592');
  183. str(q:0,s);
  184. check('8589934592');
  185. str(q:1,s);
  186. check('8589934592');
  187. str(q:2,s);
  188. check('8589934592');
  189. str(q:3,s);
  190. check('8589934592');
  191. end;
  192. (*
  193. procedure test_ansistr;
  194. type
  195. tlocalstring = ansistring;
  196. var
  197. l: longint;
  198. c: cardinal;
  199. f: real;
  200. i: int64;
  201. q: qword;
  202. s: tlocalstring;
  203. len: shortint;
  204. frac: smallint;
  205. longval : longint;
  206. procedure check(const ss: tlocalstring);
  207. begin
  208. if s <> ss then
  209. begin
  210. writeln('error!');
  211. halt(1);
  212. end;
  213. end;
  214. begin
  215. writeln('testing str(<value>,ansistring)...');
  216. l := -1;
  217. str(l,s);
  218. check('-1');
  219. str(l:0,s);
  220. check('-1');
  221. str(l:1,s);
  222. check('-1');
  223. str(l:2,s);
  224. check('-1');
  225. str(l:3,s);
  226. check(' -1');
  227. len := 4;
  228. str(l:len,s);
  229. check(' -1');
  230. c := 10;
  231. str(c,s);
  232. check('10');
  233. str(c:0,s);
  234. check('10');
  235. str(c:1,s);
  236. check('10');
  237. str(c:2,s);
  238. check('10');
  239. str(c:3,s);
  240. check(' 10');
  241. { for more in-depth tests of str_real, see ../tstreal[1,2].pp }
  242. f := -1.12345;
  243. {$IFOPT E-}
  244. str(f,s);
  245. if (sizeof(extended) = 10) or
  246. (sizeof(extended) = 12) then
  247. check('-1.12345000000000E+000')
  248. else if sizeof(extended) = 8 then
  249. check('-1.12345000000000E+000')
  250. else
  251. check('error, not yet implemented!!!!');
  252. {$endif}
  253. { the number of exponents depends on the maaping of the real type }
  254. if sizeof(real) = 8 then
  255. begin
  256. str(f:0,s);
  257. check('-1.1E+000');
  258. str(f:1,s);
  259. check('-1.1E+000');
  260. str(f:2,s);
  261. check('-1.1E+000');
  262. str(f:3,s);
  263. check('-1.1E+000');
  264. str(f:4,s);
  265. check('-1.1E+000');
  266. end
  267. else
  268. begin
  269. str(f:0,s);
  270. check('-1.1E+00');
  271. str(f:1,s);
  272. check('-1.1E+00');
  273. str(f:2,s);
  274. check('-1.1E+00');
  275. str(f:3,s);
  276. check('-1.1E+00');
  277. str(f:4,s);
  278. check('-1.1E+00');
  279. end;
  280. str(f:0:0,s);
  281. check('-1');
  282. str(f:0:1,s);
  283. check('-1.1');
  284. str(f:0:2,s);
  285. check('-1.12');
  286. str(f:1:0,s);
  287. check('-1');
  288. str(f:1:1,s);
  289. check('-1.1');
  290. str(f:5:0,s);
  291. check(' -1');
  292. str(f:5:1,s);
  293. check(' -1.1');
  294. str(f:5:2,s);
  295. check('-1.12');
  296. len := 6;
  297. frac := 2;
  298. str(f:len:frac,s);
  299. check(' -1.12');
  300. i := -1;
  301. str(i,s);
  302. check('-1');
  303. str(i:0,s);
  304. check('-1');
  305. str(i:1,s);
  306. check('-1');
  307. str(i:2,s);
  308. check('-1');
  309. str(i:3,s);
  310. check(' -1');
  311. i:=655536;
  312. str(i,s);
  313. check('655536');
  314. str(i:0,s);
  315. check('655536');
  316. str(i:1,s);
  317. check('655536');
  318. str(i:2,s);
  319. check('655536');
  320. str(i:3,s);
  321. check('655536');
  322. longval:=1;
  323. i:=int64(longval) shl 33;
  324. str(i,s);
  325. check('8589934592');
  326. str(i:0,s);
  327. check('8589934592');
  328. str(i:1,s);
  329. check('8589934592');
  330. str(i:2,s);
  331. check('8589934592');
  332. str(i:3,s);
  333. check('8589934592');
  334. q := 10;
  335. str(q,s);
  336. check('10');
  337. str(q:0,s);
  338. check('10');
  339. str(q:1,s);
  340. check('10');
  341. str(q:2,s);
  342. check('10');
  343. str(q:3,s);
  344. check(' 10');
  345. q:=655536;
  346. str(q,s);
  347. check('655536');
  348. str(q:0,s);
  349. check('655536');
  350. str(q:1,s);
  351. check('655536');
  352. str(q:2,s);
  353. check('655536');
  354. str(q:3,s);
  355. check('655536');
  356. longval:=1;
  357. q:=qword(longval) shl 33;
  358. str(q,s);
  359. check('8589934592');
  360. str(q:0,s);
  361. check('8589934592');
  362. str(q:1,s);
  363. check('8589934592');
  364. str(q:2,s);
  365. check('8589934592');
  366. str(q:3,s);
  367. check('8589934592');
  368. end;
  369. *)
  370. {$ifdef haswidestring}
  371. procedure test_widestr;
  372. type
  373. tlocalstring = widestring;
  374. var
  375. l: longint;
  376. c: cardinal;
  377. f: real;
  378. i: int64;
  379. q: qword;
  380. s: tlocalstring;
  381. len: longint;
  382. frac: cardinal;
  383. longval : longint;
  384. procedure check(const ss: tlocalstring);
  385. begin
  386. if s <> ss then
  387. begin
  388. writeln('error!');
  389. halt(1);
  390. end;
  391. end;
  392. begin
  393. writeln('testing str(<value>,widestring)...');
  394. l := -1;
  395. str(l,s);
  396. check('-1');
  397. str(l:0,s);
  398. check('-1');
  399. str(l:1,s);
  400. check('-1');
  401. str(l:2,s);
  402. check('-1');
  403. str(l:3,s);
  404. check(' -1');
  405. len := 4;
  406. str(l:len,s);
  407. check(' -1');
  408. c := 10;
  409. str(c,s);
  410. check('10');
  411. str(c:0,s);
  412. check('10');
  413. str(c:1,s);
  414. check('10');
  415. str(c:2,s);
  416. check('10');
  417. str(c:3,s);
  418. check(' 10');
  419. { for more in-depth tests of str_real, see ../tstreal[1,2].pp }
  420. f := -1.12345;
  421. {$IFOPT E-}
  422. str(f,s);
  423. if sizeof(extended) = 10 then
  424. check('-1.12345000000000E+000')
  425. else if sizeof(extended) = 8 then
  426. check('-1.12345000000000E+000')
  427. else
  428. check('error, not yet implemented!!!!');
  429. {$endif}
  430. { the number of exponents depends on the maaping of the real type }
  431. if sizeof(real) = 8 then
  432. begin
  433. str(f:0,s);
  434. check('-1.1E+000');
  435. str(f:1,s);
  436. check('-1.1E+000');
  437. str(f:2,s);
  438. check('-1.1E+000');
  439. str(f:3,s);
  440. check('-1.1E+000');
  441. str(f:4,s);
  442. check('-1.1E+000');
  443. end
  444. else
  445. begin
  446. str(f:0,s);
  447. check('-1.1E+00');
  448. str(f:1,s);
  449. check('-1.1E+00');
  450. str(f:2,s);
  451. check('-1.1E+00');
  452. str(f:3,s);
  453. check('-1.1E+00');
  454. str(f:4,s);
  455. check('-1.1E+00');
  456. end;
  457. str(f:0:0,s);
  458. check('-1');
  459. str(f:0:1,s);
  460. check('-1.1');
  461. str(f:0:2,s);
  462. check('-1.12');
  463. str(f:1:0,s);
  464. check('-1');
  465. str(f:1:1,s);
  466. check('-1.1');
  467. str(f:5:0,s);
  468. check(' -1');
  469. str(f:5:1,s);
  470. check(' -1.1');
  471. str(f:5:2,s);
  472. check('-1.12');
  473. len := 6;
  474. frac := 2;
  475. str(f:len:frac,s);
  476. check(' -1.12');
  477. i := -1;
  478. str(i,s);
  479. check('-1');
  480. str(i:0,s);
  481. check('-1');
  482. str(i:1,s);
  483. check('-1');
  484. str(i:2,s);
  485. check('-1');
  486. str(i:3,s);
  487. check(' -1');
  488. i:=655536;
  489. str(i,s);
  490. check('655536');
  491. str(i:0,s);
  492. check('655536');
  493. str(i:1,s);
  494. check('655536');
  495. str(i:2,s);
  496. check('655536');
  497. str(i:3,s);
  498. check('655536');
  499. longval:=1;
  500. i:=int64(longval) shl 33;
  501. str(i,s);
  502. check('8589934592');
  503. str(i:0,s);
  504. check('8589934592');
  505. str(i:1,s);
  506. check('8589934592');
  507. str(i:2,s);
  508. check('8589934592');
  509. str(i:3,s);
  510. check('8589934592');
  511. q := 10;
  512. str(q,s);
  513. check('10');
  514. str(q:0,s);
  515. check('10');
  516. str(q:1,s);
  517. check('10');
  518. str(q:2,s);
  519. check('10');
  520. str(q:3,s);
  521. check(' 10');
  522. q:=655536;
  523. str(q,s);
  524. check('655536');
  525. str(q:0,s);
  526. check('655536');
  527. str(q:1,s);
  528. check('655536');
  529. str(q:2,s);
  530. check('655536');
  531. str(q:3,s);
  532. check('655536');
  533. longval:=1;
  534. q:=qword(longval) shl 33;
  535. str(q,s);
  536. check('8589934592');
  537. str(q:0,s);
  538. check('8589934592');
  539. str(q:1,s);
  540. check('8589934592');
  541. str(q:2,s);
  542. check('8589934592');
  543. str(q:3,s);
  544. check('8589934592');
  545. end;
  546. {$endif haswidestring}
  547. begin
  548. test_shortstr;
  549. // test_ansistr;
  550. {$ifdef haswidestring}
  551. test_widestr;
  552. {$endif haswidestring}
  553. writeln('str tests successful!');
  554. end.