|
3 | 3 | properties |
4 | 4 | % Parameters object |
5 | 5 | Parameters |
| 6 | + nConditional = 6 |
6 | 7 | end |
7 | 8 |
|
8 | 9 | properties %(MethodSetupParameter) |
|
13 | 14 | methods (TestClassSetup) |
14 | 15 |
|
15 | 16 | function loadData(testCase) |
16 | | - % Loads validation data |
17 | | - % Graph data is a cell array where each element is the graph number |
18 | | - % (1:3) and within each element is a cell of X- and Y- axis values |
19 | | - % respecively |
20 | | -% load('data/parameters.mat', 'parameters') |
21 | | - testCase.ParamStruct = exp.choiceWorldParams; |
| 17 | + % Sets a test parameter structure |
| 18 | + % testCase.ParamStruct = exp.choiceWorldParams; |
| 19 | + n = testCase.nConditional; |
| 20 | + testCase.ParamStruct = struct(... |
| 21 | + 'numRepeats', repmat(100,1,n),... |
| 22 | + 'numRepeatsUnits', '#',... |
| 23 | + 'numRepeatsDescription', 'No. of repeats of each condition',... |
| 24 | + 'charParam', 'test',... |
| 25 | + 'charParamUnits', 'normalised',... |
| 26 | + 'charParamDescription', 'test char array parameter',... |
| 27 | + 'strParam', "testStr",... |
| 28 | + 'arrayParam', magic(n),... |
| 29 | + 'arrayParamUnits', 'mW',... |
| 30 | + 'logicalArrayParam', true(1,n),... |
| 31 | + 'logicalParam', false,... |
| 32 | + 'logicalParamUnits', 'logical',... |
| 33 | + 'doubleParam', 3,... |
| 34 | + 'functionParam', @(pars,rig)exp.configureChoiceExperiment(exp.ChoiceWorld,pars,rig)); |
22 | 35 | end |
23 | 36 |
|
24 | 37 | function setupClass(testCase) |
25 | 38 | % Check paths file |
26 | 39 | assert(endsWith(which('dat.paths'), fullfile('tests','+dat','paths.m'))); |
27 | 40 | % Create stand-alone panel |
| 41 | + testCase.startMeasuring('gen_empty_obj'); |
28 | 42 | testCase.Parameters = exp.Parameters(); |
29 | | - testCase.fatalAssertTrue(isa(testCase.ParamEditor, 'exp.Parameters')) |
| 43 | + testCase.stopMeasuring('gen_empty_obj'); |
| 44 | + testCase.fatalAssertTrue(isa(testCase.Parameters, 'exp.Parameters')) |
30 | 45 | end |
31 | 46 | end |
32 | 47 |
|
33 | 48 | methods (TestMethodSetup) |
34 | 49 | function buildParams(testCase) |
35 | | - % Re-build the parameters before each test so that changes in |
| 50 | + % Re-load the param structure before each test so that changes in |
36 | 51 | % previous test don't persist |
37 | | - PE = testCase.ParamEditor; |
38 | | - pars = exp.Parameters(testCase.Parameters); |
39 | | - PE.buildUI(pars); |
40 | | - % Number of global parameters: find all text labels |
41 | | - nGlobalLabels = numel(findobj(testCase.Figure, 'Style', 'text')); |
42 | | - nGlobalInput = numel(findobj(testCase.Figure, 'Style', 'checkbox', '-or', 'Style', 'edit')); |
43 | | - % Find Condition Table |
44 | | - conditionTable = findobj(testCase.Figure, '-property', 'ColumnName'); |
45 | | - % Ensure all global params have UI input and label |
46 | | - testCase.fatalAssertTrue(nGlobalLabels == numel(PE.Parameters.GlobalNames)) |
47 | | - testCase.fatalAssertTrue(nGlobalInput == numel(PE.Parameters.GlobalNames)) |
48 | | - % Ensure all conditional params have column in table |
49 | | - testCase.fatalAssertTrue(isequal(size(conditionTable.Data), ... |
50 | | - [PE.Parameters.numTrialConditions, numel(PE.Parameters.TrialSpecificNames)])) |
| 52 | + testCase.startMeasuring('setStruct'); |
| 53 | + testCase.Parameters.Struct = testCase.ParamStruct; |
| 54 | + testCase.stopMeasuring('setStruct'); |
| 55 | + testCase.fatalAssertTrue(length(testCase.Parameters.TrialSpecificNames) == 3) |
| 56 | + testCase.fatalAssertTrue(length(testCase.Parameters.GlobalNames) == 5) |
| 57 | + testCase.fatalAssertTrue(length(testCase.Parameters.Names) == 8) |
51 | 58 | end |
52 | 59 | end |
53 | 60 |
|
54 | 61 | methods (Test) |
55 | 62 |
|
56 | 63 | function test_isTrialSpecific(testCase) |
| 64 | + p = testCase.Parameters; |
| 65 | + % Verfy that array with n columns > 1 is trial specific |
| 66 | + testCase.verifyTrue(p.isTrialSpecific('numRepeats')) |
| 67 | + % Verify that array with n columns == 1 is not trial specific |
| 68 | + testCase.verifyTrue(~p.isTrialSpecific('doubleParam')) |
| 69 | + testCase.verifyTrue(~p.isTrialSpecific('charParam')) |
57 | 70 | end |
58 | 71 |
|
59 | 72 | function test_numTrialConditions(testCase) |
| 73 | + p = testCase.Parameters; |
| 74 | + testCase.verifyTrue(p.numTrialConditions == testCase.nConditional) |
| 75 | + testCase.verifyTrue(p.numTrialConditions == size(p.Struct.numRepeats,2)) |
60 | 76 | end |
61 | 77 |
|
62 | 78 | function test_title(testCase) |
| 79 | + p = testCase.Parameters; |
| 80 | + % Test single param title |
| 81 | + testCase.verifyEqual(p.title('numRepeats'), 'Num repeats') |
| 82 | + % Test array of param titles, including those with units |
| 83 | + testCase.startMeasuring('title'); |
| 84 | + str = p.title({'numRepeats', 'charParam', 'arrayParam', 'logicalParam'}); |
| 85 | + testCase.stopMeasuring('title'); |
| 86 | + expected = {'Num repeats', 'Char param', 'Array param (mW)', 'Logical param'}; |
| 87 | + testCase.verifyTrue(isequal(str, expected)) |
63 | 88 | end |
64 | 89 |
|
65 | 90 | function test_assortForExperiment(testCase) |
| 91 | + p = testCase.Parameters; |
| 92 | + testCase.startMeasuring('assort'); |
| 93 | + [globalParams, trialParams] = testCase.verifyWarningFree(@()p.assortForExperiment); |
| 94 | + testCase.stopMeasuring('assort'); |
| 95 | + testCase.verifyTrue(isstruct(globalParams)) |
| 96 | + testCase.verifyTrue(isequal(fieldnames(globalParams), p.GlobalNames)) |
| 97 | + testCase.verifyTrue(isequal(size(trialParams), [1 testCase.nConditional])) |
| 98 | + testCase.verifyTrue(isequal(fieldnames(trialParams), p.TrialSpecificNames)) |
66 | 99 | end |
67 | 100 |
|
68 | 101 | function test_makeGlobal(testCase) |
| 102 | + p = testCase.Parameters; |
| 103 | + testCase.startMeasuring('makeGlobal'); |
| 104 | + p.makeGlobal('numRepeats') |
| 105 | + testCase.stopMeasuring('makeGlobal'); |
| 106 | + testCase.verifyTrue(~p.isTrialSpecific('numRepeats')) |
| 107 | + testCase.verifyTrue(numel(p.Struct.numRepeats)==1) |
| 108 | + p.makeGlobal('arrayParam') |
| 109 | + expected = magic(testCase.nConditional); |
| 110 | + expected = expected(:,1); |
| 111 | + testCase.verifyTrue(isequal(p.Struct.arrayParam, expected)) |
| 112 | + p.makeGlobal('logicalArrayParam', false) |
| 113 | + testCase.verifyEqual(p.Struct.logicalArrayParam, false) |
| 114 | + try |
| 115 | + p.makeGlobal('numRepeats') |
| 116 | + catch ex |
| 117 | + testCase.verifyEqual(ex.message, '''numRepeats'' is already global') |
| 118 | + end |
69 | 119 | end |
70 | 120 |
|
71 | 121 | function test_removeConditions(testCase) |
| 122 | + p = testCase.Parameters; |
| 123 | + testCase.startMeasuring('removeConditions'); |
| 124 | + p.removeConditions([2,4,6]); |
| 125 | + testCase.stopMeasuring('removeConditions'); |
| 126 | + % Expected result for arrayParam |
| 127 | + expected = magic(testCase.nConditional); |
| 128 | + expected = expected(:,[1,3,5]); |
| 129 | + |
| 130 | + testCase.verifyTrue(p.numTrialConditions == 3) |
| 131 | + testCase.verifyTrue(isequal(p.Struct.arrayParam, expected)) |
72 | 132 | end |
73 | 133 |
|
74 | 134 | function test_description(testCase) |
| 135 | + p = testCase.Parameters; |
| 136 | + testCase.verifyEqual(p.description('numRepeats'), 'No. of repeats of each condition') |
| 137 | + testCase.verifyTrue(isempty(p.description('arrayParam'))) |
| 138 | + testCase.verifyTrue(numel(p.description({'numRepeats', 'charParam'}))==2) |
75 | 139 | end |
76 | 140 |
|
77 | 141 | function test_toConditionServer(testCase) |
| 142 | + % Test behaviour when numRepeats is conditional |
| 143 | + p = testCase.Parameters; |
| 144 | + testCase.startMeasuring('toConditionServer'); |
| 145 | + [cs, globalParams, trialParams] = p.toConditionServer; |
| 146 | + testCase.stopMeasuring('toConditionServer'); |
| 147 | + testCase.verifyTrue(isa(cs, 'exp.PresetConditionServer')) |
| 148 | + testCase.verifyTrue(isstruct(globalParams)) |
| 149 | + testCase.verifyTrue(isequal(fieldnames(globalParams), p.GlobalNames)) |
| 150 | + testCase.verifyTrue(isequal(size(trialParams), [1 sum(p.Struct.numRepeats)])) |
| 151 | + % Verify randomised |
| 152 | + noneRandom = magic(testCase.nConditional); |
| 153 | + noneRandom = [repmat(noneRandom(:,1),1,p.Struct.numRepeats(1)) ... |
| 154 | + repmat(noneRandom(:,2),1,p.Struct.numRepeats(2))]; |
| 155 | + result = [trialParams.arrayParam]; |
| 156 | + testCase.verifyTrue(~isequal(result(:,1:size(noneRandom,2)), noneRandom), ... |
| 157 | + 'Trial conditions likely not randomised by default') |
| 158 | + % Verify that numRepeats was removed |
| 159 | + testCase.verifyTrue(strcmp('numRepeats', setdiff(p.TrialSpecificNames,fieldnames(trialParams)))) |
| 160 | + |
| 161 | + % Test behaviour when numRepeats is global |
| 162 | + p.makeGlobal('numRepeats', 20) |
| 163 | + [~, globalParams, trialParams] = p.toConditionServer; |
| 164 | + testCase.verifyTrue(isequal(size(trialParams), [1 20*testCase.nConditional])) |
| 165 | + testCase.verifyTrue(strcmp('numRepeats', setdiff(p.GlobalNames,fieldnames(globalParams)))) |
| 166 | + |
| 167 | + % Test behaviour when randomiseConditions is set |
| 168 | + p.set('randomiseConditions', false, ... |
| 169 | + 'Flag for whether to randomise trial conditions', 'logical') |
| 170 | + [~, ~, trialParams] = p.toConditionServer; |
| 171 | + result = [trialParams.arrayParam]; |
| 172 | + noneRandom = repmat(magic(testCase.nConditional), 1, 20); |
| 173 | + testCase.verifyTrue(isequal(result, noneRandom), ... |
| 174 | + 'Trial conditions randomised') |
| 175 | + % Test param overide |
| 176 | + [~, ~, trialParams] = p.toConditionServer(true); |
| 177 | + result = [trialParams.arrayParam]; |
| 178 | + testCase.verifyTrue(~isequal(result, noneRandom), ... |
| 179 | + 'Expected trial conditions to be randomised') |
78 | 180 | end |
79 | 181 |
|
80 | 182 | function test_makeTrialSpecific(testCase) |
| 183 | + p = testCase.Parameters; |
| 184 | + % Test making simply numerical param trial specific |
| 185 | + testCase.startMeasuring('makeTrialSpecific'); |
| 186 | + p.makeTrialSpecific('doubleParam') |
| 187 | + testCase.stopMeasuring('makeTrialSpecific'); |
| 188 | + testCase.verifyTrue(p.isTrialSpecific('doubleParam')) |
| 189 | + testCase.verifyTrue(isequal(p.Struct.doubleParam, repmat(3,1,testCase.nConditional))) |
| 190 | + testCase.verifyTrue(ismember('doubleParam', p.TrialSpecificNames)) |
| 191 | + % Test making char array trial specific |
| 192 | + p.makeTrialSpecific('charParam') |
| 193 | + testCase.verifyTrue(p.isTrialSpecific('charParam')) |
| 194 | + testCase.verifyEqual(numel(p.Struct.charParam), testCase.nConditional) |
| 195 | + testCase.verifyTrue(iscell(p.Struct.charParam)) |
| 196 | + % Test making string trial specific |
| 197 | + p.makeTrialSpecific('strParam') |
| 198 | + testCase.verifyTrue(p.isTrialSpecific('strParam')) |
| 199 | + testCase.verifyTrue(isequal(size(p.Struct.strParam), [1 6])) |
| 200 | + testCase.verifyTrue(isstring(p.Struct.strParam)) |
| 201 | + try |
| 202 | + p.makeTrialSpecific('numRepeats') |
| 203 | + catch ex |
| 204 | + testCase.verifyEqual(ex.message, '''numRepeats'' is already trial-specific') |
| 205 | + end |
81 | 206 | end |
82 | 207 |
|
83 | 208 | function test_set(testCase) |
| 209 | + p = testCase.Parameters; |
| 210 | + % Test setting simple param with units and description |
| 211 | + testCase.startMeasuring('setPar'); |
| 212 | + p.set('randomiseConditions', true, ... |
| 213 | + 'Flag for whether to randomise trial conditions', 'logical') |
| 214 | + testCase.stopMeasuring('setPar'); |
| 215 | + testCase.verifyTrue(ismember('randomiseConditions', p.GlobalNames)) |
| 216 | + testCase.verifyTrue(p.Struct.randomiseConditions == true) |
| 217 | + testCase.verifyTrue(isfield(p.Struct, 'randomiseConditionsDescription')) |
| 218 | + testCase.verifyTrue(isfield(p.Struct, 'randomiseConditionsUnits')) |
| 219 | + |
| 220 | + % Test setting a conditional parameter |
| 221 | + p.set('conditionPar', eye(testCase.nConditional)) |
| 222 | + testCase.verifyTrue(ismember('conditionPar', p.TrialSpecificNames)) |
| 223 | + testCase.verifyTrue(isfield(p.Struct, 'conditionParDescription') ... |
| 224 | + && isempty(p.Struct.conditionParDescription)) |
| 225 | + testCase.verifyTrue(~isfield(p.Struct, 'conditionParUnits')) |
| 226 | + |
| 227 | + % Test setting various arrays |
| 228 | + p.set('globalPar', true(testCase.nConditional, 1)) |
| 229 | + testCase.verifyTrue(ismember('globalPar', p.GlobalNames)) |
| 230 | + p.set('globalPar', false(1, testCase.nConditional+1)) |
| 231 | + testCase.verifyTrue(ismember('globalPar', p.TrialSpecificNames)) |
84 | 232 | end |
85 | 233 |
|
86 | | - function test_ui(testCase) |
87 | | - end |
| 234 | +% function test_ui(testCase) |
| 235 | +% p = testCase.Parameters; |
| 236 | +% end |
88 | 237 |
|
89 | 238 | end |
90 | 239 |
|
|
0 commit comments