tstr.pp 8.9 KB

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