Difference between revisions of "assert"
(create) |
(fix spelling mistake) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
By default the [[FPC]] has code generation for assertions disabled. | By default the [[FPC]] has code generation for assertions disabled. | ||
That means, invocations of <syntaxhighlight lang="pascal" inline>assert</syntaxhighlight> do not end up in the generated binary, thus have no effect whatsoever. | That means, invocations of <syntaxhighlight lang="pascal" inline>assert</syntaxhighlight> do not end up in the generated binary, thus have no effect whatsoever. | ||
− | By specifying the [[local compiler directives|local compiler directive]] <syntaxhighlight lang="pascal" inline>{$assertions on}</syntaxhighlight> (or <syntaxhighlight lang="pascal" inline>{$C+}</syntaxhighlight> for short) or specifying the <syntaxhighlight lang="bash" inline>‑Sa</syntaxhighlight> command-line switch, appropriate code for assertions is inserted. | + | By specifying the [[local compiler directives|local compiler directive]] [[$Assertions|<syntaxhighlight lang="pascal" inline>{$assertions on}</syntaxhighlight>]] (or <syntaxhighlight lang="pascal" inline>{$C+}</syntaxhighlight> for short) or specifying the <syntaxhighlight lang="bash" inline>‑Sa</syntaxhighlight> command-line switch, appropriate code for assertions is inserted. |
== behavior == | == behavior == | ||
Line 24: | Line 24: | ||
== application == | == application == | ||
Assertions are a straightforward concept ensuring certain statements hold true. | Assertions are a straightforward concept ensuring certain statements hold true. | ||
− | However, they do not guarantee your program is indeed correct. | + | 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. | + | Assertions are frequently used ''during development''. |
For example, in the following [[Operator overloading|operator overload]] the assertion ensures certain properties about the <syntaxhighlight lang="pascal" inline>‑</syntaxhighlight> operation: | For example, in the following [[Operator overloading|operator overload]] the assertion ensures certain properties about the <syntaxhighlight lang="pascal" inline>‑</syntaxhighlight> operation: | ||
<syntaxhighlight lang="pascal"> | <syntaxhighlight lang="pascal"> | ||
Line 51: | Line 52: | ||
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | Otherwise, for Delphi-compatibility there would be no error if no case matches. | |
− | + | Using an <syntaxhighlight lang="pascal" inline>assert</syntaxhighlight> statement provides the flexibility to include or omit it from the generated code, though. | |
− | |||
Assertions are ''not'' used to verify that the compiler works: | Assertions are ''not'' used to verify that the compiler works: | ||
Line 62: | Line 62: | ||
… | … | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | Assertions are also not used for circumstances more specialized means are available for. | ||
+ | That means, assertions are not supposed to replace | ||
+ | * [[$rangeChecks|<syntaxhighlight lang="pascal" inline>{$rangeChecks}</syntaxhighlight>]] | ||
+ | * [[$objectChecks|<syntaxhighlight lang="pascal" inline>{$objectChecks}</syntaxhighlight>]] | ||
+ | * [[$overflowChecks|<syntaxhighlight lang="pascal" inline>{$overflowChecks}</syntaxhighlight>]] | ||
+ | among other kinds of checks. | ||
+ | |||
+ | The paper [[The Power of 10#Use a minimum of two runtime assertions per function|the power of 10]] suggest two assertions minimum per function. | ||
+ | See also | ||
+ | * [[The Power of Proper Planning and Practices]]. | ||
+ | * [[Defensive programming techniques#How to use meaningful Assertions|Defensive programming techniques § “How to use meaningful Assertions”]] | ||
== see also == | == see also == | ||
* Article: [https://en.wikipedia.org/wiki/Assertion_(software_development) Assertion (software development)] in the English Wikipedia | * Article: [https://en.wikipedia.org/wiki/Assertion_(software_development) Assertion (software development)] in the English Wikipedia | ||
+ | |||
+ | [[Category: Pascal]] |
Latest revision as of 22:42, 4 March 2022
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.
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
- The Power of Proper Planning and Practices.
- Defensive programming techniques § “How to use meaningful Assertions”
see also
- Article: Assertion (software development) in the English Wikipedia