123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- {****************************************************************}
- { CODE GENERATOR TEST PROGRAM }
- { By Carl Eric Codere }
- {****************************************************************}
- { NODE TESTED : secondtryfinally() }
- { secondraise() }
- {****************************************************************}
- { PRE-REQUISITES: secondload() }
- { secondassign() }
- { secondtypeconv() }
- { secondtryexcept() }
- { secondcalln() }
- { secondadd() }
- {****************************************************************}
- { DEFINES: }
- { FPC = Target is FreePascal compiler }
- {****************************************************************}
- {****************************************************************}
- program ttryfin1;
- {$ifdef fpc}
- {$mode objfpc}
- {$endif}
- Type
- TAObject = class(TObject)
- a : longint;
- end;
- TBObject = Class(TObject)
- b : longint;
- end;
- { The test cases were taken from the SAL internal architecture manual }
- procedure fail;
- begin
- WriteLn('Failure.');
- halt(1);
- end;
- var
- global_counter : integer;
- Procedure raiseanexception;
- Var A : TAObject;
- begin
- { Writeln ('Creating exception object');}
- A:=TAObject.Create;
- { Writeln ('Raising with this object');}
- raise A;
- { this should never happen, if it does there is a problem! }
- RunError(255);
- end;
- procedure IncrementCounter(x: integer);
- begin
- Inc(global_counter);
- end;
- procedure DecrementCounter(x: integer);
- begin
- Dec(global_counter);
- end;
- { Will the finally clause of a try block be called if the try block exited normally? }
- Function DoTryFinallyOne: boolean;
- var
- failed : boolean;
- begin
- Write('Try..Finally clause...');
- global_counter:=0;
- failed:=true;
- DoTryFinallyOne := failed;
- Try
- IncrementCounter(global_counter);
- DecrementCounter(global_counter);
- finally
- if global_counter = 0 then
- failed :=false;
- DoTryFinallyOne := failed;
- end;
- end;
- {
- Will the finally clause of a try block be called if the try block
- is inside a sub-block and the try block is exited with the break
- statement?
- }
- Function DoTryFinallyTwo : boolean;
- var
- failed : boolean;
- begin
- Write('Try..Finally with break statement...');
- global_counter:=0;
- failed:=true;
- DoTryFinallyTwo := failed;
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- DecrementCounter(global_counter);
- break;
- finally
- if global_counter = 0 then
- failed :=false;
- DoTryFinallyTwo := failed;
- end;
- end;
- end;
- {
- Will the finally clause of a try block be called if the try block
- is inside a sub-block and the try block is exited with the continue
- statement?
- }
- Function DoTryFinallyThree : boolean;
- var
- failed : boolean;
- begin
- Write('Try..Finally with continue statement...');
- global_counter:=0;
- failed:=true;
- DoTryFinallyThree := failed;
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- DecrementCounter(global_counter);
- continue;
- finally
- if global_counter = 0 then
- failed :=false;
- DoTryFinallyThree := failed;
- end;
- end;
- end;
- {
- Will the finally clause of a try block be called if the try block
- is inside a sub-block and the try block is exited with the exit
- statement?
- }
- Function DoTryFinallyFour: boolean;
- var
- failed : boolean;
- begin
- Write('Try..Finally with exit statement...');
- global_counter:=0;
- failed:=true;
- DoTryFinallyFour := failed;
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- DecrementCounter(global_counter);
- exit;
- finally
- if global_counter = 0 then
- failed :=false;
- DoTryFinallyFour := failed;
- end;
- end;
- end;
- (*
- { Will the finally clause of a try block be called if the try block raises an exception? }
- Procedure DoTryFinallyThree;
- var
- failed : boolean;
- begin
- Write('Try..Finally with exception rise...');
- global_counter:=0;
- failed:=true;
- Try
- IncrementCounter(global_counter);
- RaiseAnException;
- DecrementCounter(global_counter);
- finally
- if global_counter = 1 then
- failed :=false;
- if failed then
- fail
- else
- WriteLn('Success!');
- end;
- end;
- *)
- { Will the finally clause of all nested try blocks be called if the try blocks exited normally? }
- Function DoTryFinallyFive: boolean;
- var
- failed : boolean;
- x : integer;
- begin
- Write('Try..Finally nested clauses (three-level nesting)...');
- global_counter:=0;
- failed:=true;
- DoTryFinallyFive := failed;
- x:=0;
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- finally
- Inc(x);
- end;
- finally
- Inc(x);
- End;
- finally
- if (global_counter = 0) and (x = 2) then
- failed :=false;
- DoTryFinallyFive := failed;
- end;
- end;
- {
- Will the finally clauses of all try blocks be called if they are
- nested within each other and all are nested within a sub-block
- and a break statement is encountered in the innermost try
- block?
- }
- Function DoTryFinallySix : boolean;
- var
- failed : boolean;
- x: integer;
- begin
- Write('Try..Finally nested clauses with break statement...');
- global_counter:=0;
- x:=0;
- failed:=true;
- DoTryFinallySix := failed;
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- break;
- finally
- Inc(x);
- end;
- finally
- Inc(x);
- End;
- finally
- if (global_counter = 0) and (x = 2) then
- failed :=false;
- DoTryFinallySix := failed;
- end;
- end;
- end;
- {
- Will the finally clauses of all try blocks be called if they are
- nested within each other and all are nested within a sub-block
- and a continue statement is encountered in the innermost try
- block?
- }
- Function DoTryFinallySeven : boolean;
- var
- failed : boolean;
- x: integer;
- begin
- Write('Try..Finally nested clauses with continue statement...');
- global_counter:=0;
- x:=0;
- failed:=true;
- DoTryFinallySeven := failed;
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- continue;
- finally
- Inc(x);
- end;
- finally
- Inc(x);
- End;
- finally
- if (global_counter = 0) and (x = 2) then
- failed :=false;
- DoTryFinallySeven := failed;
- end;
- end;
- end;
- {
- Will the finally clauses of all try blocks be called if they are
- nested within each other and all are nested within a sub-block
- and an exit statement is encountered in the innermost try
- block?
- }
- Function DoTryFinallyEight : boolean;
- var
- failed : boolean;
- x: integer;
- begin
- Write('Try..Finally nested clauses with exit statement...');
- global_counter:=0;
- x:=0;
- failed:=true;
- DoTryFinallyEight := failed;
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- exit;
- finally
- Inc(x);
- end;
- finally
- Inc(x);
- End;
- finally
- if (global_counter = 0) and (x = 2) then
- failed :=false;
- DoTryFinallyEight := failed;
- end;
- end;
- end;
- (*
- ------------------
- *)
- {
- If several try blocks are nested within a sub-block, and that sub-block is
- nested in a try block within another try block, and the innermost try
- blocks are exited due to a break, will all finally clauses be called?
- }
- Function DoTryFinallyNine : boolean;
- var
- failed : boolean;
- x: integer;
- begin
- Write('Try..Finally nested clauses with break statement in other try-block...');
- global_counter:=0;
- x:=0;
- failed:=true;
- DoTryFinallyNine := failed;
- Try
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- break;
- finally
- Inc(x);
- end;
- finally
- Inc(x);
- End;
- finally
- if (global_counter = 0) and (x = 2) then
- failed :=false;
- DoTryFinallyNine := failed;
- end;
- end; {end while }
- finally
- { normally this should execute! }
- DoTryFinallyNine := failed;
- end;
- end;
- {
- If several try blocks are nested within a sub-block, and that sub-block is
- nested in a try block within another try block, and the innermost try
- blocks are exited due to an exit, will all finally clauses be called?
- }
- Function DoTryFinallyTen : boolean;
- var
- failed : boolean;
- x: integer;
- begin
- Write('Try..Finally nested clauses with exit statement in other try-block...');
- global_counter:=0;
- x:=0;
- failed:=true;
- DoTryFinallyTen := failed;
- Try
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- exit;
- finally
- Inc(x);
- end;
- finally
- Inc(x);
- End;
- finally
- x:=1;
- end;
- end; {end while }
- finally
- { normally this should execute! }
- if (global_counter = 0) and (x = 1) then
- failed :=false;
- DoTryFinallyTen := failed;
- end;
- end;
- var
- failed: boolean;
- begin
- failed := DoTryFinallyOne;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryFinallyTwo;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryFinallyThree;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryFinallyFour;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryFinallyFive;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryFinallySix;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryFinallySeven;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryFinallyEight;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryFinallyNine;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryFinallyTen;
- if failed then
- fail
- else
- WriteLn('Success!');
- end.
- {
- $Log$
- Revision 1.2 2002-09-07 15:40:56 peter
- * old logs removed and tabs fixed
- Revision 1.1 2002/08/03 11:05:14 carl
- + exception handling testing
- (still missing raise / on node testing)
- }
|