// file: $isip/class/algo/Mask/mask_02.cc
// version: $Id: mask_02.cc 7057 2001-06-10 20:53:51Z huang $
//

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

// method: diagnose
//
// arguments:
//  Integral::DEBUG level: (input) debug level for diagnostics
//
// return: a boolean value indicating status
//
boolean Mask::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
  //
  Mask mask0;
  Mask mask1(mask0);
  Mask mask2;
  
  if (!mask1.eq(mask0)) {
    return Error::handle(name(), L"copy constructor", Error::TEST,
			 __FILE__, __LINE__);
  }
  
  if (!mask2.eq(mask0)) {
    mask0.debug(L"mask0");
    mask2.debug(L"mask2");
    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
    //
    Mask::setGrowSize((long)500);
    
    Mask* pft = new Mask();

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

      delete [] pfts;
    } 

    delete pft;
  }

  // test the i/o methods 
  //
  Mask mask3;
  Mask mask4;
  Mask mask5;
  
  // 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);

  mask3.mask_d.assign(L"1,0,0,0,1,1,1,1,0,0");
  mask3.source_d.assign(L"0-2,4-13,20");
  mask3.setAlgorithm(REMOVE);

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

  mask4.read(tmp_file1, (long)0);
  if (!mask4.eq(mask3)) {
    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:
  //     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();
  }

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

  String set_str(L"0, 4-19, 21, 25-67");
  mask3.set(set_str);

  if (!mask3.source_d.eq(set_str)) {
    return Error::handle(name(), L"setString", Error::TEST,
			 __FILE__, __LINE__);
  }    
  if (!mask3.getString().eq(set_str)) {
    return Error::handle(name(), L"getString", Error::TEST,
			 __FILE__, __LINE__);
  }    

  // test the convert mask method
  //
  VectorByte vbyte;
  String str;

  vbyte.assign(L"1, 1, 1, 1, 1");
  mask3.convert(str, vbyte);
  if (str.ne(L"0-4")) {
    return Error::handle(name(), L"convert", Error::TEST, __FILE__, __LINE__);
  }
  
  vbyte.assign(L"0, 0, 0, 0");
  mask3.convert(str, vbyte);
  if (str.ne(L"")) {
    return Error::handle(name(), L"convert", Error::TEST, __FILE__, __LINE__);
  }

  vbyte.assign(L"1, 0, 0, 1, 1");
  mask3.convert(str, vbyte);
  if (str.ne(L"0,3-4")) {
    return Error::handle(name(), L"convert", Error::TEST, __FILE__, __LINE__);
  }
  
  vbyte.assign(L"1, 1, 1, 1, 0");
  mask3.convert(str, vbyte);
  if (str.ne(L"0-3")) {
    return Error::handle(name(), L"convert", Error::TEST, __FILE__, __LINE__);
  }

  vbyte.assign(L"1, 1, 0, 1, 1, 1");
  mask3.convert(str, vbyte);
  if (str.ne(L"0-1,3-5")) {
    return Error::handle(name(), L"convert", Error::TEST, __FILE__, __LINE__);
  }

  vbyte.assign(L"1, 0, 1, 0, 1, 0");
  mask3.convert(str, vbyte);
  if (str.ne(L"0,2,4")) {
    return Error::handle(name(), L"convert", Error::TEST, __FILE__, __LINE__);
  }

  vbyte.assign(L"0, 1, 0, 1, 0, 1");
  mask3.convert(str, vbyte);
  if (str.ne(L"1,3,5")) {
    return Error::handle(name(), L"convert", 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
  //
  VectorFloat input;
  VectorFloat output;

  input.assign(L"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,48,32,32,0,32,-32,16");
  VectorByte mask_vec(L"0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1, 1, 1, 1,1, 0, 1");
  VectorFloat expected_output(L"0,0,48,32,32,0,32,16");

  str.assign(L"3, 14-19, 21");
  Mask mask(str);
  
  // SELECT algorithm
  //
  mask.setAlgorithm(SELECT);
  mask.setImplementation(INDEX);
  mask.compute(output, input);
  if (!output.almostEqual(expected_output)) {
    expected_output.debug(L"expected vector");
    output.debug(L"output vector");
    mask.debug(L"Current Mask");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  str.assign(L"0-2, 4-13, 20");
  
  // REMOVE algorithm
  //
  mask.setAlgorithm(REMOVE);
  mask.set(str);
  mask.compute(output, input);
  if (!output.almostEqual(expected_output)) {
    expected_output.debug(L"expected vector");
    output.debug(L"output vector");
    mask.debug(L"Current Mask");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  //test for vector of zeros
  //
  input.assign(L"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0");
  expected_output.assign(L"0,0,0,0,0,0,0,0");

  str.assign(L"3, 14-19, 21");
  mask.set(str);
  
  // SELECT algorithm
  //
  mask.setAlgorithm(SELECT);
  mask.setImplementation(INDEX);
  mask.compute(output, input);
  if (!output.almostEqual(expected_output)) {
    expected_output.debug(L"expected vector");
    output.debug(L"output vector");
    mask.debug(L"Current Mask");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  str.assign(L"0-2, 4-13, 20");
  
  // REMOVE algorithm
  //
  mask.setAlgorithm(REMOVE);
  mask.set(str);
  mask.compute(output, input);
  if (!output.almostEqual(expected_output)) {
    expected_output.debug(L"expected vector");
    output.debug(L"output vector");
    mask.debug(L"Current Mask");
    return Error::handle(name(), L"compute", 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;
}
