ASSERT - Maple Programming Help

Home : Support : Online Help : Programming : Debugging : ASSERT

ASSERT

check assertions

 Calling Sequence ASSERT(cond, string) ASSERT(cond)

Parameters

 cond - expression evaluating to true or false string - string

Description

 • ASSERT is used to guarantee pre- and post-conditions while a Maple procedure is executing.
 • If the first argument evaluates to false, an error is generated and the second argument is printed in order to identify the nature of the failure. Such an error cannot be trapped via try-catch.
 • If the first argument evaluates to true, ASSERT returns NULL.
 • The ASSERT mechanism can be turned on and off with kernelopts. If turned off, the overhead of processing an ASSERT is negligible.

 • The ASSERT command is thread-safe as of Maple 15.

Examples

Query the current state.

 > $\mathrm{kernelopts}\left(\mathrm{assertlevel}\right)$
 ${0}$ (1)

Turn assertions on (kernelopts returns its previous value).

 > $\mathrm{kernelopts}\left(\mathrm{assertlevel}=1\right)$
 ${0}$ (2)

The following results in assertion failures.

 > $\mathrm{ASSERT}\left(\mathrm{type}\left(x,\mathrm{integer}\right),"x must be an integer"\right)$
 Error, assertion failed, x must be an integer
 > $a≔1:$$\mathrm{ASSERT}\left(5
 Error, assertion failed, a is too small: 1

This assertion check will succeed.

 > $\mathrm{ASSERT}\left(\mathrm{true},"This is ok"\right)$

Assertion failures are not trappable.

 > $\mathbf{try}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{ASSERT}\left(\mathrm{false}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end try}$

Turn assertions off (the previous value is returned again).

 > $\mathrm{kernelopts}\left(\mathrm{assertlevel}=0\right)$
 ${1}$ (3)

Now assertion failures are ignored.

 > $\mathrm{ASSERT}\left(\mathrm{type}\left(x,\mathrm{integer}\right),"x must be an integer"\right)$