2.66667

2.7 | 3 ratings Rate this file 131 downloads (last 30 days) File Size: 2.52 KB File ID: #10002

assert.m

by Gerald Dalley

 

15 Feb 2006 (Updated 07 Nov 2006)

No BSD License  

Assert function that expands like the C macro assert

Download Now | Watch this File

File Information
Description

%assert(testCondition)
% Evaluates the test condition in the caller's context. If the result is
% false, an error is thrown indicating that the test failed.
% TESTCONDITION should be a string.
%
%assert(truthValue)
%assert(truthValue,errMsg)
% If TRUTHVALUE is zero, an error is thrown. The text of the error
% contains ERRMSG if supplied. This version of ASSERT was made to
% promote compatibility with Kevin Murphy's Bayes Net Toolbox (i.e. he
% uses this syntax). TRUTHVALUE must be a logical or a number.
%
%EXAMPLES:
% assert('1==2') ==> assertion error generated
% assert(1==2) ==> assertion error generated (BNT syntax)
% assert 1==2; ==> assertion error generated
% x=1; assert('x==1') ==> no error
% x=1; assert x==1; ==> no error
% assert('{a==3') ==> error generated by Matlab (bad assert code)

Acknowledgements
This submission has inspired the following:
Assertion function
MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
17 Feb 2006 Duane Hanselman

I am not familiar with C so this function seems like a lot of effort when none is needed. Why not use
if ~(place some boolean test here)
error('ASSERT FAILED: name the test failed here')
end
Why not do MATLAB in MATLAB? Why post copyrighted code? If I modify your code to what I have above, I am required by you to include your copyright notice and permission. Why?

17 Mar 2007 Roger Krill

Duane Hanselman needs to expand his mind.

In the code, I noticed an inconsistency in the error messages. Please fix this.

Also, is there a way to show the testCondition condition/string itself as part of the error message?

31 Mar 2007 Naveen Mishra

Finally there is a builtin assert function in MATLAB R2007a.

25 Apr 2008 Ben FrantzDale

Just what I need for backward compatibility.

As for Duane's question: Assert in C/C++ is also wrapped in #ifdefs so it isn't used in production, only in debugging. This is good for range checking, for example. It is immensely useful for writing correct code by helping you track down bugs right as they happen. Because it is used so frequently, a function is justified. Otherwise you wind up with code full of if ~condition, error, end.

Please login to add a comment or rating.
Updates

quick bug fix relative to the last update (which added support for Kevin Murphy's syntax)

21 Feb 2006

Removed boilerplate copyright.

07 Nov 2006

quick bug fix relative to the last update (which added support for Kevin Murphy's syntax)

Tag Activity for this File
Tag Applied By Date/Time
regression tests test assert assertion Gerald Dalley 22 Oct 2008 08:15:53
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com