// file: $isip/class/algo/Connection/conn_02.cc
// version: $Id: conn_02.cc 8269 2002-07-02 19:14:33Z gao $
//

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

// method: diagnose
//
// arguments:
//  Integral::DEBUG level: (input) debug level for diagnostics
//
// return: logical error status
//
boolean Connection::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
  //
  Connection concatenate0;
  Connection concatenate1(concatenate0);
  Connection concatenate2;
  
  if (!concatenate1.eq(concatenate0)) {
    return Error::handle(name(), L"copy constructor", Error::TEST,
			 __FILE__, __LINE__);
  }
  
  if (!concatenate2.eq(concatenate0)) {
    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
    //
    Connection::setGrowSize((long)500);
    
    Connection* pft = new Connection();

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

      delete [] pfts;
    } 

    delete pft;
  }

  // test the i/o methods 
  //
  Connection concatenate3;
  Connection concatenate4;
  Connection concatenate5;
  
  // 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);

  concatenate3.setAlgorithm(CHANNEL);

  concatenate3.write(tmp_file0, (long)0);
  concatenate3.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
  //
  concatenate4.read(tmp_file0, (long)0);
  if (!concatenate4.eq(concatenate3)) {
    return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__);
  }

  concatenate4.read(tmp_file1, (long)0);
  if (!concatenate4.eq(concatenate3)) {
    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:
  //     algorithm methods
  //
  //--------------------------------------------------------------------------

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

  // test get/set methods for CHANNEL algorithm
  //
  concatenate3.setAlgorithm(CHANNEL);
  if (concatenate3.algorithm_d != CHANNEL) {
    return Error::handle(name(), L"setAlgorithm", Error::TEST,
			 __FILE__, __LINE__);
  }
  if (concatenate3.getAlgorithm() != CHANNEL) {
    return Error::handle(name(), L"getAlgorithm", Error::TEST,
			 __FILE__, __LINE__);
  }

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

  //--------------------------------------------------------------------------
  //
  // 3. class-specific public methods:
  //     computation methods
  //
  //--------------------------------------------------------------------------

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

  // declare the test variables
  //
  Vector< CircularBuffer<AlgorithmData> > input(1);
  Vector<AlgorithmData> output;

  AlgorithmData tmp;
  tmp.makeCombination().setLength(4);
  tmp.getCombination()(0).makeVectorFloat().assign(L"0, 1, 2, 3");
  tmp.getCombination()(1).makeVectorFloat().assign(L"5, 6, 7, 8");
  tmp.getCombination()(2).makeVectorFloat().assign(L"10, 11, 12, 13");
  tmp.getCombination()(3).makeVectorFloat().assign(L"12, 13, 14, 15");

  // test using one one-channel signal
  //
  input(0).append(tmp);
  VectorFloat expected_output(L"0, 1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 12, 13, 14, 15");

  // declare local variable
  //
  Connection conn;

  // test CHANNEL/CONCATENATION
  //
  conn.setAlgorithm(CHANNEL);
  conn.setImplementation(CONCATENATE);

  if ((!conn.apply(output, input)) ||
      (output.length() != 1) ||
      (output(0).getVectorFloat().ne(expected_output))) {
    return Error::handle(name(), L"concatenate", Error::TEST,
			 __FILE__, __LINE__);
  }

  // test CHANNEL/INTERLEAVE
  //
  conn.setImplementation(INTERLEAVE);
  expected_output.assign(L"0, 5, 10, 12, 1, 6, 11, 13, 2, 7, 12, 14, 3, 8, 13, 15");
  if ((!conn.apply(output, input)) ||
      (output.length() != 1) ||
      (output(0).getVectorFloat().ne(expected_output))) {
    output.debug(L"output");
    return Error::handle(name(), L"interleave", Error::TEST,
			 __FILE__, __LINE__);
  }


  // test STREAM/CONCATENATE
  //  
  conn.clear();
  conn.setAlgorithm(STREAM);
  conn.setImplementation(CONCATENATE);
  if ((!conn.apply(output, input)) ||
      (output.length() != 4) ||
      (!output.eq(tmp.getCombination()))) {
    return Error::handle(name(), L"concatnate", Error::TEST,
			 __FILE__, __LINE__);
  }

  // test STREAM/SELECT
  //
  conn.setAlgorithm(STREAM);
  conn.setImplementation(SELECT);
  conn.setChannel(0);

  expected_output.assign(L"5, 6, 7, 8");
  if ((!conn.apply(output, input)) ||
      (output.length() != 1)) {
    return Error::handle(name(), L"select", Error::TEST,
			 __FILE__, __LINE__);
  }

  // test using two two-channel signals
  //
  AlgorithmData tmpX1, tmpX2;

  tmpX1.makeCombination().setLength(2);
  tmpX2.makeCombination().setLength(2);  
  tmpX1.getCombination()(0).makeVectorFloat().assign(L"0, 1, 0");
  tmpX1.getCombination()(1).makeVectorFloat().assign(L"1, 0, 1");
  tmpX2.getCombination()(0).makeVectorFloat().assign(L"2, 3, 2");
  tmpX2.getCombination()(1).makeVectorFloat().assign(L"3, 2, 3");

  VectorFloat expected_output_01;
  VectorFloat expected_output_02;

  input.clear();
  input.setLength(2);
  input(0).append(tmpX1);
  input(1).append(tmpX2);
  expected_output_01.assign(L"0, 1, 0, 1, 0, 1");
  expected_output_02.assign(L"2, 3, 2, 3, 2, 3");

  // test CHANNEL/CONCATENATE
  //
  conn.clear();
  conn.setAlgorithm(CHANNEL);
  conn.setImplementation(CONCATENATE);
  output.clear();

  if ((!conn.apply(output, input)) ||
      (output.length() != 2) ||
      (output(0).getVectorFloat().ne(expected_output_01)) ||
      (output(1).getVectorFloat().ne(expected_output_02))) {
    output.debug(L"output");
    return Error::handle(name(), L"concatenate", Error::TEST,
			 __FILE__, __LINE__);
  }

  // test CHANNEL/INTERLEAVE
  //
  
  output.clear();
  conn.setImplementation(INTERLEAVE);
  expected_output_01.assign(L"0, 2, 1, 3, 0, 2");
  expected_output_02.assign(L"1, 3, 0, 2, 1, 3");

  if ((!conn.apply(output, input)) ||
      (output.length() != 2) ||
      (output(0).getVectorFloat().ne(expected_output_01)) ||
      (output(1).getVectorFloat().ne(expected_output_02))) {
    output.debug(L"output");
    return Error::handle(name(), L"interleave", Error::TEST,
			 __FILE__, __LINE__);
  }
  
  // test STREAM/CONCATENATE
  //

  Vector<VectorFloat> expected_output_03;
  expected_output_03.setLength(4);
  expected_output_03(0).assign(L"0, 1, 0");
  expected_output_03(1).assign(L"1, 0, 1");
  expected_output_03(2).assign(L"2, 3, 2");
  expected_output_03(3).assign(L"3, 2, 3");

  conn.clear();
  conn.setAlgorithm(STREAM);
  conn.setImplementation(CONCATENATE);
  if ((!conn.apply(output, input)) ||
      (output.length() != 4) ||
      (!(output(0).getVectorFloat()).eq(expected_output_03(0)))) {
    output.debug(L"output");
    return Error::handle(name(), L"concatnate", Error::TEST,
			 __FILE__, __LINE__);
  }

  // test STREAM/SELECT
  //
  conn.clear();
  conn.setAlgorithm(STREAM);
  conn.setImplementation(SELECT);
  conn.setChannel(1);

  if ((!conn.apply(output, input)) ||
      (output.length() != 2) ||
      (output(0).getVectorFloat().ne(expected_output_03(1))) ||
      (output(1).getVectorFloat().ne(expected_output_03(3)))) {
    output.debug(L"output");
    return Error::handle(name(), L"select", 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();
  }

  Connection con;
  
  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;
  
}
