// file: $isip/class/asr/MachineDatabase/madb_02.cc
//

// isip include files
//
#include "MachineDatabase.h"
#include <Console.h>
#include <Filename.h>

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

  // declare the database object
  //
  String val;
  MachineDatabase mdb;
  val.assign(L"XYZ");

  // test the database name methods
  //
  mdb.setDataBaseName(val);

  if (!mdb.getDataBaseName().eq(val)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  // retrieve the list of machines in the database
  //
  Vector<Machine>& machines = mdb.getDataBase();

  if (!mdb.isEmpty()) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  // set the number of machines
  //
  machines.setLength(2);

  // set the attributes for the first machine
  //
  VectorDouble speed_00(1);
  machines(0).setProtocol(Machine::REMOTE_SHELL);
  
  val.assign(L"isip100.isip.msstate.edu");
  machines(0).setNodeName(val);

  val.assign(L"i86pc");
  machines(0).setArchitecture(val);

  machines(0).setNumProcessors((long)1);
  speed_00(0) = (double)8.0e8;

  machines(0).setProcessorSpeed(speed_00);

  machines(0).setMainMemory((double)5.12e8);

  machines(0).setSwapMemory((double)9.2e7);

  val.assign(L"SunOS");
  machines(0).setOS(val);

  val.assign(L"Generic_106542-20");
  machines(0).setOSVersion(val);

  // set the attributes of the second machine
  //
  VectorDouble speed_01(2);    
  machines(1).setProtocol(Machine::REMOTE_SHELL);
  
  val.assign(L"isip217.isip.msstate.edu");
  machines(1).setNodeName(val);
  
  val.assign(L"i86pc");
  machines(1).setArchitecture(val);
  
  machines(1).setNumProcessors((long)2);
  speed_01(0) = (double)1.0e9;
  speed_01(1) = (double)1.0e9;
  
  machines(1).setProcessorSpeed(speed_01);
  
  machines(1).setMainMemory((double)2.048e9);
  
  machines(1).setSwapMemory((double)3.0e7);
  
  val.assign(L"SunOS");
  machines(1).setOS(val);
  
  val.assign(L"Generic_108529-14");
  machines(1).setOSVersion(val);  

  // test the i/o methods
  //
  Filename file_text;
  Filename file_bin;

  Integral::makeTemp(file_text);
  Integral::makeTemp(file_bin);

  // write the database in ascii and binary
  //
  Sof sof0;
  sof0.open(file_text, File::WRITE_ONLY, File::TEXT);
  Sof sof1;
  sof1.open(file_bin, File::WRITE_ONLY, File::BINARY);
    
  mdb.write(sof0, 0);
  mdb.write(sof1, 0);
  
  sof0.close();
  sof1.close();  

  // read the database in ascii and binary
  //
  sof0.open(file_text);
  sof1.open(file_bin);

  MachineDatabase mdb0;
  MachineDatabase mdb1;
  
  mdb0.read(sof0, 0);
  mdb1.read(sof1, 0);
  
  sof0.close();
  sof1.close();

  // verify that the databases are the same
  //
  if (!mdb.eq(mdb0)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  if (!mdb.eq(mdb1)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }  
  
  // 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;
}
