/*
 * Instituto Superior de Engenharia do Porto
 *
 * Estruturas de Informação
 *
 * 2000/2001
 *
 * ------------------------------------------
 *
 * Programa de teste de um Heap de valores inteiros
 *
 * HeapV_test.cpp
 *
 */

#include <string>
#include <iostream>
using namespace std;

#include "livro.h"


#include "HeapV.h"

void teste_1()
{
	cout << endl << "Teste simples" << endl;

  CKeyHeapV<int> heap;

  heap.insert( 10 );
  heap.insert(  9 );
  heap.insert( 15 );
  heap.insert(  4 );
  heap.insert( 81 );
  heap.insert(  2 );
  heap.insert(  5 );
  heap.insert( 98 );
  heap.insert(  6 );

  int val;
  while( !heap.empty() )
    {
      heap.extract_min( val );
      cout << val << endl;
    }

  cin.get();
}


void teste_2()
{  cout << endl << "Teste com Livro" << endl;

  CKeyHeapV<string, Livro> heap2;

  heap2.insert( Livro("C++", 10, "123"), "123");
  //heap2.debug_print();
  heap2.insert( Livro("Data Structures", 30, "345"), "345");
  //heap2.debug_print();
  heap2.insert( Livro("Visual Basic", 100, "051"), "051");
  //heap2.debug_print();
  heap2.insert( Livro("C & C++", 40, "681"), "681");
  //heap2.debug_print();
  heap2.insert( Livro("From Java to C++", 10, "071"), "071");
  //heap2.debug_print();
  heap2.insert( Livro("Java", 5, "100"), "100");
  //heap2.debug_print();
  heap2.insert( Livro("C++ for Dummys", 1, "546"), "546");
  //heap2.debug_print();
  heap2.insert( Livro("Visual C++", 140, "001"), "001");
  //heap2.debug_print();

  string isbn;
  Livro l;
  while( !heap2.empty() )
    {
      heap2.extract_min( l, isbn );
      cout << isbn << " - " << l << endl;
	  //heap2.debug_print();
  }

  cin.get();
}

void heapsort(int vec[], int nSize)
{
	//colocar todos os elementos no heap
	CKeyHeapV<int>	heap;
	for (int i = 0; i < nSize; i++)
		heap.insert(vec[i]);

	//ao extrair vem tudo ordenado
	for (i = 0; i < nSize; i++)
	{
		int valor;
		heap.extract_min(valor);
		vec[i] = valor;
	}
}

void teste_heap_sort()
{
	cout << endl << "teste HeapSort" << endl;

	int vec[1000];

	srand(100);
	for (int i = 0; i < sizeof(vec)/sizeof(int); i++)
		vec[i] = rand();

	heapsort(vec, sizeof(vec)/sizeof(int));

	//verificar se está ordenado
	for (i = 1; i < sizeof(vec)/sizeof(int); i++)
	{
		if (vec[i-1] > vec[i])
			cout << "ERRO " << i;
	}

	cin.get();
}

#include <ctime>
#include "sistemaoperativo.h"

void teste_scheduler()
{
	srand((int)clock());

	CSistemaOperativo so;

	//criar alguns processos iniciais no sistema operativo fantasma
	so.CriaProcesso("cursor", 2);
	so.CriaProcesso("defrag");
	so.CriaProcesso("economy");
	so.CriaProcesso("network", 5);

	char op;
	int runs = 0;
	do {
		cout << "*** Ciclo: " << runs << " (n. procs: " << so.numProcs() << ")" << endl;
		//executar o scheduler 
		so.Schedule();

		//dinamicamente alterar prioridade de alguns processos, criar e matar outros
		int r = rand();
		if (r % 100 <= 10)
		{
			//criar processo
			string pname("xpto");
			char n[10];
			sprintf(n, "%d", runs);
			pname += n;
			so.CriaProcesso(pname, rand());
		}
		if (r % 100 <= 17)
		{
			//matar processo
			//ignorar processos de sistema (iniciais)
			so.MataProcesso(rand() % so.getLastProcId() + 4);
		}
		if (r % 100 <= 50)
		{
			//alterar prioridade processo
			so.AlteraPrioridade(rand() % so.getLastProcId(), rand());
		}
		runs++;
		if (runs % 5 == 0)
			op = cin.get();
	} while (op != 'x');

	//terminou
	cin.get();
}

int main()
{
	//teste_1();

	//teste_2();

	//teste_heap_sort();

	teste_scheduler();

  return 0;
}


