#ifdef NICE_USELIB_CPPUNIT

#include <string>
#include <exception>
#include <iostream>
#include <fstream>

//----------

#include "TestVectorFeature.h"

#include "vislearning/cbaselib/FeaturePool.h"
#include "../VectorFeature.h"

const bool verbose = true;
const bool verboseStartEnd = true;

using namespace OBJREC;
using namespace NICE;
using namespace std;

CPPUNIT_TEST_SUITE_REGISTRATION( TestVectorFeature );

void TestVectorFeature::setUp() {
}

void TestVectorFeature::tearDown() {
}
void TestVectorFeature::testVectorFeature() 
{
    if (verboseStartEnd)
        std::cerr << "================== TestVectorFeature::testVectorFeature ===================== " << std::endl;

    Matrix mX;
    Vector vY;
    Vector vY_multi;

    ifstream ifs ("toyExample1.data", ios::in);
    //   ifstream ifs ("toyExampleLargeScale.data", ios::in);
    //   ifstream ifs ("toyExampleLargeLargeScale.data", ios::in);
    CPPUNIT_ASSERT ( ifs.good() );
    ifs >> mX;
    ifs >> vY;
    ifs >> vY_multi;
    ifs.close();

    if (verbose)
    {
        std::cerr << "data loaded: mX" << std::endl;
        std::cerr << mX << std::endl;
        std::cerr << "vY: " << std::endl;
        std::cerr << vY << std::endl;
        std::cerr << "vY_multi: " << std::endl;
        std::cerr << vY_multi << std::endl;
    }

    int iNumFeatureDimension = mX.cols();

    FeaturePool fp;
    VectorFeature *pVecFeature = new VectorFeature(iNumFeatureDimension);
    pVecFeature->explode(fp);


    // memory layout needs to be transposed into rows x column: features x samples
    // features must lay next to each other in memory, so that each feature vector can
    // be adressed by a starting pointer and the number of feature dimensions to come.
    Matrix mX_transposed = mX.transpose();

    Examples examples;

    bool bSuccess = Examples::wrapExamplesAroundFeatureMatrix(mX_transposed, vY, examples);
    CPPUNIT_ASSERT( bSuccess );

    CPPUNIT_ASSERT( examples.size() == mX.rows() );

    for(int i=0; i< examples.size(); i++)
    {
        Example &t_Example = examples[i].second;
        NICE::Vector    t_FeatVector;
        fp.calcFeatureVector(t_Example, t_FeatVector);
        std::cerr << "Example " << i << " Features: " <<t_FeatVector << std::endl;

        for(int f=0; f< iNumFeatureDimension;f++)
        {
            double t_f1 = t_FeatVector[f];
            double t_f2 = mX(i,f);
            CPPUNIT_ASSERT_DOUBLES_EQUAL( t_f1, t_f2, 0.001f );
        }
    }
    examples.clean();
    delete pVecFeature;

    if (verboseStartEnd)
        std::cerr << "================== TestVectorFeature::TestVectorFeature done ===================== " << std::endl;

}


#endif
