// file: $isip/class/algo/DisplayData/dspd_02.cc
// version: $Id: dspd_02.cc 7644 2002-01-27 18:37:21Z gao $
//

// isip include files
//
#include <Console.h>
#include "DisplayData.h"

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

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

  // output the class name
  //
  if (level_a > Integral::NONE) {
    SysString 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();
  }

  // declare DisplayData objects for read/write methods
  //
  DisplayData dspd2;
  DisplayData dspd3;
  DisplayData dspd4;

  // 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);

  // write to text and binary files
  //
  dspd2.write(tmp_file0, (long)0);
  dspd2.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
  //
  dspd3.read(tmp_file0, (long)0);
  if (!dspd3.eq(dspd2)) {
    return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__);
  }
  dspd4.read(tmp_file0, (long)0);
  if (!dspd4.eq(dspd2)) {
    dspd4.debug(L"dspd4");
    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);

  // test the conditional i/o methods 
  //
  DisplayData dspd_0;
  
  Sof tmp_file2;
  String tmp_filename2;
  Integral::makeTemp(tmp_filename2);

  tmp_file2.open(tmp_filename2, File::WRITE_ONLY);
  
  tmp_file2.put(CLASS_NAME, 5, -1);
  String line;

  line.assign(L"number = 4;\n");
  tmp_file2.puts(line);

  // close the file
  //
  tmp_file2.close();

  // open the file in read mode
  //
  tmp_file2.open(tmp_filename2);

  // read the values back
  //
  dspd_0.read(tmp_file2, (long)5);

  if ((dspd_0.getNumber() != 4) || (dspd_0.getAlgorithm() != DEF_ALGORITHM)) {
    dspd_0.debug(L"dspd_0");
    return Error::handle(name(), L"readData", Error::TEST, __FILE__, __LINE__);
  }
  
  // close and delete the temporary files
  //
  tmp_file2.close();
  File::remove(tmp_filename2);

  // check constructors and memory management methods
  //
  DisplayData dspd0;
  DisplayData dspd1(dspd0);

  if (!dspd1.eq(dspd0)) {
    return Error::handle(name(), L"copy constructor", Error::TEST,
                         __FILE__, __LINE__);
  }

  // test large allocation construction and deletion
  //
  if (level_a == Integral::ALL) {
    
    Console::put(L"\ntesting large chunk memory allocation and deletion:\n");
    
    // set the memory to a strange block size so we can hopefully catch any
    // frame overrun errors
    //
    
    DisplayData::setGrowSize((long)500);
    
    DisplayData* pft = new DisplayData();
    
    for (long j = 1; j <= 100; j++) {
      DisplayData** pfts = new DisplayData*[j * 100];
      
      // create the objects
      //
      for (long i = 0; i < j * 100; i++) {
	pfts[i] = new DisplayData();
      }
    
      // delete objects
      //
      for (long i = (j * 100) - 1; i >= 0; i--) {
	delete pfts[i];
      }
      delete [] pfts;
    } 
    delete pft;
  }

  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //---------------------------------------------------------------------------
  //
  // 2. class-specific public methods:
  //     set and get methods     
  //
  //---------------------------------------------------------------------------

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

  // test set/get number method
  //
  dspd2.setNumber(27);

  if (dspd2.getNumber() != 27) {
    return Error::handle(name(), L"setVarName", Error::TEST,
                         __FILE__, __LINE__);
  }

  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
 
  //---------------------------------------------------------------------------
  //
  // 3. class-specific public methods:
  //     compute methods
  //
  //---------------------------------------------------------------------------

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

  DisplayData algo;

  String output_file;
  Integral::makeTemp(output_file);
  Console::open(output_file, File::WRITE_ONLY);
  AlgorithmData data;
  AlgorithmData dummy;
  data.makeVectorFloat().assign(L"1");
  String label(L"snr");
  String suffix(L" db");
  algo.setLabel(label);
  algo.setSuffix(suffix);
  algo.compute(dummy, data);

  data.makeVectorFloat().assign(L"1, 2, 3");
  algo.compute(dummy, data);

  data.makeMatrixFloat().assign(3, 3, L"1, 2, 3", Integral::DIAGONAL);
  algo.compute(dummy, data);

  data.makeCombination().setLength(3);

  data.getCombination()(0).makeVectorFloat().assign(L"1");
  data.getCombination()(1).makeVectorFloat().assign(L"1, 2, 3");
  data.getCombination()(2).makeMatrixFloat().assign(3, 3, L"1, 2, 3",
					       Integral::DIAGONAL);

  algo.label_d.clear();
  String prefix(L"stuff := ");
  algo.setPrefix(prefix);
  algo.compute(dummy, data);

  Console::close();

  // make sure the output is the same
  //
  String ref_file(L"diagnose_output.sof");
  if (!File::compare(output_file, ref_file)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
 
  //---------------------------------------------------------------------------
  //
  // 5. print completion message
  //
  //---------------------------------------------------------------------------

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

  // exit gracefully
  //
  return true;
}
