tstr.pp 9.5 KB

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