MatrixBerryCore
CreateArraySuite.m
Go to the documentation of this file.
1 classdef CreateArraySuite < mxberry.core.type.test.mlunit.ArrayTestPropForSuite
2  methods
3  function self = CreateArraySuite(varargin)
4  self = self@mxberry.core.type.test.mlunit.ArrayTestPropForSuite(varargin{:});
5  end
6  function self = test_checkvaluematchisnull_mixedcell_sametype(self)
7  value={{1 2 3};{1,2};{1}};
8  STypeSizeInfo=mxberry.core.type.legacy.generatetypesizeinfostruct(value);
9  isOk=mxberry.core.type.legacy.istypesizeinfouniform(STypeSizeInfo);
10  self.verifyEqual(isOk,true);
11  %
12  end
13  function self = test_checkvaluematchisnull_mixedcell_differenttype(self)
14  value={{1 2 3};{'a','b'};{1}};
15  STypeSizeInfo=mxberry.core.type.legacy.generatetypesizeinfostruct(value);
16  isOk=mxberry.core.type.legacy.istypesizeinfouniform(STypeSizeInfo);
17  self.verifyEqual(isOk,false);
18  %
19  end
20  function aux_test_classname2typeinfo(self,typeList,sizeCVec)
21  for iSize=1:length(sizeCVec)
22  sizeVec=sizeCVec{iSize};
23  STypeInfoCheckVec=cellfun(@mxberry.core.type.legacy.classname2typeinfo,...
24  typeList);
25  objCell=arrayfun(@(x)mxberry.core.type.legacy.createarraybytypeinfo(...
26  x,sizeVec),...
27  STypeInfoCheckVec,'UniformOutput',false);
28  %
29  STypeSizeInfoVec=cellfun(@mxberry.core.type.legacy.generatetypesizeinfostruct,objCell);
30  [~,STypeInfoVec]=...
31  arrayfun(@mxberry.core.type.legacy.istypesizeinfouniform,...
32  STypeSizeInfoVec);
33  isEqualVec=cellfun(@isequal,num2cell(STypeInfoVec),...
34  num2cell(STypeInfoCheckVec));
35  %
36  self.verifyEqual(all(isEqualVec),...
37  true,['failed for size ',num2str(sizeVec)]);
38  end
39  end
40  function self=test_classname2typeinfo(self)
41  sizeCVec={[1 10]};
42  self.aux_test_classname2typeinfo(self.typeList,sizeCVec);
43  %
44  %
45  end
46  %
47  function self=test_typeinfo2classname(self)
48  typeList=self.typeList;
49  typeList(end-1)=[];
50  STypeInfoCheckVec=cellfun(@mxberry.core.type.legacy.classname2typeinfo,...
51  typeList);
52  resTypeList=cellfun(@mxberry.core.type.legacy.typeinfo2classname,...
53  num2cell(STypeInfoCheckVec),'UniformOutput',false);
54  self.verifyEqual(true,isequal(typeList,resTypeList));
55  end
56  %
57  function aux_test_generatetypeinfostruct(self,typeList,sizeCVec,isRelaxedComparison)
58  if nargin<4
59  isRelaxedComparison=false;
60  end
61  for iSize=1:length(sizeCVec)
62  sizeVec=sizeCVec{iSize};
63  STypeInfoCheckVec=cellfun(@mxberry.core.type.legacy.classname2typeinfo,...
64  typeList);
65  objCell=arrayfun(@(x)mxberry.core.type.legacy.createarraybytypeinfo(...
66  x,sizeVec),...
67  STypeInfoCheckVec,'UniformOutput',false);
68  %
69  [isUniformVec,STypeInfoVec]=...
70  cellfun(@mxberry.core.type.legacy.generatetypeinfostruct,...
71  objCell);
72  %
73  self.verifyEqual(all(isUniformVec),true);
74  %
75  if isRelaxedComparison
76  fCompare=@isequal_relaxed;
77  else
78  fCompare=@isequal;
79  end
80  isEqualVec=arrayfun(fCompare,STypeInfoVec,STypeInfoCheckVec);
81  %
82  self.verifyEqual(all(isEqualVec),true,...
83  ['failed for size ',mat2str(sizeVec)]);
84  end
85  function isPositive=isequal_relaxed(s1,s2)
86  if isempty(s1.type)
87  isPositive=s1.depth<=s2.depth;
88  else
89  isPositive=isequal(s1,s2);
90  end
91  end
92  end
93  function self=test_generatetypeinfostruct(self)
94  sizeCVec={[1 10]};
95  self.aux_test_generatetypeinfostruct(self.typeList,sizeCVec);
96  sizeCVec={[]};
97  self.aux_test_generatetypeinfostruct(self.typeList,sizeCVec,true);
98  %
99  end
100  function self=test_generatetypeinfostruct_mixedtype(self)
101  objCell={{{[1 2 3],{'aa','bb'}};{[1 2],[3 4]}}};
102 
103  STypeSizeInfoVec=cellfun(@mxberry.core.type.legacy.generatetypesizeinfostruct,objCell);
104  [isUniformCheckVec,STypeInfoCheckVec]=...
105  arrayfun(@mxberry.core.type.legacy.istypesizeinfouniform,...
106  STypeSizeInfoVec);
107 
108  %
109  [isUniformVec,STypeInfoVec]=...
110  cellfun(@mxberry.core.type.legacy.generatetypeinfostruct,...
111  objCell);
112  %
113  self.verifyEqual(isUniformCheckVec,isUniformVec);
114  if any(isUniformVec)&&isequal(isUniformCheckVec,isUniformVec)
115  isEqualVec=arrayfun(@isequal,STypeInfoVec(isUniformVec),STypeInfoCheckVec(isUniformVec));
116  self.verifyEqual(all(isEqualVec),true);
117  end
118  %
119  end
120  function self=test_createarraybytypesizeinfo(self)
121  sizeCVec=self.sizeCVec;
122  typeList=self.typeList;
123  for iSize=1:length(sizeCVec)
124  sizeVec=sizeCVec{iSize};
125  STypeInfoCheckVec=cellfun(...
126  @mxberry.core.type.legacy.classname2typeinfo,...
127  typeList);
128  objCell=arrayfun(...
129  @(x)mxberry.core.type.legacy.createarraybytypeinfo(...
130  x,sizeVec),...
131  STypeInfoCheckVec,'UniformOutput',false);
132  %
133  isOkVec=cellfun(@(x)isequal(size(x),sizeVec),...
134  objCell);
135  self.verifyEqual(true,all(isOkVec));
136  %
137  STypeSizeInfoVec=cellfun(...
138  @mxberry.core.type.legacy.generatetypesizeinfostruct,...
139  objCell);
140  obj2Cell=...
141  arrayfun(...
142  @mxberry.core.type.legacy.createarraybytypesizeinfo,...
143  STypeSizeInfoVec,'UniformOutput',false);
144  self.verifyEqual(isequal(objCell,obj2Cell),...
145  true,['failed for size ',num2str(sizeVec)]);
146  end
147  end
148  function self=test_createvaluearray(self)
149  sizeCVec=self.sizeCVec;
150  typeList=self.typeList;
151  for iSize=1:length(sizeCVec)
152  %
153  sizeVec=sizeCVec{iSize};
154  STypeInfoCheckVec=cellfun(...
155  @mxberry.core.type.legacy.classname2typeinfo,...
156  typeList);
157  %
158  objCell=arrayfun(...
159  @(x)mxberry.core.type.legacy.createarraybytypeinfo(...
160  x,sizeVec),...
161  STypeInfoCheckVec,'UniformOutput',false);
162  %
163  isOkVec=cellfun(@(x)isequal(size(x),sizeVec),...
164  objCell);
165  self.verifyEqual(true,all(isOkVec));
166  %
167  objEthalonCell=arrayfun(...
168  @(x)mxberry.core.type.legacy.createarraybytypeinfo(...
169  x,[1 1]),...
170  STypeInfoCheckVec,'UniformOutput',false);
171  %
172  obj2Cell=arrayfun(...
173  @(y,x)mxberry.core.type.createvaluearray(y{1}{1},...
174  x{1}(1),sizeVec),...
175  typeList,objEthalonCell,'UniformOutput',false);
176  %
177  self.verifyEqual(true,isequann(objCell,obj2Cell));
178  end
179  end
180  function self=test_createvaluearray_handleType(self)
181  nElem=10;
182  obj1Vec=mxberry.core.type.createarray(...
183  'mxberry.core.type.test.TestHandleType',[nElem 1]);
184  obj2Vec=mxberry.core.type.createvaluearray(...
185  'mxberry.core.type.test.TestHandleType',...
186  mxberry.core.type.test.TestHandleType,[nElem 1]);
187  value=rand(1);
188  obj1Vec(1).setValue(value);
189  obj2Vec(1).setValue(value);
190  obj2Vec(1).setValue(value);
191  for iElem=2:nElem
192  self.verifyEqual(false,...
193  isequal(obj1Vec(1),obj1Vec(iElem)));
194  end
195  %
196  self.verifyEqual(true,isequaln(obj1Vec,obj2Vec));
197  end
198  function self=test_createarraybytypeinfo_redundancy(self)
199  sizeCVec=self.sizeCVec;
200  typeList=self.typeList;
201  for iSize=1:length(sizeCVec)
202  %
203  sizeVec=sizeCVec{iSize};
204  STypeInfoCheckVec=cellfun(...
205  @mxberry.core.type.legacy.classname2typeinfo,...
206  typeList);
207  %
208  objCell=arrayfun(...
209  @(x)mxberry.core.type.legacy.createarraybytypeinfo(...
210  x,sizeVec),...
211  STypeInfoCheckVec,'UniformOutput',false);
212  isOkVec=cellfun(@checkRedundancy,objCell);
213  self.verifyEqual(true,all(isOkVec));
214  end
215  function isOk=checkRedundancyNested(inpArray)
216  if iscell(inpArray)
217  isOkArray=cellfun(@checkRedundancyNested,inpArray);
218  isOk=all(isOkArray(:));
219  else
220  isOk=isempty(inpArray);
221  end
222  end
223  function isOk=checkRedundancy(inpArray)
224  if iscell(inpArray)
225  isOk=checkRedundancyNested(inpArray);
226  else
227  isOk=true;
228  end
229  end
230  end
231  function self=test_generatetypeinfostruct_Precise(self)
232  self.checkGenTypeInfo({},true,1,'');
233  self.checkGenTypeInfo({{}},true,2,'');
234  self.checkGenTypeInfo({{},{}},true,2,'');
235  self.checkGenTypeInfo({{},{{}}},true,3,'');
236  self.checkGenTypeInfo({{1},{2}},true,2,'double');
237  self.checkGenTypeInfo({{1},{}},true,2,'double');
238  self.checkGenTypeInfo({{'1'},{'2'}},true,2,'char');
239  self.checkGenTypeInfo({{},{{1}},{}},true,3,'double');
240  self.checkGenTypeInfo(1,true,0,'double');
241  %negative tests
242  self.checkGenTypeInfo({{1},{{}}},false);
243  self.checkGenTypeInfo({{1},{{1}}},false);
244  self.checkGenTypeInfo({{1},{'a'}},false);
245  self.checkGenTypeInfo({{{'a'}},{'a'}},false);
246  self.checkGenTypeInfo({{1},{true}},false);
247  end
248  function self=checkGenTypeInfo(self,value,isUniformExp,expDepth,expType)
249  if nargin<=3
250  expDepth=nan;
251  expType='';
252  end
253  %
254  STypeInfoExp=struct('type',expType,'depth',expDepth);
255  [isUniform,STypeInfo]=...
257  self.verifyEqual(isUniformExp,isUniform);
258  self.verifyEqual(true,isequaln(STypeInfoExp,STypeInfo));
259  %
260  end
261  function self=test_createarray_negative(self)
262  inpArgCombList={{{'int8'},[1 2]},...
263  {'int8',[1;2]},...
264  {'int8',{[1 2]}}};
265  %
266  for iArgComb=1:length(inpArgCombList)
267  self.runAndCheckError(...
268  'mxberry.core.type.createarray(inpArgCombList{iArgComb})',...
269  ':wrongInput');
270  end
271  end
272  function self=test_createarray(self)
273  import mxberry.core.throwerror;
274  sizeCVec=[self.sizeCVec,{[]}];
275  typeList=self.simpleTypeNoCharList;
276  for iSize=1:length(sizeCVec)
277  for iType=1:length(typeList)
278  className=typeList{iType}{1};
279  errorMsg=sprintf('failed for type %s and size %s',...
280  className,mat2str(sizeCVec{iSize}));
281  %
282  try
283  resArray=mxberry.core.type.createarray(...
284  className,sizeCVec{iSize});
285  catch meObj
286  newObj=throwerror('testFailed',errorMsg);
287  %
288  newObj=addCause(newObj,meObj);
289  throw(newObj);
290  end
291 
292  %
293  self.verifyEqual(true,...
294  mxberry.core.checksize(resArray,...
295  sizeCVec{iSize}),errorMsg);
296  %
297  self.verifyEqual(true,...
298  isa(resArray,className),errorMsg);
299  end
300  end
301  end
302  end
303 end
function num2cell(in inpArray, in varargin)
NUM2CELL is an extension of Matlab built-in function "num2cell" designed to work correctly with empty...
function throwerror(in msgTag, in varargin)
THROWERROR works similarly to built-in ERROR function in case when there is no output arguments but s...
function createarray(in className, in sizeVec)
CREATEARRAY creates an array of specified size and type filling it with some values.
function createvaluearray(in className, in value, in sizeVec)
CREATEVALUEARRAY is designed for creating arrays of a specified type and size filled with a specified...
function generatetypeinfostruct(in value)
GENERATETYPEINFOSTRUCT constructs a meta structure containing a complete (recursive for cells) inform...
function classname2typeinfo(in classNameList)
CLASSNAME2TYPEINFO translates built-in class names into STypeInfo definitions.
function typeinfo2classname(in STypeInfo)