// file: $isip/class/algo/Statistics/stat_02.cc
// version: $Id: stat_02.cc 8314 2002-07-09 02:19:50Z picone $
//

// isip include files
//
#include "Statistics.h"
#include <String.h>
#include <Console.h>

// method: diagnose
//
// arguments:
//  Integral::DEBUG level: (input) debug level for diagnostics
//
// return: a boolean value indicating status
//
boolean Statistics::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: class constructors...\n");
    Console::increaseIndention();
  }

  Statistics stat0;
  Statistics stat1(stat0);

  if (!stat1.eq(stat0)) {
    return Error::handle(name(), L"copy constructor", Error::TEST,
                         __FILE__, __LINE__);
  }
  
  // test large allocation construction and deletion
  //
  if (level_a == Integral::ALL) {
    
    // set the memory to a strange block size so we can hopefully catch any
    // frame overrun errors
    //
    Statistics::setGrowSize((long)500);
    Statistics* pft = new Statistics();

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

  Statistics stat2;
  Statistics stat3;
  Statistics stat4;
  stat2.setAlgorithm(MAXIMUM);
  
  // we need binary and text sof files
  //
  String tmp_filename0;
  Integral::makeTemp(tmp_filename0);
  String tmp_filename1;
  Integral::makeTemp(tmp_filename1);
  
  // open files in the write algorithm
  //
  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);

  stat2.write(tmp_file0, (long)0);
  stat2.write(tmp_file1, (long)0);
  
  // close the files
  //
  tmp_file0.close();
  tmp_file1.close();
  
  // open the files in the read algorithm
  //
  tmp_file0.open(tmp_filename0);
  tmp_file1.open(tmp_filename1);
  
  // read the value back
  //
  if (!stat3.read(tmp_file0, (long)0) || (!stat3.eq(stat2))) {
    stat3.debug(L"stat3");
    return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__);
  }

  if (!stat4.read(tmp_file0, (long)0) || (!stat4.eq(stat2))) {
    stat4.debug(L"stat4");
    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();
  }
      
  //-------------------------------------------------------------------
  //
  // 3. class-specific public methods
  //     compute methods
  //
  //-------------------------------------------------------------------

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

  VectorFloat input;
  VectorFloat zeros;  
  input.assign(L"0, 1, 2, 3");
  zeros.assign(L"0, 0, 0, 0");  

  VectorFloat output;
 
  VectorFloat exp_test(L"0"); 
  VectorFloat exp_median(L"1.5");
  VectorFloat exp_max(L"3");
  VectorFloat exp_max_mag(L"3");
  VectorFloat exp_min(L"0");
  VectorFloat exp_min_mag(L"0");
  VectorFloat exp_mean(L"1.5");
  VectorFloat exp_var(L"1.25");
  VectorFloat exp_stddev(L"1.11803");
  
  stat2.setAlgorithm(MINIMUM);
  stat2.compute(output, input);
  if (!output.almostEqual(exp_min)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }

  stat2.compute(output, zeros);
  if (!output.almostEqual(exp_test)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }  
  
  stat2.setAlgorithm(MAXIMUM);
  stat2.compute(output, input);
  if (!output.almostEqual(exp_max)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }

  stat2.compute(output, zeros);
  if (!output.almostEqual(exp_test)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }
  
  stat2.setAlgorithm(MINIMUM_MAG);
  stat2.compute(output, input);
  if (!output.almostEqual(exp_min_mag)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }

  stat2.compute(output, zeros);
  if (!output.almostEqual(exp_test)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }
  
  stat2.setAlgorithm(MAXIMUM_MAG);
  stat2.compute(output, input);
  if (!output.almostEqual(exp_max_mag)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }

  stat2.compute(output, zeros);
  if (!output.almostEqual(exp_test)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }
  
  stat2.setAlgorithm(MEDIAN);
  stat2.compute(output, input);
  if (!output.almostEqual(exp_median)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }

  stat2.compute(output, zeros);
  if (!output.almostEqual(exp_test)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }
  
  stat2.setAlgorithm(VARIANCE);
  stat2.compute(output, input);
  if (!output.almostEqual(exp_var)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }
 
  stat2.compute(output, zeros);
  if (!output.almostEqual(exp_test)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }
  
  stat2.setAlgorithm(STDEV);
  stat2.compute(output, input);
  if (!output.almostEqual(exp_stddev)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }

  stat2.compute(output, zeros);
  if (!output.almostEqual(exp_test)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }
  
  stat2.setAlgorithm(MEAN);
  stat2.compute(output, input);
  if (!output.almostEqual(exp_mean)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }

  stat2.compute(output, zeros);
  if (!output.almostEqual(exp_test)) {
    return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__);
  }
  
  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //--------------------------------------------------------------------
  //
  // 5. print completion message
  //
  //--------------------------------------------------------------------

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

  // possibly print completion message
  //  
  if (level_a > Integral::NONE) {
    String output(L"diagnostics completed successfully for class ");
    output.concat(name());
    output.concat(L"\n");
    Console::put(output);
  }

  // exit gracefully
  //
  return true;  
}
