// file: $isip/class/algo/Calculus/calc_02.cc
// version: $Id: calc_02.cc 8165 2002-05-31 21:57:33Z picone $
//

// isip include files
//
#include "Calculus.h"

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

  // set indentation
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing required public methods: class constructors...\n");
    Console::increaseIndention();
  }
  
  Calculus cal0;
  cal0.setAlgorithm(DIFFERENTIATION);
  cal0.setImplementation(REGRESSION);
  cal0.setComputeMode(CROSS_FRAME);
  cal0.setOrder(1);
  Calculus cal1(cal0);
  
  if (!cal1.eq(cal0)) {
    return Error::handle(name(), L"copy constructor/eq", 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
    //
    
    Calculus::setGrowSize((long)500);
    
    Calculus* pcal = new Calculus();

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

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

  // declare a reference object
  //
  cal0.setAlgorithm(DIFFERENTIATION);
  cal0.setImplementation(REGRESSION);
  cal0.setComputeMode(FRAME_INTERNAL);
  cal0.setOrder(1);
  cal0.init();

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

  cal0.write(tmp_file0, (long)0);
  cal0.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
  //
  cal1.read(tmp_file0, (long)0);
  cal1.init();

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

  if (!cal0.eq(cal1)) {
    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();
  }

  // set the parameters manually
  //
  cal0.setAlgorithm(DIFFERENTIATION);
  cal0.setImplementation(REGRESSION);
  cal0.setComputeMode(CROSS_FRAME);
  cal0.setOrder(1);
  cal0.setDeltaWindow(3);

  // check that the values were set
  //
  if (cal0.algorithm_d != DIFFERENTIATION) {
    return Error::handle(name(), L"setAlgorithm",
			 Error::TEST, __FILE__, __LINE__);
  }
  else if (cal0.implementation_d != REGRESSION) {
    return Error::handle(name(), L"setImplementation",
			 Error::TEST, __FILE__, __LINE__);
  }
  else if (cal0.cmode_d != CROSS_FRAME) {
    return Error::handle(name(), L"setComputeMode",
			 Error::TEST, __FILE__, __LINE__);
  }
  else if (cal0.order_d != (long)1) {
    return Error::handle(name(), L"setOrder",
			 Error::TEST, __FILE__, __LINE__);
  }
  else if (cal0.delta_win_d != (long)3) {
    return Error::handle(name(), L"setDeltaWin",
			 Error::TEST, __FILE__, __LINE__);
  }

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

  // create some reference data
  //  
  VectorFloat input(L"2, 1, 0, 2, 4");
  VectorFloat zeros(L"0, 0, 0, 0, 0");
  VectorFloat ones(L"1, 1, 1, 1, 1");
  VectorFloat output;
  VectorFloat ans;
  
  // check DIFFERENTIATION/REGRESSION
  //
  ans.assign(L"0.5");
  cal0.setOrder((long)1);
  cal0.setDeltaWindow((long)2);
  cal0.compute(output, input);

  if (!output.almostEqual(ans)) {
    ans.debug(L"correct answer");
    output.debug(L"wrong answer");
    return  Error::handle(name(),
			  L"compute - differentiation/regression",
			  ERR, __FILE__, __LINE__);
  }

  ans.assign(L"0.0");
  cal0.compute(output, zeros);

  if (!output.almostEqual(ans)) {
    ans.debug(L"correct answer");
    output.debug(L"wrong answer");
    return  Error::handle(name(),
			  L"compute - differentiation/regression",
			  ERR, __FILE__, __LINE__);
  }

  ans.assign(L"0.0");
  cal0.compute(output, ones);

  if (!output.almostEqual(ans)) {
    ans.debug(L"correct answer");
    output.debug(L"wrong answer");
    return  Error::handle(name(),
			  L"compute - differentiation/regression",
			  ERR, __FILE__, __LINE__);
  }    
  
  // check DIFFERENTIATION/CENTRAL_DIFFERENCE
  //
  cal0.setImplementation(CENTRAL_DIFFERENCE);
  cal0.compute(output, input);

  ans.assign(L"0.5");
  if (!output.almostEqual(ans)) {
    ans.debug(L"correct answer");
    output.debug(L"wrong answer");
    return  Error::handle(name(),
			  L"compute - differentiation/central_difference",
			  ERR, __FILE__, __LINE__);
  }

  ans.assign(L"0.0");
  cal0.compute(output, zeros);

  if (!output.almostEqual(ans)) {
    ans.debug(L"correct answer");
    output.debug(L"wrong answer");
    return  Error::handle(name(),
			  L"compute - differentiation/central_difference",
			  ERR, __FILE__, __LINE__);
  }

  ans.assign(L"0.0");
  cal0.compute(output, ones);

  if (!output.almostEqual(ans)) {
    ans.debug(L"correct answer");
    output.debug(L"wrong answer");
    return  Error::handle(name(),
			  L"compute - differentiation/central_difference",
			  ERR, __FILE__, __LINE__);
  }
  
  // check DIFFERENTIATION/BACKWARD_DIFFERENCE
  //
  cal0.setImplementation(BACKWARD_DIFFERENCE);
  cal0.setDeltaWindow((long)2);
  input.assign(L"0, 0, 1");
  cal0.compute(output, input);

  ans.assign(L"0.5");
  if (!output.almostEqual(ans)) {
    ans.debug(L"correct answer");
    output.debug(L"wrong answer");
    return  Error::handle(name(),
			  L"compute - differentiation/backward_difference",
			  ERR, __FILE__, __LINE__);
  }

  ans.assign(L"0.0");
  zeros.assign(L"0, 0, 0");
  cal0.compute(output, zeros);

  if (!output.almostEqual(ans)) {
    ans.debug(L"correct answer");
    output.debug(L"wrong answer");
    return  Error::handle(name(),
			  L"compute - differentiation/backward_difference",
			  ERR, __FILE__, __LINE__);
  }

  ans.assign(L"0.0");
  ones.assign(L"1, 1, 1");
  cal0.compute(output, ones);

  if (!output.almostEqual(ans)) {
    ans.debug(L"correct answer");
    output.debug(L"wrong answer");
    return  Error::handle(name(),
			  L"compute - differentiation/backward_difference",
			  ERR, __FILE__, __LINE__);
  }
  
  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }

  //--------------------------------------------------------------------------
  //
  // 6. 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;
}
