|
1 | | -%% test basic cfg creation |
2 | | - |
3 | | -% set up |
4 | | -cfgToTest = struct( ... |
5 | | - 'testingDevice', 'pc', ... |
6 | | - 'debug', true, ... |
7 | | - 'testingTranspScreen', true, ... |
8 | | - 'testingSmallScreen', true, ... |
9 | | - 'backgroundColor', [0 0 0], ... |
10 | | - 'text', struct('font', 'Courier New', 'size', 18, 'style', 1), ... |
11 | | - 'monitorWidth', 42, ... |
12 | | - 'screenDistance', 134); |
13 | | - |
14 | | -cfgToTest.keyboard.keyboard = []; |
15 | | -cfgToTest.keyboard.responseBox = []; |
16 | | -cfgToTest.keyboard.responseKey = {}; |
17 | | -cfgToTest.keyboard.escapeKey = 'ESCAPE'; |
18 | | - |
19 | | -cfgToTest = orderfields(cfgToTest); |
20 | | - |
21 | | -% test |
22 | | -cfg = setDefaultsPTB; |
23 | | -assert(isequal(cfg, cfgToTest)); |
24 | | - |
25 | | -%% test that values are not overwritten |
26 | | -clear cfg; |
27 | | -cfg = struct('monitorWidth', 36); |
28 | | - |
29 | | -cfgToTest.monitorWidth = 36; |
30 | | - |
31 | | -cfg = setDefaultsPTB(cfg); |
32 | | -assert(isequal(cfg, cfgToTest)); |
33 | | - |
34 | | -cfgToTest.monitorWidth = 42; |
35 | | - |
36 | | -%% test with audio init |
37 | | - |
38 | | -% set up |
39 | | -cfgToTest.initAudio = 1; |
40 | | -cfgToTest.audio = struct( ... |
41 | | - 'fs', 44800, ... |
42 | | - 'channels', 2, ... |
43 | | - 'initVolume', 1, ... |
44 | | - 'requestedLatency', 3, ... |
45 | | - 'repeat', 1, ... |
46 | | - 'startCue', 0, ... |
47 | | - 'waitForDevice', 1); |
48 | | - |
49 | | -cfgToTest = orderfields(cfgToTest); |
50 | | - |
51 | | -clear cfg; |
52 | | -cfg.initAudio = 1; |
53 | | - |
54 | | -% test |
55 | | -cfg = setDefaultsPTB(cfg); |
56 | | -assert(isequal(cfg, cfgToTest)); |
| 1 | +function test_setDefaultsPTB() |
| 2 | + |
| 3 | + %% test basic cfg creation |
| 4 | + |
| 5 | + % test data |
| 6 | + expectedCFG = returnExpectedCFG(); |
| 7 | + |
| 8 | + % test |
| 9 | + cfg = setDefaultsPTB; |
| 10 | + testSubFields(expectedCFG, cfg) |
| 11 | + |
| 12 | + %% test that values are not overwritten |
| 13 | + |
| 14 | + clear cfg |
| 15 | + |
| 16 | + % test data |
| 17 | + expectedCFG = returnExpectedCFG(); |
| 18 | + expectedCFG.screen.monitorWidth = 36; |
| 19 | + |
| 20 | + % set up |
| 21 | + cfg.screen.monitorWidth = 36; |
| 22 | + |
| 23 | + % test |
| 24 | + cfg = setDefaultsPTB(cfg); |
| 25 | + testSubFields(expectedCFG, cfg) |
| 26 | + |
| 27 | + %% test with audio init |
| 28 | + |
| 29 | + clear cfg |
| 30 | + |
| 31 | + % test data |
| 32 | + expectedCFG = returnExpectedCFG(); |
| 33 | + expectedCFG.audio = struct( ... |
| 34 | + 'do', true, ... |
| 35 | + 'fs', 44800, ... |
| 36 | + 'channels', 2, ... |
| 37 | + 'initVolume', 1, ... |
| 38 | + 'requestedLatency', 3, ... |
| 39 | + 'repeat', 1, ... |
| 40 | + 'startCue', 0, ... |
| 41 | + 'waitForDevice', 1); |
| 42 | + |
| 43 | + % set up |
| 44 | + cfg.audio.do = 1; |
| 45 | + |
| 46 | + % test |
| 47 | + cfg = setDefaultsPTB(cfg); |
| 48 | + testSubFields(expectedCFG, cfg) |
| 49 | + |
| 50 | + |
| 51 | +end |
| 52 | + |
| 53 | + |
| 54 | +function expectedCFG = returnExpectedCFG() |
| 55 | + |
| 56 | + expectedCFG = struct( ... |
| 57 | + 'testingDevice', 'pc', ... |
| 58 | + 'debug', struct('do', true, 'transpWin', true, 'smallWin', true), ... |
| 59 | + 'color', struct( ... |
| 60 | + 'background', [0 0 0]), ... |
| 61 | + 'text', struct('font', 'Courier New', 'size', 18, 'style', 1), ... |
| 62 | + 'screen', struct( ... |
| 63 | + 'monitorWidth', 42, ... |
| 64 | + 'monitorDistance', 134)); |
| 65 | + |
| 66 | + expectedCFG.keyboard.keyboard = []; |
| 67 | + expectedCFG.keyboard.responseBox = []; |
| 68 | + expectedCFG.keyboard.responseKey = {}; |
| 69 | + expectedCFG.keyboard.escapeKey = 'ESCAPE'; |
| 70 | + |
| 71 | +end |
| 72 | + |
| 73 | +function testSubFields(expectedStructure, cfg) |
| 74 | + % check that that the structures match |
| 75 | + % if it fails it check from which subfield the error comes from |
| 76 | + |
| 77 | + try |
| 78 | + |
| 79 | + assert(isequal(expectedStructure, cfg)); |
| 80 | + |
| 81 | + catch ME |
| 82 | + |
| 83 | + if isstruct(expectedStructure) |
| 84 | + |
| 85 | + names = fieldnames(expectedStructure); |
| 86 | + |
| 87 | + for i = 1:numel(names) |
| 88 | + |
| 89 | + disp(names{i}); |
| 90 | + testSubFields(expectedStructure.(names{i}), cfg.(names{i})); |
| 91 | + |
| 92 | + end |
| 93 | + |
| 94 | + end |
| 95 | + |
| 96 | + expectedStructure; |
| 97 | + cfg; |
| 98 | + |
| 99 | + rethrow(ME); |
| 100 | + end |
| 101 | +end |
0 commit comments