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