rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
test_indexed_node.m
Go to the documentation of this file.
1 function ok = test_indexed_node
2  % Consistency tests for the DataTree.IdMapNode class
3  %
4  % Test cases:
5  % - 2. initialize with cell array
6  % - 3. initialize with struct
7  % - 4. initialize with struct in flat mode (default mergefun)
8  % - 5. initialize with struct in flat mode (custom mergefun)
9  % - 5b. overwrite with given DataTree.IdMapNode object from (3.) in flat
10  % mode with different mergefun than before
11 
12  ok = true;
13 
14  idmap = {'a', {'b', {'c','d'}},'e'};
15  vals1 = {1, 2, 3};
16  vals2 = struct('a', 1, 'b', 2, 'c', 3, 'd', 4, 'e', 5);
17  vals2_incomplete = struct('a', 1, 'b', 2, 'd', 4, 'e', 5);
18 
19  ref2 = [1 2 2 2 3]';
20  ref3 = (1:5)';
21  ref4 = [1 2 2 2 5]';
22  ref5a = [1 4 4 4 5]';
23  ref5b = [1 2.75 2.75 2.75 5]';
24 
25  try
26  ic2 = DataTree.IdMapNode(idmap, vals1);
27  a = get_flat_struct(ic2);
28  vals = structfun(@(x) x, a);
29  if any(vals ~= ref2)
30  disp('Test2 failed');
31  ok = false;
32  end
33  ok = ok & scalar(ic2, @(x)mean([x{:}])) == 2;
34  catch exception
35  disp('Test2 failed:')
36  disp(getReport(exception));
37  ok = false;
38  end
39 
40 
41  try
42  ic3 = DataTree.IdMapNode(idmap, vals2);
43  a= get_flat_struct(ic3);
44  vals = structfun(@(x) x, a);
45  if any(vals ~= ref3)
46  disp('Test3 failed');
47  ok = false;
48  end
49  ok = ok & scalar(ic3, @(x)mean([x{:}])) == mean([1,5,2.75]);
50  catch exception
51  disp('Test3 failed:')
52  disp(getReport(exception));
53  ok = false;
54  end
55 
56  try
57  ic4 = DataTree.IdMapNode(idmap, vals2, []);
58  a = get_flat_struct(ic4);
59  vals = structfun(@(x) x, a);
60  ok = ok & all(vals == ref4);
61  if any(vals ~= ref4)
62  disp('Test4 failed');
63  ok = false;
64  end
65  catch exception
66  disp('Test4 failed:')
67  disp(getReport(exception));
68  ok = false;
69  end
70 
71  try
72  ic5 = DataTree.IdMapNode(idmap, vals2, @(x) max([x{:}]));
73  a = get_flat_struct(ic5);
74  vals = structfun(@(x) x, a);
75  ok = ok & all(vals == ref5a);
76  set(ic5, [], ic3, @(x) mean([x{:}]));
77  if any(vals ~= ref5a)
78  disp('Test5a failed');
79  ok = false;
80  end
81  a = get_flat_struct(ic5);
82  vals = structfun(@(x) x, a);
83  if any(vals ~= ref5b)
84  disp('Test5b failed');
85  ok = false;
86  end
87  catch exception
88  disp('Test5 failed:')
89  disp(getReport(exception));
90  ok = false;
91  end
92 end