Previous: ArcTan function To the Table of Content Next: Assign procedure

- 4.62.2.5 -
Standard Units
System Unit
System Unit Procedures and Functions

Assert procedure

Targets: MS-DOS, OS/2, Win32


System Unit

Procedure Assert tests whether a Boolean expression is true.

Declaration:
  procedure Assert(expr: Boolean);
Remarks:
Assert takes a Boolean expression (expr) as parameter. If the Boolean test fails, Assert raises an Assertion Failed run-time error.

The Assert procedure is typically used to identify logic errors during program development, by implementing the expr argument to evaluate to FALSE only when the program is operating incorrectly. After debugging is complete, assertion checking can be turned off without modifying the source file by defining the $C compiler directive.

Assert prints a diagnostic message when expr evaluates to FALSE and terminates program execution. No action is taken if expr is TRUE. The diagnostic message includes the name of the source file and line number where the assertion failed.

Example:
uses Strings;  (* Tests a string to see if it is nil, *)
                (* empty, or longer than 2 characters  *)
procedure AnalyzeString (s: PChar);
begin
  Assert(s <> nil);       // Cannot be nil
  Assert(s[0] <> #0);    // Cannot be empty
  Assert(StrLen(s) > 2);  // Length must exceed 2
end;
 
var
  test1: PChar := 'abc';
  test2: PChar := nil;
  test3: PChar := '';
begin
  Writeln('Analyzing string ', test1);
  AnalyzeString(test1);
  Writeln('Analyzing string ', test2);
  AnalyzeString(test2);
  Writeln('Analyzing string ', test3);
  AnalyzeString(test3);
end.



Previous: ArcTan function To the Table of Content Next: Assign procedure
ArcTan function Table of Content Assign procedure

- 4.62.2.5 -