% brief:    Unit testing of the CodebookRandomForest functions (ERC)
% author:   Johannes Ruehle
% date:     11-04-2014 (dd-mm-yyyy)

%% test Creation of OBJREC::Examples class from sample matrix and label data

    numSamples  = 10000;
    numFeatures = 5;
    maxClass    = 3;

    matFeatures = rand(numSamples, numFeatures, 'double')';
    matLabels = randi(maxClass, numSamples,1,'double');
%%  create and train
    hClassifier = CodebookRandomForestMex('createAndTrain',... 
                            matFeatures, matLabels,...
                            'conf', 'config.conf');
%%  calcClassDistributionPerSample
    bSucceess = CodebookRandomForestMex('calcClassDistributionPerSample',... 
                                        hClassifier,...
                                        matFeatures,'verbose',true );
    assert(bSucceess);
%%  generateHistogram
    matHistogram = CodebookRandomForestMex('generateHistogram',... 
                                        hClassifier,...
                                        matFeatures, 'verbose',false);
    
%%  store
    bSuccess = CodebookRandomForestMex('storeToFile',... 
                                        hClassifier,...
                                        'codebookrf.stored.txt');    
%%  restore
    hRestoredClassifier = CodebookRandomForestMex('restoreFromFile',... 
                                            'codebookrf.stored.txt');
    assert( ~(hRestoredClassifier == false) );
    matHistogramNew = CodebookRandomForestMex('generateHistogram',... 
                                        hRestoredClassifier,...
                                        matFeatures, 'verbose',false);
    d = matHistogramNew-matHistogram;
    assert( sum(d(:)) == 0 ); % histogram are alike
%%    
    CodebookRandomForestMex('delete', hClassifier);
    CodebookRandomForestMex('delete', hRestoredClassifier);
%catch ecpn
%    disp( ecpn );
%end