// file: $isip/class/mmedia/Annotation/anno_02.cc
// version: $Id: anno_02.cc 8541 2002-08-08 19:50:51Z gao $
//

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

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

  Anchor start_01;
  Anchor end_01;  
  
  String name_00(L"Annotation01");
  String name_01(L"Annotation02");  
  String type_00(L"W/Jane");  
  
  Annotation anno_00;
  Annotation anno_01(name_00, &start_00, &end_00, type_00);
  Annotation anno_02(name_01, &anno_01);  

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

  // test the anchor methods
  //
  if (anno_01.getStartAnchor() != &start_00) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (anno_01.getEndAnchor() != &end_00) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (anno_02.getStartAnchor() != &start_00) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (anno_02.getEndAnchor() != &end_00) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }  

  anno_00.setStartAnchor(&start_01);
  anno_00.setEndAnchor(&end_01);  
  if (anno_00.getStartAnchor() != &start_01) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (anno_00.getEndAnchor() != &end_01) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  // test the type method
  //
  if (!anno_00.getType().eq(L"")) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_01.getType().eq(type_00)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_02.getType().eq(type_00)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  // declare some feature-value pairs
  //
  Vector<String> features;
  
  String feat_00(L"feature1");
  String feat_01(L"feature2");
  String feat_02(L"feature3");  
  String value_00(L"value1");
  String value_01(L"value2");
  String value_02(L"value3");    
  
  // test the set feature methods
  //
  if (!anno_00.setFeature(feat_00, value_00)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_00.setFeature(feat_01, value_01)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_00.setFeature(feat_02, value_02)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  // test the exists feature methods
  //
  if (!anno_00.existsFeature(feat_00)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_00.existsFeature(feat_01)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_00.existsFeature(feat_02)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }  

  // test the feature retrival methods
  //
  if (!anno_00.getFeature(feat_00).eq(value_00)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_00.getFeature(feat_01).eq(value_01)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_00.getFeature(feat_02).eq(value_02)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  
  // test the delete feature methods
  //
  if (!anno_00.deleteFeature(feat_02)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_00.existsFeature(feat_00)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_00.existsFeature(feat_01)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }  
  if (anno_00.existsFeature(feat_02)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  // test the get feature names method
  //
  if (!anno_00.getFeatureNames(features)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (features.length() != 2) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!features.contains(&feat_00)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!features.contains(&feat_01)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }    
  
  // test the unset features method
  //
  if (!anno_00.unsetFeatures()) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_00.getFeature(feat_00).eq(String::EMPTY)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_00.getFeature(feat_01).eq(String::EMPTY)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }

  // test the i/o methods
  //
  Annotation anno_03;
  Annotation anno_04;  

  // we need binary and text sof files
  //
  String tmp_filename0;
  Integral::makeTemp(tmp_filename0);
  String tmp_filename1;
  Integral::makeTemp(tmp_filename1);
  
  // set some features
  //
  if (!anno_01.setFeature(feat_00, value_00)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_01.setFeature(feat_01, value_01)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  if (!anno_01.setFeature(feat_02, value_02)) {
    return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__);
  }
  
  // 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);

  anno_01.write(tmp_file0, 0);
  anno_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
  //
  anno_03.read(tmp_file0, 0);
  anno_04.read(tmp_file1, 0);

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