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