[fpc-devel] Speed of TTestCase

Martin Frb lazarus at mfriebe.de
Sat Jun 14 17:50:46 CEST 2025


On 14/06/2025 15:44, Michael Van Canneyt via fpc-devel wrote:
>
> I have heaps and heaps of testcases, but I've never felt the need to 
> investigate
> the speed of the Assert* calls.

I wasn't actually looking at it... I found it by random chance.

I was trying to kcachegrind some of my code, and the results said that 
the majority of time went into string handling that wasn't part of the 
tested code.


>
> Millions of AssertEquals calls ? I'm surprised that you are surprised 
> it takes so long? :-)
I haven't counted them.

But some of my tests are simply fuzzing (ish) tests (not necessarily 
random, but just throwing large amount of data at the code).

>
> If you present a patch to avoid constructing the message in case the 
> test is
> OK, that's certainly fine for me.

I may... Need to find some time.

Also, what do you think about overloading AssertEquals (and the like) taking
AssertEqual(
   const AnFormatString: string;
   const AFormatData: array of const;
   AnExpect,
   AnActual: TFoo
)
?

That allows to defer the call to format too.

Or to replace
   AssertEqual('Testing element ' + inttostr(n), ...)
with
   AssertEqual('Testing element %d', [n], ...)


>
> But then please do it so the comparison is done only once, i.e. 
> something like:
>
> if aExpected=aValue then
>   Inc(AssertCount)
> else
>   Fail(ComparisonMsg(aMessage, aExpected,aValue),CallerAddress);
>

Either that, or (protected or public)

procedure CheckEqual(AExp, AnActual: integer): Boolean; overload;
begin
   result :=  aExpected=aValue;
   Inc(AssertCount) ;
end

procedure AssertEqual
if not checkequal(AExp, AnActual) then
   Fail(....)


Either one can be marked inline. But doesn't have to, even non inline 
they are fast enough.

After all, its testcases, its not about a few seconds extra or less.
But the changes for me, brought some tests from 5 minutes to just 1 
minute. And that helps, if I am making changes to the tested code....


More information about the fpc-devel mailing list