wolffd@0: function toolbox_test wolffd@0: % function toolbox_test wolffd@0: % wolffd@0: % This function is used to test the XML Toolbox for Matlab. wolffd@0: % wolffd@0: wolffd@0: wolffd@0: % Copyright (C) 2002-2005, University of Southampton wolffd@0: % Author: Dr Marc Molinari wolffd@0: % $Revision: 1.1 $ $Date: 2005/04/15 17:12:14 $ $Tag$ wolffd@0: wolffd@0: wolffd@0: INFO = ver('Matlab'); wolffd@0: VER = str2num(INFO.Version); wolffd@0: wolffd@0: N=1; % test index (gets increased with every run) wolffd@0: wolffd@0: % ========================================= wolffd@0: comm = 'double'; wolffd@0: c = -5.789; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='empty double'; wolffd@0: c = []; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='double array'; wolffd@0: c = [-10:0.75:10]; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='double array 2 dim'; wolffd@0: c = [-5:5; 10:20]; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='double array 3 dim'; wolffd@0: c(1,1,:) = [-10:10]; wolffd@0: c(2,2,:) = [100:120]; wolffd@0: c(3,3,:) = [-1.0:0.1:1]; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='large double'; wolffd@0: c = 999999999999999; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='small negative double'; wolffd@0: c = -0.0000000000001; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: % ========================================= wolffd@0: comm='char'; wolffd@0: c = 'z'; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='empty char'; wolffd@0: c = ['']; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='single space'; wolffd@0: c = [' ']; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='several spaces'; wolffd@0: c = [' ']; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='non-xml characters, <&''"> with leading and trailing spaces'; wolffd@0: c = [' <&''"> ']; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='char array / string'; wolffd@0: c = 'Hello World! Look out of the window'; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='char array / string with leading+trailing space'; wolffd@0: c = ' This has a leading and trailing space. '; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='funny ascii characters'; wolffd@0: c = 'Funny chars: !$^&*()_+=-@#{}[];:,./\?'; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='char array'; wolffd@0: c = ['abcdefg'; ... wolffd@0: 'hijklmn']; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: % ========================================= wolffd@0: comm='complex'; wolffd@0: c = -7.14 + 2.03i; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='pure imaginary'; wolffd@0: c = 8i; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='complex array'; wolffd@0: a = [-10:0.75:10]; wolffd@0: b = [10:-0.75:-10]; wolffd@0: c = a+b*i; wolffd@0: dotest(c, N, comm); N=N+1; clear a b c; wolffd@0: wolffd@0: comm='complex array 2 dim'; wolffd@0: a = [-5:5; 10:20]; wolffd@0: b = [-5:5; 10:20]; wolffd@0: c = a+b*i; wolffd@0: dotest(c, N, comm); N=N+1; clear a b c; wolffd@0: wolffd@0: % ========================================= wolffd@0: comm='sparse'; wolffd@0: c = sparse(4,5,1); wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='empty sparse'; wolffd@0: c = sparse(10,7,0); wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='complex sparse'; wolffd@0: c = sparse(20,40,4+2i); wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: % ========================================= wolffd@0: comm='empty struct'; wolffd@0: if (VER <= 5.3) wolffd@0: % Matlab up to V. 5.3 does not like empty structs wolffd@0: c = []; wolffd@0: else wolffd@0: c = struct([]); wolffd@0: end wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='struct'; wolffd@0: c.A = 1; wolffd@0: c.B = 2; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='struct with arrays'; wolffd@0: c.A = [1 2 3 4 5 6]; wolffd@0: c.B = [10 20; 30 40; 50 60; 70 80; 90 100]; wolffd@0: c.C = [9 8 7 6 5 4 3 2 1]'; % transposed wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='struct with chars'; wolffd@0: c.A = 'a b c d e f g'; wolffd@0: c.B = 'zz yy xx ww vv uu'; wolffd@0: c.C = ['hippopotamus']'; % transposed wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='struct with sparse'; wolffd@0: c.A = sparse(100,100,42); wolffd@0: c.B = sparse(1,1,0); wolffd@0: c.C.s = sparse(eye(4)); wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='substructures'; wolffd@0: c.A.a.b = 1; wolffd@0: c.A.b.c = 'cAbc'; wolffd@0: c.B = [5 5 5 5]; wolffd@0: if (VER <= 5.3) wolffd@0: % Matlab up to V. 5.3 does not like empty structs wolffd@0: c.C.elongated_name = []; wolffd@0: else wolffd@0: c.C.elongated_name = struct([]); wolffd@0: end wolffd@0: c.D.complex = [1+i 2+2i 3+3i]; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: wolffd@0: % ========================================= wolffd@0: comm='cell - empty double'; wolffd@0: c = {[]}; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='cell - empty char'; wolffd@0: c = {''}; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='cell - char with spaces only'; wolffd@0: c = {' '}; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='cell - empty double, char, double'; wolffd@0: c = {[], '', []}; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: if (VER>5.3) wolffd@0: comm='cell - empty struct, double'; wolffd@0: c = {struct([]), []}; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: end wolffd@0: wolffd@0: comm='cell with 3 empty cells'; wolffd@0: c = { {} {} {} }; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='cell numeric'; wolffd@0: c = {177}; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='cell complex'; wolffd@0: c = {101-99i}; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='cell alphanumeric'; wolffd@0: c = {'aabbccdd'}; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='cell structure'; wolffd@0: a.b.c = [1 2 3]; wolffd@0: a.b.d = 'Hello World!'; wolffd@0: c = {a}; wolffd@0: dotest(c, N, comm); N=N+1; clear c a; wolffd@0: wolffd@0: comm='cell containing empty char field'; wolffd@0: c = {'the next one is empty', '', 'the previous one is empty'}; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='cell containing empty double field'; wolffd@0: c = {'the next one is empty', [], 'the previous one is empty'}; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='cell mixed'; wolffd@0: c = { 'abc', 987, 'def', 654, 'ghijklmno', 10000, 9999-i }; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='cell of cells'; wolffd@0: c = { {'abc', 987}, {'def', 654}, {'ghijklmno', 10000}, {-1-i} }; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: comm='array of cells'; wolffd@0: c{1,1} = { {'abc', 987}, {'def', 654}, {'ghijklmno', 10000} }; wolffd@0: c{2,1} = { 'second row', 22222222222, 0.9222i }; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: % ========================================= wolffd@0: comm='combination of all types'; wolffd@0: c(1,1).a = 9e-9; wolffd@0: c(1,1).b = 'aaa'; wolffd@0: c(1,1).c = {'bbb', [10 20 30], 'ccccccccccc'}; wolffd@0: c(1,1).d.e.f.g.h = [10 20 30; 40 50 60; 70 80 90; 100 110 120]; wolffd@0: c(1,1).e = sparse([1 2 4 5], [1 2 4 5], [1 1 1 1]); wolffd@0: c(1,2).c = [22+33i; 44-55i; -66+77i; -88+99i]; wolffd@0: c(2,2).a.x(2).y(3).z = 'this is cool'; wolffd@0: c(2,2).b = 7e-7; wolffd@0: c(2,2).d.hello.world.hitchhiker.galaxy = 42; wolffd@0: c(2,2).e = { sparse(4,7,1), ' check this out with spaces ', pi }; wolffd@0: dotest(c, N, comm); N=N+1; clear c; wolffd@0: wolffd@0: return wolffd@0: wolffd@0: wolffd@0: wolffd@0: % ======================================== wolffd@0: % ======================================== wolffd@0: function dotest(c, N, comm) wolffd@0: % c is variable, N is id number and comm is comment. wolffd@0: wolffd@0: if nargin<3, comm=''; end wolffd@0: wolffd@0: % name based routines: wolffd@0: tsave_n(N, c); wolffd@0: str_c = xml_format(c); wolffd@0: %x = tload_n(N); wolffd@0: % % %str_x = xml_var2xml(x); wolffd@0: % % str_x = xml_format(x); wolffd@0: % % wolffd@0: % % if (~strcmp( class(c), class(x)) | ... wolffd@0: % % ~strcmp( str_c, str_x ) ) wolffd@0: % % disp(['Test ', num2str(N), ' (V.1.x) ***FAILED*** ("', comm, '") <=====================']); wolffd@0: % % return wolffd@0: % % else wolffd@0: % % disp(['Test ', num2str(N), ' (V.1.x) passed ("', comm, '")']); wolffd@0: % % end wolffd@0: wolffd@0: % test format from previous versions: wolffd@0: % save in type-based (1.x) version wolffd@0: %tsave_t(N, c); wolffd@0: str_c = xml_format(c); wolffd@0: % load with name-based (2.0) version wolffd@0: x = tload_n(N); wolffd@0: str_x = xml_format(x); wolffd@0: wolffd@0: if (~strcmp( class(c), class(x)) | ... wolffd@0: ~strcmp( str_c, str_x ) ) wolffd@0: disp(['Test ', num2str(N), ' (V.2.x,3.x) ***FAILED*** ("', comm, '") <=====================']); wolffd@0: return wolffd@0: else wolffd@0: disp(['Test ', num2str(N), ' (V.2.x,3.x) passed ("', comm, '")']); wolffd@0: end wolffd@0: wolffd@0: return wolffd@0: wolffd@0: wolffd@0: % ======================================== wolffd@0: % ======================================== wolffd@0: function tsave_t(N, c) wolffd@0: % saves variable c in file test_t_N.xml in old format of V.1.x wolffd@0: %xml_oldsave( ['test_t_', num2str(N), '.xml'], c ); wolffd@0: str = xml_format_old(c); wolffd@0: fid = fopen(['test_t_', num2str(N), '.xml'], 'w'); wolffd@0: fprintf( fid, '%s', str); wolffd@0: fclose(fid); wolffd@0: wolffd@0: % ======================================== wolffd@0: % ======================================== wolffd@0: function tsave_n(N, c) wolffd@0: % saves variable c in file test_n_N.xml wolffd@0: % xml_save( ['test_n_', num2str(N), '.xml'], c ); wolffd@0: str = xml_format(c); wolffd@0: fid = fopen(['test_n_', num2str(N), '.xml'], 'w'); wolffd@0: fprintf( fid, '%s', str); wolffd@0: fclose(fid); wolffd@0: wolffd@0: % % ======================================== wolffd@0: % % ======================================== wolffd@0: % function c = tload_t(N) wolffd@0: % % loads variable c in file test_t_N.xml wolffd@0: % c = xml_load( ['test_t_', num2str(N), '.xml'] ); wolffd@0: wolffd@0: % ======================================== wolffd@0: % ======================================== wolffd@0: function c = tload_n(N) wolffd@0: % loads variable c in file test_n_N.xml wolffd@0: str = fileread(['test_n_', num2str(N), '.xml']); wolffd@0: c = xml_parse(str);