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