123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782 |
- {$mode objfpc}
- uses
- erroru,sysutils;
- var
- i : longint;
- procedure test1;
- begin
- try
- i:=0;
- exit;
- finally
- inc(i);
- end;
- i:=-2;
- end;
- procedure test2;
- begin
- try
- i:=0;
- raise exception.create('');
- finally
- inc(i);
- end;
- i:=-2;
- end;
- procedure test3;
- begin
- try
- try
- i:=0;
- raise exception.create('');
- finally
- inc(i);
- end;
- finally
- inc(i);
- end;
- i:=-2;
- end;
- procedure test4;
- begin
- try
- try
- i:=0;
- exit;
- finally
- inc(i);
- end;
- finally
- inc(i);
- end;
- i:=-2;
- end;
- procedure test5;
- var
- j : longint;
- begin
- for j:=1 to 10 do
- begin
- try
- i:=0;
- break;
- finally
- inc(i);
- end;
- dec(i);
- end;
- end;
- procedure test6;
- var
- j : longint;
- begin
- i:=0;
- for j:=1 to 10 do
- begin
- try
- continue;
- finally
- inc(i);
- end;
- dec(i);
- end;
- end;
- procedure test7;
- var
- j : longint;
- begin
- for j:=1 to 10 do
- begin
- try
- try
- i:=0;
- break;
- finally
- inc(i);
- end;
- dec(i);
- finally
- inc(i);
- end;
- end;
- end;
- procedure test8;
- var
- j : longint;
- begin
- i:=0;
- for j:=1 to 10 do
- begin
- try
- try
- continue;
- finally
- inc(i);
- end;
- finally
- inc(i);
- end;
- dec(i);
- end;
- end;
- { some combined test ... }
- procedure test9;
- var
- j : longint;
- begin
- try
- i:=0;
- finally
- for j:=1 to 10 do
- begin
- try
- if j<2 then
- continue
- else
- break;
- finally
- inc(i);
- end;
- dec(i);
- end;
- end;
- end;
- procedure test10;
- var
- j : longint;
- begin
- try
- i:=0;
- j:=1;
- finally
- while j<=10 do
- begin
- try
- if j<2 then
- continue
- else
- break;
- finally
- inc(i);
- inc(j);
- end;
- dec(i);
- end;
- end;
- end;
- { the do_raise function is a little bit more complicated }
- { so we also check if memory is lost }
- function do_raise : ansistring;
- var
- a1,a2 : ansistring;
- j : longint;
- begin
- for j:=1 to 3 do
- begin
- a1:=copy('Hello world',1,5);
- do_raise:=copy(a2,1,1);
- end;
- raise exception.create('A string to test memory allocation');
- do_error(99998);
- end;
- { now test real exceptions }
- procedure test100;
- begin
- try
- i:=0;
- do_raise;
- except
- inc(i);
- end;
- end;
- procedure test101;
- begin
- try
- try
- i:=0;
- do_raise;
- except
- inc(i);
- do_raise;
- end;
- except
- inc(i);
- end;
- end;
- procedure test102;
- begin
- try
- try
- i:=0;
- do_raise;
- except
- inc(i);
- raise;
- end;
- except
- inc(i);
- end;
- end;
- { tests continue in try...except...end; statements }
- procedure test103;
- var
- j,k : longint;
- begin
- i:=0;
- for j:=1 to 10 do
- try
- for k:=1 to 10 do
- try
- inc(i);
- if (i mod 10)>5 then
- do_raise
- else
- continue;
- except
- continue
- end;
- if i>50 then
- do_raise
- else
- continue;
- except
- continue;
- end;
- end;
- procedure test104;
- begin
- try
- i:=1;
- exit;
- // we should never get there
- do_raise;
- except
- i:=-1;
- end;
- i:=-2;
- end;
- procedure test105;
- begin
- try
- i:=0;
- do_raise;
- // we should never get there
- i:=-1;
- except
- inc(i);
- exit;
- end;
- end;
- procedure test106;
- begin
- try
- try
- i:=1;
- exit;
- // we should never get there
- do_raise;
- except
- i:=-1;
- end;
- i:=-2;
- except
- end;
- end;
- procedure test107;
- begin
- try
- do_raise;
- except
- try
- i:=0;
- do_raise;
- // we should never get there
- i:=-1;
- except
- inc(i);
- exit;
- end;
- end;
- end;
- { tests break in try...except...end; statements }
- procedure test108;
- begin
- i:=0;
- while true do
- try
- while true do
- try
- inc(i);
- break;
- except
- end;
- inc(i);
- break;
- except
- end;
- end;
- procedure test109;
- begin
- i:=0;
- while true do
- try
- repeat
- try
- do_raise;
- i:=-1;
- except
- inc(i);
- break;
- end;
- until false;
- do_raise;
- i:=-1;
- except
- inc(i);
- break;
- end;
- end;
- { test the on statement }
- procedure test110;
- begin
- try
- i:=0;
- do_raise;
- except
- on e : exception do
- inc(i);
- end;
- end;
- procedure test111;
- begin
- try
- try
- i:=0;
- do_raise;
- except
- on e : exception do
- begin
- inc(i);
- do_raise;
- end;
- end;
- except
- on e : exception do
- inc(i);
- end;
- end;
- procedure test112;
- begin
- try
- try
- i:=0;
- do_raise;
- except
- on e : exception do
- begin
- inc(i);
- raise;
- end;
- end;
- except
- on e : exception do
- inc(i);
- end;
- end;
- procedure test113;
- var
- j,k : longint;
- begin
- i:=0;
- for j:=1 to 10 do
- try
- for k:=1 to 10 do
- try
- inc(i);
- if (i mod 10)>5 then
- do_raise
- else
- continue;
- except
- on e : exception do
- continue
- end;
- if i>50 then
- do_raise
- else
- continue;
- except
- on e : exception do
- continue;
- end;
- end;
- procedure test114;
- begin
- try
- i:=1;
- exit;
- // we should never get there
- do_raise;
- except
- on e : exception do
- i:=-1;
- end;
- i:=-2;
- end;
- procedure test115;
- begin
- try
- i:=0;
- do_raise;
- // we should never get there
- i:=-1;
- except
- on e : exception do
- begin
- inc(i);
- exit;
- end;
- end;
- end;
- procedure test116;
- begin
- try
- try
- i:=1;
- exit;
- // we should never get there
- do_raise;
- except
- on e : exception do
- i:=-1;
- end;
- i:=-2;
- except
- on e : exception do
- ;
- end;
- end;
- procedure test117;
- begin
- try
- do_raise;
- except
- try
- i:=0;
- do_raise;
- // we should never get there
- i:=-1;
- except
- on e : exception do
- begin
- inc(i);
- exit;
- end;
- end;
- end;
- end;
- { tests break in try...except...end; statements }
- procedure test118;
- begin
- i:=0;
- while true do
- try
- while true do
- try
- inc(i);
- break;
- except
- on e : exception do
- ;
- end;
- inc(i);
- break;
- except
- on e : exception do
- ;
- end;
- end;
- procedure test119;
- begin
- i:=0;
- while true do
- try
- repeat
- try
- do_raise;
- i:=-1;
- except
- on e : exception do
- begin
- inc(i);
- break;
- end;
- end;
- until false;
- do_raise;
- i:=-1;
- except
- on e : exception do
- begin
- inc(i);
- break;
- end;
- end;
- end;
- var
- mem : sizeuint;
- begin
- writeln('Testing exception handling');
-
- mem:=0;
- DoMem(mem);
-
- i:=-1;
- try
- test1;
- finally
- inc(i);
- end;
- if i<>2 then
- do_error(1001);
- i:=-1;
- try
- test2;
- except
- inc(i);
- end;
- if i<>2 then
- do_error(1002);
- i:=-1;
- try
- test3;
- except
- inc(i);
- end;
- if i<>3 then
- do_error(1003);
- i:=-1;
- test4;
- if i<>2 then
- do_error(1004);
- i:=-1;
- test5;
- if i<>1 then
- do_error(1005);
- i:=-1;
- test6;
- if i<>10 then
- do_error(1006);
- i:=-1;
- test7;
- if i<>2 then
- do_error(1007);
- i:=-1;
- test8;
- if i<>20 then
- do_error(1008);
- i:=-1;
- test9;
- if i<>2 then
- do_error(1009);
- i:=-1;
- test10;
- if i<>2 then
- do_error(1010);
- i:=-1;
- test100;
- if i<>1 then
- do_error(1100);
- i:=-1;
- test101;
- if i<>2 then
- do_error(1101);
- i:=-1;
- test102;
- if i<>2 then
- do_error(1102);
- i:=-1;
- test103;
- if i<>100 then
- do_error(1103);
- i:=-1;
- test104;
- if i<>1 then
- do_error(1104);
- i:=-1;
- test105;
- if i<>1 then
- do_error(1105);
- i:=-1;
- test106;
- if i<>1 then
- do_error(1106);
- i:=-1;
- test107;
- if i<>1 then
- do_error(1107);
- i:=-1;
- test108;
- if i<>2 then
- do_error(1108);
- i:=-1;
- test109;
- if i<>2 then
- do_error(1109);
- i:=-1;
- test110;
- if i<>1 then
- do_error(1110);
- i:=-1;
- test111;
- if i<>2 then
- do_error(1111);
- i:=-1;
- test112;
- if i<>2 then
- do_error(1112);
- i:=-1;
- test113;
- if i<>100 then
- do_error(1113);
- i:=-1;
- test114;
- if i<>1 then
- do_error(1114);
- i:=-1;
- test115;
- if i<>1 then
- do_error(1115);
- i:=-1;
- test116;
- if i<>1 then
- do_error(1116);
- i:=-1;
- test117;
- if i<>1 then
- do_error(1117);
- i:=-1;
- test118;
- if i<>2 then
- do_error(1118);
- i:=-1;
- test119;
- if i<>2 then
- do_error(1119);
- if DoMem(mem)<>0 then
- begin
- writeln('exception generates memory holes');
- do_error(99999);
- end;
- writeln('Test successfully passed');
- halt(0);
- end.
|