tstr.pp 9.4 KB

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