0001 function t_nested_struct_copy(quiet)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 if nargin < 1
0034 quiet = 0;
0035 end
0036
0037 t_begin(9, quiet);
0038
0039
0040 D = struct( ...
0041 'a', 1, ...
0042 'b', struct( ...
0043 'd', [2;3], ...
0044 'e', 4), ...
0045 'c', struct( ...
0046 'f', {{'hello', 'world'}}, ...
0047 'g', 'bye'));
0048
0049 S = struct( ...
0050 'a', 10, ...
0051 'b', struct(...
0052 'x', 100, ...
0053 'y', 200), ...
0054 'c', struct( ...
0055 'g', 'chau', ...
0056 'h', 'oops'), ...
0057 'u', struct( ...
0058 'v', -1, ...
0059 'w', -2) );
0060
0061
0062 t = 'DS = nested_struct_copy(D, S)';
0063 DS = nested_struct_copy(D, S);
0064 E = struct( ...
0065 'a', 10, ...
0066 'b', struct( ...
0067 'd', [2;3], ...
0068 'e', 4, ...
0069 'x', 100, ...
0070 'y', 200), ...
0071 'c', struct( ...
0072 'f', {{'hello', 'world'}}, ...
0073 'g', 'chau', ...
0074 'h', 'oops'), ...
0075 'u', struct( ...
0076 'v', -1, ...
0077 'w', -2 ) );
0078 t_ok(isequal(DS, E), t);
0079
0080 t = 'check = 0';
0081 opt = struct('check', 0);
0082 DS = nested_struct_copy(D, S, opt);
0083 t_ok(isequal(DS, E), t);
0084
0085 t = 'check = -1';
0086 opt = struct('check', -1);
0087 DS = nested_struct_copy(D, S, opt);
0088 E = struct( ...
0089 'a', 10, ...
0090 'b', struct( ...
0091 'd', [2;3], ...
0092 'e', 4), ...
0093 'c', struct( ...
0094 'f', {{'hello', 'world'}}, ...
0095 'g', 'chau'));
0096 t_ok(isequal(DS, E), t);
0097
0098 t = 'check = 1 ==> error';
0099 opt = struct('check', 1);
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112 try
0113 DS = nested_struct_copy(D, S, opt);
0114 t_ok(0, t);
0115 catch
0116 me = lasterr;
0117 TorF = strfind(me, 'nested_struct_copy: ''b.x'' is not a valid field name');
0118 t_ok(TorF, t);
0119 if ~TorF
0120 me
0121 end
0122 end
0123
0124
0125 t = 'check = 1, copy_mode = ''=''';
0126 S2 = rmfield(S, 'u');
0127 opt = struct('check', 1, 'copy_mode', '=');
0128 DS = nested_struct_copy(D, S2, opt);
0129 t_ok(isequal(DS, S2), t);
0130
0131 t = 'exceptions = <''b'', ''=''>';
0132 ex = struct('name', 'b', 'copy_mode', '=');
0133 opt = struct('exceptions', ex);
0134 DS = nested_struct_copy(D, S2, opt);
0135 E = struct( ...
0136 'a', 10, ...
0137 'b', struct( ...
0138 'x', 100, ...
0139 'y', 200), ...
0140 'c', struct( ...
0141 'f', {{'hello', 'world'}}, ...
0142 'g', 'chau', ...
0143 'h', 'oops'));
0144 t_ok(isequal(DS, E), t);
0145
0146 t = 'exceptions = <''b'', ''=''>, <''c'', ''=''>';
0147 ex = struct('name', {'b', 'c'}, 'copy_mode', {'=', '='});
0148 opt = struct('exceptions', ex);
0149 DS = nested_struct_copy(D, S2, opt);
0150 t_ok(isequal(DS, S2), t);
0151
0152 t = 'exceptions = <''b'', ''=''>, <''c.g'', @upper>';
0153 ex = struct('name', {'b', 'c.g'}, 'copy_mode', {'=', @upper});
0154 opt = struct('exceptions', ex);
0155 DS = nested_struct_copy(D, S2, opt);
0156 E = struct( ...
0157 'a', 10, ...
0158 'b', struct( ...
0159 'x', 100, ...
0160 'y', 200), ...
0161 'c', struct( ...
0162 'f', {{'hello', 'world'}}, ...
0163 'g', 'CHAU', ...
0164 'h', 'oops'));
0165 t_ok(isequal(DS, E), t);
0166
0167 t = 'check = 1, exceptions = <''b'', ck=-1>, <''c'', ck=0>';
0168 ex = struct('name', {'b', 'c'}, 'check', {-1,0});
0169 opt = struct('check', 1, 'exceptions', ex);
0170 DS = nested_struct_copy(D, S2, opt);
0171 E = struct( ...
0172 'a', 10, ...
0173 'b', struct( ...
0174 'd', [2;3], ...
0175 'e', 4), ...
0176 'c', struct( ...
0177 'f', {{'hello', 'world'}}, ...
0178 'g', 'chau', ...
0179 'h', 'oops'));
0180 t_ok(isequal(DS, E), t);
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191 t_end;