/** 
 * @file TestGenericLocalFeatureSelectionPersistent.cpp
 * @brief CppUnit-Testcase to create a LocalFeatureRepresentation object via GenericLocalFeatureSelection, to store it, and to restore it 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 "TestGenericLocalFeatureSelectionPersistent.h"

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

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


CPPUNIT_TEST_SUITE_REGISTRATION( TestGenericLocalFeatureSelectionPersistent );

void TestGenericLocalFeatureSelectionPersistent::setUp() {
}

void TestGenericLocalFeatureSelectionPersistent::tearDown() {
}
void TestGenericLocalFeatureSelectionPersistent::testPersistentMethods()
{
  
  if (verboseStartEnd)
    std::cerr << "================== TestGenericLocalFeatureSelectionPersistent::testPersistentMethods ===================== " << std::endl;  
  
  NICE::Config conf;
  conf.sS( "GenericLocalFeatureSelection", "localfeature_type", "LocalFeatureSift" );
  OBJREC::LocalFeature  * lf;  
  
  lf = OBJREC::GenericLocalFeatureSelection::selectLocalFeature ( &conf, "GenericLocalFeatureSelection");
  
  // TEST STORING ABILITIES
  if ( verbose )
    std::cerr << " TEST STORING ABILITIES FOR STANDARD LOCALFEATURE" << std::endl;
  
  std::string s_destination_save ( "myLocalFeatureSelection.txt" );
  
  std::filebuf fbOut;
  fbOut.open ( s_destination_save.c_str(), ios::out );
  std::ostream os (&fbOut);
  //
  lf->store( os );
  //   
  fbOut.close();
  
  // TEST RESTORING ABILITIES
  if ( verbose )
    std::cerr << " TEST RESTORING ABILITIES FOR STANDARD LOCALFEATURE" << std::endl;
    
  OBJREC::LocalFeature * lfRestore = NULL;  
      
  std::string s_destination_load ( "myLocalFeatureSelection.txt" );
  
  std::filebuf fbIn;
  fbIn.open ( s_destination_load.c_str(), ios::in );
  std::istream is (&fbIn);
  //
  OBJREC::GenericLocalFeatureSelection::restoreLocalFeature ( lfRestore, 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 ( lf != NULL )
    delete lf;
    
  if ( lfRestore != NULL )
    delete lfRestore;  
  
    
  if (verboseStartEnd)
    std::cerr << "================== TestGenericLocalFeatureSelectionPersistent::testPersistentMethods done ===================== " << std::endl;  
  
}

#endif
