123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831 |
- {****************************************************************}
- { CODE GENERATOR TEST PROGRAM }
- { By Carl Eric Codere }
- {****************************************************************}
- { NODE TESTED : secondtryexcept() }
- { secondraise() }
- {****************************************************************}
- { PRE-REQUISITES: secondload() }
- { secondassign() }
- { secondtypeconv() }
- { secondtryexcept() }
- { secondcalln() }
- { secondadd() }
- {****************************************************************}
- { DEFINES: }
- { FPC = Target is FreePascal compiler }
- {****************************************************************}
- { REMARKS : Tested with Delphi 3 as reference implementation }
- {****************************************************************}
- program ttryexc1;
- {$ifdef fpc}
- {$mode objfpc}
- {$endif}
- Type
- TAObject = class(TObject)
- a : longint;
- end;
- TBObject = Class(TObject)
- b : longint;
- constructor create(c: 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;
- constructor tbobject.create(c:longint);
- begin
- inherited create;
- b:=c;
- end;
- Procedure raiseanexception;
- Var A : TAObject;
- var B : 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;
- Function DoTryExceptOne: boolean;
- var
- failed : boolean;
- begin
- Write('Try..Except clause...');
- global_counter:=0;
- failed:=true;
- DoTryExceptOne := failed;
- Try
- IncrementCounter(global_counter);
- DecrementCounter(global_counter);
- except
- end;
- if global_counter = 0 then
- failed :=false;
- DoTryExceptOne := failed;
- end;
- Function DoTryExceptTwo : boolean;
- var
- failed : boolean;
- begin
- Write('Try..Except with break statement...');
- global_counter:=0;
- failed:=true;
- DoTryExceptTwo := failed;
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- DecrementCounter(global_counter);
- break;
- except
- end;
- end;
- if global_counter = 0 then
- failed :=false;
- DoTryExceptTwo := failed;
- end;
- Function DoTryExceptFour: boolean;
- var
- failed : boolean;
- begin
- Write('Try..Except with exit statement...');
- global_counter:=0;
- failed:=true;
- DoTryExceptFour := failed;
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- DecrementCounter(global_counter);
- DoTryExceptFour := false;
- exit;
- except
- end;
- end;
- end;
- Function DoTryExceptFive: boolean;
- var
- failed : boolean;
- x : integer;
- begin
- Write('Try..Except nested clauses (three-level nesting)...');
- global_counter:=0;
- failed:=true;
- DoTryExceptFive := failed;
- x:=0;
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- except
- Inc(x);
- end;
- except
- Inc(x);
- End;
- except
- end;
- if (global_counter = 0) then
- failed :=false;
- DoTryExceptFive := failed;
- end;
- Function DoTryExceptSix : boolean;
- var
- failed : boolean;
- x: integer;
- begin
- Write('Try..Except nested clauses with break statement...');
- global_counter:=0;
- x:=0;
- failed:=true;
- DoTryExceptSix := failed;
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- break;
- except
- Inc(x);
- end;
- except
- Inc(x);
- End;
- except
- end;
- end;
- if (global_counter = 0) then
- failed :=false;
- DoTryExceptSix := failed;
- end;
- Function DoTryExceptEight : boolean;
- var
- failed : boolean;
- x: integer;
- begin
- Write('Try..Except nested clauses with exit statement...');
- global_counter:=0;
- x:=0;
- failed:=true;
- DoTryExceptEight := failed;
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- DoTryExceptEight := false;
- exit;
- except
- Inc(x);
- end;
- except
- Inc(x);
- End;
- except
- end;
- end;
- end;
- Function DoTryExceptNine : boolean;
- var
- failed : boolean;
- x: integer;
- begin
- Write('Try..Except nested clauses with break statement in other try-block...');
- global_counter:=0;
- x:=0;
- failed:=true;
- DoTryExceptNine := failed;
- Try
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- break;
- except
- Inc(x);
- end;
- except
- Inc(x);
- End;
- except
- end;
- end; {end while }
- except
- { normally this should execute! }
- DoTryExceptNine := failed;
- end;
- if (global_counter = 0) and (x = 0) then
- failed :=false;
- DoTryExceptNine := failed;
- end;
- {****************************************************************************}
- {***************************************************************************}
- { Exception is thrown }
- {***************************************************************************}
- Function DoTryExceptTen: boolean;
- var
- failed : boolean;
- begin
- Write('Try..Except clause with raise...');
- global_counter:=0;
- failed:=true;
- DoTryExceptTen := failed;
- Try
- IncrementCounter(global_counter);
- RaiseAnException;
- DecrementCounter(global_counter);
- except
- if global_counter = 1 then
- failed :=false;
- DoTryExceptTen := failed;
- end;
- end;
- Function DoTryExceptEleven : boolean;
- var
- failed : boolean;
- begin
- Write('Try..Except with raise and break statement...');
- global_counter:=0;
- failed:=true;
- DoTryExceptEleven := failed;
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- DecrementCounter(global_counter);
- RaiseAnException;
- break;
- except
- if global_counter = 0 then
- failed :=false;
- DoTryExceptEleven := failed;
- end;
- end;
- end;
- Function DoTryExceptTwelve: boolean;
- var
- failed : boolean;
- x : integer;
- begin
- Write('Try..Except nested clauses (three-level nesting)...');
- global_counter:=0;
- failed:=true;
- DoTryExceptTwelve := failed;
- x:=0;
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- RaiseAnException;
- except
- if (global_counter = 0) then
- failed :=false;
- DoTryExceptTwelve := failed;
- end;
- except
- DoTryExceptTwelve := true;
- End;
- except
- DoTryExceptTwelve := true;
- end;
- end;
- Function DoTryExceptThirteen: boolean;
- var
- failed : boolean;
- x : integer;
- begin
- Write('Try..Except nested clauses (three-level nesting)...');
- global_counter:=0;
- failed:=true;
- DoTryExceptThirteen := failed;
- x:=0;
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- RaiseAnException;
- Try
- DecrementCounter(global_counter);
- except
- DoTryExceptThirteen := true;
- end;
- except
- if (global_counter = 1) then
- failed :=false;
- DoTryExceptThirteen := failed;
- End;
- except
- DoTryExceptThirteen := true;
- end;
- end;
- {***************************************************************************}
- { Exception is thrown in except block }
- {***************************************************************************}
- Function DoTryExceptFourteen: boolean;
- var
- failed : boolean;
- x : integer;
- begin
- Write('Try..Except nested clauses with single re-raise...');
- global_counter:=0;
- failed:=true;
- DoTryExceptFourteen := failed;
- x:=0;
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- RaiseAnException;
- except
- { raise to next block }
- Raise;
- end;
- except
- if (global_counter = 0) then
- failed :=false;
- DoTryExceptFourteen := failed;
- End;
- except
- DoTryExceptFourteen := true;
- end;
- end;
- Function DoTryExceptFifteen: boolean;
- var
- failed : boolean;
- x : integer;
- begin
- Write('Try..Except nested clauses with re-reraises (1)...');
- global_counter:=0;
- failed:=true;
- DoTryExceptFifteen := failed;
- x:=0;
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- RaiseAnException;
- except
- { raise to next block }
- Raise;
- end;
- except
- { re-raise to next block }
- Raise;
- End;
- except
- if (global_counter = 0) then
- failed :=false;
- DoTryExceptFifteen := failed;
- end;
- end;
- procedure nestedtryblock(var global_counter: integer);
- begin
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- RaiseAnException;
- except
- { raise to next block }
- Raise;
- end;
- except
- { re-raise to next block }
- Raise;
- End;
- end;
- Function DoTryExceptSixteen: boolean;
- var
- failed : boolean;
- x : integer;
- begin
- Write('Try..Except nested clauses with re-reraises (2)...');
- global_counter:=0;
- failed:=true;
- DoTryExceptSixteen := failed;
- x:=0;
- Try
- NestedTryBlock(global_counter);
- except
- if (global_counter = 0) then
- failed :=false;
- DoTryExceptSixteen := failed;
- end;
- end;
- Function DoTryExceptSeventeen: boolean;
- var
- failed : boolean;
- x : integer;
- begin
- Write('Try..Except nested clauses with raises...');
- global_counter:=0;
- failed:=true;
- DoTryExceptSeventeen := failed;
- x:=0;
- Try
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- RaiseAnException;
- except
- { raise to next block }
- raise TAObject.Create;
- end;
- except
- { re-raise to next block }
- raise TBObject.Create(1234);
- End;
- except
- if (global_counter = 0) then
- failed :=false;
- DoTryExceptSeventeen := failed;
- end;
- end;
- {***************************************************************************}
- { Exception flow control in except block }
- {***************************************************************************}
- Function DoTryExceptEighteen: boolean;
- var
- failed : boolean;
- begin
- Write('Try..Except clause with raise with break in except block...');
- global_counter:=0;
- failed:=true;
- DoTryExceptEighteen := failed;
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- RaiseAnException;
- DecrementCounter(global_counter);
- except
- if global_counter = 1 then
- failed :=false;
- DoTryExceptEighteen := failed;
- break;
- end;
- end;
- end;
- Function DoTryExceptNineteen: boolean;
- var
- failed : boolean;
- begin
- Write('Try..Except clause with raise with exit in except block...');
- global_counter:=0;
- failed:=true;
- DoTryExceptNineteen := failed;
- while (failed) do
- begin
- Try
- IncrementCounter(global_counter);
- RaiseAnException;
- DecrementCounter(global_counter);
- except
- if global_counter = 1 then
- failed :=false;
- DoTryExceptNineteen := failed;
- exit;
- end;
- end;
- end;
- Function DoTryExceptTwenty: boolean;
- var
- failed : boolean;
- x : integer;
- begin
- Write('Try..Except nested clauses with raises with break in inner try...');
- global_counter:=0;
- failed:=true;
- DoTryExceptTwenty := failed;
- x:=0;
- Try
- IncrementCounter(global_counter);
- Try
- while (x = 0) do
- begin
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- RaiseAnException;
- except
- { raise to next block }
- raise TAObject.Create;
- break;
- end;
- end;
- except
- { re-raise to next block }
- raise TBObject.Create(1234);
- End;
- except
- if (global_counter = 0) then
- failed :=false;
- DoTryExceptTwenty := failed;
- end;
- end;
- Function DoTryExceptTwentyOne: boolean;
- var
- failed : boolean;
- x : integer;
- begin
- Write('Try..Except nested clauses with raises with continue in inner try...');
- global_counter:=0;
- failed:=true;
- DoTryExceptTwentyOne := failed;
- x:=0;
- Try
- IncrementCounter(global_counter);
- Try
- while (x = 0) do
- begin
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- RaiseAnException;
- except
- { raise to next block }
- raise TAObject.Create;
- x:=1;
- continue;
- end;
- end;
- except
- { re-raise to next block }
- raise TBObject.Create(1234);
- End;
- except
- if (global_counter = 0) then
- failed :=false;
- DoTryExceptTwentyOne := failed;
- end;
- end;
- Function DoTryExceptTwentyTwo: boolean;
- var
- failed : boolean;
- x : integer;
- begin
- Write('Try..Except nested clauses with raises with exit in inner try...');
- global_counter:=0;
- failed:=true;
- DoTryExceptTwentyTwo := failed;
- x:=0;
- Try
- IncrementCounter(global_counter);
- Try
- while (x = 0) do
- begin
- DecrementCounter(global_counter);
- IncrementCounter(global_counter);
- Try
- DecrementCounter(global_counter);
- RaiseAnException;
- except
- { raise to next block }
- raise TAObject.Create;
- exit;
- end;
- end;
- except
- { re-raise to next block }
- raise TBObject.Create(1234);
- End;
- except
- if (global_counter = 0) then
- failed :=false;
- DoTryExceptTwentyTwo := failed;
- end;
- end;
- var
- failed: boolean;
- begin
- failed := DoTryExceptOne;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptTwo;
- if failed then
- fail
- else
- WriteLn('Success!');
- { failed := DoTryExceptThree;
- if failed then
- fail
- else
- WriteLn('Success!');}
- failed := DoTryExceptFour;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptFive;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptSix;
- if failed then
- fail
- else
- WriteLn('Success!');
- { failed := DoTryExceptSeven;
- if failed then
- fail
- else
- WriteLn('Success!');}
- failed := DoTryExceptEight;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptNine;
- if failed then
- fail
- else
- WriteLn('Success!');
- (************************ Exceptions are created from here ****************************)
- failed := DoTryExceptTen;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptEleven;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptTwelve;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptThirteen;
- if failed then
- fail
- else
- WriteLn('Success!');
- (************************ Exceptions in except block ****************************)
- failed := DoTryExceptFourteen;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptFifteen;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptSixteen;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptSeventeen;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptEighteen;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptNineteen;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptTwenty;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptTwentyOne;
- if failed then
- fail
- else
- WriteLn('Success!');
- failed := DoTryExceptTwentyTwo;
- if failed then
- fail
- else
- WriteLn('Success!');
- end.
- {
- $Log$
- Revision 1.2 2002-09-01 14:45:54 peter
- * updates to compile with kylix
- * fixed some tests
- Revision 1.1 2002/08/03 11:05:14 carl
- + exception handling testing
- (still missing raise / on node testing)
- }
|