Code covered by the BSD License  

Highlights from
isValidSymbol

image thumbnail

isValidSymbol

by

 

Check for valid variable, field or function name - fast C-Mex

uTest_isValidSymbol(doSpeed)
function uTest_isValidSymbol(doSpeed)
% Automatic test: isValidSymbol
% This is a routine for automatic testing. It is not needed for processing and
% can be deleted or moved to a folder, where it does not bother.
%
% uTest_isValidSymbol(doSpeed)
% INPUT:
%   doSpeed: Optional logical flag to trigger time consuming speed tests.
%            Default: TRUE. If no speed test is defined, this is ignored.
% OUTPUT:
%   On failure the test stops with an error.
%
% NOTE: ISVARNAME of Matlab 6.5 does not recognize invalid CHAR(>127)!
%
% Tested: Matlab 6.5, 7.7, 7.8, WinXP, 32 bit
% Author: Jan Simon, Heidelberg, (C) 2010 matlab.THISYEAR(a)nMINUSsimon.de

% $JRev: R0h V:031 Sum:IJsV6H5HbDoL Date:22-Sep-2010 02:30:25 $
% $License: NOT_RELEASED $
% $File: Tools\UnitTests_\uTest_isValidSymbol.m $

% Initialize: ==================================================================
% Global Interface: ------------------------------------------------------------
FuncName = mfilename;

% Initial values: --------------------------------------------------------------
if nargin == 0
   doSpeed = true;
end

% Program Interface: -----------------------------------------------------------
% User Interface: --------------------------------------------------------------
% Do the work: =================================================================
disp(['=== Test isValidSymbol:  ' datestr(now, 0)]);
disp(['Version: ', which('isValidSymbol')]);
pause(0.001);

badName = {[char(248), 'sophagus_badchar'], '123_leadnumber', ...
   'has space', ' leadtrailspace ', 'has,comma', '$leadextra', ...
   '_', '_9', '_A', 'Has.Dot', ...
   'Size64_123456789012345678901234567890123456789012345678901234567', ...
   '', '123_leadnum ber2'};
goodName = {'a', 'a9', 'a_', 'asd', ...
   'sophagus_badchar', 'leadnumber', 'hasspace', ...
   'leadtrailspace', 'hascomma', 'leadextra', 'EmptyName_7', ...
   'EmptyName_8', 'A', 'HasDot', ...
   'Size63_12345678901234567890123456789012345678901234567890123456', ...
   'EmptyName_12', 'leadnumber2'};

disp('== Known answer tests, single strings:');
for i = 1:length(badName)
   if isValidSymbol(badName{i})
      error([FuncName, ': isValidSymbol failed on: ', badName{i}]);
   else
      disp(['  OK: [', badName{i}, '] is bad']);
   end
end
for i = 1:length(goodName)
   if isValidSymbol(goodName{i})
      disp(['  OK: [', goodName{i}, '] is good']);
   else
      error([FuncName, ': isValidSymbol failed on: ', goodName{i}]);
   end
end

disp('== Known answer tests, cell strings:');
X = isValidSymbol(badName);
if any(X)
   error([FuncName, ': isValidSymbol failed on list of bad names']);
else
   disp('  OK: List of bad names refused completely');
end
X = isValidSymbol(goodName);
if all(X)
   disp('  OK: List of good names accepted completely');
else
   error([FuncName, ': isValidSymbol failed on list of good names']);
end

% Speed: -----------------------------------------------------------------------
disp('== Speed:');
Name  = cat(2, goodName, badName);
nName = length(Name);
if doSpeed
   iLoop     = 0;
   startTime = cputime;
   while cputime - startTime < 5.0
      for iName = 1:nName
         v     = isvarname(Name{iName}); %#ok<NASGU>
         clear('v');
      end
      iLoop = iLoop + 1;
   end
   nDigit = max(1, floor(log10(max(1, iLoop))) - 1);
   nLoops = max(2, round(iLoop / 10 ^ nDigit) * 10 ^ nDigit);
   fprintf('%d loops on this machine, %d strings:\n', nLoops, nName);
else
   disp('  Reduced speed measurement - displayed times are meaningless!');
   nLoops = 2;
end

tic;
for iLoop = 1:nLoops
   v = true(1, nName);
   for iName = 1:nName
      v(iName) = isvarname(Name{iName});
   end
   clear('v');
end
M_time = toc + eps;

tic;
for iLoop = 1:nLoops
   v = true(1, nName);
   for iName = 1:nName
      v(iName) = isValidSymbol(Name{iName});
   end
   clear('v');
end
Mex_time = toc + eps;

tic;
for iLoop = 1:nLoops
   v = isValidSymbol(Name); %#ok<NASGU>
   clear('v');
end
MexCell_time = toc + eps;

fprintf([ ...
   '  ISVARNAME     (loop):        %5.2f sec\n', ...
   '  isValidSymbol (loop):        %5.2f sec  ==>  %5.1f%% of ISVARNAME\n', ...
   '  isValidSymbol (cell string): %5.2f sec  ==>  %5.1f%% of ISVARNAME\n'], ...
   M_time, Mex_time, 100 * Mex_time / M_time, ...
   MexCell_time, 100 * MexCell_time / M_time);

disp([char(10), '== isValidSymbol seem to work well.']);

return;

Contact us