// file: $isip_ifc/class/algo/Constant/const_02.cc
// version: $Id: const_02.cc 10532 2006-03-29 21:26:30Z raghavan $
//

// isip include files
//
#include "Constant.h"
#include <Console.h>
#include <Filename.h>

// method: diagnose
//
// arguments:
//  Integral::DEBUG level: (input) debug level for diagnostics
//
// return: a boolean value indicating status
//
boolean Constant::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
  //     class constructors
  //
  //--------------------------------------------------------------------------

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

  // test destructor/constructor(s) and memory management
  //
  Constant const0;
  const0.setAlgorithm(DATA);
  const0.setImplementation(READ);

  Constant const1(const0);
  
  if (!const1.eq(const0)) {
    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
    //
    Constant::setGrowSize((long)500);
    
    Constant* pconst = new Constant();

    for (long j = 1; j <= 100; j++) {
      Constant** pconsts = new Constant*[j * 100];
      
      // create the objects
      //
      for (long i = 0; i < j * 100; i++) {
	pconsts[i] = new Constant();
      }
      
      // delete objects
      //
      for (long i = (j * 100) - 1; i >= 0; i--) {
	delete pconsts[i];
      }
      
      delete [] pconsts;
    }
    
    delete pconst;
  }

  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //--------------------------------------------------------------------------
  //
  // 2. required public methods
  //     i/o methods 
  //
  //--------------------------------------------------------------------------

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

  const0.setAlgorithm(DATA);
  const0.setImplementation(READ);

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


  const0.write(tmp_file0, (long)0);
  //  const0.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 object back
  //
  const1.read(tmp_file0, (long)0);
  const1.init();

  if (!const0.eq(const1)) {
    return Error::handle(name(), L"i/o", Error::TEST,
			 __FILE__, __LINE__);
  }
    
  const1.read(tmp_file1, (long)0);
  const1.init();

  if (!const0.eq(const1)) {
    return Error::handle(name(), L"i/o", 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();
  }

  //---------------------------------------------------------------------------
  //
  // 3. class-specific public methods:
  //     set and get methods
  //
  //---------------------------------------------------------------------------

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

  // establish an object
  //
  const0.setAlgorithm(DATA);
  const0.setImplementation(READ);

  // check that the values were set
  //
  if (const0.algorithm_d != DATA) {
    return Error::handle(name(), L"setAlgorithm", Error::TEST,
			 __FILE__, __LINE__);
  }
  else if (const0.implementation_d != READ) {
    return Error::handle(name(), L"setImplementation", Error::TEST,
			 __FILE__, __LINE__);
  }

  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //---------------------------------------------------------------------------
  //
  // 4. class-specific public methods:
  //     computation methods
  //
  //---------------------------------------------------------------------------

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

  // set indentation
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing reading from a file...\n");
    Console::increaseIndention();
  }

  {
    Constant c1;
    c1.setFilename(L"diagnose_file.sof");
    c1.setChannel(4);

    Vector < CircularBuffer < AlgorithmData> > in;
    Vector < AlgorithmData> out;

    // multip-channel and VectorFloat test
    //
    c1.apply(out, in);

    Vector<VectorFloat> result;
    result.setLength(4);
    result(0).assign(L"1, 3, 5, 7, 9");
    result(1).assign(L"10, 30, 50, 70, 90");  
    result(2).assign(L"100, 300, 500, 700, 900");
    result(3).assign(L"1000, 3000, 5000, 7000, 9000");

    for (long i = 0; i < 4; i++) {
      if (!out(i).getVectorFloat().almostEqual(result(i))) {
	out(i).getVectorFloat().debug(L"out_coeffs");
	result(i).debug(L"exp_coeffs");
	return  Error::handle(name(), L"apply compute from file", ERR,
			      __FILE__, __LINE__);
      }
    }

    Constant c2;
    c2.setFilename(L"diagnose_file.sof");
    c2.setChannel(1);

    Vector<AlgorithmData> out1;
  
    // VectorDouble type test
    //
    c2.setDataType(AlgorithmData::VECTOR_DOUBLE);
    c2.apply(out1, in);

    VectorDouble result_01;
    result_01.assign(L"1.2, 0.5, 1.851, 1.000009");
    if (!out1(0).getVectorDouble().almostEqual(result_01)) {
      out1(0).getVectorFloat().debug(L"out_coeffs");
      result_01.debug(L"exp_coeffs");
      return  Error::handle(name(), L"apply compute from file", ERR,
			    __FILE__, __LINE__);
    }

    // VectorComplexDouble type test
    //
    c2.clear();
    c2.setDataType(AlgorithmData::VECTOR_COMPLEX_DOUBLE);
    c2.apply(out1, in);

    VectorComplexDouble result_02;
    result_02.assign(L"1.2+8j,0.5+0.90002j,1.8500001+9j,1+7.87000001j");
    if (!out1(0).getVectorComplexDouble().almostEqual(result_02)) {
      out1(0).getVectorFloat().debug(L"out_coeffs");
      result_02.debug(L"exp_coeffs");
      return  Error::handle(name(), L"apply compute from file", ERR,
			    __FILE__, __LINE__);
    }

    // VectorComplexFloat type test
    //
    c2.clear();
    c2.setDataType(AlgorithmData::VECTOR_COMPLEX_FLOAT);
    c2.apply(out1, in);

    VectorComplexFloat result_03;
    result_03.assign(L"1+2.3j,3+4.5j,5+6.7j,7+3.234j,9+4.765j");
    if (!out1(0).getVectorComplexFloat().almostEqual(result_03)) {
      out1(0).getVectorFloat().debug(L"out_coeffs");
      result_03.debug(L"exp_coeffs");
      return  Error::handle(name(), L"apply compute from file", ERR,
			    __FILE__, __LINE__);
    }

    // MatrixDouble type test
    //
    c2.clear();
    c2.setDataType(AlgorithmData::MATRIX_DOUBLE);
    c2.apply(out1, in);

    MatrixDouble result_04;
    result_04.assign(3, 3, L"4, 3, 1, 7, 0, 4, 2, 8, 1");
    if (!out1(0).getMatrixDouble().almostEqual(result_04)) {
      out1(0).getMatrixDouble().debug(L"out_coeffs");
      result_04.debug(L"exp_coeffs");
      return  Error::handle(name(), L"apply compute from file", ERR,
			    __FILE__, __LINE__);
    }

    // MatrixFloat type test
    //
    c2.clear();
    c2.setDataType(AlgorithmData::MATRIX_FLOAT);
    c2.apply(out1, in);

    MatrixFloat result_05;
    result_05.assign(3, 3, L"3,7,34,6,1,88,6,3,5");
    if (!out1(0).getMatrixFloat().almostEqual(result_05)) {
      out1(0).getMatrixFloat().debug(L"out_coeffs");
      result_05.debug(L"exp_coeffs");
      return  Error::handle(name(), L"apply compute from file", ERR,
			    __FILE__, __LINE__);
    }

    // MatrixComplexFloat type test
    //
    c2.clear();
    c2.setDataType(AlgorithmData::MATRIX_COMPLEX_FLOAT);
    c2.apply(out1, in);

    MatrixComplexFloat result_06;
    result_06.assign(3, 3, L"3+5.678j,7+8.765j,34+23.45j,6+0.4j,1+3.2j,88+0.3j,6+4.3j,3+0.5j,5+0.7j");
    if (!out1(0).getMatrixComplexFloat().almostEqual(result_06)) {
      out1(0).getMatrixFloat().debug(L"out_coeffs");
      result_06.debug(L"exp_coeffs");
      return  Error::handle(name(), L"apply compute from file", ERR,
			    __FILE__, __LINE__);
    }
  }
  
  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  // test write to a file
  //

  // set indentation
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing writing out to a file...\n");
    Console::increaseIndention();
  }

  {
    //    Filename output1(L".diagnose_outfile.sof");
    // Filename output2(L"diagnose_outfile.sof");

    // we need two files
    //
    Filename output1;
    Integral::makeTemp(output1);
    Filename output2;
    Integral::makeTemp(output2);

    File::registerTemp(output1);
    File::registerTemp(output2);
	
    Constant c1;
    c1.setSignalDuration(0.01);
    c1.setImplementation(WRITE);
    c1.setFilename(output2);
    c1.setChannel(4);
    c1.setDataType(AlgorithmData::VECTOR_FLOAT);
    
    Vector < CircularBuffer < AlgorithmData> > in;
    Vector < AlgorithmData> out;
    
    Vector<VectorFloat> result;
    result.setLength(4);
    result(0).assign(L"1, 3, 5, 7, 9");
    result(1).assign(L"10, 30, 50, 70, 90");  
    result(2).assign(L"100, 300, 500, 700, 900");
    result(3).assign(L"1000, 3000, 5000, 7000, 9000");

    Sof sof;
    sof.open(output1, File::WRITE_ONLY);

    result(0).write(sof, 0);
    result(1).write(sof, 1);
    result(2).write(sof, 2);
    result(3).write(sof, 3);
    sof.close();
    
    long N = 4;
    in.setLength(N);
  
    for (long i = 0; i < N; i++) {
      in(i)(0).makeVectorFloat();
      in(i)(0).getVectorFloat().assign(result(i));
      in(i)(0).setCoefType(AlgorithmData::SIGNAL);
    }

    // multip-channel and VectorFloat test
    //
    c1.apply(out, in);

    if (File::size(output1) != File::size(output2)) {
      return  Error::handle(name(), L"apply compute from file", ERR,
			    __FILE__, __LINE__);
    }

    Constant c2;
    c2.setSignalDuration(0.01);
    c2.setFilename(output2);
    c2.setChannel(1);
    c2.setImplementation(WRITE);
    
    Vector<AlgorithmData> out1;

    VectorDouble result_01;
    result_01.assign(L"1.2, 0.5, 1.851, 1.000009");

    N = 1;
    in.setLength(N);
    for (long i = 0; i < N; i++) {
      in(i)(0).makeVectorDouble();
      in(i)(0).getVectorDouble().assign(result_01);
      in(i)(0).setCoefType(AlgorithmData::SIGNAL);
    }
  
    // VectorDouble type test
    //
    c2.setDataType(AlgorithmData::VECTOR_DOUBLE);
    c2.apply(out1, in);

    sof.open(output1, File::WRITE_ONLY);
    result_01.write(sof, 0);
    sof.close();

    if (File::size(output1) != File::size(output2)) {
      return  Error::handle(name(), L"apply compute from file", ERR,
			    __FILE__, __LINE__);
    }

    // VectorComplexDouble type test
    //
    c2.clear();
    c2.setSignalDuration(0.01);
    c2.setDataType(AlgorithmData::VECTOR_COMPLEX_DOUBLE);
    c2.setFilename(output2);
    c2.setChannel(1);
    c2.setImplementation(WRITE);
  
    VectorComplexDouble result_02;
    result_02.assign(L"1.2+8j,0.5+0.90002j,1.8500001+9j,1+7.87000001j");

    N = 1;
    in.setLength(N);
    for (long i = 0; i < N; i++) {
      in(i)(0).makeVectorComplexDouble();
      in(i)(0).getVectorComplexDouble().assign(result_02);
      in(i)(0).setCoefType(AlgorithmData::SIGNAL);
    }
  
    c2.apply(out1, in);

    sof.open(output1, File::WRITE_ONLY);
    result_02.write(sof, 0);
    sof.close();
    
    if (File::size(output1) != File::size(output2)) {
      return  Error::handle(name(), L"apply compute from file", ERR,
			    __FILE__, __LINE__);
    }
    
    // VectorComplexFloat type test
    //
    c2.clear();
    c2.setSignalDuration(0.01);
    c2.setDataType(AlgorithmData::VECTOR_COMPLEX_FLOAT);
    c2.setFilename(output2);
    c2.setChannel(1);
    c2.setImplementation(WRITE);

    VectorComplexFloat result_03;
    result_03.assign(L"1+2.3j,3+4.5j,5+6.7j,7+3.234j,9+4.765j");
  
    N = 1;
    in.setLength(N);
    for (long i = 0; i < N; i++) {
      in(i)(0).makeVectorComplexFloat();
      in(i)(0).getVectorComplexFloat().assign(result_03);
      in(i)(0).setCoefType(AlgorithmData::SIGNAL);
    }

    c2.apply(out1, in);

    sof.open(output1, File::WRITE_ONLY);
    result_03.write(sof, 0);
    sof.close();

    if (File::size(output1) != File::size(output2)) {
      return  Error::handle(name(), L"apply compute from file", ERR,
			    __FILE__, __LINE__);
    }
    
    // MatrixDouble type test
    //
    c2.clear();
    c2.setSignalDuration(0.01);
    c2.setDataType(AlgorithmData::MATRIX_DOUBLE);

    c2.setFilename(output2);
    c2.setChannel(1);
    c2.setImplementation(WRITE);

    MatrixDouble result_04;
    result_04.assign(3, 3, L"4, 3, 1, 7, 0, 4, 2, 8, 1");

    N = 1;
    in.setLength(N);
    for (long i = 0; i < N; i++) {
      in(i)(0).makeMatrixDouble();
      in(i)(0).getMatrixDouble().assign(result_04);
      in(i)(0).setCoefType(AlgorithmData::SIGNAL);
    }
  
    c2.apply(out1, in);

    sof.open(output1, File::WRITE_ONLY);
    result_04.write(sof, 0);
    sof.close();

    if (File::size(output1) != File::size(output2)) {
      return  Error::handle(name(), L"apply compute from file", ERR,
			    __FILE__, __LINE__);
    }
    
    // MatrixFloat type test
    //
    c2.clear();
    c2.setSignalDuration(0.01);
    c2.setDataType(AlgorithmData::MATRIX_FLOAT);
    c2.setFilename(output2);
    c2.setChannel(1);
    c2.setImplementation(WRITE);
  
    MatrixFloat result_05;
    result_05.assign(3, 3, L"3,7,34,6,1,88,6,3,5");

    N = 1;
    in.setLength(N);
    for (long i = 0; i < N; i++) {
      in(i)(0).makeMatrixFloat();
      in(i)(0).getMatrixFloat().assign(result_05);
      in(i)(0).setCoefType(AlgorithmData::SIGNAL);
    }

    c2.apply(out1, in);

    sof.open(output1, File::WRITE_ONLY);
    result_05.write(sof, 0);
    sof.close();

    if (File::size(output1) != File::size(output2)) {
      return  Error::handle(name(), L"apply compute from file", ERR,
			    __FILE__, __LINE__);
    }

    // MatrixComplexFloat type test
    //
    c2.clear();
    c2.setSignalDuration(0.01);
    c2.setDataType(AlgorithmData::MATRIX_COMPLEX_FLOAT);
    c2.setFilename(output2);
    c2.setChannel(1);
    c2.setImplementation(WRITE);

    MatrixComplexFloat result_06;
    result_06.assign(3, 3, L"3+5.678j,7+8.765j,34+23.45j,6+0.4j,1+3.2j,88+0.3j,6+4.3j,3+0.5j,5+0.7j");
  
    N = 1;
    in.setLength(N);
    for (long i = 0; i < N; i++) {
      in(i)(0).makeMatrixComplexFloat();
      in(i)(0).getMatrixComplexFloat().assign(result_06);
      in(i)(0).setCoefType(AlgorithmData::SIGNAL);
    }

    c2.apply(out1, in);

    sof.open(output1, File::WRITE_ONLY);
    result_06.write(sof, 0);
    sof.close();

    if (File::size(output1) != File::size(output2)) {
      return  Error::handle(name(), L"apply compute from file", ERR,
			    __FILE__, __LINE__);
    }
  }

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

  // reset indentation
  //
  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) {
    String output(L"diagnostics passed for class ");
    output.concat(name());
    output.concat(L"\n");
    Console::put(output);
  }

  // exit gracefully
  //
  return true;
}
