annotate toolboxes/xml_toolbox/tests/xml_tests.m @ 0:cc4b1211e677 tip

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