/** 
 * @file TestLFonHSGPersistent.cpp
 * @brief CppUnit-Testcase to create a LFonHSG-Wrapper around an LocalFeature-object. Checks whether this is storable and restorable again.
 * @author Alexander Freytag
 * @date 12-02-2014 ( dd-mm-yyyy )
*/

#ifdef NICE_USELIB_CPPUNIT

// STL includes

// NICE-core includes

// gp-hik-core includes

#include "TestLFonHSGPersistent.h"

using namespace std; //C basics
using namespace NICE;  // nice-core

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


CPPUNIT_TEST_SUITE_REGISTRATION( TestLFonHSGPersistent );

void TestLFonHSGPersistent::setUp() {
}

void TestLFonHSGPersistent::tearDown() {
}


void TestLFonHSGPersistent::testPersistentMethodsForLFonHSG()
{
  
  if (verboseStartEnd)
    std::cerr << "================== TestLFonHSGPersistent::testPersistentMethodsForLFonHSG ===================== " << std::endl;  
  
  NICE::Config conf;
  conf.sS( "GenericLFSelection", "localfeature_type", "LFonHSG" );
  conf.sS( "LFonHSG", "localfeature_type", "LocalFeatureSift" );
  OBJREC::LocalFeatureRepresentation  * lfrep;  
  
  lfrep = OBJREC::GenericLFSelection::selectLocalFeatureRep ( &conf, "GenericLFSelection");
  
  // TEST STORING ABILITIES
  if ( verbose )
    std::cerr << " TEST STORING ABILITIES FOR LFonHSG Wrapper" << std::endl;
  
  std::string s_destination_save ( "myLFonHSG.txt" );
  
  std::filebuf fbOut;
  fbOut.open ( s_destination_save.c_str(), ios::out );
  std::ostream os (&fbOut);
  //
  lfrep->store( os );
  //   
  fbOut.close();
  
  // TEST RESTORING ABILITIES
  if ( verbose )
    std::cerr << " TEST RESTORING ABILITIES FOR LFonHSG Wrapper" << std::endl;
    
  OBJREC::LocalFeatureRepresentation * lfrepRestore = NULL;  
      
  std::string s_destination_load ( "myLFonHSG.txt" );
  
  std::filebuf fbIn;
  fbIn.open ( s_destination_load.c_str(), ios::in );
  std::istream is (&fbIn);
  //
  OBJREC::GenericLFSelection::restoreLocalFeatureRep ( lfrepRestore, is );
  //   
  fbIn.close(); 
  
  // currently, we have no possibility to actually verify that the restore-operation was successfull, i.e.,
  // it returned an object identical to the stored one.
  // However, if we reached this point, at least something went right, so we should be happy...
  
  // final clean up -- don't waste memory
  if ( lfrep != NULL )
    delete lfrep;
    
  if ( lfrepRestore != NULL )
    delete lfrepRestore;  
  
    
  if (verboseStartEnd)
    std::cerr << "================== TestLFonHSGPersistent::testPersistentMethodsForLFonHSG done ===================== " << std::endl;  
  
}
#endif
