ó
"ˆIc           @   sÔ   d  Z  d d l Z d d l m Z d d l m Z d d l m Z d e j f d „  ƒ  YZ d „  Z	 d	 „  Z
 d
 e j f d „  ƒ  YZ d e j f d „  ƒ  YZ e d k rÐ e j d d ƒ Z e j d e ƒ n  d S(   s9   Tests for an Organism in a Genetic Algorithm population.
iÿÿÿÿN(   t   Alphabet(   t
   MutableSeq(   t   Organismt   TestAlphabetc           B   s    e  Z d  Z d d d d g Z RS(   s'   Simple alphabet for test purposes.
    t   1t   2t   3t   4(   t   __name__t
   __module__t   __doc__t   letters(    (    (    s   test_GAOrganism.pyR      s   c           C   s   t  d t ƒ  ƒ S(   s,   Generate a genome for testing purposes.
    t   1234(   R   R   (    (    (    s   test_GAOrganism.pyt   genome_generator   s    c         C   s4   t  |  t ƒ s t d ƒ ‚ |  j ƒ  } t | j ƒ S(   s,   Calculate fitness for testing purposes.
    s!   Expected MutableSeq for a genome.(   t
   isinstanceR   t   AssertionErrort   toseqt   intt   data(   t   genomet   regular_seq(    (    s   test_GAOrganism.pyt   fitness_calculator   s    t   CreatePopulationTestc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s:   Tests for utility functions for creating populations.
    c         C   s   t  ƒ  |  _ d  S(   N(   R   t   alphabet(   t   self(    (    s   test_GAOrganism.pyt   setUp'   s    c         C   s·   d } t  j t | t ƒ } t | ƒ | k sI t d | t t ƒ f ƒ ‚ xg | D]_ } t | t  j  ƒ sx t d | ƒ ‚ t | j ƒ } | j	 | k sP t d | j	 | f ƒ ‚ qP Wd S(   sB   Create a population using a function to generate genomes.
        i
   s   Expected %s organisms, got %ss#   Expected to get an organism, got %rs   Expected fitness of %s, got %sN(
   R   t   function_populationR   R   t   lenR   t   new_popsR   R   t   fitness(   R   t   num_orgst   new_popt   orgt   exp_fit(    (    s   test_GAOrganism.pyt   test_function_population*   s    	c         C   s÷   d } d } t  j |  j | | t ƒ } t | ƒ | k sU t d | t t ƒ f ƒ ‚ x› | D]“ } t | t  j  ƒ s„ t d | ƒ ‚ t | j ƒ } | j	 | k s» t d | j	 | f ƒ ‚ t | j ƒ | k s\ t d t | j ƒ | f ƒ ‚ q\ Wd S(   s6   Create a population randomly from a alphabet.
        i
   i   s   Expected %s organisms, got %ss#   Expected to get an organism, got %rs   Expected fitness of %s, got %ss"   Expected genome size of %s, got %sN(
   R   t   random_populationR   R   R   R   R   R   R   R   (   R   R   t   genome_sizeR   R    R!   (    (    s   test_GAOrganism.pyt   test_random_population<   s    c         C   s‡   d d d „  ƒ  Y} d d d „  ƒ  Y} d d d „  ƒ  Y} d „  } | ƒ  | ƒ  | ƒ  g } x& | D] } t  j | d d	 | ƒ } qa Wd
 S(   sH   Creating a random population with different types of alphabets.
        t   DoubleAlphabetc           B   s   e  Z d  d g Z RS(   g      ð?g       @(   R   R	   R   (    (    (    s   test_GAOrganism.pyR&   V   s   t   CharacterAlphabetc           B   s   e  Z d  d g Z RS(   t   at   b(   R   R	   R   (    (    (    s   test_GAOrganism.pyR'   Y   s   t   IntegerAlphabetc           B   s   e  Z d  d g Z RS(   i   i   (   R   R	   R   (    (    (    s   test_GAOrganism.pyR*   \   s   c         S   s   d S(   Ni   (    (   R   (    (    s   test_GAOrganism.pyt   test_fitness_   s    i   i
   N(    (    (    (   R   R#   (   R   R&   R'   R*   R+   t   all_alphabetsR   R   (    (    s   test_GAOrganism.pyt   test_random_population_typesS   s    	(   R   R	   R
   R   R"   R%   R-   (    (    (    s   test_GAOrganism.pyR   $   s
   			t   OrganismTestc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s.   Tests for an organism in a GA population.
    c         C   s=   t  ƒ  |  _ t d |  j ƒ |  _ t j |  j t ƒ |  _ d  S(   NR   (   R   R   R   R   R   R   t   organism(   R   (    (    s   test_GAOrganism.pyR   l   s    c         C   s‚   t  d |  j ƒ } t j | t ƒ } t  d |  j ƒ } t j | t ƒ } |  j | k sc t d ƒ ‚ |  j | k s~ t d ƒ ‚ d S(   s/   Exercise basic organism functionality.
        R   t   1111s0   Comparison doesn't work for identical organisms.s/   Comparison doesn't work for different organism.N(   R   R   R   R   R/   R   (   R   t   same_genomet   same_organismt
   dif_genomet   dif_organism(    (    s   test_GAOrganism.pyt   test_organism_basicq   s    	c         C   s   |  j  j d k s( t d |  j  j ƒ ‚ t d |  j ƒ } | |  j  _ |  j  j ƒ  |  j  j d k s{ t d |  j  j ƒ ‚ d S(   sA   Test the ability to deal with the fitness of the genome.
        iÒ  s   Unexpected fitness %sR0   iW  N(   R/   R   R   R   R   R   t   recalculate_fitness(   R   t
   new_genome(    (    s   test_GAOrganism.pyt   test_organism_fitness€   s    c         C   sD   |  j  j ƒ  } | j j d ƒ | j |  j  j k s@ t d ƒ ‚ d S(   s#   Test copying of organisms.
        R   s'   Did not provide a copy of the organism.N(   R/   t   copyR   t   appendR   (   R   t   new_organism(    (    s   test_GAOrganism.pyt   test_organism_copy   s    c         C   sd   d „  } |  j  j } y" t j | | ƒ } t d ƒ ‚ Wn t k
 rJ n Xt j | | d ƒ } d S(   s<   Test that providing a pre-calculated fitness works.
        c         S   s   t  d ƒ ‚ d  S(   Ns   Should not have been executed.(   t
   ValueError(   R   (    (    s   test_GAOrganism.pyt   fitness_calcš   s    s(   Did not calculate fitness when expected.i2   N(   R/   R   R   R   R=   (   R   R>   R   t   new_org(    (    s   test_GAOrganism.pyt   test_provide_fitness—   s    	(   R   R	   R
   R   R5   R8   R<   R@   (    (    (    s   test_GAOrganism.pyR.   i   s   				
t   __main__t	   verbosityi   t
   testRunner(   R
   t   unittestt   BioR    t   Bio.SeqR   t   Bio.GAR   R   R   R   t   TestCaseR   R.   R   t   TextTestRunnert   runnert   main(    (    (    s   test_GAOrganism.pyt   <module>   s   		E@