// file: $isip/class/pr/NGramNode/nnode_02.cc
// version: $Id: nnode_02.cc 8501 2002-07-30 05:03:25Z zheng $
//

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

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

  if (!nnode1.eq(nnode0)) {
    return Error::handle(name(), L"copy constructor", Error::TEST,
			 __FILE__, __LINE__);
  }

  // test large allocation construction and deletion
  //
  if (level_a > Integral::BRIEF) {
    
    Console::put(L"testing large chunk memory allocation and deletion:\n");
    
    // set the memory to a strange block size so we can hopefully catch any
    // frame overrun errors
    //
    NGramNode::setGrowSize((long)500);
    
    NGramNode* pft = new NGramNode();
    
    for (long j = 1; j <= 100; j++) {
      NGramNode** pfts = new NGramNode*[j * 100];
      
      // create the objects
      //
      for (long i = 0; i < j * 100; i++) {
	pfts[i] = new NGramNode();
      }
    
      // delete objects
      //
      for (long i = (j * 100) - 1; i >= 0; i--) {
	delete pfts[i];
      }
    
      delete [] pfts;
    } 
    delete pft;
  }

  // test the i/o methods 
  //
  NGramNode node1, node2, node3;
  NGramNode rd_node1, rd_node2;
  HashTable<Long, NGramNode>* ht1;
  ht1 = new HashTable<Long, NGramNode>;
  ht1->setCapacity(3);

  node1.setNextGram(ht1);
  ht1->insert(2, &node2);
  ht1->insert(3, &node3);
  
  // 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);

  if (level_a > Integral::BRIEF) {  
    node1.debug(L"node1");
  }

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

  rd_node2.read(tmp_file1, (long)0);
  if (!rd_node2.eq(node1)) {
    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. 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;
}
