// file: $isip/class/pr/ProductionRuleTokenType/prtt_02.cc
// version: $Id: prtt_02.cc 10291 2005-11-09 18:05:29Z wholland $

// ISIP include files
//
#include "ProductionRuleTokenType.h"

// method: diagnose
//
// arguments:
//  Integral::DEBUG level: (input) debug level for diagnostics
//
// return: a boolean value indicating status
//
boolean ProductionRuleTokenType::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 indention
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing required public methods...\n");
    Console::increaseIndention();
  }
  
  // test constructor
  //
  ProductionRuleTokenType prtt;
  
  prtt.setType(ProductionRuleTokenType::ALTERNATION);
  
  // test clear method
  //
  prtt.clear(Integral::RESET);
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing clear method...");
  }
  
  // check if it worked
  //
  if(prtt.getType()!=ProductionRuleTokenType::NONE) {
    return Error::handle(name(), L"clear", Error::TEST, __FILE__, __LINE__);
  }
  
  // test eq method
  //
  ProductionRuleTokenType prtt2;
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing eq method...");
  }
  
  if(!prtt2.eq(prtt)) {
    return Error::handle(name(), L"eq", Error::TEST, __FILE__, __LINE__);
  }
  
  // test assign
  //
  prtt2.setType(ProductionRuleTokenType::ALTERNATION);
  prtt.assign(prtt2);
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing assign method...");
  }
  
  if(prtt.getType()!=ProductionRuleTokenType::ALTERNATION) {
    return Error::handle(name(), L"assign", Error::TEST, __FILE__, __LINE__);
  } 
  
  // reset indention
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //--------------------------------------------------------------------------
  //
  // 2. class-specific public methods:
  //     
  //
  //--------------------------------------------------------------------------

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

  //--------------------------------------------------------------------------
  //
  // 3. Test file i/o
  //
  //--------------------------------------------------------------------------    
  
  // set indention
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing file i/o methods...\n");
    Console::increaseIndention();
  }
  
  // test file i/o methods
  Filename out_file(L"testout1.sof");
  SysString temp_file1(L"testout1.sof");
  File::registerTemp(temp_file1);
  
  Sof sof;

  if (level_a > Integral::BRIEF) {
    Console::put(L"testing ascii write...");
  }  
  // test text
  //
  if(!sof.open(out_file, File::WRITE_ONLY)) {
    return Error::handle(out_file, L"open", Error::TEST,
			 __FILE__, __LINE__);
  }
  
  prtt.write(sof, 0);
  sof.close();

  if (level_a > Integral::BRIEF) {
    Console::put(L"testing ascii read...");
  }  
  
  if(!sof.open(out_file, File::READ_ONLY)) {
    return Error::handle(out_file, L"open", Error::TEST,
			 __FILE__, __LINE__);
  }

  prtt2.read(sof, 0);
  sof.close();

  if (!prtt.eq(prtt2)) {
    return Error::handle(name(), L"open", Error::TEST,
			 __FILE__, __LINE__);
  }

  // test binary
  //
  prtt2.clear(Integral::RESET);

  out_file=L"testout2.sof";
  SysString temp_file2(L"testout2.sof");
  File::registerTemp(temp_file2);
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing binary write...");
  }    
  if(!sof.open(out_file, File::WRITE_ONLY, File::BINARY)) {
    return Error::handle(out_file, L"open", Error::TEST,
			 __FILE__, __LINE__);
  }

  prtt.write(sof, 0);
  sof.close();

  if (level_a > Integral::BRIEF) {
    Console::put(L"testing binary read...");
  }    
  if(!sof.open(out_file, File::READ_ONLY)) {
    return Error::handle(out_file, L"open", Error::TEST,
			 __FILE__, __LINE__);
  }

  prtt2.read(sof, 0);
  sof.close();

  if (!prtt.eq(prtt2)) {
    return Error::handle(name(), L"open", Error::TEST,
			 __FILE__, __LINE__);
  }
  
  

  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  
  //--------------------------------------------------------------------------
  //
  // 4. Clean up.
  //
  //--------------------------------------------------------------------------    

  File::cleanTemps();
  
  // reset indention
  //
  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);
  }
  
  // indicate success
  //
  return true;
}
