// file: $isip/class/algo/LogAreaRatio/lar_02.cc
// version: $Id: lar_02.cc 7696 2002-02-06 03:17:04Z zheng $
//

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

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

  // test destructor/constructor(s) and memory management
  //
  LogAreaRatio lar0;
  LogAreaRatio lar1(lar0);
  LogAreaRatio lar2;
  
  if (!lar1.eq(lar0)) {
    return Error::handle(name(), L"copy constructor", Error::TEST,
			 __FILE__, __LINE__);
  }
  
  if (!lar2.eq(lar0)) {
    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
    //
    LogAreaRatio::setGrowSize((long)500);
    
    LogAreaRatio* pft = new LogAreaRatio();

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

  // test the i/o methods
  //
  LogAreaRatio lar3;
  LogAreaRatio lar4;
  LogAreaRatio lar5;
  
  // 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);

  lar3.write(tmp_file0, (long)0);
  lar3.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
  //
  lar4.read(tmp_file0, (long)0);

  if (!lar4.eq(lar3)) {
    return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__);
  }

  if (!lar4.eq(lar3)) {
    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. class-specific public methods:
  //     computational methods
  //
  //--------------------------------------------------------------------------

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

  // declare local variables
  //
  LogAreaRatio lar;
  VectorFloat vec_in(L"-0.9378492, 0.03325092, 0.03422026, 0.03525937");
  VectorFloat vec_out, result;

  // case: Algorithm = LATTICE, Implementation = KELLY_LOCHBAUM,
  //       Input = non-zero REFLECTION vector
  //
  {  
    // compute log-area-ratio from reflection coefficients
    //
    lar.compute(vec_out, vec_in, AlgorithmData::REFLECTION);
    
    result.assign(L"3.43977, -0.0665264, -0.0684673, -0.070548");
    if (!vec_out.almostEqual(result)) {
      vec_out.debug(L"Computed output");
      result.debug(L"Correct output");
      return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
    }
  }
  
  // case: Algorithm = LATTICE, Implementation = KELLY_LOCHBAUM,
  //       Input = zero REFLECTION vector
  //
  {
    // compute log-area-ratio from reflection coefficients, for all zeros
    //
    vec_in.assign(L"0.0, 0.0, 0.0, 0.0");
    lar.compute(vec_out, vec_in, AlgorithmData::REFLECTION);
    
    result.assign(L"0.0, 0.0, 0.0, 0.0");
  
    if (!vec_out.almostEqual(result)) {
      vec_out.debug(L"Computed output");
      result.debug(L"Correct output");
      return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
    }
  }
  
  // case: Algorithm = LATTICE, Implementation = KELLY_LOCHBAUM,
  //       Input = constant REFLECTION vector
  //
  { 
    // compute log-area-ratio from reflection coefficients, for all constants
    //
    vec_in.assign(L"0.5, 0.5, 0.5, 0.5");
    lar.compute(vec_out, vec_in, AlgorithmData::REFLECTION);
    
    result.assign(L"-1.09861, -1.09861, -1.09861, -1.09861");
    
    if (!vec_out.almostEqual(result)) {
      vec_out.debug(L"Computed output");
    result.debug(L"Correct output");
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
    }
  }

  // case: Algorithm = LATTICE, Implementation = KELLY_LOCHBAUM,
  //       Input = non-zero PREDICTION vector
  //
  {  
    // compute log-area-ratio from reflection coefficients
    //
    vec_in.assign(L"1.000000, -0.9666891, 0.00009353, 0.00009286241, 0.03525937");
    lar.compute(vec_out, vec_in, AlgorithmData::PREDICTION);
    
    result.assign(L"3.43977, -0.0665264, -0.0684673, -0.070548");
    if (!vec_out.almostEqual(result)) {
      vec_out.debug(L"Computed output");
      result.debug(L"Correct output");
      return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
    }
  }

  // case: Algorithm = LATTICE, Implementation = KELLY_LOCHBAUM,
  //       Input = zero PREDICTION vector
  //
  {
    // compute log-area-ratio from reflection coefficients, for all zeros
    //
    vec_in.assign(L"0.0, 0.0, 0.0, 0.0, 0.0");
    lar.compute(vec_out, vec_in, AlgorithmData::PREDICTION);
    
    result.assign(L"0.0, 0.0, 0.0, 0.0");
  
    if (!vec_out.almostEqual(result)) {
      vec_out.debug(L"Computed output");
      result.debug(L"Correct output");
      return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
    }
  }
  
  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  // --------------------------------------------------------------------
  //
  // 5. class-specific public methods
  //     apply methods
  //
  // --------------------------------------------------------------------

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

  // local variables
  //
  Vector< CircularBuffer<AlgorithmData> > in;
  Vector<AlgorithmData> out;
  AlgorithmData data;

  // number of channels
  //
  long N = 2;
  
  in.setLength(N);
  out.setLength(N);
  
  for (long i = 0; i < N; i++) {
    in(i).append(data);
    in(i)(0).makeVectorFloat();
    in(i)(0).setCoefType(AlgorithmData::REFLECTION);
  }
  in(0)(0).getVectorFloat().assign(L"-0.9378492, 0.03325092, 0.03422026, 0.03525937");
  in(1)(0).getVectorFloat().assign(L"0.0, 0.0, 0.0, 0.0");
  
  // case: Algorithm = LATTICE, Implementation = KELLY_LOCHBAUM,
  //       Input = non-zero and zero REFLECTION vector at channel 0 and 1
  //
  {  
    // compute log-area-ratio from reflection coefficients
    //
    lar.apply(out, in);

    result.assign(L"3.43977, -0.0665264, -0.0684673, -0.070548");
    if (!out(0).getVectorFloat().almostEqual(result)) {
      out(0).getVectorFloat().debug(L"output(0)");
      result.debug(L"Correct output");
      return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
    }

    result.assign(L"0.0, 0.0, 0.0, 0.0");
    if (!out(1).getVectorFloat().almostEqual(result)) {
      out(1).getVectorFloat().debug(L"output(1)");
      result.debug(L"Correct output");
      return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
    }
  }

  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  // --------------------------------------------------------------------
  //
  // 3. 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;
}
