assert

Check that condition is true.

📝 Syntax

  • assert(x)

  • r = assert(x)

  • [r, msg] = assert(x)

  • assert(x, err_msg)

  • r = assert(x, err_msg)

  • [r, msg] = assert(x, err_msg)

📥 Input argument

  • x - a logical value to be tested for truthfulness.

  • err_msg - a string containing the custom error message to display in case of assertion failure (optional).

📤 Output argument

  • r - a logical value: true if the assertion passes, false otherwise.

  • msg - a string containing the error message. If x == true, then msg == ''. If x == false, then msg contains the assertion failure message.

📄 Description

assert raises an error if the input value is false.

This function also raises an error if the input is not a logical value, ensuring type safety.

When the optionalerr_msg parameter is provided, it will be used as the error message instead of the default message when the assertion fails.

This is the fundamental assertion function that forms the basis for testing conditions in programs and unit tests.

💡 Examples

Test assertion failure with custom error message:

Test successful assertion:

Using return values to handle assertion results:

Basic assertion without custom message:

🔗 See also

assert_istrue, assert_isfalse, assert_isequal, assert_checkerror.

🕔 History

Version
📄 Description

1.0.0

initial version

Last updated

Was this helpful?