#include "BistProblemTest.h"
#include "../BistProblem.h"
#include <cstdio>

unit_test::BistProblemTest::BistProblemTest() :
    UnitTest(), chromosome( NULL ) { /* Empty */ }
unit_test::BistProblemTest::~BistProblemTest() {
  if ( chromosome )
    delete[] chromosome;
}

unsigned int unit_test::BistProblemTest::setChromosomeRule30() {
  if ( chromosome )
    delete[] chromosome;
  unsigned int n = 5u * 2u;
  unsigned int middle = ( n / 2u ) - 1u;
  chromosome = new AlleleType[n];
  for ( unsigned int i = 0u; i < n; i += 2u ) {
    chromosome[i] = ( i == middle ) ?
                    static_cast<AlleleType>( 1 ) : static_cast<AlleleType>( 0 );
    chromosome[i+1u] = static_cast<AlleleType>( 3 );
  }
  return n;
}

int unit_test::BistProblemTest::run( int argc, char *argv[] ) {
  std::string fn = "c17.bench.vct";
  BistProblem bp( fn );
  unsigned int n = bp.getNumberOfGenes();
  std::printf( "numberOfGenes::%d\n", n );
  const AlleleType *noa = bp.getNumberOfAlleles();
  for ( unsigned int i = 0u;i < n;i++ )
    std::printf( "numberOfAlleles[%2d]::%d\n", i, noa[i] );
  setChromosomeRule30();
  {
    std::printf( "chromosome: [" );
    for (unsigned int i = 0u; i < n; i++ )
      std::printf( "%4u", chromosome[i] );
    std::printf( "]\n" );
  }
  FloatType f = bp.getFitness( chromosome );
  std::printf( "---Fitness: %.1f\n", f );
  {
    std::printf( "chromosome: [" );
    for (unsigned int i = 0u; i < n; i++ )
      std::printf( "%4u", chromosome[i] );
    std::printf( "]\n" );
  }
  return 0;
}

int main( int argc, char *argv[] ) {
  unit_test::BistProblemTest test;
  return test.run( argc, argv );
}
