Code covered by the BSD License  

Highlights from
Assertion function

3.75

3.8 | 5 ratings Rate this file 3 Downloads (last 30 days) File Size: 1.56 KB File ID: #10366

Assertion function

by Malcolm Wood

 

16 Mar 2006 (Updated 06 Jul 2010)

A function similar to "assert" in C to assist in bug detection

| Watch this File

File Information
Description

Widely used in languages such as C and (now) Java, assertions are a very useful way of detecting bugs as early as possible in development.

Simply assert in the code that you believe something to be true:
  ASSERT(ischar(x) && ~isempty(x),'x is a non-empty string')
If, at runtime, x is not a non-empty string, the ASSERT function throws an error. Using the assertion, rather than waiting for something else to go wrong, guarantees that the problem doesn't go unnoticed, and gives you a meaningful error message.

It also serves as a kind of documentation: when you come back to this code in future to change the way that variable "x" is handled, you can be sure that you don't need to consider the case where "x" is empty or is not a string.

There are two reasons to use ASSERT rather than just using "error" itself.

Firstly, assertions are concise. The above line takes up less space (and arguably is more readable) than:

if ~ischar(x) || isempty(x)
  error('x must be a non-empty string');
end

Secondly, when the assertion fails this function shows the message and the current callstack in the MATLAB command window even if the error is caught. By simply using "error", you run the risk that the problem can be hidden by over-zealous error handling code.

Note: The ASSERT function should not be thought of as run-time error detection. You should only assert things are true unless your code contains bugs. An obvious example of when *not* to use an assertion is:
  f = fopen('myfile.txt')
  ASSERT(f~=-1,'f is a valid file handle'); % do not do this!
This is a valid error condition, and "error" should be used.

Another note: Since R2007a, MATLAB has its own function called "assert", fairly similar to this one. Errors thrown by it aren't automatically shown in the command window, so it could be used for normal run-time error handling, which makes its purpose slightly different from the one supplied here. Take your pick.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
assert.m

MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
17 Mar 2007 Roger Krill

Is there any reason why the function name is uppercase? This is non-standard. Also, is there a way to show the test condition itself as part of the error message?

31 Mar 2007 Naveen Mishra

Finally there is a builtin assert function in MATLAB R2007a.

13 Apr 2007 T Whipple

This doesn't seem to work in Matlab 2006b.

x=1;
assert('x==1')
??? Undefined function or method 'assert' for input arguments of type 'char'.

13 Apr 2007 T Whipple

Please disregard my earlier comment. It works fine.

07 Jul 2010 Petter

Already present in standard Matlab since many years ago.

23 Dec 2010 Matthias Geissbuehler

Perfect for backwards-compatibility!
@Peter: this is the case since 2007, but if you are "forced" to work with older versions, the file is perfect.

Please login to add a comment or rating.
Updates
17 Dec 2009

Review

30 Apr 2010

Added copyright line.

06 Jul 2010

Added copyright notice and discussion of MATLAB's "assert" function.

Tag Activity for this File
Tag Applied By Date/Time
test Malcolm Wood 22 Oct 2008 08:18:40
testing Malcolm Wood 22 Oct 2008 08:18:40
tests Malcolm Wood 22 Oct 2008 08:18:40
assert Malcolm Wood 22 Oct 2008 08:18:40
failure Malcolm Wood 22 Oct 2008 08:18:40
regression Malcolm Wood 22 Oct 2008 08:18:40

Contact us at files@mathworks.com