-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit.m
77 lines (61 loc) · 2.34 KB
/
submit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function [in, expected, actual] = submit(function_name, epsilon)
% Perform a test to see if the given function generates the right outputs for
% the given input. Inputs and outputs are predefined in a .mat file that is
% located in the same folder and has the same name (except the extension) as the
% function being checked.
%
% NOTE: This function expects a path to a file - relative or absolute.
% NOTE: The optional argument epsilon (zero by default) defines the allowed
% tolerance for the difference between the generated result and the expected
% result.
% One optional out of two arguments
narginchk(1,2);
% The default value for the optional argument
if nargin < 2
epsilon = 0;
endif
[fpath, fname, fext] = fileparts(function_name);
assert(isempty(fext) || fext == '.m', 'The supplied file should have a .m extension or no extension.)')
% Go to the dir where the funciton is
old_dir = pwd(); % save the path, so we can return to it in the end
cd(fpath)
% Files validation
results_file = sprintf('%s.result.mat', fname);
assert(2 == exist(fname), 'Cannot find the supplied file.')
assert(2 == exist(results_file),
'A matching .result.mat file was expected in the same folder where the function file is located.')
% Read the info from the result spec
res = load(results_file);
% Read function inputs
inputs = res.inputs;
% Read expected function outputs
exp_outputs = res.outputs;
% Generate function outputs for comparison
num_outputs = numel(exp_outputs);
candidate_outputs = cell(1, num_outputs);
[candidate_outputs{:}] = feval(fname, inputs{:});
assert(num_outputs == numel(candidate_outputs),
sprintf('Function returned %d outputs, expected %d.', num_outputs, numel(candidate_outputs)))
is_correct_num_outputs = 3 == nargout;
% Compare the outputs
for ii = 1 : num_outputs
missmatches = abs(candidate_outputs{ii} - exp_outputs{ii}) > epsilon;
s = sum(missmatches(:));
if (0 == s)
fprintf('Success!\n');
else
fprintf('Failed! The submitted function did not generate a correct result.\n');
if ~is_correct_num_outputs
fprintf('Call the script like this to get the epected and actual outputs:\n');
fprintf('[input, expected, actual] = submit(file_name);\n');
endif
endif
endfor
if is_correct_num_outputs
expected = exp_outputs;
actual = candidate_outputs;
in = inputs;
endif
% Return to original dir
cd(old_dir)
endfunction