Cody

Solution 125433

Submitted on 9 Aug 2012 by Óscar
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
%% a = 1; b = 2; c_correct = 3; assert(isequal(add_two_numbers(a,b),c_correct))

ans = /users/msssystem1 add_two_numbers.m +cody Shared add_two_numbers_testsuite.m Published function testSuiteResults = verifyCode( sourceFile, testFile ) %VERIFYCODE Evaluates and verifies MATLAB Code against provided test suite. % TESTSUITERESULTS = VERIFYCODE(SOURCEFILE, TESTFILE) evaluates SOURCEFILE against % TESTFILE and returns the results. % Copyright 1984-2012 The MathWorks, Inc. % Load the test file code = fileread(testFile); % Split it into cells code2 = regexprep(code,'\n%%','\nxxx-cellbreak-xxx%%'); cellList = regexp(code2,'xxx-cellbreak-xxx','split'); % Test each cell % Each testpoint structure % testPoint = struct(... % 'pass', false, ... % 'code', '', ... % 'output', ''); testSuite = struct([]); [containsIllegalFcnFlag, illegalFcnMessage] = containsIllegalFcn(sourceFile); for countVariable = 1:length(cellList) cellCode = cellList{countVariable}; cleanCellCode = cleanCode(cellCode); % Test is guilty until proven successful pass = false; % Run it try output = evaluateCode(cleanCellCode); pass = true; catch me output = ['Error: ' me.message]; end if containsIllegalFcnFlag pass = false; output = illegalFcnMessage; end cellOutput = cleanCode(output); testSuite(countVariable).code = cleanCellCode; testSuite(countVariable).output = cellOutput; testSuite(countVariable).pass = pass; end % Score it score = calculateSize(sourceFile); % Set up data structure to return testSuiteResults = struct(... 'pass', false, ... 'score', 0, ... 'testPoints', struct([])); % Populate the result struct % If any test point is failed, sets the overall pass status to false. testSuiteResults.pass = all([testSuite.pass]); % sets the score testSuiteResults.score = sprintf('%d',score); % Set test points testSuiteResults.testPoints = testSuite; end %========================================================================== function [tf,illegalFcnMessage] = containsIllegalFcn(file) %CONTAINSILLEGALFCN Checks if the code contains banned function. % [TF,ILLEGALFCNMESSAGE] = CONTAINSILLEGALFCN(FILE) checks if the % FILE contains eval function or not. fcnsUsedList = findFcns(file,'file'); illegalFcnList = getIllegalFcnList; tf = false; illegalFcnMessage = ''; for i = 1:length(illegalFcnList) % Are any of the illegal functions being used in the file? if ismember(illegalFcnList{i},fcnsUsedList) tf = true; illegalFcnMessage = sprintf('Error: You may not use the command %s in your code', ... upper(illegalFcnList{i})); return end end end %========================================================================== function fcnList = getIllegalFcnList %GETILLEGALFCNLIST Returns a list of functions banned from Cody % FCNLIST = GETILLEGALFCNLIST fcnList = { ... 'assignin', ... 'builtin', ... 'eval', ... 'evalc', ... 'evalin', ... 'inline'}; end %========================================================================== function funs = findFcns(sourceCode,option) %FINDFCNS Finds all the functions used. % FUNS = FINDFCNS(SOURCECODE) finds all the functions in the SOURCECODE. % % FUNS = FINDFCNS(SOURCECODE, OPTION) finds all the function in % SOURCECODE. OPTION can be 'string' or 'file'. if nargin < 2 option = 'string'; end % MTREE is an undocumented function. It will change in the future % versions of MATLAB. if strcmp(option, 'file') tree = mtree(sourceCode,'-file'); else tree = mtree(sourceCode); end if count(tree) == 1 && iskind(tree,'ERR') error(string( tree )); end calls = find(tree,'Kind',{'CALL','DCALL'}); funs = strings(path(calls,'Left'))'; funs = unique(funs); end %========================================================================== function cleanOutput = cleanCode(inputCode) %CLEANCODE Cleans code to avoid anomolies when displaying as HTML. % Normalize line endings to Unix-style. code = regexprep(inputCode,'\r\n?','\n'); newLine = sprintf('\n'); % Trim leading whitespace. code = regexprep(code,'^[ \t\n]*','\n'); % Trim trailing whitespace. code = regexprep(code,'[ \t\n]*(\n|$)','\n'); % Remove MATLAB-specific HTML markup. code = regexprep(code,'(.*?)','$1'); % Remove illegal ASCII character 8 % ASCII 8 was surrounding a warning in one result. I'm not sure why, % but it caused the XSLT processing to fail. code = regexprep(code,char(8),''); % Exactly one newline at the end of the file. code = regexprep(code,'\n*$',''); code(end+1) = newLine; % Truncate code if necessary charCountLimit = 10000; if length(code) > charCountLimit code = [code(1:charCountLimit) '...']; end cleanOutput = code; end %========================================================================== function output = evaluateCode( matlabCode ) %EVALUATECODE Evaluates MATLAB Code. output = evalc(matlabCode); end %========================================================================== function size = calculateSize( fileName ) %CALCULATESIZE Calculates the size of a MATLAB file for use in the game Cody % CALCULATESIZE(FILENAME) determines the number of nodes in the parse tree % associated with the file FILENAME. % % For more information, visit http://www.mathworks.com/matlabcentral/cody % Copyright 1984-2012 The MathWorks, Inc. % MTREE is an undocumented function. It will change in the future % versions of MATLAB. t = mtree(fileName,'-file'); size = length(t.nodesize); end

