assert

From Free Pascal wiki
Jump to navigationJump to search

The compiler procedure assert inserts an assertion. This is a procedure requiring the Boolean value true in order to proceed, otherwise a run-time error is generated.

use

The procedure assert has the following signature:

assert(Boolean)

The passed parameter has to be true to continue program flow. Usually this is an expression.

By default the FPC has code generation for assertions disabled. That means, invocations of assert do not end up in the generated binary, thus have no effect whatsoever. By specifying the local compiler directive {$assertions on} (or {$C+} for short) or specifying the ‑Sa command-line switch, appropriate code for assertions is inserted.

behavior

If the first parameter is false, the assertErrorProc procedure is called. This is by default a procedure generating the RTE 227 “Assertion failed error”. In order to convey more information, the assert procedure accepts a second optional short string parameter:

assert(Boolean, shortstring)

The second parameter’s value is passed to the current assertErrorProc. The default handler prints the message to standard error.

Light bulb  Note: The sysUtils unit installs an assertErrorProc handler generating an eAssertionFailed exception.

application

Assertions are a straightforward concept ensuring certain statements hold true. However, they do not guarantee your program is indeed correct: Assertions can be a tool to confirm the presence of programming mistakes (“bugs”), but they cannot prove the absence of any.

Assertions are frequently used during development. For example, in the following operator overload the assertion ensures certain properties about the operation:

operator - (const positive: foo): foo;
begin
	result := negation(foo);
	assert(sum(positive, result) = neutralElementOfAddition);
end;

However, checking this over and over again is not necessary in a production program. This is a decision that has to be made on a per-application-basis, sometimes on a per-assertion basis.

Although assert is usually provided with a non-trivial Boolean expression, constants are allowed too. In the following piece of code the programmer used an assertion to ensure there is always one alternative taken.

case  of
	a: 
	b: 
	c: 
	otherwise
	begin
		assert(false, 'one case has to match!');
	end;
end;

Otherwise, for Delphi-compatibility there would be no error if no case matches. Using an assert statement provides the flexibility to include or omit it from the generated code, though.

Assertions are not used to verify that the compiler works:

procedure foo(var bar: toot);
begin
	assert(assigned(@bar)); // wrong
	

Assertions are also not used for circumstances more specialized means are available for. That means, assertions are not supposed to replace

among other kinds of checks.

The paper the power of 10 suggest two assertions minimum per function. See also

see also