// file: $isip/class/asr/Anchor/ancr_02.cc
//

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

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

  // test the constructors
  //
  String name_00(L"Anchor00");
  String name_01(L"Anchor01");

  String unit_00(L"seconds");
  String unit_01(L"milliseconds");    
  
  Anchor ancr_00;
  Anchor ancr_01(name_00, (float)0.1, unit_00);
  Anchor ancr_02(name_01, &ancr_01);    

  // test the id method
  //
  if (!ancr_00.getId().eq(L"")) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!ancr_01.getId().eq(name_00)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!ancr_02.getId().eq(name_01)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  // test the offset methods
  //
  if (!Integral::almostEqual(ancr_00.getOffset(), (float)0.0)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!Integral::almostEqual(ancr_01.getOffset(), (float)0.1)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!Integral::almostEqual(ancr_02.getOffset(), (float)0.1)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }  

  ancr_02.setOffset(0.6);
  if (!Integral::almostEqual(ancr_02.getOffset(), (float)0.6)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!ancr_02.getAnchored()) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  ancr_02.unsetOffset();
  if (ancr_02.getAnchored()) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  // test the unit method
  //
  if (!ancr_00.getUnit().eq(L"")) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!ancr_01.getUnit().eq(unit_00)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!ancr_02.getUnit().eq(unit_00)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  ancr_00.setUnit(unit_01);
  if (!ancr_00.getUnit().eq(unit_01)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  // test the i/o methods
  //
  Anchor ancr_03;
  Anchor ancr_04;  

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

  ancr_01.write(tmp_file0, 0);
  ancr_01.write(tmp_file1, 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 anchor from the text/binary files
  //
  ancr_03.read(tmp_file0, 0);
  ancr_04.read(tmp_file1, 0);

  if (!ancr_03.eq(ancr_01)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!ancr_04.eq(ancr_01)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  // close and  delete the temporary files
  //
  tmp_file0.close();
  tmp_file1.close();
  
  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }

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