comparison toolboxes/xml_toolbox/tests/xml_tests.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function toolbox_test
2 % function toolbox_test
3 %
4 % This function is used to test the XML Toolbox for Matlab.
5 %
6
7
8 % Copyright (C) 2002-2005, University of Southampton
9 % Author: Dr Marc Molinari <m.molinari@soton.ac.uk>
10 % $Revision: 1.1 $ $Date: 2005/04/15 17:12:14 $ $Tag$
11
12
13 INFO = ver('Matlab');
14 VER = str2num(INFO.Version);
15
16 N=1; % test index (gets increased with every run)
17
18 % =========================================
19 comm = 'double';
20 c = -5.789;
21 dotest(c, N, comm); N=N+1; clear c;
22
23 comm='empty double';
24 c = [];
25 dotest(c, N, comm); N=N+1; clear c;
26
27 comm='double array';
28 c = [-10:0.75:10];
29 dotest(c, N, comm); N=N+1; clear c;
30
31 comm='double array 2 dim';
32 c = [-5:5; 10:20];
33 dotest(c, N, comm); N=N+1; clear c;
34
35 comm='double array 3 dim';
36 c(1,1,:) = [-10:10];
37 c(2,2,:) = [100:120];
38 c(3,3,:) = [-1.0:0.1:1];
39 dotest(c, N, comm); N=N+1; clear c;
40
41 comm='large double';
42 c = 999999999999999;
43 dotest(c, N, comm); N=N+1; clear c;
44
45 comm='small negative double';
46 c = -0.0000000000001;
47 dotest(c, N, comm); N=N+1; clear c;
48
49 % =========================================
50 comm='char';
51 c = 'z';
52 dotest(c, N, comm); N=N+1; clear c;
53
54 comm='empty char';
55 c = [''];
56 dotest(c, N, comm); N=N+1; clear c;
57
58 comm='single space';
59 c = [' '];
60 dotest(c, N, comm); N=N+1; clear c;
61
62 comm='several spaces';
63 c = [' '];
64 dotest(c, N, comm); N=N+1; clear c;
65
66 comm='non-xml characters, <&''"> with leading and trailing spaces';
67 c = [' <&''"> '];
68 dotest(c, N, comm); N=N+1; clear c;
69
70 comm='char array / string';
71 c = 'Hello World! Look out of the window';
72 dotest(c, N, comm); N=N+1; clear c;
73
74 comm='char array / string with leading+trailing space';
75 c = ' This has a leading and trailing space. ';
76 dotest(c, N, comm); N=N+1; clear c;
77
78 comm='funny ascii characters';
79 c = 'Funny chars: !$^&*()_+=-@#{}[];:,./\?';
80 dotest(c, N, comm); N=N+1; clear c;
81
82 comm='char array';
83 c = ['abcdefg'; ...
84 'hijklmn'];
85 dotest(c, N, comm); N=N+1; clear c;
86
87 % =========================================
88 comm='complex';
89 c = -7.14 + 2.03i;
90 dotest(c, N, comm); N=N+1; clear c;
91
92 comm='pure imaginary';
93 c = 8i;
94 dotest(c, N, comm); N=N+1; clear c;
95
96 comm='complex array';
97 a = [-10:0.75:10];
98 b = [10:-0.75:-10];
99 c = a+b*i;
100 dotest(c, N, comm); N=N+1; clear a b c;
101
102 comm='complex array 2 dim';
103 a = [-5:5; 10:20];
104 b = [-5:5; 10:20];
105 c = a+b*i;
106 dotest(c, N, comm); N=N+1; clear a b c;
107
108 % =========================================
109 comm='sparse';
110 c = sparse(4,5,1);
111 dotest(c, N, comm); N=N+1; clear c;
112
113 comm='empty sparse';
114 c = sparse(10,7,0);
115 dotest(c, N, comm); N=N+1; clear c;
116
117 comm='complex sparse';
118 c = sparse(20,40,4+2i);
119 dotest(c, N, comm); N=N+1; clear c;
120
121 % =========================================
122 comm='empty struct';
123 if (VER <= 5.3)
124 % Matlab up to V. 5.3 does not like empty structs
125 c = [];
126 else
127 c = struct([]);
128 end
129 dotest(c, N, comm); N=N+1; clear c;
130
131 comm='struct';
132 c.A = 1;
133 c.B = 2;
134 dotest(c, N, comm); N=N+1; clear c;
135
136 comm='struct with arrays';
137 c.A = [1 2 3 4 5 6];
138 c.B = [10 20; 30 40; 50 60; 70 80; 90 100];
139 c.C = [9 8 7 6 5 4 3 2 1]'; % transposed
140 dotest(c, N, comm); N=N+1; clear c;
141
142 comm='struct with chars';
143 c.A = 'a b c d e f g';
144 c.B = 'zz yy xx ww vv uu';
145 c.C = ['hippopotamus']'; % transposed
146 dotest(c, N, comm); N=N+1; clear c;
147
148 comm='struct with sparse';
149 c.A = sparse(100,100,42);
150 c.B = sparse(1,1,0);
151 c.C.s = sparse(eye(4));
152 dotest(c, N, comm); N=N+1; clear c;
153
154 comm='substructures';
155 c.A.a.b = 1;
156 c.A.b.c = 'cAbc';
157 c.B = [5 5 5 5];
158 if (VER <= 5.3)
159 % Matlab up to V. 5.3 does not like empty structs
160 c.C.elongated_name = [];
161 else
162 c.C.elongated_name = struct([]);
163 end
164 c.D.complex = [1+i 2+2i 3+3i];
165 dotest(c, N, comm); N=N+1; clear c;
166
167
168 % =========================================
169 comm='cell - empty double';
170 c = {[]};
171 dotest(c, N, comm); N=N+1; clear c;
172
173 comm='cell - empty char';
174 c = {''};
175 dotest(c, N, comm); N=N+1; clear c;
176
177 comm='cell - char with spaces only';
178 c = {' '};
179 dotest(c, N, comm); N=N+1; clear c;
180
181 comm='cell - empty double, char, double';
182 c = {[], '', []};
183 dotest(c, N, comm); N=N+1; clear c;
184
185 if (VER>5.3)
186 comm='cell - empty struct, double';
187 c = {struct([]), []};
188 dotest(c, N, comm); N=N+1; clear c;
189 end
190
191 comm='cell with 3 empty cells';
192 c = { {} {} {} };
193 dotest(c, N, comm); N=N+1; clear c;
194
195 comm='cell numeric';
196 c = {177};
197 dotest(c, N, comm); N=N+1; clear c;
198
199 comm='cell complex';
200 c = {101-99i};
201 dotest(c, N, comm); N=N+1; clear c;
202
203 comm='cell alphanumeric';
204 c = {'aabbccdd'};
205 dotest(c, N, comm); N=N+1; clear c;
206
207 comm='cell structure';
208 a.b.c = [1 2 3];
209 a.b.d = 'Hello World!';
210 c = {a};
211 dotest(c, N, comm); N=N+1; clear c a;
212
213 comm='cell containing empty char field';
214 c = {'the next one is empty', '', 'the previous one is empty'};
215 dotest(c, N, comm); N=N+1; clear c;
216
217 comm='cell containing empty double field';
218 c = {'the next one is empty', [], 'the previous one is empty'};
219 dotest(c, N, comm); N=N+1; clear c;
220
221 comm='cell mixed';
222 c = { 'abc', 987, 'def', 654, 'ghijklmno', 10000, 9999-i };
223 dotest(c, N, comm); N=N+1; clear c;
224
225 comm='cell of cells';
226 c = { {'abc', 987}, {'def', 654}, {'ghijklmno', 10000}, {-1-i} };
227 dotest(c, N, comm); N=N+1; clear c;
228
229 comm='array of cells';
230 c{1,1} = { {'abc', 987}, {'def', 654}, {'ghijklmno', 10000} };
231 c{2,1} = { 'second row', 22222222222, 0.9222i };
232 dotest(c, N, comm); N=N+1; clear c;
233
234 % =========================================
235 comm='combination of all types';
236 c(1,1).a = 9e-9;
237 c(1,1).b = 'aaa';
238 c(1,1).c = {'bbb', [10 20 30], 'ccccccccccc'};
239 c(1,1).d.e.f.g.h = [10 20 30; 40 50 60; 70 80 90; 100 110 120];
240 c(1,1).e = sparse([1 2 4 5], [1 2 4 5], [1 1 1 1]);
241 c(1,2).c = [22+33i; 44-55i; -66+77i; -88+99i];
242 c(2,2).a.x(2).y(3).z = 'this is cool';
243 c(2,2).b = 7e-7;
244 c(2,2).d.hello.world.hitchhiker.galaxy = 42;
245 c(2,2).e = { sparse(4,7,1), ' check this out with spaces ', pi };
246 dotest(c, N, comm); N=N+1; clear c;
247
248 return
249
250
251
252 % ========================================
253 % ========================================
254 function dotest(c, N, comm)
255 % c is variable, N is id number and comm is comment.
256
257 if nargin<3, comm=''; end
258
259 % name based routines:
260 tsave_n(N, c);
261 str_c = xml_format(c);
262 %x = tload_n(N);
263 % % %str_x = xml_var2xml(x);
264 % % str_x = xml_format(x);
265 % %
266 % % if (~strcmp( class(c), class(x)) | ...
267 % % ~strcmp( str_c, str_x ) )
268 % % disp(['Test ', num2str(N), ' (V.1.x) ***FAILED*** ("', comm, '") <=====================']);
269 % % return
270 % % else
271 % % disp(['Test ', num2str(N), ' (V.1.x) passed ("', comm, '")']);
272 % % end
273
274 % test format from previous versions:
275 % save in type-based (1.x) version
276 %tsave_t(N, c);
277 str_c = xml_format(c);
278 % load with name-based (2.0) version
279 x = tload_n(N);
280 str_x = xml_format(x);
281
282 if (~strcmp( class(c), class(x)) | ...
283 ~strcmp( str_c, str_x ) )
284 disp(['Test ', num2str(N), ' (V.2.x,3.x) ***FAILED*** ("', comm, '") <=====================']);
285 return
286 else
287 disp(['Test ', num2str(N), ' (V.2.x,3.x) passed ("', comm, '")']);
288 end
289
290 return
291
292
293 % ========================================
294 % ========================================
295 function tsave_t(N, c)
296 % saves variable c in file test_t_N.xml in old format of V.1.x
297 %xml_oldsave( ['test_t_', num2str(N), '.xml'], c );
298 str = xml_format_old(c);
299 fid = fopen(['test_t_', num2str(N), '.xml'], 'w');
300 fprintf( fid, '%s', str);
301 fclose(fid);
302
303 % ========================================
304 % ========================================
305 function tsave_n(N, c)
306 % saves variable c in file test_n_N.xml
307 % xml_save( ['test_n_', num2str(N), '.xml'], c );
308 str = xml_format(c);
309 fid = fopen(['test_n_', num2str(N), '.xml'], 'w');
310 fprintf( fid, '%s', str);
311 fclose(fid);
312
313 % % ========================================
314 % % ========================================
315 % function c = tload_t(N)
316 % % loads variable c in file test_t_N.xml
317 % c = xml_load( ['test_t_', num2str(N), '.xml'] );
318
319 % ========================================
320 % ========================================
321 function c = tload_n(N)
322 % loads variable c in file test_n_N.xml
323 str = fileread(['test_n_', num2str(N), '.xml']);
324 c = xml_parse(str);