// src/testsuite/test-statistics.cc - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2. If not, see http://www.gnu.org/licenses/.


/* This executable tests to check if the calculus for the statistics are exact,
 * and nothing else. */

#include "config.h"
#include "gettext.h"

#include <vector>
#include <cassert>
#include <iostream>

#include "sgpemv2/simulation.hh"
#include "sgpemv2/cpu_policies_gatekeeper.hh"
#include "sgpemv2/resource_policies_gatekeeper.hh"
#include "sgpemv2/cpu_policy_manager.hh"
#include "sgpemv2/resource_policy_manager.hh"
#include "sgpemv2/cpu_policy.hh"
#include "sgpemv2/resource_policy.hh"
#include "sgpemv2/plugin_manager.hh"
#include "sgpemv2/statistics.hh"
#include "sgpemv2/process_statistics.hh"
#include "sgpemv2/thread_statistics.hh"
#include "sgpemv2/module.hh"

#include <glibmm/module.h>
#include <glibmm/ustring.h>
#include <glibmm/thread.h>

using namespace sgpem;
using namespace std;
using Glib::ustring;



int
main(int, char**)
{
  // Set up Glib thread support
  Glib::thread_init();

  ostream& info = cout;
  ostream& test = cerr;

  using namespace sgpem;

  // Initialize all preferences and load plugins
  PluginManager::get_instance();

  vector<Module*> modules = PluginManager::get_instance().get_module_list();

  for (vector<Module*>::iterator it = modules.begin(); it != modules.end(); ++it)
    (*it)->set_enabled(true);

  Simulation& sim=Simulation::get_instance();

  History& hi=sim.get_history();

  Process& pz1=hi.add_process("P1",0,2);
  hi.add_thread("T1",pz1,3,0,2);

  Process& pz2=hi.add_process("P2",1,3);
  hi.add_thread("T2",pz2,7,1,3);

  Process& pz3=hi.add_process("P3",3,5);
  hi.add_thread("T3",pz3,4,3,5);

  Process& pz4=hi.add_process("P4",5,3);
  hi.add_thread("T4",pz4,6,5,4);

  Process& pz5=hi.add_process("P5",8,2);
  hi.add_thread("T5",pz5,2,8,2);

  cout<<sim.get_mode();


  typedef CPUPoliciesGatekeeper::Managers CpuManagerVec;
  typedef CPUPoliciesGatekeeper::Manager::Policies CpuPolicyVec;

  CpuManagerVec cpu_managers = CPUPoliciesGatekeeper::get_instance().get_registered();
  CpuPolicyVec cpu_policies = cpu_managers[0]->get_avail_policies();
  CPUPolicy* cpu_p=cpu_policies[0];		
  sim.set_policy(cpu_p);

  typedef ResourcePoliciesGatekeeper::Managers ResourceManagerVec;
  typedef ResourcePoliciesGatekeeper::Manager::Policies ResourcePolicyVec;

  ResourceManagerVec resource_managers = ResourcePoliciesGatekeeper::get_instance().get_registered();
  ResourcePolicyVec resource_policies = resource_managers[0]->get_avail_policies();
  ResourcePolicy* resource_p=resource_policies[0];		
  sim.set_resource_policy(resource_p);
  sim.run();


  info << "Simulation Started\n";
  info<<"Starting to test the statistics claculus regarding the simulation\n";
  
  Statistics::get_instance().calculateStatisticsAt(1);
  
  const SimulationStatistics* stats = Statistics::get_instance().get_simulation_statistics();
  info << "Checking simulation statisics... ";
  test<<"Checking method get_average_inactivity_time()";
  if(stats->get_average_inactivity_time()==6.00)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
  test<<"Checking method get_average_turn_around()";
  if(stats->get_average_turn_around()==10.40)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
  test<<"Checking method get_average_response_time()";
  if(stats->get_average_response_time()==6.00)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
  test<<"Checking method get_terminated_processes()";
  if(stats->get_terminated_processes()==5)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
  test<<"Checking method get_terminated_threads()";
  if(stats->get_terminated_threads()==5)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
  
  info<<"Checking process statistics.........";
  
  const std::vector<const ProcessStatistics*> procs=Statistics::get_instance().get_process_statistics(); 
  test<<"Checking method get_execution_time()";
  if(procs[0]->get_execution_time()==3)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
  test<<"Checking get_total_inactivity()";
  if(procs[1]->get_total_inactivity()==2)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
  test<<"Checking get_response_time()";
  if(procs[2]->get_response_time()==7)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
  test<<"Checking get_average_response_time()";
  if(procs[2]->get_response_time()==7)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
//  test<<"Checking get_turn_around()";
//  if(procs[5]->get_turn_around()==14)
//	  test<<"PASS \n";
//  else
//	  test<<"FAIL \n";
  
  test<<"Checking get_resource_usage_time()";
  if(procs[3]->get_turn_around()==0)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
  
  
  info<<"Starting to check thread statistics of the process P2....";
  const std::vector<const ThreadStatistics*> thstat=procs[1]->get_threads_statistics();
  
  test<<"Checking get_threads_statistics()";
  if(thstat.size()==1)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
  test<<"Checking get_execution_time()";
  if(thstat[0]->get_execution_time()==7)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
  test<<"Checking get_response_time()";
  if(thstat[0]->get_response_time()==2)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
  
  test<<"Checking get_turn_around()";
  if(thstat[0]->get_turn_around()==9)
	  test<<"PASS \n";
  else
	  test<<"FAIL \n";
  
  
  
  return 0;
}
