MatrixBerryCore
mlunit_test_xmlloadsave.m
Go to the documentation of this file.
1 classdef mlunit_test_xmlloadsave < mxberry.unittest.TestCase
2  properties
3  locDir
4  fileName
5  simpleData
6  simpleMetaData
7  xmlsaveParams
8  resTmpDir
9  end
10  properties (MethodSetupParameter)
11  argList={{},{'insertTimestamp',false},{'forceon'}};
12  marker={'','insertTimestampFalse','forceon'};
13  end
14  %
15  methods (TestMethodTeardown)
16  function self = tearDown(self)
17  mxberry.io.rmdir(self.resTmpDir,'s');
18  end
19  end
20  methods (TestMethodSetup,ParameterCombination='sequential')
21  function self=setUp(self,marker,argList)
22  import mxberry.xml.*;
23  self.resTmpDir=mxberry.test.TmpDataManager.getDirByCallerKey();
24  self.fileName=[self.resTmpDir,filesep,'tmp.xml'];
25  Data.a=[1 2 3];
26  Data.b=[1 2 3].';
27  Data.c='sdfasdfa';
28  Data.d='sdfasdfa'.';
29  self.simpleData=Data;
30  self.simpleMetaData=struct('version','0.1','someparam',...
31  'abra-kadabra');
32  self.xmlsaveParams=argList;
33  end
34  end
35  methods
36  function self = mlunit_test_xmlloadsave(varargin)
37  import mxberry.xml.*;
38  self = self@mxberry.unittest.TestCase(varargin{:});
39  metaClass=metaclass(self);
40  self.locDir=fileparts(which(metaClass.Name));
41  end
42  end
43  methods (Test)
44  function testTrickySize(self)
45  Data.alpha={'alpha'.'};
46  check();
47  Data.theta.gamma={'alpha'.'};
48  Data.beta=repmat('c',20,30);
49  Data.vega=zeros(2,3,4);
50  check();
51  function check()
52  import mxberry.xml.*;
53  reg=mxberry.core.parseparams(self.xmlsaveParams);
54  ResData=xmlparse(xmlformat(Data,reg{:}));
55  self.verifyEqual(true,isequal(Data,ResData));
56  end
57  end
58 
59  function testEmpty(self)
60  expVar=struct('alpha',zeros(1,0));
61  check();
62  expVar=zeros(1,0);
63  check();
64  function check()
65  import mxberry.xml.*;
66  resVar=xmlparse(xmlformat(expVar));
67  self.verifyEqual(true,isequal(resVar,expVar));
68  end
69  end
70  function self=testMultidimStructField(self)
71  import mxberry.xml.*;
72  SData.alpha(2,3).a=1;
73  SRes=xmlparse(xmlformat(SData,'on'));
74  self.verifyEqual(isequaln(SRes,SData),true);
75  end
76  function self = testInteger(self)
77  import mxberry.xml.*;
78  SData.a=int32([1,2,3]);
79  SData.b=int64([1,2,3]);
80  SData.c=uint64([1,2,3;2 3 3]);
81  SData.d=uint32([1,2,3;4 2 4]);
82  SData.a1=uint32([]);
83  SData.b1=uint64([]);
84  SData.c1=int64([]);
85  SData.d1=int32([]);
86  SData.test=SData;
87  %
88  self.xmlsave(self.fileName,SData);
89  SRes=xmlload(self.fileName);
90  self.verifyEqual(isequaln(SRes,SData),true);
91  end
92  function testNegative(self)
93  import mxberry.xml.*;
94  if verLessThan('matlab','R2016a')
95  checkN(handle([1,2]));
96  else
97  checkP(handle([1,2]));
98  end
99  %
100  checkN(complex([1,2],[1,2]));
101  checkN(complex(int32([1,2]),int32([1,2])));
102  checkN(sparse([1,2]));
103  %
104  function checkP(inpArray)
105  SData.alpha=inpArray;
106  self.xmlsave(self.fileName,SData);
107  end
108  function checkN(inpArray)
109  SData.alpha=inpArray; %#ok<STRNU>
110  self.runAndCheckError('self.xmlsave(self.fileName,SData);',...
111  ':wrongInput');
112 
113  end
114  end
115  function self = test_complexstructure(self)
116  import mxberry.xml.*;
117  Data(1)=mxberry.core.genteststruct(1);
118  Data(2)=mxberry.core.genteststruct(2);
119  self.xmlsave(self.fileName,Data);
120  SRes=xmlload(self.fileName);
121  self.verifyEqual(isequaln(SRes,Data),true);
122  delete(self.fileName);
123  %
124  Data=mxberry.core.genteststruct(1);
125  self.xmlsave(self.fileName,Data);
126  SRes=xmlload(self.fileName);
127  self.verifyEqual(isequaln(SRes,Data),true);
128  delete(self.fileName);
129  end
130  function self = test_complexstructure_backwardcompatibility(self)
131  import mxberry.xml.*;
132  import mxberry.test.TmpDataManager;
133  tmpDir=TmpDataManager.getDirByCallerKey();
134  file1ElemShortName='test_complexstructure_1elem.xml';
135  file1ElemName=[self.locDir,filesep,file1ElemShortName];
136  file1ElemTmpName=[tmpDir,filesep,file1ElemShortName];
137  fileArrayShortName='test_complexstructure_array.xml';
138  fileArrayName=[self.locDir,filesep,fileArrayShortName];
139  fileArrayTmpName=[tmpDir,filesep,fileArrayShortName];
140  %
141  SRes=xmlload(file1ElemName);
142  self.xmlsave(file1ElemTmpName,SRes);
143  SResTmp=xmlload(file1ElemTmpName);
144  self.verifyEqual(isequaln(SRes,SResTmp),true);
145  delete(file1ElemTmpName);
146  %
147  SRes=xmlload(fileArrayName);
148  self.xmlsave(fileArrayTmpName,SRes);
149  SResTmp=xmlload(fileArrayTmpName);
150  self.verifyEqual(isequaln(SRes,SResTmp),true);
151  delete(fileArrayTmpName);
152  end
153  function self = test_simple(self)
154  import mxberry.xml.*;
155  self.xmlsave(self.fileName,self.simpleData);
156  SRes=xmlload(self.fileName);
157  self.verifyEqual(isequaln(SRes,...
158  self.simpleData),true)
159  delete(self.fileName);
160  end
161  function self=test_simple_metadata(self)
162  import mxberry.xml.*;
163  self.xmlsave(self.fileName,self.simpleData,'on',self.simpleMetaData);
164  [SRes,SMetaData]=xmlload(self.fileName);
165  self.verifyTrue(isequaln(SRes,...
166  self.simpleData));
167  self.verifyTrue(isequal(SMetaData,...
168  self.simpleMetaData));
169  end
170  function self=test_simple_metadata_negative(self)
171  import mxberry.xml.*;
172  self.runAndCheckError(@runWithFailure,':wrongInput');
173  %
174  function runWithFailure()
175  metaData=self.simpleMetaData;
176  metaData.badParam=zeros(1,3);
177  self.xmlsave(self.fileName,self.simpleData,'on',metaData);
178  self.verifyEqual(true,false);
179  end
180  end
181  function self = test_parseFormatEmptyStruct(self)
182  import mxberry.xml.*;
183  self.verifyEqual(true,isequal(....
184  struct(),...
185  xmlparse(xmlformat(struct()),'on')));
186  self.verifyEqual(true,isequal(....
187  struct([]),...
188  xmlparse(xmlformat(struct([])),'on')));
189  end
190  end
191  methods
192  function xmlsave(self,filePath,data,varargin)
193  import mxberry.xml.*;
194  [reg1,prop1]=mxberry.core.parseparams(varargin,...
195  {'insertTimestamp'});
196  [reg2,prop2]=mxberry.core.parseparams(self.xmlsaveParams);
197  nReg1=numel(reg1);
198  nReg2=numel(reg2);
199  if nReg1<nReg2
200  reg=[reg1,reg2(nReg1+1:end)];
201  else
202  reg=reg1;
203  end
204  xmlsave(filePath,data,reg{:},prop1{:},prop2{:});
205  end
206  end
207 end
function filesep()
function xmlload(in fileName)
XMLLOAD loads XML file and converts it into Matlab structure or variable.
function genteststruct(in inpNum)
function repmat(in inpArray, in varargin)
function rmdir(in dirName, in sFlag)
RMDIR removes a directory (optionally recursively)
function xmlformat(in SData, in attSwitch, in name, in level, in SMetaData)
XMLFORMAT formats the variable V into a name-based tag XML string xstr.
function xmlsave(in fileName, in SData, in varargin)
XMLSAVE saves structure or variable(s) to a file using XML format.
function xmlparse(in inpXmlString, in attSwitch, in SData, in level)
XMLPARSE parses XML string str and returns matlab variable/structure. This is a non-validating parser...
function parseparams(in args, in propNameList, in nRegExpected, in nPropExpected)
PARSEPARAMS behaves exactly as a built-in Matlab function apart from the incorrect behavior of Matlab...