function testLoadUVVisDataForRobustness(varargin) % TESTLOADUVVISDATAFORROBUSTNESS Check user function reading UV/Vis data % files for robustness. % % Usage % testForRobustness % testForRobustness(nameOfFunctionUnderTest) % (c) 2014, Till Biskup % 2014-01-26 nameOfFunctionUnderTest = 'loadUVVisData'; % Define function under test functionUnderTest = str2func(nameOfFunctionUnderTest); % Define structure for tests tests = struct(... 'title','',... 'filename','' ... ); % Empty structure tests(1) = []; % Define tests, aka fill test structure with content tests(end+1) = struct(... 'title','Existing and properly formatted UV/Vis data file',... 'filename','uvvis.txt' ... ); tests(end+1) = struct(... 'title','Existing proper file, but no file extension given',... 'filename','uvvis' ... ); tests(end+1) = struct(... 'title','Existing file with bare ASCII data',... 'filename','background.txt' ... ); tests(end+1) = struct(... 'title','Existing file in different ASCII format',... 'filename',[mfilename '.m'] ... ); tests(end+1) = struct(... 'title','Existing file in some binary format',... 'filename','binary.dat' ... ); tests(end+1) = struct(... 'title','Non-existing file (random filename)',... 'filename',generateRandomFilename ... ); results = logical(length(tests)); warnings = logical(length(tests)); % Iterate over all tests for test = 1:length(tests) [results(test),warnings(test)] = testFilenameWithNumberAndTitle(... tests(test).filename,... test,... tests(test).title ... ); end displayTestStatistics(tests,results,warnings); function [success,warnings] = ... testFilenameWithNumberAndTitle(filename,testNumber,testTitle) % TESTFILENAMEWITHNUMBERANDTITLE Test function with given filename. % % Usage % [success,warnings] = testFilename(filename,testNumber,testTitle) success = false; warnings = false; displayTestNumberAndTitle(testNumber,testTitle); % Test function call try data = functionUnderTest(filename); catch exception fprintf(' %s\n %s\n %s\n',... sprintf('%s "%s", line %i.',... 'ATTENTION: An exception occurred in',... exception.stack(1).name,... exception.stack(1).line... ),... 'Exception message:',... exception.message); disp('FAILED.') return; end % Check result if isempty(data) disp(['Problem reading file "' filename '": no data.']); warnings = true; elseif ~isnumeric(data) disp(['Problem reading file "' filename '": no numeric data.']); warnings = true; end disp('PASSED.'); success = true; end end function filename = generateRandomFilename % GENERATERANDOMFILENAME Filename with randomly chosen length and (allowed) % characters % Set limits for filename length filenameMinLength = 3; filenameMaxLength = 20; % Only alphanumeric characters and "-" and "_" allowedCharacters = char([65:90,97:122,48:57,45,95]); % Generate random filename using randi for integer pseudorandom numbers filename = allowedCharacters(... randi(length(allowedCharacters),... 1,randi([filenameMinLength filenameMaxLength],1))); end function displayTestNumberAndTitle(testNumber,testTitle) % DISPLAYTESTNUMBERANDTITLE Display message on Matlab command line % with number and title of current test. % % Usage % displayTestNumberAndTitle(testNumber,testTitle) fprintf('\n%s\n',char(ones(1,60)*45)); fprintf('Test %i: %s\n',testNumber,testTitle); end function displayTestStatistics(tests,results,warnings) % DISPLAYTESTSTATISTICS Display summary and statistics of tests run % % Usage % displayTestStatistics(tests,results,warnings) failedTests = find(~results); fprintf('\n%s\n',char(ones(1,60)*61)); fprintf('%s\n\n','Summary and statistics of tests run:'); fprintf(' Passed tests: %i\n',numel(find(results))); fprintf(' warnings: %i\n',numel(find(warnings))); fprintf(' Failed tests: %i\n',numel(failedTests)); % If some tests failed, display number and title if ~all(results) fprintf('\n %s\n','FAILED tests (no and title):'); for failedTest = 1:length(failedTests) fprintf(' %i %s\n',... failedTests(failedTest),... tests(failedTests(failedTest)).title); end end fprintf('\n%s\n\n',char(ones(1,60)*61)); end