{$DEFINE STRINGASSERT} // switches on or off the StringAssert
{$IFDEF STRINGASSERT}
uses Asserts;
{$ENDIF}
var Par: integer;
Str: string[8];
Str2: string[10];
{$IFDEF STRINGASSERT}
procedure Assert_Init; // Initialises the device that will be used as output for assertion messages.
begin
// Uart1 will be used as assert output device
{$IFDEF P24}
PPS_Mapping (7, _INPUT, _U1RX); // RP7 is uart1 input pin 16
PPS_Mapping (6, _OUTPUT, _U1TX); // RP6 is uart1 output pin 15
{$ENDIF}
Uart1_Init(115200);
Delay_ms(250);
end;
procedure SendAssertMessage(var S: string[50]); // see chapter "Assert Output"
begin
Uart1_Write_text(S);
end;
{$ENDIF}
...
{$IFDEF STRINGASSERT}
Assert_Init; // only needed if the Uart is not already intialised
{$ENDIF}
...
Str2 := 'abc';
Par := -32000;
IntToStr(Par, Str);
Str := 'Task2 ' + Str + #13 +#10 +#13 +#10;
{$IFDEF STRINGASSERT}
Assert_Send_Message('Started');
AssertStr(Str, SizeOf(Str), 'Task2 AssertStr'); // do assert the string "Str" on overfilling, or
Assert(StrLen(Str) < SizeOf(Str), 'Task2 Assert'); // do the same with the generic assert routine
Assert_Send_Message('Done');
{$ENDIF}
Since assertion on string assignments probably only will take place in the development period of the program, and not in the
release version, a compiler directive defines whether string assertion will be done or not.