* New pure tests that evaluate analysis of while and repeat loops, including infinite loops

This commit is contained in:
J. Gareth "Curious Kit" Moreton 2024-05-07 08:37:38 +01:00 committed by J. Gareth "Kit" Moreton
parent d469e3e68d
commit 5cb2f823d2
11 changed files with 236 additions and 0 deletions

18
tests/test/tpure7.pp Normal file
View File

@ -0,0 +1,18 @@
{ %NORUN }
{ Evaluate maliciously-written pure function that contains an infinite loop }
{$MODE OBJFPC}
program tpure7;
function MaliciousPure(Input: Boolean): Cardinal; pure;
begin
Result := 0;
repeat
Inc(Result);
until Input;
end;
begin
WriteLn(MaliciousPure(False));
end.

18
tests/test/tpure7a.pp Normal file
View File

@ -0,0 +1,18 @@
{ %NORUN }
{ Evaluate maliciously-written pure function that contains an infinite recursive loop }
{$MODE OBJFPC}
program tpure7a;
function MaliciousPure(Input: Boolean): Cardinal; pure;
begin
Result := 0;
repeat
Inc(Result);
until (MaliciousPure(not Input) = 0);
end;
begin
WriteLn(MaliciousPure(False));
end.

17
tests/test/tpure7b.pp Normal file
View File

@ -0,0 +1,17 @@
{ %NORUN }
{ Evaluate maliciously-written pure function that contains an infinite loop }
{$MODE OBJFPC}
program tpure7b;
function MaliciousPure(Input: Boolean): Cardinal; pure;
begin
Result := 0;
while Input do
Inc(Result);
end;
begin
WriteLn(MaliciousPure(True));
end.

17
tests/test/tpure7c.pp Normal file
View File

@ -0,0 +1,17 @@
{ %NORUN }
{ Evaluate maliciously-written pure function that contains an infinite recursive loop }
{$MODE OBJFPC}
program tpure7b;
function MaliciousPure(Input: Boolean): Cardinal; pure;
begin
Result := 0;
while (MaliciousPure(Input) = 0) do
Inc(Result);
end;
begin
WriteLn(MaliciousPure(True));
end.

17
tests/test/tpure7d.pp Normal file
View File

@ -0,0 +1,17 @@
{ %NORUN }
{ Evaluate maliciously-written pure function that contains an infinite loop (the while condition should evaluate successfully to 0) }
{$MODE OBJFPC}
program tpure7b;
function MaliciousPure(Input: Boolean): Cardinal; pure;
begin
Result := 0;
while (Input and (MaliciousPure(not Input) = 0)) do
Inc(Result);
end;
begin
WriteLn(MaliciousPure(True));
end.

26
tests/test/tpure7e.pp Normal file
View File

@ -0,0 +1,26 @@
{ %OPT=-Sew }
{ Ensure repeat..until with recursion is properly handled in pure functions }
{$MODE OBJFPC}
program tpure7e;
function RepeatRecursePure(Input: Boolean): Cardinal; pure;
begin
Result := 0;
if Input then
Exit;
repeat
Inc(Result);
until (RepeatRecursePure(not Input) = 0);
end;
begin
if RepeatRecursePure(False) <> 1 then
begin
WriteLn('FAIL: Pure analysis returned wrong value');
Halt(1);
end;
WriteLn('ok');
end.

24
tests/test/tpure7f.pp Normal file
View File

@ -0,0 +1,24 @@
{ %OPT=-Sew }
{ Test unconditional Break in repeat..until False loop }
{$MODE OBJFPC}
program tpure7f;
function UnconditionalBreak(Input: Boolean): Boolean; pure;
begin
Result := not Input;
repeat
Break;
until False;
end;
begin
if UnconditionalBreak(True) then
begin
WriteLn('FAIL: Pure analysis returned wrong value');
Halt(1);
end;
WriteLn('ok');
end.

23
tests/test/tpure7g.pp Normal file
View File

@ -0,0 +1,23 @@
{ %OPT=-Sew }
{ Test unconditional Break in "while True do" loop }
{$MODE OBJFPC}
program tpure7g;
function UnconditionalBreak(Input: Boolean): Boolean; pure;
begin
Result := not Input;
while True do
Break;
end;
begin
if UnconditionalBreak(True) then
begin
WriteLn('FAIL: Pure analysis returned wrong value');
Halt(1);
end;
WriteLn('ok');
end.

25
tests/test/tpure7h.pp Normal file
View File

@ -0,0 +1,25 @@
{ %OPT=-Sew }
{ Test unconditional Break in "while True do" loop }
{$MODE OBJFPC}
program tpure7h;
function UnconditionalBreak(Input: Boolean): Boolean; pure;
begin
Result := not Input;
while True do
begin
Break;
end;
end;
begin
if UnconditionalBreak(True) then
begin
WriteLn('FAIL: Pure analysis returned wrong value');
Halt(1);
end;
WriteLn('ok');
end.

26
tests/test/tpure7i.pp Normal file
View File

@ -0,0 +1,26 @@
{ %OPT=-Sew }
{ Test unconditional Break in repeat..until False loop }
{$MODE OBJFPC}
program tpure7i;
function UnconditionalBreak(Input: Boolean): Boolean; pure;
begin
Result := not Input;
repeat
begin
Break;
end;
until False;
end;
begin
if UnconditionalBreak(True) then
begin
WriteLn('FAIL: Pure analysis returned wrong value');
Halt(1);
end;
WriteLn('ok');
end.

25
tests/test/tpure7j.pp Normal file
View File

@ -0,0 +1,25 @@
{ %OPT=-Sew }
{ Test unconditional Break in repeat..until False (sort of) loop }
{$MODE OBJFPC}
program tpure7j;
function UnconditionalBreak(Input: Boolean): Boolean; pure;
begin
Result := not Input;
repeat
if Input then
Break;
until not Input;
end;
begin
if UnconditionalBreak(True) then
begin
WriteLn('FAIL: Pure analysis returned wrong value');
Halt(1);
end;
WriteLn('ok');
end.