* Floating-point and internal function pure tests

This commit is contained in:
J. Gareth "Curious Kit" Moreton 2024-04-11 08:15:52 +01:00 committed by J. Gareth "Kit" Moreton
parent 3cbbd64a20
commit 878b51e24f
3 changed files with 130 additions and 0 deletions

43
tests/test/tpure4.pp Normal file
View File

@ -0,0 +1,43 @@
{ $OPT=-O2 -Sew }
{$MODE OBJFPC}
program tpure4;
function TestFrac(d : ValReal) : ValReal; pure; [internproc:fpc_in_frac_real];
function intpower(base : Double;exponent : longint) : Double; pure;
begin
if exponent<0 then
begin
base:=1.0/base;
exponent:=-exponent;
end;
intpower:=1.0;
while exponent<>0 do
begin
if exponent and 1<>0 then
intpower:=intpower*base;
exponent:=exponent shr 1;
base:=sqr(base);
end;
end;
function Power(base,exponent : Double) : Double; pure;
begin
if Exponent=0.0 then
result:=1.0
else if (base=0.0) and (exponent>0.0) then
result:=0.0
else if (TestFrac(exponent)=0.0) and (abs(exponent)<=maxint) then
result:=intpower(base,trunc(exponent))
else
result:=exp(exponent * ln (base));
end;
begin
if Power(2, 3) <> 8.0 then
Halt(1);
WriteLn('ok');
end.

43
tests/test/tpure4a.pp Normal file
View File

@ -0,0 +1,43 @@
{ $OPT=-O2 -Sew }
{$MODE OBJFPC}
program tpure4a;
function TestFrac(d : ValReal) : ValReal; [internproc:fpc_in_frac_real]; { This should still work even if "pure" is missing }
function intpower(base : Double;exponent : longint) : Double; pure;
begin
if exponent<0 then
begin
base:=1.0/base;
exponent:=-exponent;
end;
intpower:=1.0;
while exponent<>0 do
begin
if exponent and 1<>0 then
intpower:=intpower*base;
exponent:=exponent shr 1;
base:=sqr(base);
end;
end;
function Power(base,exponent : Double) : Double; pure;
begin
if Exponent=0.0 then
result:=1.0
else if (base=0.0) and (exponent>0.0) then
result:=0.0
else if (TestFrac(exponent)=0.0) and (abs(exponent)<=maxint) then
result:=intpower(base,trunc(exponent))
else
result:=exp(exponent * ln (base));
end;
begin
if Power(2, 3) <> 8.0 then
Halt(1);
WriteLn('ok');
end.

44
tests/test/tpure4b.pp Normal file
View File

@ -0,0 +1,44 @@
{ %FAIL }
{ $OPT=-O2 -Sew }
{$MODE OBJFPC}
program tpure4b;
function TestFrac(d : ValReal) : ValReal; pure; [internproc:fpc_in_frac_real];
function intpower(base : Double;exponent : longint) : Double;
begin
if exponent<0 then
begin
base:=1.0/base;
exponent:=-exponent;
end;
intpower:=1.0;
while exponent<>0 do
begin
if exponent and 1<>0 then
intpower:=intpower*base;
exponent:=exponent shr 1;
base:=sqr(base);
end;
end;
function Power(base,exponent : Double) : Double; pure;
begin
if Exponent=0.0 then
result:=1.0
else if (base=0.0) and (exponent>0.0) then
result:=0.0
else if (TestFrac(exponent)=0.0) and (abs(exponent)<=maxint) then
result:=intpower(base,trunc(exponent))
else
result:=exp(exponent * ln (base));
end;
begin
if Power(2, 3) <> 8.0 then
Halt(1);
WriteLn('ok');
end.