forked from aleixpinardell/tudat-matlab
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tudat.m
237 lines (212 loc) · 8.22 KB
/
tudat.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
classdef tudat
properties (Constant, Hidden)
rootdir = fileparts(mfilename('fullpath'))
sourcedir = fullfile(tudat.rootdir,'MatlabInterface')
testsdir = fullfile(tudat.rootdir,'UnitTests')
settingsfile = fullfile(tudat.rootdir,'settings.mat')
PATHKey = 'PATH'
commandPrefixKey = 'commandPrefix'
bundlePathKey = 'bundlePath'
binaryPathKey = 'binaryPath'
testsSourcesDirectoryPathKey = 'testsSourcesDirectoryPath'
testsBinariesDirectoryPathKey = 'testsBinariesDirectoryPath'
defaultPATH = '';
defaultCommandPrefix = '';
defaultBundlePath = fileparts(tudat.rootdir);
defaultInBundleBinaryPath = fullfile('tudat','bin','json_interface')
defaultInBundleTestsSourcesPath = fullfile('tudat','Tudat','JsonInterface','UnitTests')
defaultInBundleTestsBinariesPath = fullfile('tudat','bin','unit_tests')
testsBinariesPrefix = 'test_JsonInterface'
defaultBinaryPath = fullfile(tudat.defaultBundlePath,tudat.defaultInBundleBinaryPath)
defaultTestsSourcesPath = fullfile(tudat.defaultBundlePath,tudat.defaultInBundleTestsSourcesPath)
defaultTestsBinariesPath = fullfile(tudat.defaultBundlePath,tudat.defaultInBundleTestsBinariesPath)
end
methods (Static)
function load(forceReload)
if nargin < 1
forceReload = false;
end
dirs = horzcat(tudat.sourcedir,getNonPackageDirectories(tudat.sourcedir,true));
loadedpaths = regexp(path,pathsep,'split');
for i = 1:length(dirs)
loaded = any(strcmp(dirs{i},loadedpaths));
if ~loaded || forceReload
addpath(dirs{i});
end
end
end
function find(bundlePath)
tudat.bundle(bundlePath);
tudat.binary(fullfile(bundlePath,tudat.defaultInBundleBinaryPath));
tudat.testsSourcesDirectory(fullfile(bundlePath,tudat.defaultInBundleTestsSourcesPath));
tudat.testsBinariesDirectory(fullfile(bundlePath,tudat.defaultInBundleTestsBinariesPath));
end
function test(varargin)
t0 = tic;
clc;
n = length(varargin);
if n == 0 % run all tests
testFiles = dir(fullfile(tudat.testsdir,'*.m'));
testNames = {testFiles.name};
else % run specified tests
testNames = varargin;
end
n = length(testNames);
title = sprintf('Running %i test',n);
if n > 1
title = [title 's'];
end
filenamewidth = 0;
for i = 1:n
[~,filename,~] = fileparts(testNames{i});
testNames{i} = filename;
filenamewidth = max(filenamewidth,length(filename) - 8);
end
fprintf([title '\n']);
separator = repmat('=',1,length(title));
fprintf([separator '\n']);
passed = {};
addpath(tudat.testsdir);
testOutputs = cell(size(testNames));
for i = 1:n
testName = testNames{i};
fprintf(sprintf('Test %%%ii/%%i %%-%is ',length(sprintf('%i',n)),filenamewidth),i,n,...
strrep(testName,'unitTest',''));
try
tic;
evalc(sprintf('[failures,testOutputs{i}] = %s',testName));
if failures == 0
result = sprintf('%-24s','PASSED');
passed{end+1} = testName;
elseif failures == -1
result = sprintf('<strong>%-24s</strong>','*** TUDAT ERROR');
else
result = sprintf('<strong>%-24s</strong>',sprintf('*** FAILED (%i errors)',failures));
end
catch
result = sprintf('<strong>%-24s</strong>','*** MATLAB ERROR');
end
fprintf('%s (%.3f s)\n',result,toc);
end
fprintf([separator '\n']);
p = length(passed);
fprintf('Total elapsed time: %g s.\n\n%i of %i tests (%g%%) passed.\n',toc(t0),p,n,p/n*100);
f = n - p;
if f > 0
if f == 1
ts = '';
else
ts = 's';
end
fprintf('%i test%s failed:\n',f,ts);
for i = 1:n
testName = testNames{i};
if ~any(strcmp(testName,passed))
testOutput = testOutputs{i};
issueURL = test.getIssueURL(strrep(testName,'unitTest',tudat.testsBinariesPrefix),testOutput);
fprintf(' * <a href="matlab: open(which(''%s.m''))">%s.m</a>',testName,testName);
fprintf(' (<a href="matlab: web(''%s'',''-browser'')">open issue</a>)',issueURL);
fprintf('\n');
end
end
end
fprintf('\n');
end
end
methods (Static, Hidden)
function s = settings
s = load(tudat.settingsfile);
end
function p = PATH(p)
if nargin == 0 % get
try
p = tudat.settings.(tudat.PATHKey);
catch
p = tudat.defaultPATH;
end
else % set
updateSetting(tudat.PATHKey,p);
end
end
function prefix = commandPrefix(prefix)
if nargin == 0 % get
try
prefix = tudat.settings.(tudat.commandPrefixKey);
catch
prefix = tudat.defaultCommandPrefix;
end
else % set
updateSetting(tudat.commandPrefixKey,prefix);
end
end
function path = bundle(newPath)
if nargin == 0 % get
try
path = tudat.settings.(tudat.bundlePathKey);
catch
path = tudat.defaultBundlePath;
end
else % set
updateSetting(tudat.bundlePathKey,newPath);
end
end
function path = binary(newPath)
if nargin == 0 % get
try
path = tudat.settings.(tudat.binaryPathKey);
catch
path = tudat.defaultBinaryPath;
end
else % set
updateSetting(tudat.binaryPathKey,newPath);
end
end
function path = testsSourcesDirectory(newPath)
if nargin == 0 % get
try
path = tudat.settings.(tudat.testsSourcesDirectoryPathKey);
catch
path = tudat.defaultTestsSourcesPath;
end
else % set
updateSetting(tudat.testsSourcesDirectoryPathKey,newPath);
end
end
function path = testsBinariesDirectory(newValue)
if nargin == 0 % get
try
path = tudat.settings.(tudat.testsBinariesDirectoryPathKey);
catch
path = tudat.defaultTestsBinariesPath;
end
else % set
updateSetting(tudat.testsBinariesDirectoryPathKey,newValue);
end
end
end
end
function updateSetting(name,value)
eval(sprintf('%s = ''%s'';',name,value));
if exist(tudat.settingsfile,'file') == 2
save(tudat.settingsfile,name,'-append');
else
save(tudat.settingsfile,name);
end
end
function directories = getNonPackageDirectories(directory,recursive)
if nargin < 2
recursive = false;
end
files = dir(directory);
dirs = files([files.isdir]);
dirnames = {dirs.name};
directories = {};
for i = 1:length(dirnames)
if ~any(strcmp(dirnames{i}(1),{'+','.'}))
directories{end+1} = fullfile(directory,dirnames{i});
if recursive
directories = horzcat(directories,getNonPackageDirectories(directories{end},true));
end
end
end
end