This repository has been archived by the owner on Jul 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathUnitTest.m
909 lines (760 loc) · 26.2 KB
/
UnitTest.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
function varargout = UnitTest(varargin)
% UnitTest executes the unit tests for this application, and can be called
% either independently (when testing just the latest version) or via
% UnitTestHarness (when testing for regressions between versions). Either
% two or three input arguments can be passed to UnitTest as described
% below.
%
% The following variables are required for proper execution:
% varargin{1}: string containing the path to the main function. This is
% only required if UnitTest is to be operated across multiple file
% names; otherwise it can be an empty string.
% varargin{2}: variable containing the test data. this can be a string
% to a file, a structure, or any MATLAB variable.
% varargin{3} (optional): variable containing reference data to be used
% for comparison. If not provided, it is assumed that this version
% is the reference and therefore all comparison tests will "Pass".
%
% The following variables are returned upon succesful completion when input
% arguments are provided. If no return variables are given, the results
% will be printed to stdout.
% varargout{1}: cell array of strings containing preamble text that
% summarizes the test, where each cell is a line. This text will
% precede the results table in the report.
% varargout{2}: n x 3 cell array of strings containing the test ID in
% the first column, name in the second, and result (Pass/Fail or
% numerical values typically) of the test in the third.
% varargout{3}: cell array of strings containing footnotes referenced by
% the tests, where each cell is a line. This text will follow the
% results table in the report.
% varargout{4} (optional): variable containing reference data created by
% executing this version. This variable can be passed back into
% subsequent executions of UnitTest as varargin{3} to compare results
% between versions (or to a priori validated reference data).
%
% The following variables are returned when no input arguments are
% provided (required only if called by UnitTestHarness):
% varargout{1}: string containing the application name (with .m
% extension)
% varargout{2}: string containing the path to the version application
% whose results will be used as reference
% varargout{3}: 1 x n cell array of strings containing paths to the other
% applications which will be tested
% varargout{4}: 2 x m cell array of strings containing the name of each
% test suite (first column) and path to the test data (second column)
% varargout{5}: string containing the path and name of report file (will
% be appended by _R201XX.md based on the MATLAB version)
%
% Below is an example of how this function is used:
%
% % Start profiler
% profile on -history
%
% % Execute unit test, printing the test results to stdout
% UnitTest('', load('../test_data/InputData.mat'), ...
% load('../test_data/OutputData.mat'));
%
% % Stop and view the profiler results
% profile viewer
%
% % Execute unit test, storing the test results
% [preamble, table, footnotes] = UnitTest('', ...
% load('../test_data/InputData.mat'), ...
% load('../test_data/OutputData.mat'));
%
% % Execute unit test again but without reference data, this time storing
% % the output from UnitTest as a new reference file
% [preamble, table, footnotes, newreference] = ...
% UnitTest('', load('../test_data/InputData.mat'));
%
% Author: Mark Geurts, mark.w.geurts@gmail.com
% Copyright (C) 2015 University of Wisconsin Board of Regents
%
% This program is free software: you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the
% Free Software Foundation, either version 3 of the License, or (at your
% option) any later version.
%
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
% Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program. If not, see http://www.gnu.org/licenses/.
%% Return Application Information
% If UnitTest was executed without input arguments
if nargin == 0
% Declare the application filename
varargout{1} = '';
% Declare current version directory
varargout{2} = '';
% Declare prior version directories
varargout{3} = {};
% Declare location of test data. Column 1 is the name of the
% test suite, column 2 is the absolute path to the file(s)
varargout{4} = {};
% Declare name of report file (will be appended by _R201XX.md based on
% the MATLAB version)
varargout{5} = '';
% Return to invoking function
return;
end
%% Initialize Unit Testing
% Initialize static test result text variables
pass = 'Pass';
fail = 'Fail';
unk = 'N/A'; %#ok<NASGU>
% Initialize preamble text
preamble = {
'| Input Data | Value |'
'|------------|-------|'
};
% Initialize results cell array
results = cell(0,3);
% Initialize footnotes cell array
footnotes = cell(0,1);
% Initialize reference structure
if nargout == 4
reference = struct;
end
%% TEST 1/2/3/4: Analytic 1D Gamma
%
% DESCRIPTION: This unit test creates a large analytic reference dataset,
% modifies it by a known amount, and executes CalcGamma. The gamma
% result is then compared to a known solution
%
% RELEVANT REQUIREMENTS: none
%
% INPUT DATA: varargin{3}.gammaA
%
% CONDITION A (+): When computed using identical criteria, CalcGamma
% produces an identical array to the reference with GPU Calculation
%
% CONDITION B (+): When computed using identical criteria, CalcGamma
% produces an identical array to the reference with CPU Calculation
%
% CONDITION C (-): When computed using modified criteria, CalcGamma
% produces a different result
% Add path to Event() function (to test Event calls function)
addpath('../test_data/');
% Declare gamma criteria
percent = 3;
dta = 0.3;
local = 1;
% Declare size of data arrays
n = 2000;
% Create reference structure (sine function)
ref.start = 0;
ref.width = pi / 1000;
ref.data = sin(ref.start:ref.width:...
ref.start + ref.width * n) + 2;
% Declare modification values (to be applied to target data)
shift = 0.1;
scale = 1.01;
% Modify reference data, saving to target structure
target.start = ref.start + shift;
target.width = ref.width;
target.data = ref.data * scale;
% Execute in try/catch statement
try
% Execute CalcGamma using test dataset
t = tic;
gamma = CalcGamma(ref, target, percent, dta, 'local', local);
gtime = sprintf('%0.1f sec', toc(t));
gpf = pass;
cpf = pass;
% If reference data exists
if nargin == 3
% If current value does not equal the reference
if max(abs(gamma - varargin{3}.gammaA)) > 1e-5
gpf = fail;
end
% Execute CalcGamma again using CPU
t = tic;
gamma = CalcGamma(ref, target, percent, dta, 'cpu', 1, ...
'local', local);
ctime = sprintf('%0.1f sec', toc(t));
% If CPU value does not equal the reference
if max(abs(gamma - varargin{3}.gammaA)) > 1e-5
cpf = fail;
end
% Execute CalcGamma again, this time using modified criteria
gamma = CalcGamma(ref, target, percent, dta * 2, ...
'local', local);
% If modified value equals the reference, the test fails
if max(abs(gamma - varargin{3}.gammaA)) < 1e-5
gpf = fail;
end
% Execute CalcGamma again, this time using modified criteria
gamma = CalcGamma(ref, target, percent * 2, dta, ...
'local', local);
% If modified value equals the reference, the test fails
if max(abs(gamma - varargin{3}.gammaA)) < 1e-5
gpf = fail;
end
% Otherwise, set reference
elseif nargout == 4
reference.gammaA = gamma;
gpf = pass;
cpf = pass;
end
catch
gpf = fail;
cpf = fail;
end
% Add the dataset size to the preamble
preamble{length(preamble)+1} = ...
sprintf('| Analytic 1D Gamma Array | %i x %i |', size(gamma));
% Add header to results table
results{size(results,1)+1,1} = 'ID';
results{size(results,1),2} = 'Test Case';
results{size(results,1),3} = 'Result';
% Add GPU computation result
results{size(results,1)+1,1} = '1';
results{size(results,1),2} = 'Analytic 1D Gamma GPU Result within 1e-5';
results{size(results,1),3} = gpf;
% Add computation time
results{size(results,1)+1,1} = '2';
results{size(results,1),2} = 'Analytic 1D Gamma GPU Computation Time';
results{size(results,1),3} = gtime;
% Add CPU computation result
results{size(results,1)+1,1} = '3';
results{size(results,1),2} = 'Analytic 1D Gamma CPU Result within 1e-5';
results{size(results,1),3} = cpf;
% Add computation time
results{size(results,1)+1,1} = '4';
results{size(results,1),2} = 'Analytic 1D Gamma CPU Computation Time';
results{size(results,1),3} = ctime;
%% TEST 5/6/7/8: Beam Profile 1D Gamma
%
% DESCRIPTION: This unit test creates a real world profile dataset
% and executes CalcGamma. The gamma result is then compared to a known
% solution
%
% RELEVANT REQUIREMENTS: none
%
% INPUT DATA: varargin{2}.referenceB, varargin{2}.targetB,
% varargin{3}.gammaB
%
% CONDITION A (+): When computed using identical criteria, CalcGamma
% produces an identical array to the reference with GPU Calculation
%
% CONDITION B (+): When computed using identical criteria, CalcGamma
% produces an identical array to the reference with CPU Calculation
%
% CONDITION C (-): When computed using modified criteria, CalcGamma
% produces a different result
% Declare gamma criteria
percent = 2;
dta = 0.1;
local = 0;
% Load reference and target data from varargin
ref = varargin{2}.referenceB;
target = varargin{2}.targetB;
% Determine location and value of maximum
[C, I] = max(target.data);
% Search left side for half-maximum value
for x = 1:I-1
if target.data(x) == C/2
left = target.start + target.width * (x-1);
break;
elseif target.data(x) < C/2 && target.data(x+1) > C/2
left = interp1(target.data(x:x+1), target.start + ...
target.width * (x-1:x), C/2);
break;
end
end
% Search right side for half-maximum value
for x = I:size(target.data,1)-1
if target.data(x) == C/2
right = target.start + target.width * (x-1);
break;
elseif target.data(x) > C/2 && target.data(x+1) < C/2
right = interp1(target.data(x:x+1), target.start + ...
target.width * (x-1:x), C/2);
break;
end
end
% Center target profile on FWHM
target.start = target.start - (right + left)/2;
% Execute in try/catch statement
try
% Execute CalcGamma using test dataset
t = tic;
gamma = CalcGamma(ref, target, percent, dta, 'local', local);
gtime = sprintf('%0.1f sec', toc(t));
gpf = pass;
cpf = pass;
% If reference data exists
if nargin == 3
% If current value does not equal the reference
if max(abs(gamma - varargin{3}.gammaB)) > 1e-5
gpf = fail;
end
% Execute CalcGamma again using CPU
t = tic;
gamma = CalcGamma(ref, target, percent, dta, 'cpu', 1, ...
'local', local);
ctime = sprintf('%0.1f sec', toc(t));
% If CPU value does not equal the reference
if max(abs(gamma - varargin{3}.gammaB)) > 1e-5
cpf = fail;
end
% Execute CalcGamma again, this time using modified criteria
gamma = CalcGamma(ref, target, percent, dta, ...
'local', local, 'limit', 0.1);
% If modified value equals the reference, the test fails
if max(abs(gamma - varargin{3}.gammaB)) < 1e-5
gpf = fail;
end
% Execute CalcGamma again, this time using modified criteria
gamma = CalcGamma(ref, target, percent, dta, ...
'local', local, 'refval', 10);
% If modified value equals the reference, the test fails
if max(abs(gamma - varargin{3}.gammaB)) < 1e-5
gpf = fail;
end
% Otherwise, set reference
elseif nargout == 4
reference.gammaB = gamma;
gpf = pass;
cpf = pass;
end
catch
gpf = fail;
cpf = fail;
end
% Add the dataset size to the preamble
preamble{length(preamble)+1} = ...
sprintf('| Beam Profile 1D Gamma Array | %i x %i |', size(gamma));
% Add GPU computation result
results{size(results,1)+1,1} = '5';
results{size(results,1),2} = 'Beam Profile 1D Gamma GPU Result within 1e-5';
results{size(results,1),3} = gpf;
% Add computation time
results{size(results,1)+1,1} = '6';
results{size(results,1),2} = 'Beam Profile 1D Gamma GPU Computation Time';
results{size(results,1),3} = gtime;
% Add CPU computation result
results{size(results,1)+1,1} = '7';
results{size(results,1),2} = 'Beam Profile 1D Gamma CPU Result within 1e-5';
results{size(results,1),3} = cpf;
% Add computation time
results{size(results,1)+1,1} = '8';
results{size(results,1),2} = 'Beam Profile 1D Gamma CPU Computation Time';
results{size(results,1),3} = ctime;
%% TEST 9/10/11/12: Simple 2D Gamma
%
% DESCRIPTION: This unit test creates a simple analytic reference dataset,
% modifies it by a known amount, and executes CalcGamma. The gamma
% result is then compared to a known solution
%
% RELEVANT REQUIREMENTS: none
%
% INPUT DATA: varargin{3}.gammaC
%
% CONDITION A (+): When computed using identical criteria, CalcGamma
% produces an identical array to the reference with GPU Calculation
%
% CONDITION B (+): When computed using identical criteria, CalcGamma
% produces an identical array to the reference with CPU Calculation
%
% CONDITION C (-): When computed using modified criteria, CalcGamma
% produces a different result
% Declare Gamma criteria
percent = 3;
dta = 0.3;
local = 0;
res = 20;
% Declare size of data arrays
n = 100;
% Create reference data structure (peaks function)
ref.start = [0 0];
ref.width = [0.01 0.01];
ref.data = peaks(n) + 5;
% Declare modification values (to be applied to reference data)
shift = [0.05 0.05];
scale = 1.03;
% Modify reference data, saving to target structure
target.start = ref.start + shift;
target.width = ref.width;
target.data = ref.data * scale;
% Execute in try/catch statement
try
% Execute CalcGamma using test dataset
t = tic;
gamma = CalcGamma(ref, target, percent, dta, 'local', local, ...
'res', res);
gtime = sprintf('%0.1f sec', toc(t));
gpf = pass;
cpf = pass;
% If reference data exists
if nargin == 3
% If current value does not equal the reference
if max(max(abs(gamma - varargin{3}.gammaC))) > 1e-5
gpf = fail;
end
% Execute CalcGamma again using CPU
t = tic;
gamma = CalcGamma(ref, target, percent, dta, 'cpu', 1, ...
'local', local, 'res', res);
ctime = sprintf('%0.1f sec', toc(t));
% If CPU value does not equal the reference
if max(max(abs(gamma - varargin{3}.gammaC))) > 1e-5
cpf = fail;
end
% Execute CalcGamma again, this time using modified criteria
gamma = CalcGamma(ref, target, percent, dta * 2, ...
'local', local, 'res', res);
% If modified value equals the reference, the test fails
if max(max(abs(gamma - varargin{3}.gammaC))) < 1e-5
gpf = fail;
end
% Execute CalcGamma again, this time using modified criteria
gamma = CalcGamma(ref, target, percent * 2, dta, ...
'local', local, 'res', res);
% If modified value equals the reference, the test fails
if max(max(abs(gamma - varargin{3}.gammaC))) < 1e-5
gpf = fail;
end
% Otherwise, set reference
elseif nargout == 4
reference.gammaC = gamma;
gpf = pass;
cpf = pass;
end
catch
gpf = fail;
cpf = fail;
end
% Add the dataset size to the preamble
preamble{length(preamble)+1} = ...
sprintf('| Simple 2D Gamma Array | %i x %i |', size(gamma));
% Add GPU computation result
results{size(results,1)+1,1} = '9';
results{size(results,1),2} = 'Simple 2D Gamma GPU Result within 1e-5';
results{size(results,1),3} = gpf;
% Add computation time
results{size(results,1)+1,1} = '10';
results{size(results,1),2} = 'Simple 2D Gamma GPU Computation Time';
results{size(results,1),3} = gtime;
% Add CPU computation result
results{size(results,1)+1,1} = '11';
results{size(results,1),2} = 'Simple 2D Gamma CPU Result within 1e-5';
results{size(results,1),3} = cpf;
% Add computation time
results{size(results,1)+1,1} = '12';
results{size(results,1),2} = 'Simple 2D Gamma CPU Computation Time';
results{size(results,1),3} = ctime;
%% TEST 13/14/15/16: Dose Volume 3D Gamma
%
% DESCRIPTION: This unit test creates a real world dose volume dataset
% and executes CalcGamma. The gamma result is then compared to a known
% solution
%
% RELEVANT REQUIREMENTS: none
%
% INPUT DATA: varargin{2}.referenceD, varargin{2}.targetD,
% varargin{3}.gammaD
%
% CONDITION A (+): When computed using identical criteria, CalcGamma
% produces an identical array to the reference with GPU Calculation
%
% CONDITION B (+): When computed using identical criteria, CalcGamma
% produces an identical array to the reference with CPU Calculation
% Declare gamma criteria
percent = 3;
dta = 3;
local = 0;
restrict = 1;
% Load reference and target data from varargin
ref = varargin{2}.referenceD;
target = varargin{2}.targetD;
% Execute in try/catch statement
try
% Execute CalcGamma using test dataset
t = tic;
gamma = CalcGamma(ref, target, percent, dta, 'local', local, ...
'restrict', restrict);
gtime = sprintf('%0.1f sec', toc(t));
gpf = pass;
cpf = pass;
% If reference data exists
if nargin == 3
% If current value does not equal the reference
if max(max(max(abs(gamma - varargin{3}.gammaD)))) > 1e-5
gpf = fail;
end
% Execute CalcGamma again using CPU
t = tic;
gamma = CalcGamma(ref, target, percent, dta, 'cpu', 1, ...
'local', local, 'restrict', restrict);
ctime = sprintf('%0.1f sec', toc(t));
% If CPU value does not equal the reference
if max(max(max(abs(gamma - varargin{3}.gammaD)))) > 1e-5
cpf = fail;
end
% Otherwise, set reference
elseif nargout == 4
reference.gammaD = gamma;
gpf = pass;
cpf = pass;
end
catch
gpf = fail;
cpf = fail;
end
% Add the dataset size to the preamble
preamble{length(preamble)+1} = ...
sprintf('| Dose Volume 3D Gamma Array | %i x %i x %i |', size(gamma));
% Add GPU computation result
results{size(results,1)+1,1} = '13';
results{size(results,1),2} = 'Dose Volume 3D Gamma GPU Result within 1e-5';
results{size(results,1),3} = gpf;
% Add computation time
results{size(results,1)+1,1} = '14';
results{size(results,1),2} = 'Dose Volume 3D Gamma GPU Computation Time';
results{size(results,1),3} = gtime;
% Add CPU computation result
results{size(results,1)+1,1} = '15';
results{size(results,1),2} = 'Dose Volume 3D Gamma CPU Result within 1e-5';
results{size(results,1),3} = cpf;
% Add computation time
results{size(results,1)+1,1} = '16';
results{size(results,1),2} = 'Dose Volume 3D Gamma CPU Computation Time';
results{size(results,1),3} = ctime;
%% TEST 17: Data Validity
%
% DESCRIPTION: This test verifies that CalcGamma correctly fails when input
% data is not valid. This function tests error messages both with an
% without the Event() function.
%
% RELEVANT REQUIREMENTS: none
%
% INPUT DATA: No input data required
%
% CONDITION A (-): CalcGamma fails with too few arguments
%
% CONDITION B (-): CalcGamma fails if the reference structure is poorly
% formatted
%
% CONDITION C (-): CalcGamma fails if the target structure is poorly
% formatted
%
% CONDITION D (-): CalcGamma fails if the reference and target dimensions
% differ
%
% CONDITION E (-): CalcGamma fails if the reference structure contains more
% than three dimensions
%
% CONDITION F (-): CalcGamma fails if the target structure contains more
% than three dimensions
% Start with passing result
pf = pass;
% Try to execute CalcGamma with too few arguments
try
CalcGamma();
pf = fail;
catch
% The test passes if an error is thrown
end
% Initialize bad data
ref.data = rand(5);
ref.start = [0 0];
ref.width = [1 1 1];
target.data = rand(5);
target.start = [0 0];
target.width = [1 1 1];
% Try to execute CalcGamma with bad reference structure
try
CalcGamma(ref, target, 1, 1);
pf = fail;
catch
% The test passes if an error is thrown
end
% Fix reference
ref.width = [1 1];
% Try to execute CalcGamma with bad target structure
try
CalcGamma(ref, target, 1, 1);
pf = fail;
catch
% The test passes if an error is thrown
end
% Fix target
target.data = rand(5, 5, 5);
target.start = [0 0 0];
target.width = [1 1 1];
% Try to execute CalcGamma with different dimensions
try
CalcGamma(ref, target, 1, 1);
pf = fail;
catch
% The test passes if an error is thrown
end
% Create 4-D datasets
ref.data = rand(5, 5, 5, 5);
ref.start = [0 0 0 0];
ref.width = [1 1 1 1];
target.data = rand(5, 5, 5, 5);
target.start = [0 0 0 0];
target.width = [1 1 1 1];
% Try to execute CalcGamma with different dimensions
try
CalcGamma(ref, target, 1, 1);
pf = fail;
catch
% The test passes if an error is thrown
end
% Repeat without Event()
restoredefaultpath
% Try to execute CalcGamma with too few arguments
try
CalcGamma();
pf = fail;
catch
% The test passes if an error is thrown
end
% Initialize bad data
ref.data = rand(5);
ref.start = [0 0];
ref.width = [1 1 1];
target.data = rand(5);
target.start = [0 0];
target.width = [1 1 1];
% Try to execute CalcGamma with bad reference structure
try
CalcGamma(ref, target, 1, 1);
pf = fail;
catch
% The test passes if an error is thrown
end
% Fix reference
ref.width = [1 1];
% Try to execute CalcGamma with bad target structure
try
CalcGamma(ref, target, 1, 1);
pf = fail;
catch
% The test passes if an error is thrown
end
% Fix target
target.data = rand(5, 5, 5);
target.start = [0 0 0];
target.width = [1 1 1];
% Try to execute CalcGamma with different dimensions
try
CalcGamma(ref, target, 1, 1);
pf = fail;
catch
% The test passes if an error is thrown
end
% Create 4-D datasets
ref.data = rand(5, 5, 5, 5);
ref.start = [0 0 0 0];
ref.width = [1 1 1 1];
target.data = rand(5, 5, 5, 5);
target.start = [0 0 0 0];
target.width = [1 1 1 1];
% Try to execute CalcGamma with different dimensions
try
CalcGamma(ref, target, 1, 1);
pf = fail;
catch
% The test passes if an error is thrown
end
% Add test result
results{size(results,1)+1,1} = '17';
results{size(results,1),2} = 'Data Validation Checks Functional';
results{size(results,1),3} = pf;
%% TEST 18/19: Code Analyzer Messages, Cyclomatic Complexity
%
% DESCRIPTION: This unit test uses the checkcode() MATLAB function to check
% each function used by the application and return any Code Analyzer
% messages that result. The cumulative cyclomatic complexity is also
% computed for each function and summed to determine the total
% application complexity. Although this test does not reference any
% particular requirements, it is used during development to help identify
% high risk code.
%
% RELEVANT REQUIREMENTS: none
%
% INPUT DATA: No input data required
%
% CONDITION A (+): Report any code analyzer messages for all functions
% called by CalcGamma
%
% CONDITION B (+): Report the cumulative cyclomatic complexity for all
% functions called by CalcGamma
% Search for required functions
fList = matlab.codetools.requiredFilesAndProducts('CalcGamma.m');
% Initialize complexity and messages counters
comp = 0;
mess = 0;
% Loop through each dependency
for i = 1:length(fList)
% Execute checkcode
inform = checkcode(fList{i}, '-cyc');
% Loop through results
for j = 1:length(inform)
% Check for McCabe complexity output
c = regexp(inform(j).message, ...
'^The McCabe complexity .+ is ([0-9]+)\.$', 'tokens');
% If regular expression was found
if ~isempty(c)
% Add complexity
comp = comp + str2double(c{1});
else
% If not an invalid code message
if ~strncmp(inform(j).message, 'Filename', 8)
% Log message
Event(sprintf('%s in %s', inform(j).message, fList{i}), ...
'CHCK');
% Add as code analyzer message
mess = mess + 1;
end
end
end
end
% Add code analyzer messages counter to results
results{size(results,1)+1,1} = '18';
results{size(results,1),2} = 'Code Analyzer Messages';
results{size(results,1),3} = sprintf('%i', mess);
% Add complexity results
results{size(results,1)+1,1} = '19';
results{size(results,1),2} = 'Cumulative Cyclomatic Complexity';
results{size(results,1),3} = sprintf('%i', comp);
%% Finish up
% Close all figures
close all force;
% If no return variables are present, print the results
if nargout == 0
% Print preamble
for j = 1:length(preamble)
fprintf('%s\n', preamble{j});
end
fprintf('\n');
% Loop through each table row
for j = 1:size(results,1)
% Print table row
fprintf('| %s |\n', strjoin(results(j,:), ' | '));
% If this is the first column
if j == 1
% Also print a separator row
fprintf('|%s\n', repmat('----|', 1, size(results,2)));
end
end
fprintf('\n');
% Print footnotes
for j = 1:length(footnotes)
fprintf('%s<br>\n', footnotes{j});
end
% Otherwise, return the results as variables
else
% Store return variables
if nargout >= 1; varargout{1} = preamble; end
if nargout >= 2; varargout{2} = results; end
if nargout >= 3; varargout{3} = footnotes; end
if nargout >= 4; varargout{4} = reference; end
end