Home > matpower5.0 > 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 %   $Id: t_nested_struct_copy.m 2460 2014-12-12 20:21:07Z ray $
0006 %   by Ray Zimmerman, PSERC Cornell
0007 %   Copyright (c) 2013 by Power System Engineering Research Center (PSERC)
0008 %
0009 %   This file is part of MATPOWER.
0010 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0011 %
0012 %   MATPOWER is free software: you can redistribute it and/or modify
0013 %   it under the terms of the GNU General Public License as published
0014 %   by the Free Software Foundation, either version 3 of the License,
0015 %   or (at your option) any later version.
0016 %
0017 %   MATPOWER is distributed in the hope that it will be useful,
0018 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0019 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0020 %   GNU General Public License for more details.
0021 %
0022 %   You should have received a copy of the GNU General Public License
0023 %   along with MATPOWER. If not, see <http://www.gnu.org/licenses/>.
0024 %
0025 %   Additional permission under GNU GPL version 3 section 7
0026 %
0027 %   If you modify MATPOWER, or any covered work, to interface with
0028 %   other modules (such as MATLAB code and MEX-files) available in a
0029 %   MATLAB(R) or comparable environment containing parts covered
0030 %   under other licensing terms, the licensors of MATPOWER grant
0031 %   you additional permission to convey the resulting work.
0032 
0033 if nargin < 1
0034     quiet = 0;
0035 end
0036 
0037 t_begin(9, quiet);
0038 
0039 %% set up some structs
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 %% default
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 % if have_fcn('catchme')
0101 %     try
0102 %         DS = nested_struct_copy(D, S, opt);
0103 %         t_ok(0, t);
0104 %     catch me
0105 %         TorF = strcmp(me.message, 'nested_struct_copy: ''b.x'' is not a valid field name');
0106 %         t_ok(TorF, t);
0107 %         if ~TorF
0108 %             me
0109 %         end
0110 %     end
0111 % else
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 % end
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 % DS
0184 % DS.b
0185 % DS.c
0186 %
0187 % E
0188 % E.b
0189 % E.c
0190 
0191 t_end;

Generated on Mon 26-Jan-2015 15:21:31 by m2html © 2005