Home > atmlab > V7-4 > assert.m

assert

PURPOSE ^

assert(testCondition)

SYNOPSIS ^

function assert(varargin)

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)

by Gerald Dalley

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

assert.m

SOURCE CODE ^

0001 function assert(varargin)
0002 %assert(testCondition)
0003 %   Evaluates the test condition in the caller's context.  If the result is
0004 %   false, an error is thrown indicating that the test failed.
0005 %   TESTCONDITION should be a string.
0006 %
0007 %assert(truthValue)
0008 %assert(truthValue,errMsg)
0009 %   If TRUTHVALUE is zero, an error is thrown.  The text of the error
0010 %   contains ERRMSG if supplied.  This version of ASSERT was made to
0011 %   promote compatibility with Kevin Murphy's Bayes Net Toolbox (i.e. he
0012 %   uses this syntax).  TRUTHVALUE must be a logical or a number.
0013 %
0014 %EXAMPLES:
0015 %   assert('1==2')      ==> assertion error generated
0016 %   assert(1==2)        ==> assertion error generated (BNT syntax)
0017 %   assert 1==2;        ==> assertion error generated
0018 %   x=1; assert('x==1') ==> no error
0019 %   x=1; assert x==1;   ==> no error
0020 %   assert('{a==3')     ==> error generated by Matlab (bad assert code)
0021 %
0022 %by Gerald Dalley
0023 
0024 % Make ourselves compatible with Kevin Murphy's Bayes Net toolbox.
0025 if (ismember(nargin,[1 2]) && ...
0026     (islogical(varargin{1}) || isnumeric(varargin{1})))
0027   if nargin<2, msg = ''; else, msg = varargin{2}; end
0028   if ~varargin{1}
0029     error('assertion violated: %s', msg);
0030   end
0031   return
0032 end
0033 
0034 % Standard string-based usage
0035 for i=1:nargin
0036     if (~ischar(varargin{i}))
0037         error('Type HELP ASSERT for usage');
0038     end
0039 end
0040 
0041 testCondition = join(' ', varargin);
0042 if (~evalin('caller', testCondition))
0043     error(['ASSERT FAILED: ' testCondition]);
0044 end
0045 
0046 
0047 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0048 function s = join(d,varargin)
0049 % This JOIN function is an inlined version of Gerald Dalley's one posted at the
0050 % Matlab Central website.  It is placed here as a convenience to users that
0051 % have not downloaded it.
0052 
0053 if (isempty(varargin)), 
0054     s = '';
0055 else
0056     if (iscell(varargin{1}))
0057         s = join(d, varargin{1}{:});
0058     else
0059         s = varargin{1};
0060     end
0061     
0062     for ss = 2:length(varargin)
0063         s = [s d join(d, varargin{ss})];
0064     end
0065 end
0066 
0067 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0068 function test
0069 % Copy-and-paste the below code into the command window to test this
0070 % function.
0071 
0072 clear all;
0073 
0074 worked = 0; try, assert('1==2'); catch, worked = 1; end
0075 if (~worked), error('Test failed'); end
0076     
0077 worked = 1; try, x=1; assert('x==1'); catch, worked = 0; end
0078 if (~worked), error('Test failed'); end
0079     
0080 worked = 0; try, assert(1==2); catch, worked = 1; end
0081 if (~worked), error('Test failed'); end
0082     
0083 worked = 0; try, assert('{a==3'); catch, worked = 1; end
0084 if (~worked), error('Test failed'); end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005