#include <ctime>
#include "genetic_operators.h"
#include "generic_individual.h"
#include "individual.h"
#include "population_block.h"
#include "genetic_operators_test.h"

using namespace std;

/**
 * performs a series of tests and returns on the first error
 * with a corresponding error code
 */
int genetic_operators_test::test(int p_cells) {
  RANDOM_GENERATOR rg (time(0));
  int size = p_cells * (individual::get_block_size() + 1);
  
  // prepare the test chromosomes
  GENE chr_1 [size];
  for(int i = 0; i < size; i++) chr_1[i] = 0;
  individual ind_1 (TYPE_RULES_SEED, p_cells, 1.0, chr_1);
  GENE chr_2 [size];
  for(int i = 0; i < size; i++) chr_2[i] = 1;
  individual ind_2 (TYPE_RULES_SEED, p_cells, 1.0, chr_2);

  // test the single point crossover
  population_block *tmp_blk = genetic_operators::cross_over(
      CO_TYPE_SINGLE_POINT, &ind_1, &ind_2, &rg);

  while(tmp_blk != 0) {
    individual *tmp_ind = tmp_blk->get_individual();
    tmp_ind->print();
    delete tmp_ind;

    population_block *tmp = tmp_blk;
    tmp_blk = tmp_blk->get_next();
    delete tmp;
  }

  // test the double point crossover
  tmp_blk = genetic_operators::cross_over(CO_TYPE_DOUBLE_POINT,
      &ind_1, &ind_2, &rg);

  while(tmp_blk != 0) {
    individual *tmp_ind = tmp_blk->get_individual();
    tmp_ind->print();
    delete tmp_ind;

    population_block *tmp = tmp_blk;
    tmp_blk = tmp_blk->get_next();
    delete tmp;
  }

  // test the uniform crossover
  tmp_blk = genetic_operators::cross_over(CO_TYPE_UNIFORM,
      &ind_1, &ind_2, &rg);

  while(tmp_blk != 0) {
    individual *tmp_ind = tmp_blk->get_individual();
    tmp_ind->print();
    delete tmp_ind;

    population_block *tmp = tmp_blk;
    tmp_blk = tmp_blk->get_next();
    delete tmp;
  }

  // test the mutation
  individual *tmp_ind = genetic_operators::mutate(&ind_1, &rg);
  tmp_ind->print();
  delete tmp_ind;

  return TGO_OK;
}

