function setupm(flag)
% setupm( [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');

    vprintf('allm\n');
    if(~isequal(allm([1 1 2; 0 1 1; 1 1 1]),0)); fprintf('ERROR: allm() is broken.\n\n'); end;;
    if(~isequal(anym([1 1 2; 0 1 1; 1 1 1]),1)); fprintf('ERROR: anym() is broken.\n\n'); end;;
    
    vprintf('sph2cartm...\n');
    if(norm([2 3 1]-sph2cartm(cart2sphm([2 3 1])))>10e-12); fprintf('ERROR: sph2cartm()/cart2sphm() is broken.\n\n'); end;
    if(abs(norm(sph2cartm2(cart2sphm2([2 3 1])))-1)>10e-12); fprintf('ERROR: sph2cartm()/cart2sphm() is broken.\n\n'); end;
    
    vprintf('convm\n');
    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('ERROR: convm() is broken.\n\n'); end;
    
    vprintf('basem\n');
    basem=dec2basem([10; 1],[1 2; -1 2]);
    if(~isequal(basem,[1 1 2;1 1 1])); fprintf('ERROR: dec2basem() is broken.\n\n'); end;
    
    vprintf('factorialm\n');
    if(~isequal(factorialm([3 4]),144)); fprintf('ERROR: factorialm() is broken.\n\n'); end;
    
    vprintf('ind2subm\n');
    ind2subm([2 1 3],[1 6]);
    
    vprintf('isvectorm\n');
    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('ERROR: isvectorm() is broken.\n\n'); end;
    
    vprintf('kronm\n');
    val=kronm([1 2 3],[2; 3]);
    if(~isequal(val,[2 4 6; 3 6 9])); fprintf('ERROR: kronm() is broken.\n\n'); end;
    
    vprintf('lcmm\n');
    if(~isequal(lcmm(10,15,20),60)); fprintf('ERROR: lcmm() is broken.\n\n'); end;;
    
    vprintf('maxm\n');
    if(~isequal(maxm([1 2 2; 2 3 4; 2 1 3;4 5 NaN]),5)); fprintf('ERROR: maxm() is broken.\n\n'); end;;
    
    vprintf('minm\n');
    if(~isequal(minm([1 2 2; 2 3 4; 2 1 3;4 5 NaN]),1)); fprintf('ERROR: minm() is broken.\n\n'); end;;
    
    vprintf('nchoosekm\n');
    if(~isequal(nchoosekm([10 5],[5 2]),2520)); fprintf('ERROR: nchoosekm() is broken.\n\n'); end;;
    
    vprintf('ndimsm\n');
    if(~isequal(ndimsm([1; 2; 3]),1)); fprintf('ERROR: ndimsm() is broken.\n\n'); end;;
    
    vprintf('onesm\n');
    if(~isequal(onesm(3),[1;1;1])); fprintf('ERROR: onesm() is broken.\n\n'); end;;
    
    vprintf('padarraym\n');
    val=padarraym([1 2],1,'pre');
    if(~isequal(val,[0 0 0;0 1 2])); fprintf('ERROR: padarraym() is broken.\n\n'); end;;
    
    vprintf('parsem\n');
    [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('ERROR: 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('ERROR: parsem() is broken.\n\n','cpr','err'); end;
    [v,arg]=parsem('tommsch',{'asd'});
    if(~isequal(v,0) || ~isequal(arg, {'asd'})); vprintf('ERROR: parsem() is broken.\n\n','cpr','err'); end;

    vprintf('plotm\n');
    figure;
    plotm(randn(1,40))
    title('Some straight lines or points');
    fprintf('FIGURE.\n'); 
    figure;
    plotm(randn(2,40),'.-');
    title('Some lines.');
    fprintf('FIGURE.\n'); 
    figure;
    plotm(randn(3,40),'resolution',0,'MarkerSize',100)
    title('A pointcloud.');
    fprintf('FIGURE.\n'); 
    
    vprintf('repcellm\n');
    if(~isequal(repcellm(10 ,2),{10;10})); fprintf('ERROR: repcellm() is broken.\n\n'); end;;
    if(~isequal(repcellm(10 ,[2 3]),{10 10 10;10 10 10})); fprintf('ERROR: repcellm() is broken.\n\n'); end;;
    
    vprintf('repmatm\n');
    if(~isequal(repmatm(10 ,2),[10;10])); fprintf('ERROR: repmatm() is broken.\n\n'); end;;
    if(~isequal(repmatm(10 ,[2 3]),[10 10 10;10 10 10])); fprintf('ERROR: repmatm() is broken.\n\n'); end;;
    
    vprintf('sizem\n');
    [a,b,c]=sizem(zeros(2,0,1));
    
    %sph2cartm % tested above
    %sph2cart2m % tested above
    
    vprintf('squeezem\n');
    if(~isequal(squeezem([2 1 3]),[2;1;3])); fprintf('ERROR: squeezem() is broken.\n\n'); end;;
    
    vprintf('summ\n');
    if(~isequal(summ([2 3; -4 -5],[],'abs'),14)); fprintf('ERROR: squeezem() is broken.\n\n'); end;;
    
    %testm %this function
    
    vprintf('upsamplem\n');
    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('ERROR: upsamplem() is broken.\n\n'); end;
    catch
        fprintf('ERROR: ''upsamplem'' broken. Probably tmisc package not installed.\n');
    end
    
    if(~isequal(zerosm(3),[0;0;0])); fprintf('ERROR: zerosm() is broken.\n\n'); end;;
end


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