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