Home > matpower7.0 > lib > t > t_nested_struct_copy.m

t_nested_struct_copy

PURPOSE ^

T_NESTED_STUCT_COPY Tests for NESTED_STUCT_COPY.

SYNOPSIS ^

function t_nested_struct_copy(quiet)

DESCRIPTION ^

T_NESTED_STUCT_COPY  Tests for NESTED_STUCT_COPY.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function t_nested_struct_copy(quiet)
0002 %T_NESTED_STUCT_COPY  Tests for NESTED_STUCT_COPY.
0003 
0004 %   MATPOWER
0005 %   Copyright (c) 2013-2016, Power Systems Engineering Research Center (PSERC)
0006 %   by Ray Zimmerman, PSERC Cornell
0007 %
0008 %   This file is part of MATPOWER.
0009 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0010 %   See https://matpower.org for more info.
0011 
0012 if nargin < 1
0013     quiet = 0;
0014 end
0015 
0016 t_begin(11, quiet);
0017 
0018 %% set up some structs
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 %% default
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 % if have_fcn('catchme')
0080 %     try
0081 %         DS = nested_struct_copy(D, S, opt);
0082 %         t_ok(0, t);
0083 %     catch me
0084 %         TorF = strcmp(me.message, 'nested_struct_copy: ''b.x'' is not a valid field name');
0085 %         t_ok(TorF, t);
0086 %         if ~TorF
0087 %             me
0088 %         end
0089 %     end
0090 % else
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 % end
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 t = 'default, with struct array field';
0188 S3 = S;
0189 S3.u(2).v = -3;
0190 S3.u(2).w = -4;
0191 E = struct( ...
0192     'a', 10, ...
0193     'b', struct( ...
0194         'd', [2;3], ...
0195         'e', 4, ...
0196         'x', 100, ...
0197         'y', 200), ...
0198     'c', struct( ...
0199         'f', {{'hello', 'world'}}, ...
0200         'g', 'chau', ...
0201         'h', 'oops'), ...
0202     'u', struct( ...
0203         'v', {-1, -3}, ...
0204         'w', {-2, -4} ) );
0205 DS = nested_struct_copy(D, S3);
0206 t_ok(isequal(DS, E), t);
0207 
0208 % D2
0209 % S3
0210 % DS
0211 % E
0212 
0213 % DS
0214 % DS.b
0215 % DS.c
0216 %
0217 % E
0218 % E.b
0219 % E.c
0220 
0221 t_end;

Generated on Mon 24-Jun-2019 15:58:45 by m2html © 2005