2   Pass
%% a = 17; b = 2; c_correct = 19; assert(isequal(add_two_numbers(a,b),c_correct))

ans = /users/msssystem1 add_two_numbers.m +cody Shared add_two_numbers_testsuite.m Published function testSuiteResults = verifyCode( sourceFile, testFile ) %VERIFYCODE Evaluates and verifies MATLAB Code against provided test suite. % TESTSUITERESULTS = VERIFYCODE(SOURCEFILE, TESTFILE) evaluates SOURCEFILE against % TESTFILE and returns the results. % Copyright 1984-2012 The MathWorks, Inc. % Load the test file code = fileread(testFile); % Split it into cells code2 = regexprep(code,'\n%%','\nxxx-cellbreak-xxx%%'); cellList = regexp(code2,'xxx-cellbreak-xxx','split'); % Test each cell % Each testpoint structure % testPoint = struct(... % 'pass', false, ... % 'code', '', ... % 'output', ''); testSuite = struct([]); [containsIllegalFcnFlag, illegalFcnMessage] = containsIllegalFcn(sourceFile); for countVariable = 1:length(cellList) cellCode = cellList{countVariable}; cleanCellCode = cleanCode(cellCode); % Test is guilty until proven successful pass = false; % Run it try output = evaluateCode(cleanCellCode); pass = true; catch me output = ['Error: ' me.message]; end if containsIllegalFcnFlag pass = false; output = illegalFcnMessage; end cellOutput = cleanCode(output); testSuite(countVariable).code = cleanCellCode; testSuite(countVariable).output = cellOutput; testSuite(countVariable).pass = pass; end % Score it score = calculateSize(sourceFile); % Set up data structure to return testSuiteResults = struct(... 'pass', false, ... 'score', 0, ... 'testPoints', struct([])); % Populate the result struct % If any test point is failed, sets the overall pass status to false. testSuiteResults.pass = all([testSuite.pass]); % sets the score testSuiteResults.score = sprintf('%d',score); % Set test points testSuiteResults.testPoints = testSuite; end %========================================================================== function [tf,illegalFcnMessage] = containsIllegalFcn(file) %CONTAINSILLEGALFCN Checks if the code contains banned function. % [TF,ILLEGALFCNMESSAGE] = CONTAINSILLEGALFCN(FILE) checks if the % FILE contains eval function or not. fcnsUsedList = findFcns(file,'file'); illegalFcnList = getIllegalFcnList; tf = false; illegalFcnMessage = ''; for i = 1:length(illegalFcnList) % Are any of the illegal functions being used in the file? if ismember(illegalFcnList{i},fcnsUsedList) tf = true; illegalFcnMessage = sprintf('Error: You may not use the command %s in your code', ... upper(illegalFcnList{i})); return end end end %========================================================================== function fcnList = getIllegalFcnList %GETILLEGALFCNLIST Returns a list of functions banned from Cody % FCNLIST = GETILLEGALFCNLIST fcnList = { ... 'assignin', ... 'builtin', ... 'eval', ... 'evalc', ... 'evalin', ... 'inline'}; end %========================================================================== function funs = findFcns(sourceCode,option) %FINDFCNS Finds all the functions used. % FUNS = FINDFCNS(SOURCECODE) finds all the functions in the SOURCECODE. % % FUNS = FINDFCNS(SOURCECODE, OPTION) finds all the function in % SOURCECODE. OPTION can be 'string' or 'file'. if nargin < 2 option = 'string'; end % MTREE is an undocumented function. It will change in the future % versions of MATLAB. if strcmp(option, 'file') tree = mtree(sourceCode,'-file'); else tree = mtree(sourceCode); end if count(tree) == 1 && iskind(tree,'ERR') error(string( tree )); end calls = find(tree,'Kind',{'CALL','DCALL'}); funs = strings(path(calls,'Left'))'; funs = unique(funs); end %========================================================================== function cleanOutput = cleanCode(inputCode) %CLEANCODE Cleans code to avoid anomolies when displaying as HTML. % Normalize line endings to Unix-style. code = regexprep(inputCode,'\r\n?','\n'); newLine = sprintf('\n'); % Trim leading whitespace. code = regexprep(code,'^[ \t\n]*','\n'); % Trim trailing whitespace. code = regexprep(code,'[ \t\n]*(\n|$)','\n'); % Remove MATLAB-specific HTML markup. code = regexprep(code,'(.*?)','$1'); % Remove illegal ASCII character 8 % ASCII 8 was surrounding a warning in one result. I'm not sure why, % but it caused the XSLT processing to fail. code = regexprep(code,char(8),''); % Exactly one newline at the end of the file. code = regexprep(code,'\n*$',''); code(end+1) = newLine; % Truncate code if necessary charCountLimit = 10000; if length(code) > charCountLimit code = [code(1:charCountLimit) '...']; end cleanOutput = code; end %========================================================================== function output = evaluateCode( matlabCode ) %EVALUATECODE Evaluates MATLAB Code. output = evalc(matlabCode); end %========================================================================== function size = calculateSize( fileName ) %CALCULATESIZE Calculates the size of a MATLAB file for use in the game Cody % CALCULATESIZE(FILENAME) determines the number of nodes in the parse tree % associated with the file FILENAME. % % For more information, visit http://www.mathworks.com/matlabcentral/cody % Copyright 1984-2012 The MathWorks, Inc. % MTREE is an undocumented function. It will change in the future % versions of MATLAB. t = mtree(fileName,'-file'); size = length(t.nodesize); end

