tcalext6.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. { Tests passing of different records by value to C methods.
  2. One type of these records has one field which is a simple array of bytes,
  3. the other consists of a few fields of atomic size.
  4. Note that it does not only test a single field of these records, but all
  5. by comparing the sum of the field values with the sum returned by the
  6. C function.
  7. }
  8. program calext6;
  9. {$MODE DELPHI}
  10. { requires libgcc for the C functions }
  11. {$ifdef FPUSOFT}
  12. {$define NO_FLOAT}
  13. {$endif}
  14. type
  15. int8_t = shortint;
  16. pint8_t = ^int8_t;
  17. int16_t = smallint;
  18. int32_t = longint;
  19. int64_t = int64;
  20. var
  21. success : boolean;
  22. {$packrecords c}
  23. type
  24. struct1 = record
  25. v : single;
  26. end;
  27. struct2 = record
  28. v : double;
  29. end;
  30. struct3 = record
  31. v1 : single;
  32. v2 : single;
  33. end;
  34. struct4 = record
  35. v1 : double;
  36. v2 : single;
  37. end;
  38. struct5 = record
  39. v1 : double;
  40. v2 : double;
  41. end;
  42. struct6 = record
  43. v1 : double;
  44. v2 : single;
  45. v3 : single;
  46. end;
  47. struct7 = record
  48. v1 : single;
  49. v2 : int32_t;
  50. v3 : double;
  51. end;
  52. struct8 = record
  53. case byte of
  54. 0: (v1: single);
  55. 1: (d: double);
  56. end;
  57. struct9 = record
  58. v1 : int64_t;
  59. v2 : single;
  60. end;
  61. struct10 = record
  62. v1 : int64_t;
  63. v2 : int16_t;
  64. v3 : single;
  65. end;
  66. struct11 = record
  67. v1 : int64_t;
  68. v2 : double;
  69. end;
  70. struct12 = record
  71. v1 : int64_t;
  72. v2 : single;
  73. v3 : single;
  74. end;
  75. struct13 = record
  76. v1 : double;
  77. v2 : int64_t;
  78. end;
  79. struct14 = record
  80. v1 : double;
  81. v2 : int32_t;
  82. v3 : int16_t;
  83. end;
  84. struct15 = record
  85. v1 : double;
  86. v2 : int32_t;
  87. v3 : single;
  88. end;
  89. struct16 = record
  90. v1 : single;
  91. v2 : single;
  92. v3 : single;
  93. v4 : single;
  94. end;
  95. struct17 = record
  96. v1 : single;
  97. v2 : double;
  98. end;
  99. struct31 = record
  100. v1 : cextended;
  101. v2 : single;
  102. end;
  103. procedure fill(var mem; size : integer);
  104. var
  105. i : Integer;
  106. p : pint8_t;
  107. begin
  108. p := @mem;
  109. for i := 0 to size-1 do begin
  110. p^ := random(255)+1;
  111. inc(p);
  112. end;
  113. end;
  114. procedure verify(val1, val2 : int64_t; nr : Integer); overload;
  115. begin
  116. success := success and (val1 = val2);
  117. Write('Testing test ', nr , ', was ', val1, ', should be ', val2, '...');
  118. if (val1 = val2) then
  119. WriteLn('Success.')
  120. else
  121. WriteLn('Failed');
  122. end;
  123. procedure verify(val1, val2 : double; nr : Integer); overload;
  124. begin
  125. success := success and (val1 = val2);
  126. Write('Testing test ', nr , ', was ', val1, ', should be ', val2, '...');
  127. if (val1 = val2) then
  128. WriteLn('Success.')
  129. else
  130. WriteLn('Failed');
  131. end;
  132. procedure verify(val1, val2 : cextended; nr : Integer); overload;
  133. begin
  134. success := success and (val1 = val2);
  135. Write('Testing test ', nr , ', was ', val1, ', should be ', val2, '...');
  136. if (val1 = val2) then
  137. WriteLn('Success.')
  138. else
  139. WriteLn('Failed');
  140. end;
  141. function check1(s : struct1) : single;
  142. begin
  143. result := s.v;
  144. end;
  145. function check2(s : struct2) : double;
  146. begin
  147. result := s.v;
  148. end;
  149. function check3(s : struct3) : single;
  150. begin
  151. result := s.v1 + s.v2;
  152. end;
  153. function check4(s : struct4) : double;
  154. begin
  155. result := s.v1 + s.v2;
  156. end;
  157. function check5(s : struct5) : double;
  158. begin
  159. result := s.v1 + s.v2;
  160. end;
  161. function check6(s : struct6) : double;
  162. begin
  163. result := s.v1 + s.v2;
  164. end;
  165. function check7(s : struct7) : double;
  166. begin
  167. result := s.v1 + s.v2 + s.v3;
  168. end;
  169. function check8(s : struct8) : double;
  170. begin
  171. result := s.d;
  172. end;
  173. function check9(s : struct9) : int64_t;
  174. begin
  175. result := s.v1 + trunc(s.v2);
  176. end;
  177. function check10(s : struct10) : int64_t;
  178. begin
  179. result := s.v1 + s.v2 + trunc(s.v3);
  180. end;
  181. function check11(s : struct11) : int64_t;
  182. begin
  183. result := s.v1 + trunc(s.v2);
  184. end;
  185. function check12(s : struct12) : int64_t;
  186. begin
  187. result := s.v1 + trunc(s.v2) + trunc(s.v3);
  188. end;
  189. function check13(s : struct13) : int64_t;
  190. begin
  191. result := trunc(s.v1) + s.v2 ;
  192. end;
  193. function check14(s : struct14) : int64_t;
  194. begin
  195. result := trunc(s.v1) + s.v2 + s.v3;
  196. end;
  197. function check15(s : struct15) : int64_t;
  198. begin
  199. result := trunc(s.v1) + s.v2 + trunc(s.v3);
  200. end;
  201. function check16(s : struct16) : single;
  202. begin
  203. result := s.v1 + s.v2 + s.v3 + s.v4;
  204. end;
  205. function check17(s : struct17) : double;
  206. begin
  207. result := s.v1 + s.v2;
  208. end;
  209. function check31(s : struct31) : cextended;
  210. begin
  211. result := s.v1 + s.v2;
  212. end;
  213. {$L tcext6.o}
  214. function pass1(s : struct1; b: byte) : single; cdecl; external;
  215. function pass2(s : struct2; b: byte) : double; cdecl; external;
  216. function pass3(s : struct3; b: byte) : single; cdecl; external;
  217. function pass4(s : struct4; b: byte) : double; cdecl; external;
  218. function pass5(s : struct5; b: byte) : double; cdecl; external;
  219. function pass6(s : struct6; b: byte) : double; cdecl; external;
  220. function pass61(d1,d2,d3,d4,d5: double; s : struct6; b: byte) : double; cdecl; external;
  221. function pass7(s : struct7; b: byte) : double; cdecl; external;
  222. function pass8(s : struct8; b: byte) : double; cdecl; external;
  223. function pass9(s : struct9; b: byte) : int64_t; cdecl; external;
  224. function pass10(s : struct10; b: byte) : int64_t; cdecl; external;
  225. function pass11(s : struct11; b: byte) : int64_t; cdecl; external;
  226. function pass12(s : struct12; b: byte) : int64_t; cdecl; external;
  227. function pass13(s : struct13; b: byte) : int64_t; cdecl; external;
  228. function pass14(s : struct14; b: byte) : int64_t; cdecl; external;
  229. function pass15(s : struct15; b: byte) : int64_t; cdecl; external;
  230. function pass16(s : struct16; b: byte) : single; cdecl; external;
  231. function pass17(s : struct17; b: byte) : single; cdecl; external;
  232. {$ifdef FPC_HAS_TYPE_EXTENDED}
  233. function pass31(s : struct31; b: byte) : cextended; cdecl; external;
  234. {$endif}
  235. function pass1a(b: byte; s : struct1) : struct1; cdecl; external;
  236. function pass2a(b: byte; s : struct2) : struct2; cdecl; external;
  237. function pass3a(b: byte; s : struct3) : struct3; cdecl; external;
  238. function pass4a(b: byte; s : struct4) : struct4; cdecl; external;
  239. function pass5a(b: byte; s : struct5) : struct5; cdecl; external;
  240. function pass6a(b: byte; s : struct6) : struct6; cdecl; external;
  241. function pass7a(b: byte; s : struct7) : struct7; cdecl; external;
  242. function pass8a(b: byte; s : struct8) : struct8; cdecl; external;
  243. function pass9a(b: byte; s : struct9) : struct9; cdecl; external;
  244. function pass10a(b: byte; s : struct10) : struct10; cdecl; external;
  245. function pass11a(b: byte; s : struct11) : struct11; cdecl; external;
  246. function pass12a(b: byte; s : struct12) : struct12; cdecl; external;
  247. function pass13a(b: byte; s : struct13) : struct13; cdecl; external;
  248. function pass14a(b: byte; s : struct14) : struct14; cdecl; external;
  249. function pass15a(b: byte; s : struct15) : struct15; cdecl; external;
  250. function pass16a(b: byte; s : struct16) : struct16; cdecl; external;
  251. function pass17a(b: byte; s : struct17) : struct17; cdecl; external;
  252. {$ifdef FPC_HAS_TYPE_EXTENDED}
  253. function pass31a(b: byte; s : struct31) : struct31; cdecl; external;
  254. {$endif}
  255. procedure dotest;
  256. var
  257. s1 : struct1;
  258. s2 : struct2;
  259. s3 : struct3;
  260. s4 : struct4;
  261. s5 : struct5;
  262. s6 : struct6;
  263. s7 : struct7;
  264. s8 : struct8;
  265. s9 : struct9;
  266. s10 : struct10;
  267. s11 : struct11;
  268. s12 : struct12;
  269. s13 : struct13;
  270. s14 : struct14;
  271. s15 : struct15;
  272. s16 : struct16;
  273. s17 : struct17;
  274. s31 : struct31;
  275. begin
  276. success := true;
  277. {$ifndef NO_FLOAT}
  278. s1.v:=2.0;
  279. s2.v:=3.0;
  280. s3.v1:=4.5;
  281. s3.v2:=5.125;
  282. s4.v1:=6.175;
  283. s4.v2:=7.5;
  284. s5.v1:=8.075;
  285. s5.v2:=9.000125;
  286. s6.v1:=10.25;
  287. s6.v2:=11.5;
  288. s6.v3:=12.125;
  289. s7.v1:=13.5;
  290. s7.v2:=14;
  291. s7.v3:=15.0625;
  292. s8.d:=16.000575;
  293. s9.v1:=$123456789012345;
  294. s9.v2:=17.0;
  295. s10.v1:=$234567890123456;
  296. s10.v2:=-12399;
  297. s10.v3:=18.0;
  298. s11.v1:=$345678901234567;
  299. s11.v2:=19.0;
  300. s12.v1:=$456789012345678;
  301. s12.v2:=20.0;
  302. s12.v3:=21.0;
  303. s13.v1:=22.0;
  304. s13.v2:=$567890123456789;
  305. s14.v1:=23.0;
  306. s14.v2:=$19283774;
  307. s14.v3:=12356;
  308. s15.v1:=24.0;
  309. s15.v2:=$28195647;
  310. s15.v3:=25.0;
  311. s16.v1:=26.5;
  312. s16.v2:=27.75;
  313. s16.v3:=28.25;
  314. s16.v4:=29.125;
  315. s17.v1:=31.25;
  316. s17.v2:=32.125;
  317. s31.v1:=32.625;
  318. s31.v2:=33.5;
  319. verify(pass1(s1,1), check1(s1), 1);
  320. verify(pass2(s2,2), check2(s2), 2);
  321. verify(pass3(s3,3), check3(s3), 3);
  322. verify(pass4(s4,4), check4(s4), 4);
  323. verify(pass5(s5,5), check5(s5), 5);
  324. verify(pass6(s6,6), check6(s6), 6);
  325. verify(pass7(s7,7), check7(s7), 7);
  326. verify(pass8(s8,8), check8(s8), 8);
  327. verify(pass9(s9,9), check9(s9), 9);
  328. verify(pass10(s10,10), check10(s10), 10);
  329. verify(pass11(s11,11), check11(s11), 11);
  330. verify(pass12(s12,12), check12(s12), 12);
  331. verify(pass13(s13,13), check13(s13), 13);
  332. verify(pass14(s14,14), check14(s14), 14);
  333. verify(pass15(s15,15), check15(s15), 15);
  334. verify(pass16(s16,16), check16(s16), 16);
  335. verify(pass17(s17,17), check17(s17), 17);
  336. {$ifdef FPC_HAS_TYPE_EXTENDED}
  337. verify(pass31(s31,31), check31(s31), 31);
  338. {$endif}
  339. verify(check1(pass1a(1,s1)), check1(s1), 41);
  340. verify(check2(pass2a(2,s2)), check2(s2), 42);
  341. verify(check3(pass3a(3,s3)), check3(s3), 43);
  342. verify(check4(pass4a(4,s4)), check4(s4), 44);
  343. verify(check5(pass5a(5,s5)), check5(s5), 45);
  344. verify(check6(pass6a(6,s6)), check6(s6), 46);
  345. verify(check7(pass7a(7,s7)), check7(s7), 47);
  346. verify(check8(pass8a(8,s8)), check8(s8), 48);
  347. verify(check9(pass9a(9,s9)), check9(s9), 49);
  348. verify(check10(pass10a(10,s10)), check10(s10), 50);
  349. verify(check11(pass11a(11,s11)), check11(s11), 51);
  350. verify(check12(pass12a(12,s12)), check12(s12), 52);
  351. verify(check13(pass13a(13,s13)), check13(s13), 53);
  352. verify(check14(pass14a(14,s14)), check14(s14), 54);
  353. verify(check15(pass15a(15,s15)), check15(s15), 55);
  354. verify(check16(pass16a(16,s16)), check16(s16), 56);
  355. verify(check17(pass17a(17,s17)), check17(s17), 57);
  356. {$ifdef FPC_HAS_TYPE_EXTENDED}
  357. verify(check31(pass31a(31,s31)), check31(s31), 71);
  358. {$endif}
  359. verify(pass1a(1,s1).v, s1.v, 81);
  360. verify(pass2a(2,s2).v, s2.v, 82);
  361. verify(pass3a(3,s3).v1, s3.v1, 83);
  362. verify(pass3a(3,s3).v2, s3.v2, 103);
  363. verify(pass4a(4,s4).v1, s4.v1, 84);
  364. verify(pass5a(5,s5).v1, s5.v1, 85);
  365. verify(pass6a(6,s6).v1, s6.v1, 86);
  366. verify(pass7a(7,s7).v1, s7.v1, 87);
  367. verify(pass7a(7,s7).v2, s7.v2, 107);
  368. verify(pass8a(8,s8).d, s8.d, 88);
  369. verify(pass9a(9,s9).v1, s9.v1, 89);
  370. verify(pass10a(10,s10).v1, s10.v1, 90);
  371. verify(pass10a(10,s10).v2, s10.v2, 90);
  372. verify(pass11a(11,s11).v1, s11.v1, 91);
  373. verify(pass12a(12,s12).v1, s12.v1, 92);
  374. verify(pass13a(13,s13).v1, s13.v1, 93);
  375. verify(pass14a(14,s14).v1, s14.v1, 94);
  376. verify(pass15a(15,s15).v1, s15.v1, 95);
  377. verify(pass16a(16,s16).v1, s16.v1, 96);
  378. verify(pass17a(17,s17).v1, s17.v1, 97);
  379. {$ifdef FPC_HAS_TYPE_EXTENDED}
  380. verify(pass31a(31,s31).v1, s31.v1, 101);
  381. {$endif}
  382. {$endif ndef nofloat}
  383. if (not success) then
  384. halt(1);
  385. end;
  386. begin
  387. dotest;
  388. end.