1function fail = testOutput(outdir, in_installer, fbcEnabled)
2
3%  Filename    :   TestOutput.m
4%  Description :
5%  $Source v $
6%
7%<!---------------------------------------------------------------------------
8% This file is part of SBMLToolbox.  Please visit http://sbml.org for more
9% information about SBML, and the latest version of SBMLToolbox.
10%
11% Copyright (C) 2013-2018 jointly by the following organizations:
12%     1. California Institute of Technology, Pasadena, CA, USA
13%     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
14%     3. University of Heidelberg, Heidelberg, Germany
15%
16% Copyright (C) 2009-2013 jointly by the following organizations:
17%     1. California Institute of Technology, Pasadena, CA, USA
18%     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
19%
20% Copyright (C) 2006-2008 by the California Institute of Technology,
21%     Pasadena, CA, USA
22%
23% Copyright (C) 2002-2005 jointly by the following organizations:
24%     1. California Institute of Technology, Pasadena, CA, USA
25%     2. Japan Science and Technology Agency, Japan
26%
27% This library is free software; you can redistribute it and/or modify it
28% under the terms of the GNU Lesser General Public License as published by
29% the Free Software Foundation.  A copy of the license agreement is provided
30% in the file named "LICENSE.txt" included with this software distribution.
31% and also available online as http://sbml.org/software/sbmltoolbox/license.html
32%----------------------------------------------------------------------- -->
33
34if (~isdir(outdir))
35mkdir (outdir);
36end;
37
38if exist('OCTAVE_VERSION')
39  ff = dir('test-data');
40  j = 1;
41  for i=1:length(ff)
42    if (ff(i).isdir == 0 && ~isempty(strfind(ff(i).name, '.xml')))
43      files(j) = ff(i);
44      j = j+1;
45    end;
46  end;
47else
48  files = dir(['test-data', filesep, '*.xml']);
49end;
50
51disp('Testing output model');
52
53fail = 0;
54test = 0;
55
56for i=1:length(files)
57  if isFileExpected(files(i).name) == 0
58    %donothing
59  %skip models that will cause read errors
60  elseif (strcmp(files(i).name, 'readerror.xml'))
61    % donothing
62  elseif (strcmp(files(i).name, 'fatal.xml'))
63    %do nothing
64  elseif (fbcEnabled == 0 && strcmp(files(i).name, 'fbc.xml'))
65    % do nothing
66  elseif (fbcEnabled == 0 && strcmp(files(i).name, 'fbcV2.xml'))
67    % do nothing
68  else
69    model = [];
70    disp(sprintf('Reading  %s', files(i).name));
71    model = TranslateSBML(['test-data', filesep, files(i).name]);
72    if (~isempty(model))
73        disp(sprintf('Printing  %s', files(i).name));
74
75      if (in_installer == 1)
76        OutputSBML(model, [outdir, filesep, files(i).name], in_installer);
77      else
78        OutputSBML(model, [outdir, filesep, files(i).name]);
79      end;
80      test = test + 1;
81      if (compareFiles(['test-data', filesep, files(i).name], [outdir, filesep, files(i).name]))
82        disp(sprintf('Output of %s failed', files(i).name));
83        fail = fail + 1;
84      end;
85    end;
86  end;
87end;
88
89if (fbcEnabled)
90    % test new arguments to Translate/Output
91    filename = ['test-data', filesep, 'fbcV2Labels.xml'];
92    outfile = [outdir, filesep, 'fbcV2Labels.xml'];
93    outfile1 = [outdir, filesep, 'fbcV2Labels-ids.xml'];
94    outfile2 = [outdir, filesep, 'fbcV2Labels-explicit.xml'];
95    model = TranslateSBML(filename);
96    if (~isempty(model))
97        disp(sprintf('Printing  %s', filename));
98        OutputSBML(model, outfile);
99    end;
100    test = test + 1;
101    if (compareFiles(filename, outfile))
102        disp(sprintf('Output of %s failed', outfile));
103        fail = fail + 1;
104    end;
105    model = TranslateSBML(filename, 0, 0, [1,0]);
106    if (~isempty(model))
107        disp(sprintf('Printing  %s', outfile1));
108        OutputSBML(model, outfile1, 0, 0, [1,0]);
109    end;
110    test = test + 1;
111    if (compareFiles(filename, outfile1))
112        disp(sprintf('Output of %s failed', outfile1));
113        fail = fail + 1;
114    end;
115    model = TranslateSBML(filename, 0, 0, [0,1]);
116    if (~isempty(model))
117        disp(sprintf('Printing  %s', outfile2));
118        OutputSBML(model, outfile2, 0, 0, [0,1]);
119    end;
120    test = test + 1;
121    if (compareFiles(filename, outfile2))
122        disp(sprintf('Output of %s failed', outfile2));
123        fail = fail + 1;
124    end;
125end; % fbc enabled
126
127if (isEnabled('qual'))
128    disp('Reading qual');
129    filename = ['test-data', filesep, 'qual.xml'];
130    outfile = [outdir, filesep, 'qual.xml'];
131
132    model = TranslateSBML(filename);
133    if (~isempty(model))
134        disp(sprintf('Printing  %s', filename));
135        OutputSBML(model, outfile);
136    end;
137    test = test + 1;
138    if (compareFiles(filename, outfile))
139        disp(sprintf('Output of %s failed', outfile));
140        fail = fail + 1;
141    end;
142
143end;
144
145if (isEnabled('groups'))
146    disp('Reading groups-example1');
147    filename = ['test-data', filesep, 'groups-example1.xml'];
148    outfile = [outdir, filesep, 'groups-example1.xml'];
149
150    model = TranslateSBML(filename);
151    if (~isempty(model))
152        disp(sprintf('Printing  %s', filename));
153        OutputSBML(model, outfile);
154    end;
155    test = test + 1;
156    if (compareFiles(filename, outfile))
157        disp(sprintf('Output of %s failed', outfile));
158        fail = fail + 1;
159    end;
160end;
161
162if (isEnabled('groups') && isEnabled('fbc'))
163    disp('Reading fbc_groups');
164    filename = ['test-data', filesep, 'fbc_groups.xml'];
165    outfile = [outdir, filesep, 'fbc_groups.xml'];
166
167    model = TranslateSBML(filename);
168    if (~isempty(model))
169        disp(sprintf('Printing  %s', filename));
170        OutputSBML(model, outfile);
171    end;
172    test = test + 1;
173    if (compareFiles(filename, outfile))
174        disp(sprintf('Output of %s failed', outfile));
175        fail = fail + 1;
176    end;
177end;
178
179if (isEnabled('qual') && isEnabled('fbc'))
180    disp('Reading fbc_qual');
181    filename = ['test-data', filesep, 'fbc_qual.xml'];
182    outfile = [outdir, filesep, 'fbc_qual.xml'];
183
184    model = TranslateSBML(filename);
185    if (~isempty(model))
186        disp(sprintf('Printing  %s', filename));
187        OutputSBML(model, outfile);
188    end;
189    test = test + 1;
190    if (compareFiles(filename, outfile))
191        disp(sprintf('Output of %s failed', outfile));
192        fail = fail + 1;
193    end;
194end;
195
196if (isEnabled('groups') && isEnabled('fbc') && isEnabled('qual'))
197    disp('Reading fbc_qual_groups');
198    filename = ['test-data', filesep, 'fbc_qual_groups.xml'];
199    outfile = [outdir, filesep, 'fbc_qual_groups.xml'];
200
201    model = TranslateSBML(filename);
202    if (~isempty(model))
203        disp(sprintf('Printing  %s', filename));
204        OutputSBML(model, outfile);
205    end;
206    test = test + 1;
207    if (compareFiles(filename, outfile))
208        disp(sprintf('Output of %s failed', outfile));
209        fail = fail + 1;
210    end;
211end;
212
213if (isEnabled('groups') && isEnabled('qual'))
214    disp('Reading groups_qual');
215    filename = ['test-data', filesep, 'groups_qual.xml'];
216    outfile = [outdir, filesep, 'groups_qual.xml'];
217
218    model = TranslateSBML(filename);
219    if (~isempty(model))
220        disp(sprintf('Printing  %s', filename));
221        OutputSBML(model, outfile);
222    end;
223    test = test + 1;
224    if (compareFiles(filename, outfile))
225        disp(sprintf('Output of %s failed', outfile));
226        fail = fail + 1;
227    end;
228end;
229
230
231disp ('************************************');
232disp('Overall tests:');
233disp(sprintf('Number tests: %d', test));
234disp(sprintf('Number fails: %d', fail));
235disp(sprintf('Pass rate: %d%%\n', ((test-fail)/test)*100));
236
237
238disp('Testing invalid model structures');
239test = 0;
240invalidFail = 0;
241
242test = test + 2;
243m = [];
244[v, mess] = isSBML_Model(m);
245
246expected = sprintf('Invalid Model structure');
247if v ~= 0 || ~strcmp(mess, expected)
248  invalidFail = invalidFail + 1;
249  disp('empty [] failed');
250end;
251
252try
253  OutputSBML(m, [outdir, filesep, 'temp.xml']);
254  invalidFail = invalidFail + 1;
255  disp('empty [] write failed');
256catch
257end;
258
259test = test + 2;
260m = struct();
261[v, mess] = isSBML_Model(m);
262
263expected = sprintf('model missing typecode field');
264if v ~= 0 || ~strcmp(mess, expected)
265  invalidFail = invalidFail + 1;
266  disp('empty structure failed');
267end;
268
269try
270  OutputSBML(m, [outdir, filesep, 'temp.xml']);
271  invalidFail = invalidFail + 1;
272  disp('empty structure write failed');
273catch
274end;
275
276disp ('************************************');
277disp('Overall tests:');
278disp(sprintf('Number tests: %d', test));
279disp(sprintf('Number fails: %d', invalidFail));
280disp(sprintf('Pass rate: %d%%\n', ((test-invalidFail)/test)*100));
281
282fail = fail + invalidFail;
283
284%%%%%%%%%%%%%%%%%%%%%%%%%%%
285% unexpected files cause problems
286
287function isExpected = isFileExpected(filename)
288
289expected_files = { ...
290'algebraicRules.xml', ...
291'both.xml', ...
292'convertedFormulas.xml', ...
293'convertedFormulas2.xml', ...
294'convertedFormulasL2.xml', ...
295'csymbolAvogadro.xml', ...
296'csymbolDelay.xml', ...
297'csymbolTime-reaction-l2.xml', ...
298'csymbolTime.xml', ...
299'errors.xml', ...
300'fatal.xml', ...
301'fbc.xml', ...
302'fbcV2.xml', ...
303'fbcL3V2V1.xml', ...
304'fbcL3V2V2.xml', ...
305'funcDefsWithInitialAssignments.xml', ...
306'functionDefinition.xml', ...
307'initialAssignments.xml', ...
308'l1v1-branch.xml', ...
309'l1v1-minimal.xml', ...
310'l1v1-rules.xml', ...
311'l1v1-units.xml', ...
312'l1v1.xml', ...
313'l1v2-all.xml', ...
314'l2v1-all.xml', ...
315'l2v1-allelements.xml', ...
316'l2v1-assignment.xml', ...
317'l2v2-all.xml', ...
318'l2v2-newComponents.xml', ...
319'l2v2-newelements.xml', ...
320'l2v3-all.xml', ...
321'l2v3-newelements.xml', ...
322'l2v3-newMath.xml', ...
323'l2v4-all.xml', ...
324'l2v5-all.xml', ...
325'l3v1core.xml', ...
326'math_no_arguments.xml', ...
327'nestedPiecewise.xml', ...
328'none.xml', ...
329'nonLogicals.xml', ...
330'notes_annotations.xml', ...
331'piecewise.xml', ...
332'rateRules.xml', ...
333'readerror.xml', ...
334'test-greek.xml', ...
335 'l3v2core.xml', ...
336 'l3v2-no-model.xml', ...
337 'l3v2-empty-math.xml', ...
338 'l3v2-empty-event.xml', ...
339 'l3v2-newmath.xml', ...
340 };
341
342if sum(ismember(expected_files, filename)) == 1
343  isExpected = 1;
344else
345  isExpected = 0;
346end;
347