#include <ctime>
#include "generic_individual.h"
#include "individual.h"
#include "population_block.h"
#include "population_block_test.h"

using namespace std;

/**
 * performs a series of tests and returns on the first error
 * with a corresponding error code
 */
int population_block_test::test(int p_cells) {
  RANDOM_GENERATOR rg (time(0)); // defined in individual.h

  // test the first constructor and the getters
  population_block pop_blk_1 (TYPE_RULES_SEED, p_cells, 0, &rg);
  individual *tmp_ind_1 = pop_blk_1.get_individual();

  if((tmp_ind_1->get_type() != TYPE_RULES_SEED)
      || (tmp_ind_1->get_cells() != p_cells)
      || (pop_blk_1.get_next() != 0)) {
    delete tmp_ind_1;
    return TPB_INIT_ERROR;
  }
  else delete tmp_ind_1;

  // test the second constructor and the getters
  individual ind_1 (TYPE_RULES_SEED, p_cells, &rg);
  population_block pop_blk_2 (&ind_1, &pop_blk_1);
  individual *tmp_ind_2 = pop_blk_2.get_individual();

  if(!tmp_ind_2->compare(&ind_1)
      || (pop_blk_2.get_next() != &pop_blk_1)) {
    delete tmp_ind_2;
    return TPB_INIT_ERROR;
  }
  else delete tmp_ind_2;

  // test the setter
  individual ind_2 (TYPE_RULES_SEED, p_cells, &rg);
  population_block pop_blk_3 (&ind_2, &pop_blk_2);

  if((pop_blk_1.set_next(&pop_blk_3) != &pop_blk_3)
      || (pop_blk_1.get_next() != &pop_blk_3))
    return TPB_SET_NEXT_ERROR;

  return TPB_OK;
}

