// file: $isip/class/sp/Component/cmp_02.cc
// version: $Id: cmp_02.cc 8236 2002-06-26 19:06:05Z gao $
//

// isip include files
//
#include <Console.h>
#include "Component.h"
#include <typeinfo>

#include <FourierTransform.h>
#include <FilterBank.h>
#include <Energy.h>
#include <Cepstrum.h>
#include <Window.h>

// method: diagnose
//
// arguments:
//  Integral::DEBUG level: (input) debug level for diagnostics
//
// return: a boolean value indicating status
//
boolean Component::diagnose(Integral::DEBUG level_a) {

  //---------------------------------------------------------------------
  //
  // 0. preliminaries
  //
  //---------------------------------------------------------------------

  // output the class name
  //
  if (level_a > Integral::NONE) {
    String output(L"diagnosing class ");
    output.concat(CLASS_NAME);
    output.concat(L": ");
    Console::put(output);
    Console::increaseIndention();
  } 

  //--------------------------------------------------------------------
  //
  // 1. required public methods
  //
  //--------------------------------------------------------------------

  // set indentation
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing required public methods...\n");
    Console::increaseIndention();
  }

  // we need binary and text sof files
  //
  String tmp_filename0;
  Integral::makeTemp(tmp_filename0);
  String tmp_filename1;
  Integral::makeTemp(tmp_filename1);

  // open files in write mode
  //
  Sof tmp_file0;
  tmp_file0.open(tmp_filename0, File::WRITE_ONLY, File::TEXT);
  Sof tmp_file1;
  tmp_file1.open(tmp_filename1, File::WRITE_ONLY, File::BINARY);

  Component rcp1;
  Algorithm algo;
  algo.setType(Algorithm::FILTER);
  rcp1.setAlgorithm(algo);
  
  rcp1.write(tmp_file0, (long)0);
  rcp1.write(tmp_file1, (long)0);
  
  // close the files
  //
  tmp_file0.close();
  tmp_file1.close();

  // open the files in read mode
  //
  tmp_file0.open(tmp_filename0);
  tmp_file1.open(tmp_filename1);

  // read the value back
  //

  Component read_component;

  read_component.read(tmp_file0, 0);
  if (!read_component.eq(rcp1)) {
    read_component.debug(L"read text");
    rcp1.debug(L"expected");
    return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__);
  }

  read_component.read(tmp_file1, 0);
  if (!read_component.eq(rcp1)) {
    read_component.debug(L"read bin");
    rcp1.debug(L"expected");
    return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__);
  }

  // close and  delete the temporary files
  //
  tmp_file0.close();
  tmp_file1.close();
  
  File::remove(tmp_filename0);
  File::remove(tmp_filename1);

  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }

  // --------------------------------------------------------------------
  //
  // 2. print completion message
  //
  // --------------------------------------------------------------------

  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  if (level_a > Integral::NONE) {
    String output(L"diagnostics passed for class ");
    output.concat(name());
    output.concat(L"\n");
    Console::put(output);
  }

  // exit gracefully
  //
  return true;
}
