Home > matpower5.1 > 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-2015 by Power System Engineering Research Center (PSERC)
0006 %   by Ray Zimmerman, PSERC Cornell
0007 %
0008 %   $Id: t_nested_struct_copy.m 2644 2015-03-11 19:34:22Z ray $
0009 %
0010 %   This file is part of MATPOWER.
0011 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0012 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0013 
0014 if nargin < 1
0015     quiet = 0;
0016 end
0017 
0018 t_begin(9, quiet);
0019 
0020 %% set up some structs
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 %% default
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 % if have_fcn('catchme')
0082 %     try
0083 %         DS = nested_struct_copy(D, S, opt);
0084 %         t_ok(0, t);
0085 %     catch me
0086 %         TorF = strcmp(me.message, 'nested_struct_copy: ''b.x'' is not a valid field name');
0087 %         t_ok(TorF, t);
0088 %         if ~TorF
0089 %             me
0090 %         end
0091 %     end
0092 % else
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 % end
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 % DS
0165 % DS.b
0166 % DS.c
0167 %
0168 % E
0169 % E.b
0170 % E.c
0171 
0172 t_end;

Generated on Fri 20-Mar-2015 18:23:34 by m2html © 2005