3   Pass
%% a = -5; b = 2; c_correct = -3; assert(isequal(add_two_numbers(a,b),c_correct))

ans = /users/msssystem1 add_two_numbers.m +cody Shared add_two_numbers_testsuite.m Published function testSuiteResults = verifyCode( sourceFile, testFile ) %VERIFYCODE Evaluates and verifies MATLAB Code against provided test suite. % TESTSUITERESULTS = VERIFYCODE(SOURCEFILE, TESTFILE) evaluates SOURCEFILE against % TESTFILE and returns the results. % Copyright 1984-2012 The MathWorks, Inc. % Load the test file code = fileread(testFile); % Split it into cells code2 = regexprep(code,'\n%%','\nxxx-cellbreak-xxx%%'); cellList = regexp(code2,'xxx-cellbreak-xxx','split'); % Test each cell % Each testpoint structure % testPoint = struct(... % 'pass', false, ... % 'code', '', ... % 'output', ''); testSuite = struct([]); [containsIllegalFcnFlag, illegalFcnMessage] = containsIllegalFcn(sourceFile); for countVariable = 1:length(cellList) cellCode = cellList{countVariable}; cleanCellCode = cleanCode(cellCode); % Test is guilty until proven successful pass = false; % Run it try output = evaluateCode(cleanCellCode); pass = true; catch me output = ['Error: ' me.message]; end if containsIllegalFcnFlag pass = false; output = illegalFcnMessage; end cellOutput = cleanCode(output); testSuite(countVariable).code = cleanCellCode; testSuite(countVariable).output = cellOutput; testSuite(countVariable).pass = pass; end % Score it score = calculateSize(sourceFile); % Set up data structure to return testSuiteResults = struct(... 'pass', false, ... 'score', 0, ... 'testPoints', struct([])); % Populate the result struct % If any test point is failed, sets the overall pass status to false. testSuiteResults.pass = all([testSuite.pass]); % sets the score testSuiteResults.score = sprintf('%d',score); % Set test points testSuiteResults.testPoints = testSuite; end %========================================================================== function [tf,illegalFcnMessage] = containsIllegalFcn(file) %CONTAINSILLEGALFCN Checks if the code contains banned function. % [TF,ILLEGALFCNMESSAGE] = CONTAINSILLEGALFCN(FILE) checks if the % FILE contains eval function or not. fcnsUsedList = findFcns(file,'file'); illegalFcnList = getIllegalFcnList; tf = false; illegalFcnMessage = ''; for i = 1:length(illegalFcnList) % Are any of the illegal functions being used in the file? if ismember(illegalFcnList{i},fcnsUsedList) tf = true; illegalFcnMessage = sprintf('Error: You may not use the command %s in your code', ... upper(illegalFcnList{i})); return end end end %========================================================================== function fcnList = getIllegalFcnList %GETILLEGALFCNLIST Returns a list of functions banned from Cody % FCNLIST = GETILLEGALFCNLIST fcnList = { ... 'assignin', ... 'builtin', ... 'eval', ... 'evalc', ... 'evalin', ... 'inline'}; end %========================================================================== function funs = findFcns(sourceCode,option) %FINDFCNS Finds all the functions used. % FUNS = FINDFCNS(SOURCECODE) finds all the functions in the SOURCECODE. % % FUNS = FINDFCNS(SOURCECODE, OPTION) finds all the function in % SOURCECODE. OPTION can be 'string' or 'file'. if nargin < 2 option = 'string'; end % MTREE is an undocumented function. It will change in the future % versions of MATLAB. if strcmp(option, 'file') tree = mtree(sourceCode,'-file'); else tree = mtree(sourceCode); end if count(tree) == 1 && iskind(tree,'ERR') error(string( tree )); end calls = find(tree,'Kind',{'CALL','DCALL'}); funs = strings(path(calls,'Left'))'; funs = unique(funs); end %========================================================================== function cleanOutput = cleanCode(inputCode) %CLEANCODE Cleans code to avoid anomolies when displaying as HTML. % Normalize line endings to Unix-style. code = regexprep(inputCode,'\r\n?','\n'); newLine = sprintf('\n'); % Trim leading whitespace. code = regexprep(code,'^[ \t\n]*','\n'); % Trim trailing whitespace. code = regexprep(code,'[ \t\n]*(\n|$)','\n'); % Remove MATLAB-specific HTML markup. code = regexprep(code,'(.*?)','$1'); % Remove illegal ASCII character 8 % ASCII 8 was surrounding a warning in one result. I'm not sure why, % but it caused the XSLT processing to fail. code = regexprep(code,char(8),''); % Exactly one newline at the end of the file. code = regexprep(code,'\n*$',''); code(end+1) = newLine; % Truncate code if necessary charCountLimit = 10000; if length(code) > charCountLimit code = [code(1:charCountLimit) '...']; end cleanOutput = code; end %========================================================================== function output = evaluateCode( matlabCode ) %EVALUATECODE Evaluates MATLAB Code. output = evalc(matlabCode); end %========================================================================== function size = calculateSize( fileName ) %CALCULATESIZE Calculates the size of a MATLAB file for use in the game Cody % CALCULATESIZE(FILENAME) determines the number of nodes in the parse tree % associated with the file FILENAME. % % For more information, visit http://www.mathworks.com/matlabcentral/cody % Copyright 1984-2012 The MathWorks, Inc. % MTREE is an undocumented function. It will change in the future % versions of MATLAB. t = mtree(fileName,'-file'); size = length(t.nodesize); end