function testm(flag)
% testm( [flag] )
% Demonstrates how to use the m-packge and tests if the m-package works correctly
% if flag is 1, then the functions in the package are tested.
%
% Written by: tommsch, 2018

%#ok<*NOPRT>
%#ok<*ASGLU>
%#ok<*NASGU>

if(nargin>=1 && flag); selftestm; return; end;

fprintf('Either start the function with argument <1> or read the source code of this file.\n'); 
return; 
2; %#ok<UNRCH>

end

function selftestm
    fprintf('The m-package will be tested. All output on the screen should be followed by an explanation.\n');
    fprintf('If there is some error or un-commented output, the test fails.\n\n');

    if(~isequal(allm([1 1 2; 0 1 1; 1 1 1]),0)); fprintf('allm() is broken.\n\n'); end;;
    if(~isequal(anym([1 1 2; 0 1 1; 1 1 1]),1)); fprintf('anym() is broken.\n\n'); end;;
    
    if(norm([2 3 1]-sph2cartm(cart2sphm([2 3 1])))>10e-12); fprintf('sph2cartm()/cart2sphm() is broken.\n\n'); end;
    if(abs(norm(sph2cartm2(cart2sphm2([2 3 1])))-1)>10e-12); fprintf('sph2cartm()/cart2sphm() is broken.\n\n'); end;
    
    seq1=convm([1 2 1],[1; 3; 1],'mult',[2 1]);
    if(~isequal(seq1,[1 4 6 4 1;3 12 18 12 3;1 4 6 4 1])); fprintf('convm() is broken.\n\n'); end;
    
    basem=dec2basem([10; 1],[1 2; -1 2]);
    if(~isequal(basem,[1 1 2;1 1 1])); fprintf('dec2basem() is broken.\n\n'); end;
    
    ind2subm([2 1 3],[1 6]);
    
    isvectorval=[isvectorm(ones(1,1,3)) isvectorm(ones(0)) isvector(ones(1,1,3)) isvector(ones(0))];
    if(~isequal(isvectorval,[true false false false])); fprintf('isvectorm() is broken.\n\n'); end;
    
    val=kronm([1 2 3],[2; 3]);
    if(~isequal(val,[2 4 6; 3 6 9])); fprintf('kronm() is broken.\n\n'); end;
    
    if(~isequal(lcmm(10,15,20),60)); fprintf('lcmm() is broken.\n\n'); end;;
    
    if(~isequal(maxm([1 2],[2 3],[-4 4],[5 NaN]),[2 3])); fprintf('maxm() is broken.\n\n'); end;;
    
    if(~isequal(minm([1 2],[2 3],[-4 4],[5 NaN]),[1 2])); fprintf('minm() is broken.\n\n'); end;;
    
    if(~isequal(ndimsm([1; 2; 3]),1)); fprintf('ndimsm() is broken.\n\n'); end;;
    
    if(~isequal(onesm(3),[1;1;1])); fprintf('onesm() is broken.\n\n'); end;;
    
    val=padarraym([1 2],1,'pre');
    if(~isequal(val,[0 0 0;0 1 2])); fprintf('padarraym() is broken.\n\n'); end;;
    
    [v,arg]=parsem('tommsch',{'asd','tommsch',2},'is the best','set');
    if(~isequal(v,'is the best') || ~isequal(arg, {'asd', 'tommsch', 'is the best'})); vprintf('parsem() is broken.\n\n','cpr','err'); end;
    [v,arg]=parsem('tommsch',{'asd','tommsch',0,'tommsch'},'is the second best','condset');
    if(~isequal(v,1) || ~isequal(arg, {'asd', 'tommsch', 1})); vprintf('parsem() is broken.\n\n','cpr','err'); end;
    [v,arg]=parsem('tommsch',{'asd'});
    if(~isequal(v,0) || ~isequal(arg, {'asd'})); vprintf('parsem() is broken.\n\n','cpr','err'); end;

    figure;
    plotm(randn(1,40))
    fprintf('FIGURE: A figure should show vertical lines.\n\n'); 
    figure;
    plotm(randn(2,40),'.-');
    fprintf('FIGURE: A figure should show connected lines.\n\n'); 
    figure;
    plotm(randn(3,40))
    fprintf('FIGURE: A figure should show something 3-dimensional.\n\n'); 
    
    if(~isequal(repcellm(10 ,2),{10;10})); fprintf('repcellm() is broken.\n\n'); end;;
    if(~isequal(repcellm(10 ,[2 3]),{10 10 10;10 10 10})); fprintf('repcellm() is broken.\n\n'); end;;
    
    if(~isequal(repmatm(10 ,2),[10;10])); fprintf('repmatm() is broken.\n\n'); end;;
    if(~isequal(repmatm(10 ,[2 3]),[10 10 10;10 10 10])); fprintf('repmatm() is broken.\n\n'); end;;
    
    [a,b,c]=sizem(zeros(2,0,1));
    
    %sph2cartm % tested above
    %sph2cart2m % tested above
    
    if(~isequal(squeezem([2 1 3]),[2;1;3])); fprintf('squeezem() is broken.\n\n'); end;;
    
    if(~isequal(summ([2 3; -4 -5],[],'abs'),14)); fprintf('squeezem() is broken.\n\n'); end;;
    
    %testm %this function
    
    try
        [d,dmin]=upsamplem([1 2 ; 3 4], [0;0], [2 1; 0 -2],Inf);
        if(~isequal(d,[inf inf 1;2 inf inf;inf inf 3;4 inf inf]) || ~isequal(dmin,[0;-2])); fprintf('upsamplem() is broken.\n\n'); end;
    catch
        fprintf('''upsamplem'' broken. Probably tmisc package not installed.\n');
    end
    
    if(~isequal(zerosm(3),[0;0;0])); fprintf('zerosm() is broken.\n\n'); end;;
end


function dummy; end %#ok<DEFNU> %Generates an error, if the 'end' of a function is missing. 
