assert(x)
r = assert(x)
[r, msg] = assert(x)
assert(x, err_msg)
r = assert(x, err_msg)
[r, msg] = assert(x, err_msg)
| Parameter | Description |
|---|---|
| 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). |
| Parameter | Description |
|---|---|
| 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. |
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 optional err_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.
try
assert(4 == 3, _('error for comparison.'))
catch ME
disp(['Error: ' ME.message])
end
assert(5 > 3);
disp('Assertion passed: 5 is greater than 3')
[r, msg] = assert(false, 'This condition is false');
if ~r
disp(['Assertion failed: ' msg])
end
x = 10;
assert(x > 0) % Will pass
assert(x < 0) % Will fail with default message
| Version | Description |
|---|---|
| 1.0.0 | initial version |