-
Notifications
You must be signed in to change notification settings - Fork 1
/
spinwFunctionTests.m
161 lines (137 loc) · 5.43 KB
/
spinwFunctionTests.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
classdef spinwFunctionTests < matlab.unittest.TestCase
properties
generate_ref = false;
seed = 3142;
relToll = 0.025;
absToll = 1E-6;
refDir = fullfile('.', 'tutorialResults');
end
properties(Access = protected)
r1
end
properties (TestParameter)
testFun = arrayfun(@(x) fullfile(x.folder, x.name), ...
dir(fullfile(sw_rootdir, 'tutorials', 'publish', 'tutorial*.m')),...
'UniformOutput', false);
% Incase we need to debug one tutorial.
% testFun = {fullfile(sw_rootdir, 'tutorials', 'publish', 'tutorial8.m')};
end
methods (TestMethodSetup)
function prepareForRun(testCase)
close all
clc
testCase.r1 = rng;
rng(testCase.seed)
end
end
methods (TestMethodTeardown)
function clearnupRun(testCase)
rng(testCase.r1)
end
end
methods (TestClassSetup)
function checkDirs(testCase)
if ~exist(testCase.refDir,'dir')
testCase.generate_ref = true;
warning('Making references as none exist')
end
end
function turnOffWarnings(testCase)
warning('off','figure:JavaSupport');
end
end
methods (TestClassTeardown)
function closeAllFigs(testCase) %#ok<*MANU>
close all
clearvars
end
function turnOnWarnings(testCase)
warning('on','figure:JavaSupport');
end
end
methods (Test)
function testFunction(testCase, testFun)
outStat = 1;
str= fileread(which(testFun)); %read in text file to memory
urls = regexp(str, 'http(\S+)(\s*)$', 'match', 'lineAnchors'); %find urls
if ~isempty(urls)
for i = 1:length(urls)
url = strtok(urls{i},''')');
if ~contains(url,'goo.gl')
break
end
try
[~] = webread(strtok(url,''')'));
catch ME
switch ME.identifier
case 'MATLAB:webservices:HTTP404StatusCodeError'
warning('WARNING! Remote content not found for tutorial %s', testFun);
ident = 'MATLAB:webservices:HTTP404StatusCodeError';
case 'MATLAB:webservices:UnknownHost'
warning('WARNING! Can not find remote content. DNS problem?')
ident = 'MATLAB:webservices:UnknownHost';
case 'MATLAB:webservices:Timeout'
warning('WARNING! Can not connect to the internet.')
ident = 'MATLAB:webservices:Timeout';
otherwise
rethrow(ME)
end
import matlab.unittest.constraints.Matches
testCase.verifyMatches(ME.identifier, ident);
return
end
end
end
try
tic
run(testFun)
t = toc;
catch ME
outStat = 0;
sprintf('%s',ME.message)
end
% Fail if the test fails
testCase.verifyEqual(outStat, 1);
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.RelativeTolerance
import matlab.unittest.constraints.AbsoluteTolerance
theseBounds = RelativeTolerance(testCase.relToll) | AbsoluteTolerance(testCase.absToll);
% Verify Generated data is accurate
axes = findobj('type','axes');
axes = axes(arrayfun(@(x) isempty(x.Tag),axes));
for i = 1:length(axes)
data_s = extractData(axes(i));
temp = strtok(testFun(end:-1:1), filesep);
r_name = fullfile(testCase.refDir, sprintf('%s_ref%i.mat', temp(end:-1:3), i));
if testCase.generate_ref
data_r = data_s;
data_r = arrayfun(@(x) setfield(x, 'matVer', version), data_r);
data_r = arrayfun(@(x) setfield(x, 'time', t), data_r);
data_r = arrayfun(@(x) setfield(x, 'spinwVer', sw_version), data_r);
save(r_name, 'data_r')
else
load(r_name, 'data_r');
for j = 1:length(data_s)
testCase.verifyThat(data_s(j).data, IsEqualTo(data_r(j).data, 'Within', theseBounds))
end
clear data_r;
end
end
end
end
end
function dataOut = extractData(axisIN)
children = axisIN.Children;
for i = 1:length(children)
child = children(i);
dataOut(i).axisNo = i; %#ok<*AGROW>
switch get(child,'Type')
case 'line'
dataOut(i).data = [child.XData(:) child.YData(:)];
case 'surface'
dataOut(i).data = [child.XData(:) child.YData(:) child.ZData(:)];
otherwise
dataOut(i).data = [];
end
end
end