ttryfin1.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. {****************************************************************}
  2. { CODE GENERATOR TEST PROGRAM }
  3. { By Carl Eric Codere }
  4. {****************************************************************}
  5. { NODE TESTED : secondtryfinally() }
  6. { secondraise() }
  7. {****************************************************************}
  8. { PRE-REQUISITES: secondload() }
  9. { secondassign() }
  10. { secondtypeconv() }
  11. { secondtryexcept() }
  12. { secondcalln() }
  13. { secondadd() }
  14. {****************************************************************}
  15. { DEFINES: }
  16. { FPC = Target is FreePascal compiler }
  17. {****************************************************************}
  18. {****************************************************************}
  19. program ttryfin1;
  20. {$ifdef fpc}
  21. {$mode objfpc}
  22. {$endif}
  23. Type
  24. TAObject = class(TObject)
  25. a : longint;
  26. end;
  27. TBObject = Class(TObject)
  28. b : longint;
  29. end;
  30. { The test cases were taken from the SAL internal architecture manual }
  31. procedure fail;
  32. begin
  33. WriteLn('Failure.');
  34. halt(1);
  35. end;
  36. var
  37. global_counter : integer;
  38. Procedure raiseanexception;
  39. Var A : TAObject;
  40. begin
  41. { Writeln ('Creating exception object');}
  42. A:=TAObject.Create;
  43. { Writeln ('Raising with this object');}
  44. raise A;
  45. { this should never happen, if it does there is a problem! }
  46. RunError(255);
  47. end;
  48. procedure IncrementCounter(x: integer);
  49. begin
  50. Inc(global_counter);
  51. end;
  52. procedure DecrementCounter(x: integer);
  53. begin
  54. Dec(global_counter);
  55. end;
  56. { Will the finally clause of a try block be called if the try block exited normally? }
  57. Function DoTryFinallyOne: boolean;
  58. var
  59. failed : boolean;
  60. begin
  61. Write('Try..Finally clause...');
  62. global_counter:=0;
  63. failed:=true;
  64. DoTryFinallyOne := failed;
  65. Try
  66. IncrementCounter(global_counter);
  67. DecrementCounter(global_counter);
  68. finally
  69. if global_counter = 0 then
  70. failed :=false;
  71. DoTryFinallyOne := failed;
  72. end;
  73. end;
  74. {
  75. Will the finally clause of a try block be called if the try block
  76. is inside a sub-block and the try block is exited with the break
  77. statement?
  78. }
  79. Function DoTryFinallyTwo : boolean;
  80. var
  81. failed : boolean;
  82. begin
  83. Write('Try..Finally with break statement...');
  84. global_counter:=0;
  85. failed:=true;
  86. DoTryFinallyTwo := failed;
  87. while (failed) do
  88. begin
  89. Try
  90. IncrementCounter(global_counter);
  91. DecrementCounter(global_counter);
  92. break;
  93. finally
  94. if global_counter = 0 then
  95. failed :=false;
  96. DoTryFinallyTwo := failed;
  97. end;
  98. end;
  99. end;
  100. {
  101. Will the finally clause of a try block be called if the try block
  102. is inside a sub-block and the try block is exited with the continue
  103. statement?
  104. }
  105. Function DoTryFinallyThree : boolean;
  106. var
  107. failed : boolean;
  108. begin
  109. Write('Try..Finally with continue statement...');
  110. global_counter:=0;
  111. failed:=true;
  112. DoTryFinallyThree := failed;
  113. while (failed) do
  114. begin
  115. Try
  116. IncrementCounter(global_counter);
  117. DecrementCounter(global_counter);
  118. continue;
  119. finally
  120. if global_counter = 0 then
  121. failed :=false;
  122. DoTryFinallyThree := failed;
  123. end;
  124. end;
  125. end;
  126. {
  127. Will the finally clause of a try block be called if the try block
  128. is inside a sub-block and the try block is exited with the exit
  129. statement?
  130. }
  131. Function DoTryFinallyFour: boolean;
  132. var
  133. failed : boolean;
  134. begin
  135. Write('Try..Finally with exit statement...');
  136. global_counter:=0;
  137. failed:=true;
  138. DoTryFinallyFour := failed;
  139. while (failed) do
  140. begin
  141. Try
  142. IncrementCounter(global_counter);
  143. DecrementCounter(global_counter);
  144. exit;
  145. finally
  146. if global_counter = 0 then
  147. failed :=false;
  148. DoTryFinallyFour := failed;
  149. end;
  150. end;
  151. end;
  152. (*
  153. { Will the finally clause of a try block be called if the try block raises an exception? }
  154. Procedure DoTryFinallyThree;
  155. var
  156. failed : boolean;
  157. begin
  158. Write('Try..Finally with exception rise...');
  159. global_counter:=0;
  160. failed:=true;
  161. Try
  162. IncrementCounter(global_counter);
  163. RaiseAnException;
  164. DecrementCounter(global_counter);
  165. finally
  166. if global_counter = 1 then
  167. failed :=false;
  168. if failed then
  169. fail
  170. else
  171. WriteLn('Success!');
  172. end;
  173. end;
  174. *)
  175. { Will the finally clause of all nested try blocks be called if the try blocks exited normally? }
  176. Function DoTryFinallyFive: boolean;
  177. var
  178. failed : boolean;
  179. x : integer;
  180. begin
  181. Write('Try..Finally nested clauses (three-level nesting)...');
  182. global_counter:=0;
  183. failed:=true;
  184. DoTryFinallyFive := failed;
  185. x:=0;
  186. Try
  187. IncrementCounter(global_counter);
  188. Try
  189. DecrementCounter(global_counter);
  190. IncrementCounter(global_counter);
  191. Try
  192. DecrementCounter(global_counter);
  193. finally
  194. Inc(x);
  195. end;
  196. finally
  197. Inc(x);
  198. End;
  199. finally
  200. if (global_counter = 0) and (x = 2) then
  201. failed :=false;
  202. DoTryFinallyFive := failed;
  203. end;
  204. end;
  205. {
  206. Will the finally clauses of all try blocks be called if they are
  207. nested within each other and all are nested within a sub-block
  208. and a break statement is encountered in the innermost try
  209. block?
  210. }
  211. Function DoTryFinallySix : boolean;
  212. var
  213. failed : boolean;
  214. x: integer;
  215. begin
  216. Write('Try..Finally nested clauses with break statement...');
  217. global_counter:=0;
  218. x:=0;
  219. failed:=true;
  220. DoTryFinallySix := failed;
  221. while (failed) do
  222. begin
  223. Try
  224. IncrementCounter(global_counter);
  225. Try
  226. DecrementCounter(global_counter);
  227. IncrementCounter(global_counter);
  228. Try
  229. DecrementCounter(global_counter);
  230. break;
  231. finally
  232. Inc(x);
  233. end;
  234. finally
  235. Inc(x);
  236. End;
  237. finally
  238. if (global_counter = 0) and (x = 2) then
  239. failed :=false;
  240. DoTryFinallySix := failed;
  241. end;
  242. end;
  243. end;
  244. {
  245. Will the finally clauses of all try blocks be called if they are
  246. nested within each other and all are nested within a sub-block
  247. and a continue statement is encountered in the innermost try
  248. block?
  249. }
  250. Function DoTryFinallySeven : boolean;
  251. var
  252. failed : boolean;
  253. x: integer;
  254. begin
  255. Write('Try..Finally nested clauses with continue statement...');
  256. global_counter:=0;
  257. x:=0;
  258. failed:=true;
  259. DoTryFinallySeven := failed;
  260. while (failed) do
  261. begin
  262. Try
  263. IncrementCounter(global_counter);
  264. Try
  265. DecrementCounter(global_counter);
  266. IncrementCounter(global_counter);
  267. Try
  268. DecrementCounter(global_counter);
  269. continue;
  270. finally
  271. Inc(x);
  272. end;
  273. finally
  274. Inc(x);
  275. End;
  276. finally
  277. if (global_counter = 0) and (x = 2) then
  278. failed :=false;
  279. DoTryFinallySeven := failed;
  280. end;
  281. end;
  282. end;
  283. {
  284. Will the finally clauses of all try blocks be called if they are
  285. nested within each other and all are nested within a sub-block
  286. and an exit statement is encountered in the innermost try
  287. block?
  288. }
  289. Function DoTryFinallyEight : boolean;
  290. var
  291. failed : boolean;
  292. x: integer;
  293. begin
  294. Write('Try..Finally nested clauses with exit statement...');
  295. global_counter:=0;
  296. x:=0;
  297. failed:=true;
  298. DoTryFinallyEight := failed;
  299. while (failed) do
  300. begin
  301. Try
  302. IncrementCounter(global_counter);
  303. Try
  304. DecrementCounter(global_counter);
  305. IncrementCounter(global_counter);
  306. Try
  307. DecrementCounter(global_counter);
  308. exit;
  309. finally
  310. Inc(x);
  311. end;
  312. finally
  313. Inc(x);
  314. End;
  315. finally
  316. if (global_counter = 0) and (x = 2) then
  317. failed :=false;
  318. DoTryFinallyEight := failed;
  319. end;
  320. end;
  321. end;
  322. (*
  323. ------------------
  324. *)
  325. {
  326. If several try blocks are nested within a sub-block, and that sub-block is
  327. nested in a try block within another try block, and the innermost try
  328. blocks are exited due to a break, will all finally clauses be called?
  329. }
  330. Function DoTryFinallyNine : boolean;
  331. var
  332. failed : boolean;
  333. x: integer;
  334. begin
  335. Write('Try..Finally nested clauses with break statement in other try-block...');
  336. global_counter:=0;
  337. x:=0;
  338. failed:=true;
  339. DoTryFinallyNine := failed;
  340. Try
  341. while (failed) do
  342. begin
  343. Try
  344. IncrementCounter(global_counter);
  345. Try
  346. DecrementCounter(global_counter);
  347. IncrementCounter(global_counter);
  348. Try
  349. DecrementCounter(global_counter);
  350. break;
  351. finally
  352. Inc(x);
  353. end;
  354. finally
  355. Inc(x);
  356. End;
  357. finally
  358. if (global_counter = 0) and (x = 2) then
  359. failed :=false;
  360. DoTryFinallyNine := failed;
  361. end;
  362. end; {end while }
  363. finally
  364. { normally this should execute! }
  365. DoTryFinallyNine := failed;
  366. end;
  367. end;
  368. {
  369. If several try blocks are nested within a sub-block, and that sub-block is
  370. nested in a try block within another try block, and the innermost try
  371. blocks are exited due to an exit, will all finally clauses be called?
  372. }
  373. Function DoTryFinallyTen : boolean;
  374. var
  375. failed : boolean;
  376. x: integer;
  377. begin
  378. Write('Try..Finally nested clauses with exit statement in other try-block...');
  379. global_counter:=0;
  380. x:=0;
  381. failed:=true;
  382. DoTryFinallyTen := failed;
  383. Try
  384. while (failed) do
  385. begin
  386. Try
  387. IncrementCounter(global_counter);
  388. Try
  389. DecrementCounter(global_counter);
  390. IncrementCounter(global_counter);
  391. Try
  392. DecrementCounter(global_counter);
  393. exit;
  394. finally
  395. Inc(x);
  396. end;
  397. finally
  398. Inc(x);
  399. End;
  400. finally
  401. x:=1;
  402. end;
  403. end; {end while }
  404. finally
  405. { normally this should execute! }
  406. if (global_counter = 0) and (x = 1) then
  407. failed :=false;
  408. DoTryFinallyTen := failed;
  409. end;
  410. end;
  411. var
  412. failed: boolean;
  413. begin
  414. failed := DoTryFinallyOne;
  415. if failed then
  416. fail
  417. else
  418. WriteLn('Success!');
  419. failed := DoTryFinallyTwo;
  420. if failed then
  421. fail
  422. else
  423. WriteLn('Success!');
  424. failed := DoTryFinallyThree;
  425. if failed then
  426. fail
  427. else
  428. WriteLn('Success!');
  429. failed := DoTryFinallyFour;
  430. if failed then
  431. fail
  432. else
  433. WriteLn('Success!');
  434. failed := DoTryFinallyFive;
  435. if failed then
  436. fail
  437. else
  438. WriteLn('Success!');
  439. failed := DoTryFinallySix;
  440. if failed then
  441. fail
  442. else
  443. WriteLn('Success!');
  444. failed := DoTryFinallySeven;
  445. if failed then
  446. fail
  447. else
  448. WriteLn('Success!');
  449. failed := DoTryFinallyEight;
  450. if failed then
  451. fail
  452. else
  453. WriteLn('Success!');
  454. failed := DoTryFinallyNine;
  455. if failed then
  456. fail
  457. else
  458. WriteLn('Success!');
  459. failed := DoTryFinallyTen;
  460. if failed then
  461. fail
  462. else
  463. WriteLn('Success!');
  464. end.
  465. {
  466. $Log$
  467. Revision 1.2 2002-09-07 15:40:56 peter
  468. * old logs removed and tabs fixed
  469. Revision 1.1 2002/08/03 11:05:14 carl
  470. + exception handling testing
  471. (still missing raise / on node testing)
  472. }