MatrixBerryCore
StructCompareTC.m
Go to the documentation of this file.
1 classdef StructCompareTC < matlab.unittest.TestCase
2  properties
3  SXComp
4  SYComp
5  end
6  methods (TestMethodSetup)
7  function self = setUp(self)
8  s.a=1;
9  s.b=2;
10  S(1)=s;
11  S(2)=s;
12  S2=S;
13  S(2).b=3;
14  X=S;
15  Y=S;
16  X([1 2])=S;
17  Y([1 2])=S2;
18  [X.c]=deal(S);
19  [Y.c]=deal(S2);
20  X=[X;X];
21  Y=[Y;Y];
22  self.SXComp=X;
23  self.SYComp=Y;
24  end
25  end
26  methods
27  function self = StructCompareTC(varargin)
28  self = self@matlab.unittest.TestCase(varargin{:});
29  end
30  end
31  methods (Test)
32  function self = test_simplestruct_positive(self)
33  S1=struct('a',1,'b',2);
34  S2=struct('a',2,'b',2);
35  isEqual=mxberry.core.struct.structcompare(S1,S2,0);
36  self.verifyEqual(isEqual,false);
37  end
38  %
39  function testUpdateLeavesEmpty(self)
40  S.alpha=struct();
41  S.beta=1;
42  Res=mxberry.core.struct.updateleaves(S,@(x,y)x);
43  self.verifyTrue(isequal(S,Res));
44  end
45  %
46  function testVectorialStruct(self)
47  S1=struct();
48  S1(2,2).alpha=4;
49  S2=struct();
50  S2(4).alpha=4;
51  check();
52  %
53  S1=struct();
54  S1.alpha(2,3).a=6;
55  %
56  S2=struct();
57  S2.alpha(6).a=6;
58  check();
59  function check()
60  isEqual=mxberry.core.struct.structcompare(S1,S2);
61  self.verifyEqual(false,isEqual);
62  end
63  end
64  %
65  function self = test_simplestruct_int64(self)
66  check(int64(1),int64(1),0,true);
67  check(uint64(1),uint64(2),3,true);
68  check(uint64(1),uint64(2),0,false);
69  %
70  function check(value1,value2,tol,expRes)
71  S1=struct('a',1,'b',value1);
72  S2=struct('a',1,'b',value2);
73  isEqual=mxberry.core.struct.structcompare(S1,S2,tol);
74  self.verifyEqual(isEqual,expRes);
75  end
76  end
77  %
78  function testInf(self)
79  S1=struct('a',1,'b',[nan inf -inf 1]);
80  isEqual=mxberry.core.struct.structcompare(S1,S1,0);
81  self.verifyEqual(isEqual,true);
82  end
83  %
84  function self = test_simplestruct_negative(self)
85  S1=struct('a',1,'b',nan);
86  S2=struct('a',1,'b',2);
87  isEqual=mxberry.core.struct.structcompare(S1,S2,0);
88  self.verifyEqual(isEqual,false);
89  end
90  function self = test_simplestruct_negative2(self)
91  S1=struct('a',1,'b',2);
92  S2=struct('a',1,'b',2);
93  isEqual=mxberry.core.struct.structcompare(S1,S2,0);
94  self.verifyEqual(isEqual,true);
95  end
96  function self = test_simplestruct2_negative(self)
97  S1=struct('a',struct('a',1+1e-10,'b',1),'b',2);
98  S2=struct('a',struct('a',1,'b',1),'b',2);
99  isEqual=mxberry.core.struct.structcompare(S1,S2,1e-11);
100  self.verifyEqual(isEqual,false);
101  end
102  function self = test_simplestruct2_positive(self)
103  S1=struct('a',struct('a',1+1e-10,'b',1),'b',2);
104  S2=struct('a',struct('a',1,'b',1),'b',2);
105  [isEqual,reportStr]=mxberry.core.struct.structcompare(S1,S2,1e-9);
106  self.verifyEqual(isEqual,true,reportStr);
107  end
108  function self = test_simplestruct3_negative(self)
109  S1=struct('a',struct('a',nan,'b',1),'b',2);
110  S2=struct('a',struct('a',1,'b',1),'b',2);
111  isEqual=mxberry.core.struct.structcompare(S1,S2,0);
112  self.verifyEqual(isEqual,false);
113  end
114 
115  function self = test_simplestructarray1_negative(self)
116  S1(1)=struct('a',struct('a',1+1e-10,'b',1),'b',2);
117  S1(2)=struct('a',struct('a',nan,'b',1),'b',2);
118  S2(1)=struct('a',struct('a',1,'b',1),'b',2);
119  S2(2)=struct('a',struct('a',1,'b',1),'b',2);
120  isEqual=mxberry.core.struct.structcompare(S1,S2,0);
121  self.verifyEqual(isEqual,false);
122  end
123  function self = test_simplestructarray1_positive(self)
124  S2(1)=struct('a',struct('a',1,'b',1),'b',2);
125  S2(2)=struct('a',struct('a',1,'b',1),'b',2);
126  S1=S2;
127  [isEqual,reportStr]=mxberry.core.struct.structcompare(S1,S2,0);
128  self.verifyEqual(isEqual,true,reportStr);
129  end
130  function self = test_complex1_positive(self)
131  [isEqual,reportStr]=mxberry.core.struct.structcompare(self.SXComp,self.SXComp,0);
132  self.verifyEqual(isEqual,true,reportStr);
133  end
134  function self = test_complex1_negative(self)
135  [isEqual,reportStr]=mxberry.core.struct.structcompare(self.SXComp,self.SYComp,0);
136  self.verifyEqual(isEqual,false);
137  self.verifyEqual(numel(strfind(reportStr,sprintf('\n'))),5);
138  end
139  function self = test_optional_tolerance_arg(self)
140  [isEqual,reportStr]=mxberry.core.struct.structcompare(self.SXComp,self.SYComp,0);
141  [isEqual2,reportStr2]=mxberry.core.struct.structcompare(self.SXComp,self.SYComp);
142  self.verifyEqual(isEqual,isEqual2);
143  self.verifyEqual(reportStr,reportStr2);
144  end
145  function self = test_complex2_negative(self)
146  S1=struct('a',1,'b',repmat([2 nan 3],2,1));
147  S2=struct('a',2,'b',repmat([1 nan 2],2,1));
148  [isEqual,reportStr]=mxberry.core.struct.structcompare(S1,S2,0.1);
149  self.verifyEqual(isEqual,false);
150  self.verifyEqual(2,numel(strfind(reportStr,'Max.')));
151  end
152  function self = test_differentsize_negative(self)
153  S1=struct('a',1,'b',repmat([2 nan 3 3],2,1));
154  S2=struct('a',2,'b',repmat([1 nan 2],2,1));
155  [isEqual,reportStr]=mxberry.core.struct.structcompare(S1,S2,0.1);
156  self.verifyEqual(isEqual,false);
157  self.verifyEqual(1,numel(strfind(reportStr,'Max.')));
158  self.verifyEqual(1,numel(strfind(reportStr,'Different sizes')));
159  end
160  function self = test_cell_positive(self)
161  S1=struct('a',1,'b',{{NaN;struct('c',{'aaa'})}});
162  isEqual=mxberry.core.struct.structcompare(S1,S1,0);
163  self.verifyEqual(isEqual,true);
164  end
165  function self = test_cell_negative(self)
166  S1=struct('a',1,'b',{{NaN;struct('c',{'aaa'})}});
167  S2=struct('a',1,'b',{{NaN;struct('c',{'bbb'})}});
168  [isEqual,reportStr]=mxberry.core.struct.structcompare(S1,S2,0);
169  self.verifyEqual(isEqual,false);
170  self.verifyEqual(1,numel(strfind(reportStr,'values are different')));
171  end
172  function self = test_simplestruct_order_positive(self)
173  S1=struct('a',1,'b',2);
174  S2=struct('b',2,'a',1);
175  isEqual=mxberry.core.struct.structcompare(S1,S2,0);
176  self.verifyEqual(isEqual,true);
177  end
178  function self = test_relative_negative(self)
179  S1=struct('a',1e+10,'b',2e+12);
180  %
181  S2=struct('b',2e+12, 'a',1e+10 + 1e+6);
182  [isEqual,reportStr]=mxberry.core.struct.structcompare(S1, S2, ...
183  1e-10, 1e-5);
184  check_neg(1);
185  %
186  S2=struct('b',2e+12 - 1e+2, 'a',1e+10 + 1e+6);
187  [isEqual,reportStr]=mxberry.core.struct.structcompare(S1, S2, ...
188  1e+3, 1e-5);
189  check_neg(1);
190  %
191  S2=struct('b',2e+12 - 1e+9, 'a',1e+10 + 1e+6);
192  [isEqual,reportStr]=mxberry.core.struct.structcompare(S1, S2, ...
193  1e+3, 1e-5);
194  check_neg(2);
195  %
196  S1=struct('a',1e+6 - 2,'b',2e+6, 'c', 'aab');
197  S2=struct('a',1e+6,'b',2e+6 + 4, 'c', 'aab');
198  [isEqual,reportStr]=mxberry.core.struct.structcompare(S1, S2, ...
199  1, 1e-7);
200  check_neg(2);
201  function check_neg(repMsgCount)
202  self.verifyEqual(isEqual,false);
203  self.verifyEqual(repMsgCount, ...
204  numel(strfind(reportStr, 'Max. relative difference')));
205  end
206  end
207  function self = test_relative_positive(self)
208  S1=struct('a',1e+6 - 0.5,'b',2e+6, 'c', 'aab');
209  S2=struct('a',1e+6,'b',2e+6 +1, 'c', 'aab');
210  isEqual=mxberry.core.struct.structcompare(S1, S2, 1e-10, 1e-6);
211  self.verifyEqual(isEqual,true);
212  %
213  S1=struct('a',1e+10,'b',2e+12);
214  S2=struct('b',2e+12, 'a',1e+10 + 1e+2);
215  isEqual=mxberry.core.struct.structcompare(S1, S2, 1e-10, 1e-5);
216  self.verifyEqual(isEqual,true);
217  %
218  S2=struct('b',2e+12 - 1e+4, 'a',1e+10 + 1e+2);
219  isEqual=mxberry.core.struct.structcompare(S1, S2, 1e+3, 1e-5);
220  self.verifyEqual(isEqual,true);
221  end
222  end
223 end
function repmat(in inpArray, in varargin)
function structcompare(in SX, in SY, in absTol, in relTol)
STRUCTCOMPARE compares two structures using the specified tolerance.
function updateleaves(in SData, in fUpdateFunc)
UPDATELEAVES applies the specified function to each structure leave value and returns the updated str...