tincdec.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. { Program to test the system unit inc/dec routines }
  2. { By Carl Eric Codere Copyright (c) 2002 }
  3. program tincdec;
  4. const
  5. INCDEC_COUNT_SIMPLE = 8;
  6. INCDEC_COUNT_COMPLEX = -12;
  7. INIT_U8BIT = $0F;
  8. INIT_U16BIT = $FF00;
  9. INIT_U32BIT = $FF00FF00;
  10. INIT_S8BIT = $0F;
  11. INIT_S16BIT = -13333;
  12. INIT_S32BIT = -2335754;
  13. INIT_S64BIT = Low(longint);
  14. var
  15. global_s8bit : shortint;
  16. global_s16bit : smallint;
  17. global_s32bit :longint;
  18. global_s64bit : int64;
  19. global_u8bit : byte;
  20. global_u16bit : word;
  21. global_u32bit : longword;
  22. { the result must be calculated manually since
  23. FPC 1.0.x does not support adding directly 64-bit
  24. constants
  25. }
  26. result_s64bit_complex : int64;
  27. procedure init_globals;
  28. begin
  29. global_s8bit := INIT_S8BIT;
  30. global_s16bit := INIT_S16BIT;
  31. global_s32bit := longint(INIT_S32BIT);
  32. global_s64bit := INIT_S64BIT;
  33. global_u8bit := INIT_U8BIT;
  34. global_u16bit := INIT_U16BIT;
  35. global_u32bit := INIT_U32BIT;
  36. result_s64bit_complex := INIT_S64BIT;
  37. result_s64bit_complex := result_s64bit_complex + INCDEC_COUNT_COMPLEX;
  38. end;
  39. procedure fail;
  40. begin
  41. WriteLn('Failed!');
  42. Halt(1);
  43. end;
  44. function getcomplex_count_s32 : longint;
  45. begin
  46. getcomplex_count_s32 := INCDEC_COUNT_COMPLEX;
  47. end;
  48. function getcomplex_count_s8 :shortint;
  49. begin
  50. getcomplex_count_s8 := INCDEC_COUNT_COMPLEX;
  51. end;
  52. function getcomplex_count_s64 : int64;
  53. begin
  54. getcomplex_count_s64 := INCDEC_COUNT_COMPLEX;
  55. end;
  56. {***********************************************************************}
  57. { INC }
  58. {***********************************************************************}
  59. procedure test_inc_s8;
  60. var
  61. b: smallint;
  62. _result : boolean;
  63. begin
  64. _result := true;
  65. Write('Inc() signed 8-bit tests...');
  66. init_globals;
  67. Inc(global_s8bit);
  68. if global_S8bit <> (INIT_S8BIT+1) then
  69. _result := false;
  70. init_globals;
  71. Inc(global_S8bit, INCDEC_COUNT_SIMPLE);
  72. if global_S8bit <> (INCDEC_COUNT_SIMPLE+INIT_S8BIT) then
  73. _result := false;
  74. init_globals;
  75. Inc(global_S8bit, INCDEC_COUNT_COMPLEX);
  76. if global_S8bit <> (INCDEC_COUNT_COMPLEX+INIT_S8BIT) then
  77. _result := false;
  78. init_globals;
  79. b:= INCDEC_COUNT_SIMPLE;
  80. Inc(global_S8bit, b);
  81. if global_S8bit <> (INCDEC_COUNT_SIMPLE+INIT_S8BIT) then
  82. _result := false;
  83. init_globals;
  84. b:= INCDEC_COUNT_COMPLEX;
  85. Inc(global_S8bit, b);
  86. if global_S8bit <> (INCDEC_COUNT_COMPLEX+INIT_S8BIT) then
  87. _result := false;
  88. init_globals;
  89. Inc(global_S8bit, getcomplex_count_s32);
  90. if global_S8bit <> (INCDEC_COUNT_COMPLEX+INIT_S8BIT) then
  91. _result := false;
  92. init_globals;
  93. Inc(global_S8bit, getcomplex_count_s8);
  94. if global_S8bit <> (INCDEC_COUNT_COMPLEX+INIT_S8BIT) then
  95. _result := false;
  96. if not _result then
  97. fail
  98. else
  99. WriteLn('Success!');
  100. end;
  101. procedure test_inc_s16;
  102. var
  103. b: smallint;
  104. _result : boolean;
  105. begin
  106. _result := true;
  107. Write('Inc() signed 16-bit tests...');
  108. init_globals;
  109. Inc(global_s16bit);
  110. if global_S16bit <> (INIT_S16BIT+1) then
  111. _result := false;
  112. init_globals;
  113. Inc(global_s16bit, INCDEC_COUNT_SIMPLE);
  114. if global_s16bit <> (INCDEC_COUNT_SIMPLE+INIT_s16BIT) then
  115. _result := false;
  116. init_globals;
  117. Inc(global_s16bit, INCDEC_COUNT_COMPLEX);
  118. if global_s16bit <> (INCDEC_COUNT_COMPLEX+INIT_s16BIT) then
  119. _result := false;
  120. init_globals;
  121. b:= INCDEC_COUNT_SIMPLE;
  122. Inc(global_s16bit, b);
  123. if global_s16bit <> (INCDEC_COUNT_SIMPLE+INIT_s16BIT) then
  124. _result := false;
  125. init_globals;
  126. b:= INCDEC_COUNT_COMPLEX;
  127. Inc(global_s16bit, b);
  128. if global_s16bit <> (INCDEC_COUNT_COMPLEX+INIT_s16BIT) then
  129. _result := false;
  130. init_globals;
  131. Inc(global_s16bit, getcomplex_count_s32);
  132. if global_s16bit <> (INCDEC_COUNT_COMPLEX+INIT_s16BIT) then
  133. _result := false;
  134. init_globals;
  135. Inc(global_s16bit, getcomplex_count_s8);
  136. if global_s16bit <> (INCDEC_COUNT_COMPLEX+INIT_s16BIT) then
  137. _result := false;
  138. if not _result then
  139. fail
  140. else
  141. WriteLn('Success!');
  142. end;
  143. procedure test_inc_s32;
  144. var
  145. b: smallint;
  146. _result : boolean;
  147. begin
  148. _result := true;
  149. Write('Inc() signed 32-bit tests...');
  150. init_globals;
  151. Inc(global_s32bit);
  152. if global_S32bit <> (INIT_S32BIT+1) then
  153. _result := false;
  154. init_globals;
  155. Inc(global_s32bit, INCDEC_COUNT_SIMPLE);
  156. if global_s32bit <> (INCDEC_COUNT_SIMPLE+INIT_s32BIT) then
  157. _result := false;
  158. init_globals;
  159. Inc(global_s32bit, INCDEC_COUNT_COMPLEX);
  160. if global_s32bit <> (INCDEC_COUNT_COMPLEX+INIT_s32BIT) then
  161. _result := false;
  162. init_globals;
  163. b:= INCDEC_COUNT_SIMPLE;
  164. Inc(global_s32bit, b);
  165. if global_s32bit <> (INCDEC_COUNT_SIMPLE+INIT_s32BIT) then
  166. _result := false;
  167. init_globals;
  168. b:= INCDEC_COUNT_COMPLEX;
  169. Inc(global_s32bit, b);
  170. if global_s32bit <> (INCDEC_COUNT_COMPLEX+INIT_s32BIT) then
  171. _result := false;
  172. init_globals;
  173. Inc(global_s32bit, getcomplex_count_s32);
  174. if global_s32bit <> (INCDEC_COUNT_COMPLEX+INIT_s32BIT) then
  175. _result := false;
  176. init_globals;
  177. Inc(global_s32bit, getcomplex_count_s8);
  178. if global_s32bit <> (INCDEC_COUNT_COMPLEX+INIT_s32BIT) then
  179. _result := false;
  180. init_globals;
  181. Inc(global_s32bit, getcomplex_count_s64);
  182. if global_s32bit <> (INCDEC_COUNT_COMPLEX+INIT_s32BIT) then
  183. _result := false;
  184. if not _result then
  185. fail
  186. else
  187. WriteLn('Success!');
  188. end;
  189. procedure test_inc_s64;
  190. var
  191. b: int64;
  192. _result : boolean;
  193. begin
  194. _result := true;
  195. Write('Inc() signed 64-bit tests...');
  196. init_globals;
  197. Inc(global_s64bit);
  198. if global_S64bit <> (result_s64bit_complex-INCDEC_COUNT_COMPLEX+1) then
  199. _result := false;
  200. init_globals;
  201. Inc(global_s64bit, INCDEC_COUNT_COMPLEX);
  202. if global_s64bit <> (result_s64bit_complex) then
  203. _result := false;
  204. init_globals;
  205. Inc(global_s64bit, INCDEC_COUNT_COMPLEX);
  206. if global_s64bit <> (result_s64bit_complex) then
  207. _result := false;
  208. init_globals;
  209. b:= INCDEC_COUNT_COMPLEX;
  210. Inc(global_s64bit, b);
  211. if global_s64bit <> (result_s64bit_complex) then
  212. _result := false;
  213. {$ifndef ver1_0}
  214. init_globals;
  215. Inc(global_s64bit, getcomplex_count_s8);
  216. if global_s64bit <> (INCDEC_COUNT_COMPLEX+INIT_S64BIT) then
  217. _result := false;
  218. init_globals;
  219. Inc(global_s64bit, getcomplex_count_s32);
  220. if global_s64bit <> (INCDEC_COUNT_COMPLEX+INIT_s64BIT) then
  221. _result := false;
  222. {$endif}
  223. if not _result then
  224. fail
  225. else
  226. WriteLn('Success!');
  227. end;
  228. procedure test_inc_u32;
  229. var
  230. b: smallint;
  231. _result : boolean;
  232. begin
  233. _result := true;
  234. Write('Inc() unsigned 32-bit tests...');
  235. init_globals;
  236. Inc(global_u32bit);
  237. if global_u32bit <> (INIT_U32BIT+1) then
  238. _result := false;
  239. init_globals;
  240. Inc(global_u32bit, INCDEC_COUNT_SIMPLE);
  241. if global_u32bit <> (INCDEC_COUNT_SIMPLE+INIT_u32BIT) then
  242. _result := false;
  243. init_globals;
  244. Inc(global_u32bit, INCDEC_COUNT_COMPLEX);
  245. if global_u32bit <> (INCDEC_COUNT_COMPLEX+INIT_u32BIT) then
  246. _result := false;
  247. init_globals;
  248. b:= INCDEC_COUNT_SIMPLE;
  249. Inc(global_u32bit, b);
  250. if global_u32bit <> (INCDEC_COUNT_SIMPLE+INIT_u32BIT) then
  251. _result := false;
  252. init_globals;
  253. b:= INCDEC_COUNT_COMPLEX;
  254. Inc(global_u32bit, b);
  255. if global_u32bit <> (INCDEC_COUNT_COMPLEX+INIT_u32BIT) then
  256. _result := false;
  257. init_globals;
  258. Inc(global_u32bit, getcomplex_count_s32);
  259. if global_u32bit <> (INCDEC_COUNT_COMPLEX+INIT_u32BIT) then
  260. _result := false;
  261. init_globals;
  262. Inc(global_u32bit, getcomplex_count_s8);
  263. if global_u32bit <> (INCDEC_COUNT_COMPLEX+INIT_u32BIT) then
  264. _result := false;
  265. init_globals;
  266. Inc(global_u32bit, getcomplex_count_s64);
  267. if global_u32bit <> (INCDEC_COUNT_COMPLEX+INIT_u32BIT) then
  268. _result := false;
  269. if not _result then
  270. fail
  271. else
  272. WriteLn('Success!');
  273. end;
  274. {***********************************************************************}
  275. { DEC }
  276. {***********************************************************************}
  277. procedure test_dec_s8;
  278. var
  279. b: smallint;
  280. _result : boolean;
  281. l: byte;
  282. begin
  283. _result := true;
  284. Write('dec() signed 8-bit tests...');
  285. init_globals;
  286. dec(global_S8bit, INCDEC_COUNT_SIMPLE);
  287. if global_S8bit <> (INIT_S8BIT-INCDEC_COUNT_SIMPLE) then
  288. _result := false;
  289. init_globals;
  290. dec(global_S8bit, INCDEC_COUNT_COMPLEX);
  291. if global_S8bit <> (INIT_S8BIT-INCDEC_COUNT_COMPLEX) then
  292. _result := false;
  293. init_globals;
  294. b:= INCDEC_COUNT_SIMPLE;
  295. dec(global_S8bit, b);
  296. if global_S8bit <> (INIT_S8BIT-INCDEC_COUNT_SIMPLE) then
  297. _result := false;
  298. init_globals;
  299. b:= INCDEC_COUNT_COMPLEX;
  300. dec(global_S8bit, b);
  301. if global_S8bit <> (INIT_S8BIT-INCDEC_COUNT_COMPLEX) then
  302. _result := false;
  303. init_globals;
  304. dec(global_S8bit, getcomplex_count_s32);
  305. if global_S8bit <> (INIT_S8BIT-INCDEC_COUNT_COMPLEX) then
  306. _result := false;
  307. init_globals;
  308. dec(global_S8bit, getcomplex_count_s8);
  309. if global_S8bit <> (INIT_S8BIT-INCDEC_COUNT_COMPLEX) then
  310. _result := false;
  311. { extra test for overflow checking }
  312. l:=byte(high(shortint));
  313. global_s8bit := high(shortint);
  314. dec(global_s8bit,l);
  315. if global_s8bit <> 0 then
  316. _result := false;
  317. if not _result then
  318. fail
  319. else
  320. WriteLn('Success!');
  321. end;
  322. procedure test_dec_s16;
  323. var
  324. b: smallint;
  325. _result : boolean;
  326. begin
  327. _result := true;
  328. Write('dec() signed 16-bit tests...');
  329. init_globals;
  330. dec(global_s16bit, INCDEC_COUNT_SIMPLE);
  331. if global_s16bit <> (INIT_S16BIT-INCDEC_COUNT_SIMPLE) then
  332. _result := false;
  333. init_globals;
  334. dec(global_s16bit, INCDEC_COUNT_COMPLEX);
  335. if global_s16bit <> (INIT_S16BIT-INCDEC_COUNT_COMPLEX) then
  336. _result := false;
  337. init_globals;
  338. b:= INCDEC_COUNT_SIMPLE;
  339. dec(global_s16bit, b);
  340. if global_s16bit <> (INIT_S16BIT-INCDEC_COUNT_SIMPLE) then
  341. _result := false;
  342. init_globals;
  343. b:= INCDEC_COUNT_COMPLEX;
  344. dec(global_s16bit, b);
  345. if global_s16bit <> (INIT_S16BIT-INCDEC_COUNT_COMPLEX) then
  346. _result := false;
  347. init_globals;
  348. dec(global_s16bit, getcomplex_count_s32);
  349. if global_s16bit <> (INIT_S16BIT-INCDEC_COUNT_COMPLEX) then
  350. _result := false;
  351. init_globals;
  352. dec(global_s16bit, getcomplex_count_s8);
  353. if global_s16bit <> (INIT_S16BIT-INCDEC_COUNT_COMPLEX) then
  354. _result := false;
  355. if not _result then
  356. fail
  357. else
  358. WriteLn('Success!');
  359. end;
  360. procedure test_dec_s32;
  361. var
  362. b: smallint;
  363. _result : boolean;
  364. begin
  365. _result := true;
  366. Write('dec() signed 32-bit tests...');
  367. init_globals;
  368. dec(global_s32bit, INCDEC_COUNT_SIMPLE);
  369. if global_s32bit <> (INIT_S32BIT-INCDEC_COUNT_SIMPLE) then
  370. _result := false;
  371. init_globals;
  372. dec(global_s32bit, INCDEC_COUNT_COMPLEX);
  373. if global_s32bit <> (INIT_S32BIT-INCDEC_COUNT_COMPLEX) then
  374. _result := false;
  375. init_globals;
  376. b:= INCDEC_COUNT_SIMPLE;
  377. dec(global_s32bit, b);
  378. if global_s32bit <> (INIT_S32BIT-INCDEC_COUNT_SIMPLE) then
  379. _result := false;
  380. init_globals;
  381. b:= INCDEC_COUNT_COMPLEX;
  382. dec(global_s32bit, b);
  383. if global_s32bit <> (INIT_S32BIT-INCDEC_COUNT_COMPLEX) then
  384. _result := false;
  385. init_globals;
  386. dec(global_s32bit, getcomplex_count_s32);
  387. if global_s32bit <> (INIT_S32BIT-INCDEC_COUNT_COMPLEX) then
  388. _result := false;
  389. init_globals;
  390. dec(global_s32bit, getcomplex_count_s8);
  391. if global_s32bit <> (INIT_S32BIT-INCDEC_COUNT_COMPLEX) then
  392. _result := false;
  393. init_globals;
  394. dec(global_s32bit, getcomplex_count_s64);
  395. if global_s32bit <> (INIT_S32BIT-INCDEC_COUNT_COMPLEX) then
  396. _result := false;
  397. if not _result then
  398. fail
  399. else
  400. WriteLn('Success!');
  401. end;
  402. procedure test_dec_s64;
  403. var
  404. b: smallint;
  405. _result : boolean;
  406. begin
  407. _result := true;
  408. Write('dec() signed 64-bit tests...');
  409. {$ifndef ver1_0}
  410. init_globals;
  411. dec(global_s64bit, getcomplex_count_s8);
  412. if global_s64bit <> (INIT_S64BIT-INCDEC_COUNT_COMPLEX) then
  413. _result := false;
  414. init_globals;
  415. dec(global_s64bit, getcomplex_count_s32);
  416. if global_s64bit <> (INIT_S64BIT-INCDEC_COUNT_COMPLEX) then
  417. _result := false;
  418. {$endif}
  419. if not _result then
  420. fail
  421. else
  422. WriteLn('Success!');
  423. end;
  424. procedure test_dec_u32;
  425. var
  426. b: smallint;
  427. _result : boolean;
  428. begin
  429. _result := true;
  430. Write('dec() unsigned 32-bit tests...');
  431. init_globals;
  432. dec(global_u32bit, INCDEC_COUNT_SIMPLE);
  433. if global_u32bit <> (INIT_u32BIT-INCDEC_COUNT_SIMPLE) then
  434. _result := false;
  435. init_globals;
  436. dec(global_u32bit, INCDEC_COUNT_COMPLEX);
  437. if global_u32bit <> (INIT_u32BIT-INCDEC_COUNT_COMPLEX) then
  438. _result := false;
  439. init_globals;
  440. b:= INCDEC_COUNT_SIMPLE;
  441. dec(global_u32bit, b);
  442. if global_u32bit <> (INIT_u32BIT-INCDEC_COUNT_SIMPLE) then
  443. _result := false;
  444. init_globals;
  445. b:= INCDEC_COUNT_COMPLEX;
  446. dec(global_u32bit, b);
  447. if global_u32bit <> (INIT_u32BIT-INCDEC_COUNT_COMPLEX) then
  448. _result := false;
  449. init_globals;
  450. dec(global_u32bit, getcomplex_count_s32);
  451. if global_u32bit <> (INIT_u32BIT-INCDEC_COUNT_COMPLEX) then
  452. _result := false;
  453. init_globals;
  454. dec(global_u32bit, getcomplex_count_s8);
  455. if global_u32bit <> (INIT_u32BIT-INCDEC_COUNT_COMPLEX) then
  456. _result := false;
  457. init_globals;
  458. dec(global_u32bit, getcomplex_count_s64);
  459. if global_u32bit <> (INIT_u32BIT-INCDEC_COUNT_COMPLEX) then
  460. _result := false;
  461. if not _result then
  462. fail
  463. else
  464. WriteLn('Success!');
  465. end;
  466. procedure test_inc_ptr;
  467. type
  468. tstruct = packed record
  469. b: byte;
  470. w: word;
  471. end;
  472. const
  473. word_array : array[1..4] of word =
  474. ($0000,$FFFF,$F0F0,$F00F);
  475. struct_array : array[1..4] of tstruct = (
  476. (b:00;w:0001),
  477. (b:01;w:0102),
  478. (b:02;w:0203),
  479. (b:03;w:0304)
  480. );
  481. var
  482. _result : boolean;
  483. pw : ^word;
  484. podd : ^tstruct;
  485. i: integer;
  486. B : byte;
  487. begin
  488. _result := true;
  489. Write('Inc() pointer to word...');
  490. pw:=@word_array;
  491. for i:=1 to 4 do
  492. begin
  493. if (word_array[i] <> pw^) then
  494. _result := false;
  495. Inc(pw)
  496. end;
  497. pw:=@word_array;
  498. inc(pw,2);
  499. if pw^<>word_array[3] then
  500. _result := false;
  501. pw:=@word_array;
  502. b:=2;
  503. inc(pw,b);
  504. if pw^<>word_array[3] then
  505. _result := false;
  506. podd:=@struct_array;
  507. b:=3;
  508. inc(podd,b);
  509. if (podd^.b<>struct_array[4].b) and (podd^.w<>struct_array[4].w) then
  510. _result := false;
  511. podd:=@struct_array;
  512. inc(podd,3);
  513. if (podd^.b<>struct_array[4].b) and (podd^.w<>struct_array[4].w) then
  514. _result := false;
  515. if not _result then
  516. fail
  517. else
  518. WriteLn('Success!');
  519. end;
  520. Begin
  521. test_inc_s8;
  522. test_inc_s16;
  523. test_inc_s32;
  524. test_inc_s64;
  525. test_inc_u32;
  526. test_inc_ptr;
  527. test_dec_s8;
  528. test_dec_s16;
  529. test_dec_s32;
  530. test_dec_s64;
  531. test_dec_u32;
  532. end.