function testtmisc(flag)
% testt( [flag] )
% Tests the package tmisc
% 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); selftesttmisc; return; end;

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

end

function selftesttmisc
    vprintf('tmisc package will be tested. All output on the screen should be followed by an explanation.\n','cpr',[.1 .4 .1]);
    vprintf('If there is some error or un-commented output, the test fails.\n\n','cpr',[.1 .4 .1]);
    
    cprintf('_green','This should be ugly green text.\n\n');
    
    val=flatten({{2 3 {4; 5} 6} {{{7}}}});
    if(~isequal(val, {2 3 4 5 6 7})); vprintf('flatten() is broken.\n\n','cpr','err'); end;    
    
    G=[1 2;2 5; 5 6; 2 3; 3 4; 4 7; 4 8; 8 9];
    val=grCenter(G);
    if(~isequal(val, 3)); vprintf('grCenter() is broken.\n\n','cpr','err'); end;    
    
    G=[1 2; 2 3; 3 4; 2 5];
    G=digraph(G(:,1),G(:,2));
    val=grVerCover(G);
    if(numel(val)~=3 || ~searchincellarray([1 3 5],val,1) || ~searchincellarray([2 3],val,1) || ~searchincellarray([2 4],val,1)); vprintf('grVerCover() is broken.\n\n','cpr','err'); end;    
    
    identifymatrix({[1 2 3; 0 2 1; 0 0 1],[2]});
    
    intersectspace([1 -1 0 0; 1 0 -2 1; 0 1 -2 -1]',[1 1 0 0; 1 0 -1 0]');
    
    if(~isequal(issquare(rand(3,3,3)),true)); vprintf('issquare() is broken.\n\n','cpr','err'); end;
    
    if(~isequal(issym([sym('23.2') 0]),true)); vprintf('issym() is broken.\n\n','cpr','err'); end;
    
    if(~isequal(iswholenumber([2 2.1]),[true false])); vprintf('iswholenumber() is broken.\n\n','cpr','err'); end;
   
    val=lexicographic( [0 1 0; 0 1 2; 3 1 1] ,2);
    if(~isequal(val,[1 0 0;1 2 0;1 1 3])); vprintf('lexicographic() is broken.\n\n','cpr','err'); end;
    
    val=sortrows(mixvector(0:1, 2)')';
    if(~isequal(val,sortrows([0 0 1 1;0 1 0 1]')')); vprintf('mixvector() is broken.\n\n','cpr','err'); end;
    
    val=nestedcellfun(@(x) ndimsm(x),{{[1 2 1]},[1 ;2 ]},'UniformOutput',false);
    if(~isequal(val,{{2},1})); vprintf('nestedcellfun() is broken.\n\n','cpr','err'); end;
    
    val=nondiag({[1 2] [2 3] ;[1] [40] },1);
    if(~isequal(val,{[1 2] []; 1 40})); vprintf('nondiag() is broken.\n\n','cpr','err'); end;
    
    val=normalizematrix(randn(4),'colsum');
    if(norm(sum(val,1)-1)>10e-12); vprintf('normalizematrix() is broken.\n\n','cpr','err'); end;
    
    vprintf('This should be colored text.\n\n','cpr',num2color(randi(1000),100));
        
    val=removezero([0 0 0 0;0 1 0 0;0 0 0 1],'border');
    if(~isequal(val,[1 0 0;0 0 1])); vprintf('removezero() is broken.\n\n','cpr','err'); end;
    
    if(~isequal(repcell(10 ,2),{10 10;10 10})); vprintf('repcell() is broken.\n\n','cpr','err'); end;
    
    val=savetocellarray([1 2 3],logical([1 1 0 0 1 ]),{[-1 -2], [-3 -4 -5]});
    if(~isequal(val,{[1 2], [-3 -4 3]})); vprintf('savetocellarray() is broken.\n\n','cpr','err'); end;
    
    [found, ioo]=searchincellarray([1 2 3 4],{[1 2; 3 4],[1 2 3 4],[2 3 1]',[5]}, 0);
    if(~isequal(found,true) || ~isequal(ioo,2)); vprintf('searchincellarray() is broken.\n\n','cpr','err'); end;
    
    val=setplus([1 0; 1 1]',[10 10; 20 20; 30 30]');
    if(~isequal(val,[11 11 21 21 31 31;10 11 20 21 30 31])); vprintf('setplus() is broken.\n\n','cpr','err'); end;    
    
    val=subsco([1 2 3; 4 5 6],[1 2; 2 3]');
    if(~isequal(val,[2 6])); vprintf('subsco() is broken.\n\n','cpr','err'); end;
    val=subsco([1 2 3; 4 5 6],[1 2; 2 3]',[-1 -1]);
    if(~isequal(val,[1 -1 3;4 5 -1])); vprintf('subsco() is broken.\n\n','cpr','err'); end;
    
    val=tbuildproduct({[0.8 0.2; 0.2 0.8],[0.4 0.6;0.4 0.6]}, {[],[1 2]});
    if(norm(val-[.44 .56;.44 .56])>10e-12); errorstring=vprintf('tbuildproduct() is broken.\n\n','cpr','err','str',errorstring); end;
    
    %testt %test function for everything.
    
    %testtmisc %this file
    
    val=[tif(true, 1, 2) tif(false, 1, 2)];
    if(~isequal(val,[1 2])); vprintf('tif() is broken.\n\n','cpr','err'); end;
    
    C={[2 3 4] [5 ; 5 ; 5 ] {3 7; 4 6}; 40 50 {60  80}};
    Cf=flatten(C);
    D=unflatten(Cf,C);
    if(~isequal(C,D)); vprintf('tif() is broken.\n\n','cpr','err'); end;
    val=unflatten(10,{1 2 3},'assign');
    if(~isequal(val,{10 10 10})); vprintf('unflatten() is broken.\n\n','cpr','err'); end;
    
    val=uniquecell({1 2 1 [1 2] [1 3] [1 2]})';
    if(numel(val)~=4); vprintf('uniquecell() is broken.\n\n','cpr','err'); end;
    
    val=vdisp({[1 2 3]});
    if(~ischar(val) || ~strfind(val,'1') || ~strfind(val,'2') || ~strfind(val,'3'));  vprintf('vdisp() is broken.\n\n','cpr','err'); end; %#ok<STRIFCND>
    
    %vprintf is already called often enough
end

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