/** 
 * @file TestGenericClusterAlgoSelectionPersistent.cpp
 * @brief CppUnit-Testcase to create a ClusterAlgoRepresentation object via GenericClusterAlgoSelection, 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 "TestGenericClusterAlgorithmSelectionPersistent.h"

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

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


CPPUNIT_TEST_SUITE_REGISTRATION( TestGenericClusterAlgoSelectionPersistent );

void TestGenericClusterAlgoSelectionPersistent::setUp() {
}

void TestGenericClusterAlgoSelectionPersistent::tearDown() {
}
void TestGenericClusterAlgoSelectionPersistent::testPersistentMethods()
{
  
  if (verboseStartEnd)
    std::cerr << "================== TestGenericClusterAlgoSelectionPersistent::testPersistentMethods ===================== " << std::endl;  
  
  NICE::Config conf;
  conf.sS( "GenericClusterAlgoSelection", "clusterTechnique", "KMeans" );
  OBJREC::ClusterAlgorithm  * cluster;  
  
  cluster = OBJREC::GenericClusterAlgorithmSelection::selectClusterAlgorithm ( &conf, "GenericClusterAlgoSelection");
  
  // TEST STORING ABILITIES
  if ( verbose )
    std::cerr << " TEST STORING ABILITIES FOR STANDARD LOCALFEATURE" << std::endl;
  
  std::string s_destination_save ( "myClusterAlgoSelection.txt" );
  
  std::filebuf fbOut;
  fbOut.open ( s_destination_save.c_str(), ios::out );
  std::ostream os (&fbOut);
  //
  cluster->store( os );
  //   
  fbOut.close();
  
  // TEST RESTORING ABILITIES
  if ( verbose )
    std::cerr << " TEST RESTORING ABILITIES FOR STANDARD LOCALFEATURE" << std::endl;
    
  OBJREC::ClusterAlgorithm * clusterRestore = NULL;  
      
  std::string s_destination_load ( "myClusterAlgoSelection.txt" );
  
  std::filebuf fbIn;
  fbIn.open ( s_destination_load.c_str(), ios::in );
  std::istream is (&fbIn);
  //
  OBJREC::GenericClusterAlgorithmSelection::restoreClusterAlgorithm ( clusterRestore, 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 ( cluster != NULL )
    delete cluster;
    
  if ( clusterRestore != NULL )
    delete clusterRestore;
  
  if (verboseStartEnd)
    std::cerr << "================== TestGenericClusterAlgoSelectionPersistent::testPersistentMethods done ===================== " << std::endl;  
  
}

#endif