B
    u9a_                 @   s:  d dl Z d dlZ d dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
mZmZmZmZmZ d dlmZ d dlmZ G dd dZye   W n ek
r   dZY nX d	Ze ed
G dd dee jZG dd dee jZde d fddZG dd de jZG dd de jZedkr6e    dS )    N)partial)logexppifsumsin	factorial)support)Fractionc               @   s   e Zd Zdd Zdd Zdd Zdd Zej	d	d
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&S )'TestBasicOpsc                s    fddt |D S )z0Helper function to make a list of random numbersc                s   g | ]} j  qS  )genrandom).0i)selfr   !/usr/lib/python3.7/test_random.py
<listcomp>   s    z+TestBasicOps.randomlist.<locals>.<listcomp>)range)r   nr   )r   r   
randomlist   s    zTestBasicOps.randomlistc             C   sB   | j   | j  }td | j   | j  }| || d S )Ng?)r   seedgetstatetimeZsleepassertNotEqual)r   Zstate1Zstate2r   r   r   test_autoseed   s    




zTestBasicOps.test_autoseedc             C   sD   d}| j   | j  }| |}| j | | || | d S )Ni  )r   r   r   r   setstateassertEqual)r   NstateZrandseqr   r   r   test_saverestore   s    


zTestBasicOps.test_saverestorec             C   s   G dd dt }x:d dddddddddd	d
td| gD ]}| j| q8W x0ttdtddgD ]}| t| jj| qdW | t| jjdddd | tt	| jg  d S )Nc               @   s   e Zd Zdd ZdS )z*TestBasicOps.test_seedargs.<locals>.MySeedc             S   s   dS )Ni?r   )r   r   r   r   __hash__*   s    z3TestBasicOps.test_seedargs.<locals>.MySeed.__hash__N)__name__
__module____qualname__r!   r   r   r   r   MySeed)   s   r%   r      l      Fx:^V l   Fx:^V gQ	@y      ?       @aabc   )Zone      )
objecttupler   r   listr   dictassertRaises	TypeErrortype)r   r%   argr   r   r   test_seedargs'   s    zTestBasicOps.test_seedargszrandom._urandomc             C   s   t |_|   d S )N)NotImplementedErrorside_effectr5   )r   Zurandom_mockr   r   r   *test_seed_when_randomness_source_not_found4   s    z7TestBasicOps.test_seed_when_randomness_source_not_foundc             C   s  | j j}g }|| | |g  dg}|| | |dg dd tdD }dd tdD }x|D ]}|| qfW x>t||D ]0\}}| t|t| | t|t| qW ttd}ttd}|| | ||k || | ||k | 	t
|d d S )N%   c             S   s   g | ]}t t|qS r   )r/   r   )r   r   r   r   r   r   F   s    z-TestBasicOps.test_shuffle.<locals>.<listcomp>
   c             S   s   g | ]}t t|qS r   )r/   r   )r   r   r   r   r   r   G   s    i  )r&   r+   r*   )r   shuffler   r   ziplensetr/   
assertTruer1   r2   )r   r;   ZlstZseqsZshuffled_seqsZshuffled_seqseqZshuffled_lstr   r   r   test_shuffle>   s*    
zTestBasicOps.test_shufflec             C   s4   | j j}tjjdd}td}||| |  d S )Ng      ?)return_values   abcdefghijk)r   r;   unittestmockZMock	bytearrayZassert_called_with)r   r;   Zmock_randomr@   r   r   r   test_shuffle_random_argument_   s
    
z)TestBasicOps.test_shuffle_random_argumentc          	   C   sT   | j j}| t |g  W d Q R X | |dgd | |ddgddg d S )N2      K   )r   choicer1   
IndexErrorr   assertIn)r   rJ   r   r   r   test_choiceg   s
    zTestBasicOps.test_choicec             C   s   d}t |}x\t |d D ]L}| j||}| t|| t|}| t|| | |t|k qW | | jg dg  | t| jj||d  | t| jjg d d S )Nd   r&   r   r'   )	r   r   sampler   r=   r>   r?   r1   
ValueError)r   r   
populationksZuniqr   r   r   test_samplen   s    zTestBasicOps.test_samplec          	   C   s|   d}t |}d}xft |D ]Z}t|t||  }i }x<t |D ](}d |t| j||< t||kr@P q@W |   qW d S )N   i'  )r   r   r.   r   rO   r=   Zfail)r   r   popZtrialsrR   expectedZpermsr   r   r   r   test_sample_distribution~   s    z%TestBasicOps.test_sample_distributionc             C   sb   | j ttdd | j tdd | j tdd | j tdd | j tdd d S )N   r+   Zabcdefghijklmnopqrst)r   rO   r>   r   strr.   )r   r   r   r   test_sample_inputs   s
    zTestBasicOps.test_sample_inputsc             C   s   |  t| jjtdd d S )NZabcdefr+   )r1   r2   r   rO   r0   fromkeys)r   r   r   r   test_sample_on_dicts   s    z!TestBasicOps.test_sample_on_dictsc          	   C   s~  | j j}ddddg}d}td}ttd}x|||dd||tddd|d|tdd	|d|tdd
gD ]:}| t|d | t|t | t|t|k qlW | 	t
 |d W d Q R X | ||ddg  | ||ddg  | 	t
 ||dd W d Q R X | t||ddt|k | t||ddt|k | 	t
 ||dd W d Q R X | t||d ddt|k | t||d ddt|k | 	t ||ddgdd W d Q R X | 	t
 ||ddd W d Q R X | 	t
 ||d gd dd W d Q R X xnddddgddddgtddtddtddtddgddddggD ]&}| t|||ddt|k qlW | 	t ||ddgdd W d Q R X | 	t
 ||ddd W d Q R X | 	t
 ||d gd dd W d Q R X | 	t
 ||tdtddd W d Q R X xdddddgddddgtddtddtddtddggD ]&}| t|||ddt|k qvW | |dddddgdg | |dddddgdg | |dddddgd g | |dddddgd!g | 	t |g dd W d Q R X | 	t |g g dd W d Q R X | 	t |g g dd W d Q R X d S )"NZredZgreenZblueZyellowZabcdr,   rU   )rR   )rR   rQ   weights)rR   rQ   cum_weightsr+   r   r'   g      @)r^   rR   r&   r:      rH      g333333.@gffffff$@g3333339@gL>@r*      TF)r_   rR   r(   bcd)r   choicesr   r>   r   r=   r3   r/   r?   r1   r2   rP   r
   rK   )r   rf   dataZstr_dataZ
range_dataset_datarO   r^   r   r   r   test_choices   sx    
  

"&"

*&zTestBasicOps.test_choicesc             C   s"   | j j}|ddgddgdd d S )Nr&   r+   g       i  )rQ   r^   rR   )r   rf   )r   rf   r   r   r   test_choices_subnormal   s    z#TestBasicOps.test_choices_subnormalc             C   sr   xldD ]d}| j | | j  }| j dd}| j | | j  }| j dd}| || | || qW d S )N)r&      {   i  i90  i@ i	 r   r&   )r   r   r   gaussr   )r   r   Zx1Zy1Zx2Zy2r   r   r   
test_gauss   s    


zTestBasicOps.test_gaussc                sl   xft tjd D ]T}tj|}fddt dD }t|  fddt dD }|| qW d S )Nr&   c                s   g | ]} j  qS r   )r   r   )r   r   )r   r   r   r     s    z.TestBasicOps.test_pickling.<locals>.<listcomp>r:   c                s   g | ]}   qS r   )r   )r   r   )newgenr   r   r     s    )r   pickleHIGHEST_PROTOCOLdumpsr   loadsr   )r   protor   ZorigseqZrestoredseqr   )ro   r   r   test_pickling   s    
zTestBasicOps.test_picklingc             C   sZ   dddg}xJ|D ]B\}}t t|d}t|}|  | t| d | qW d S )N)zrandv2_32.pcki  )zrandv2_64.pckib  )z
randv3.pckiW  rbi  )	openr	   Zfindfilerp   loadcloser   intr   )r   filesfilevaluefrr   r   r   test_bug_1727780  s    
zTestBasicOps.test_bug_1727780c                sR   d}| j j t fddt|D }| d||   k o@dk n  ||  d S )Ni c             3   s   | ]} d d dkV  qdS )l          r*   r+   Nr   )r   r   )	randranger   r   	<genexpr>  s    z-TestBasicOps.test_bug_9025.<locals>.<genexpr>g333333?gGz?)r   r   sumr   r?   )r   r   rR   r   )r   r   test_bug_9025  s    zTestBasicOps.test_bug_9025N)r"   r#   r$   r   r   r    r5   rC   rD   patchr8   rA   rF   rM   rT   rX   r[   r]   ri   rj   rn   ru   r   r   r   r   r   r   r      s$   
!Mr   FTz!random.SystemRandom not availablec               @   s   e Zd Ze Zdd Zdd Zdd Zdd Z	d	d
 Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZeefddZdS )SystemRandom_TestBasicOpsc             C   s   | j   d S )N)r   r   )r   r   r   r   r   '  s    z'SystemRandom_TestBasicOps.test_autoseedc             C   s&   |  t| jj |  t| jjd  d S )N)r1   r6   r   r   r   )r   r   r   r   r    +  s    z*SystemRandom_TestBasicOps.test_saverestorec             C   s   | j d d S )NrN   )r   r   )r   r   r   r   r5   /  s    z'SystemRandom_TestBasicOps.test_seedargsc             C   s(   d | j _| j d | | j jd  d S )NrN   )r   Z
gauss_nextr   r   )r   r   r   r   rn   3  s    z$SystemRandom_TestBasicOps.test_gaussc             C   s0   x*t tjd D ]}| ttj| j| qW d S )Nr&   )r   rp   rq   r1   r6   rr   r   )r   rt   r   r   r   ru   8  s    z'SystemRandom_TestBasicOps.test_picklingc             C   sD   d}d}x&t dD ]}|t| j | O }qW | ||d  d S )Nl          r   rN   r&   )r   rz   r   r   r   )r   spancumr   r   r   r   test_53_bits_per_float<  s
    z0SystemRandom_TestBasicOps.test_53_bits_per_floatc             C   sd   dd }d}xBt dD ]6}| j|}| d|  ko<|k n   ||O }qW | ||d  d S )Nr+   i  r   rN   r&   )r   r   r   r?   r   )r   r   r   r   r   r   r   r   test_bigrandD  s    z&SystemRandom_TestBasicOps.test_bigrandc             C   sh   xbdD ]Z}| j d|d  }| j d| }||kr8q| || j ||  koX|k n   qW d S )N)	(   P               iw  i   i&  r+   )r   r   r?   )r   r   startstopr   r   r   test_bigrand_rangesO  s    
z-SystemRandom_TestBasicOps.test_bigrand_rangesc                sF   x@dD ]8\  ttt fddtdD  qW d S )N))r   )l        l         )l            l           c                s   g | ]} j qS r   )r   r   )r   r   )r   r   r   r   r   r   Z  s    z>SystemRandom_TestBasicOps.test_rangelimits.<locals>.<listcomp>rN   )r   r>   r   )r   r   )r   r   r   r   test_rangelimitsW  s    z*SystemRandom_TestBasicOps.test_rangelimitsc             C   s<   | j ddd}| |d | j ddd}| |d d S )Nr   r:   r+   )r   r+   r,   rb      )r   r   rL   r   )r   Zrintr   r   r   test_randrange_nonunit_step\  s    z5SystemRandom_TestBasicOps.test_randrange_nonunit_stepc             C   s^   t | jt| jj}|dd |d |ddd |d |dd |ddd |ddd d S )	Nr*   i/r   rN   ign!	@g	@*   )r   r1   rP   r   r   )r   Zraisesr   r   r   test_randrange_errorsb  s    

z/SystemRandom_TestBasicOps.test_randrange_errorsc             C   s   x<t ddD ].}| d| j|  ko2d| k n   qW | jj}xBdD ]:}d}xt dD ]}|||O }q^W | |d| d  qLW | t| jj | t| jjdd | t| jjd | t| jjd | t| jjd d S )	Nr&   i  r   r+   )r&   r+   r*   r,          r   4   5   6   w            rN   r'   g333333$@)r   r?   r   getrandbitsr   r1   r2   rP   )r   rR   getbitsr   r   r   r   r   r   test_genrandbitso  s    .
z*SystemRandom_TestBasicOps.test_genrandbitsc             C   s  xt ddD ]}d|> }|d }|d||d }| || | |d|d   ||d 7 }|d||d }| |||d g | d| |  kod|d  kn   ||d? 8 }|d||d }| || | d| |  kod|d  kn   qW d S )Nr&   i  grZ|
 ?r+   r`   )r   r   rL   r?   )r   _logrz   r   r   numbitsrR   r   r   r   test_randbelow_logic  s    *z.SystemRandom_TestBasicOps.test_randbelow_logicN)r"   r#   r$   r   SystemRandomr   r   r    r5   rn   ru   r   r   r   r   r   r   r   r   rz   r   r   r   r   r   r   #  s   r   c               @   s   e Zd Ze Zdd Zdd Zdd Zdd Z	d	d
 Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZeefddZejdd d! Zd"d# Zd$d% Zd&S )'MersenneTwister_TestBasicOpsc                sp    j jddd   fddtdD ddd	d
g  j jddd   fddtdD ddddg d S )Ni4 r&   )versionc                s   g | ]} j   qS r   )r   r   hex)r   r   )r   r   r   r     s    zGMersenneTwister_TestBasicOps.test_guaranteed_stable.<locals>.<listcomp>r,   z0x1.ac362300d90d2p-1z0x1.9d16f74365005p-1z0x1.1ebb4352e4c4dp-1z0x1.1a7422abf9c11p-1zthe quick brown foxr+   c                s   g | ]} j   qS r   )r   r   r   )r   r   )r   r   r   r     s    z0x1.1239ddfb11b7cp-3z0x1.b3cbb5c51b120p-4z0x1.8c4f55116b60fp-1z0x1.63eb525174a27p-1)r   r   r   r   )r   r   )r   r   test_guaranteed_stable  s    
z3MersenneTwister_TestBasicOps.test_guaranteed_stablec                s    j jddd   fddtdD ddd	d
g  j jddd   fddtdD ddddg  j jddd   fddtdD ddddg d S )NZnofarr&   )r   c                s   g | ]} j   qS r   )r   r   r   )r   r   )r   r   r   r     s    z?MersenneTwister_TestBasicOps.test_bug_27706.<locals>.<listcomp>r,   z0x1.8645314505ad7p-1z0x1.afb1f82e40a40p-5z0x1.2a59d2285e971p-1z0x1.56977142a7880p-6Zrachelc                s   g | ]} j   qS r   )r   r   r   )r   r   )r   r   r   r     s    z0x1.0b294cc856fcdp-1z0x1.2ad22d79e77b8p-3z0x1.3052b9c072678p-2z0x1.578f332106574p-3 c                s   g | ]} j   qS r   )r   r   r   )r   r   )r   r   r   r     s    z0x1.b0580f98a7dbep-1z0x1.84129978f9c1ap-1z0x1.aeaa51052e978p-2z0x1.092178fb945a6p-2)r   r   r   r   )r   r   )r   r   test_bug_27706  s    

z+MersenneTwister_TestBasicOps.test_bug_27706c             C   s<   G dd dt }y| j|  W n tk
r6   Y nX d S )Nc               @   s   e Zd Zdd ZdS )z;MersenneTwister_TestBasicOps.test_bug_31478.<locals>.BadIntc             S   s   d S )Nr   )r   r   r   r   __abs__  s    zCMersenneTwister_TestBasicOps.test_bug_31478.<locals>.BadInt.__abs__N)r"   r#   r$   r   r   r   r   r   BadInt  s   r   )rz   r   r   r2   )r   r   r   r   r   test_bug_31478  s
    z+MersenneTwister_TestBasicOps.test_bug_31478c                s    j jddd   fddtdD ddd	d
g  j jddd   fddtdD ddddg  j jddd   fddtdD ddddg d} j j|dd   fddtdD ddddg d S )Ns   nofarr&   )r   c                s   g | ]} j   qS r   )r   r   r   )r   r   )r   r   r   r     s    z?MersenneTwister_TestBasicOps.test_bug_31482.<locals>.<listcomp>r,   z0x1.8645314505ad7p-1z0x1.afb1f82e40a40p-5z0x1.2a59d2285e971p-1z0x1.56977142a7880p-6s   rachelc                s   g | ]} j   qS r   )r   r   r   )r   r   )r   r   r   r     s    z0x1.0b294cc856fcdp-1z0x1.2ad22d79e77b8p-3z0x1.3052b9c072678p-2z0x1.578f332106574p-3    c                s   g | ]} j   qS r   )r   r   r   )r   r   )r   r   r   r     s    z0x1.b0580f98a7dbep-1z0x1.84129978f9c1ap-1z0x1.aeaa51052e978p-2z0x1.092178fb945a6p-2s	     @`c                s   g | ]} j   qS r   )r   r   r   )r   r   )r   r   r   r     s    z0x1.52c2fde444d23p-1z0x1.875174f0daea4p-2z0x1.9e9b2c50e5cd2p-1z0x1.fa57768bd321cp-2)r   r   r   r   )r   rc   r   )r   r   test_bug_31482  s"    


z+MersenneTwister_TestBasicOps.test_bug_31482c             C   s   |  t| jjd d S )N)r&   NN)r1   rP   r   r   )r   r   r   r   test_setstate_first_arg  s    z4MersenneTwister_TestBasicOps.test_setstate_first_argc          	   C   sJ  | j  }| t| j jd | t| j jd | t| j jddd d f | t| j jddd d d f | ttf  | j ddd d	 d f W d Q R X | ttf  | j ddd d
 d f W d Q R X | j d}| j | | | j d| | j  d }t	|}t
d|d< dd |D }| t| j jd|d f d S )N)r+   NN)r+   )r&   r+   r*   Nr+   )r(   iq  )r   ip  )r&   )iq  )r'   rN   r&   nanr'   c             s   s   | ]}t |V  qd S )N)rz   )r   xr   r   r   r     s    zHMersenneTwister_TestBasicOps.test_setstate_middle_arg.<locals>.<genexpr>)r   r   r1   r2   r   rP   OverflowErrorr   r   r/   float)r   Zstart_stateZbits100Zstate_valuesr   r   r   r   test_setstate_middle_arg  s"    
 $$z5MersenneTwister_TestBasicOps.test_setstate_middle_argc          
   C   sb   ddddddddd	d
g
}| j d | ddd  }x&t||D ]\}}| j||dd qBW d S )Ng>FdV?gwR1ۉ?gQ"?g~5?g mb?gYKbX?gH,r̊?gV8?gOֶ?g4Ԗ?l   #q  `&  'R i  i   )places)r   r   r   r<   assertAlmostEqual)r   rW   actualr(   er   r   r   test_referenceImplementation   s    z9MersenneTwister_TestBasicOps.test_referenceImplementationc          
   C   st   ddl m} ddddddd	d
ddg
}| jd | ddd  }x,t||D ]\}}| t||d| qNW d S )Nr   )ldexpl   #1,u l   wbl' l    JX l   uak~[ l   @"p1y l   sV%bX$ l   E3EJ l   Vb l   aoek l   4)[v l   #q  `&  'R i  ir   )mathr   r   r   r   r<   r   rz   )r   r   rW   r   r(   r   r   r   r   $test_strong_reference_implementation  s    zAMersenneTwister_TestBasicOps.test_strong_reference_implementationc             C   s   dd> d }| j | d S )Nr&   i8 )r   r   )r   r   r   r   r   test_long_seed5  s    z+MersenneTwister_TestBasicOps.test_long_seedc             C   sD   d}d}x&t dD ]}|t| j | O }qW | ||d  d S )Nl          r   rN   r&   )r   rz   r   r   r   )r   r   r   r   r   r   r   r   >  s
    z3MersenneTwister_TestBasicOps.test_53_bits_per_floatc             C   sd   dd }d}xBt dD ]6}| j|}| d|  ko<|k n   ||O }qW | ||d  d S )Nr+   i  r   rN   r&   )r   r   r   r?   r   )r   r   r   r   r   r   r   r   r   F  s    z)MersenneTwister_TestBasicOps.test_bigrandc             C   sh   xbdD ]Z}| j d|d  }| j d| }||kr8q| || j ||  koX|k n   qW d S )N)	r   r   r   r   r   r   iw  i   i&  r+   )r   r   r?   )r   r   r   r   r   r   r   r   Q  s    
z0MersenneTwister_TestBasicOps.test_bigrand_rangesc                sF   x@dD ]8\  ttt fddtdD  qW d S )N))r   r   )l        l         )l            l           c                s   g | ]} j qS r   )r   r   )r   r   )r   r   r   r   r   r   \  s    zAMersenneTwister_TestBasicOps.test_rangelimits.<locals>.<listcomp>rN   )r   r>   r   )r   r   )r   r   r   r   r   Y  s    z-MersenneTwister_TestBasicOps.test_rangelimitsc             C   s  | j d | | j dd x<tddD ].}| d| j |  koRd| k n   q,W | j j}xBdD ]:}d}xtdD ]}|||O }q~W | |d| d  qlW | t| j j | t| j jd	 | t| j jdd | t| j jd | t| j jd
 d S )Ni rN   l   S!m.vFXO r&   i  r   r+   )r&   r+   r*   r,   r   r   r   r   r   r   r   r   r   r   r(   r'   )	r   r   r   r   r   r?   r1   r2   rP   )r   rR   r   r   r   r   r   r   r   r   ^  s     .
z-MersenneTwister_TestBasicOps.test_genrandbitsc             C   s  xt ddD ]}d|> }|d }|d||d }| || | |d|d   ||d 7 }|d||d }| |||d g | d| |  kod|d  kn   ||d? 8 }|d||d }| || | d| |  kod|d  kn   qW d S )Nr&   i  grZ|
 ?r+   r`   )r   r   rL   r?   )r   r   rz   r   r   r   rR   r   r   r   r   v  s    *z1MersenneTwister_TestBasicOps.test_randbelow_logiczrandom.Random.randomc          	   C   s   t  j |_dt j> }t & tdt | jj	|d |d W d Q R X | jj	d|d | 
t | jj	d|d W d Q R X d}d}|||  | }|| || g|_| jj	||d d S )Nr&   ignore)maxsizei  r   r   g{Gz?)r   r   r7   ZBPFwarningscatch_warningssimplefilterUserWarningr   Z
_randbelowr1   rP   )r   random_mockr   r   epsilonlimitr   r   r    test_randbelow_overridden_random  s    

z=MersenneTwister_TestBasicOps.test_randbelow_overridden_randomc             C   sR   d}d}d}| j |||}| ||  k o2|kn   | || | d d S )Nl    J)l   Fx:^V i8r   )r   r   r?   r   )r   r   r   stepr   r   r   r   test_randrange_bug_1590891  s    z7MersenneTwister_TestBasicOps.test_randrange_bug_1590891c       
      C   sH  | j j}d}| j d | j jt|dd}| j d | j jt|dg| dd}| || | j d | j jt|td|d dd}| || ddd	g}d
d
dg}d
ddg}dgd
 dgd
  d	gd  }	| j d | j j|	dd}| j d | j j||dd}| || | j d | j j||dd}| || d S )Ni i_ i'  )rR   r&   )r_   rR   ZRedZBlackZGreen   r+   $   &   i߉ )r   rf   r   r   r   )
r   rf   r   r(   rc   rd   rQ   r^   r_   Zexpanded_populationr   r   r   test_choices_algorithms  s,     


z4MersenneTwister_TestBasicOps.test_choices_algorithmsN)r"   r#   r$   r   Randomr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rz   r   rC   rD   r   r   r   r   r   r   r   r   r     s&   	#r   g       @g      ?c             C   s   | dk r$t tt |   td|   S | d }|| d  t| | tdd|  d| d  d| d  d	| d
  d| d  d| d  d| d  d| d  g	 S )Ng      ?g      ?g      @g?g
ö)$@g&Ԏgta@g       @gQfg      @g>@~)@g      @gA~g      @g~\>g      @giE>g      @)r   r   gammar   r   )zZsqrt2piazr   r   r   r     s    





r   c               @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	j
ddd Ze	j
ddd ZdS )TestDistributionsc                s  t    fddtdD dgd  }|d d  j _  dd |d d  j _  d |d d  j _  d |d d  j _  dd |d d  j _  dd |d d  j _  	dd |d d  j _  
dd |d d  j _  dd |d d  j _  dd |d d  j _  d	d |d d  j _  dd |d d  j _  d
d |d d  j _  dd |d d  j _  ddd d S )Nc                s   g | ]}   qS r   )r   )r   r   )gr   r   r     s    z5TestDistributions.test_zeroinputs.<locals>.<listcomp>rG   g        rU   r&   r:   g      ?g{Gz?g      i@g      @gUUUUUU?)r   r   r   rV   uniformparetovariateexpovariateweibullvariatevonmisesvariatenormalvariaterm   lognormvariategammavariatebetavariate
triangular)r   r   r   )r   r   test_zeroinputs  s<       
 
           z!TestDistributions.test_zeroinputsc                sp  t  }d  fddtd D }xD|jdddf|jdd	d
f|jddd	f|jdttd d f|jdddf|j	dt
dt
dt
dd  fgD ]\}}}}|d d  j|_ g }x<tt|D ],}y|||  W q tk
r   Y qX qW d }	}
x$|D ]}|	|7 }	|
|| d 7 }
qW t| | j|	  |dd|j|f d | j|
 d  |dd|j|f d qW d S )Ni  c                s   g | ]}|t   qS r   )r   )r   r   )r   r   r   r     s    z2TestDistributions.test_avg_std.<locals>.<listcomp>r&   )g      ?g      $@g      @g      @)g        g      ?gUUUUUU?gqq?g<ݚ?)g      ?gUUUUUU?)gGz?r   r+   r*   )g      @g      ?g?)g      ?g      @gUUUUUU?g?r   z%s%r)r   msg)r   r   r   r   r   r   r   r   r   r   r   rV   r=   appendrK   r   r"   )r   r   r   variateargsmuZ	sigmasqrdyr   s1s2r   r   )r   r   test_avg_std  s8    $

zTestDistributions.test_avg_stdc             C   s   t  }d}x|jddf|jddf|jddf|jtdfdf|jdtdfdf|jddf|jd	d
f|jtd dfdf|j	ddf|j
tdfd
f|jdtdfdf|jddfgD ],\}}}x t|D ]}| || | qW qW d S )NrN   )g      $@g      $@g      $@)g      $@g      $@g      $@infg        g      @)g      $@g        )g        g        g      ?)g        g      $@)r   r   r   r   r   r   r   rm   r   r   r   r   r   r   )r   r   r   r   r   rW   r   r   r   r   test_constant  s"    





zTestDistributions.test_constantc          
   C   sx   t  }d}xfdD ]^}xXdD ]P}xJt|D ]>}|||}| jd|  koRt jkn  d|||d q*W qW qW d S )NrN   )g        g?g@g@)g        gffffff@g     @@r   zCvonmisesvariate({}, {}) produced a result {} out of range [0, 2*pi])r   )r   r   r   r   r?   TWOPIformat)r   r   r   r   Zkappa_rO   r   r   r   test_von_mises_range1  s    

z&TestDistributions.test_von_mises_rangec             C   s   t dd t dd d S )Nr   g  4&kCg}Ô%IT)r   r   )r   r   r   r   test_von_mises_large_kappa?  s    z,TestDistributions.test_von_mises_large_kappac             C   sL   |  ttjdd |  ttjdd |  ttjdd |  ttjdd d S )Nr'   r*   r   r+   r&   )r1   rP   r   r   )r   r   r   r   test_gammavariate_errorsD  s    z*TestDistributions.test_gammavariate_errorszrandom.Random.randomc             C   s   dddg|_ tdd}| |d ddg|_ tdd	}| |d
 tj}tj}tj}d}d}|| | }d}	d}
d}|
||	 |
|g|_ t||}| |d d}
d}|
||	 |
|g|_ t||}| |d d S )Ng:0yE>g      ?g333333?g?gffffff@g=
ףp=@g?g      ?gQ	@gdº@gffffff?g333333?g{Gz?gX Y?g=,6V?g.3333?gl%t?gK,	9?g7SvT?)r7   r   r   r   _e_expr   )r   r   Zreturned_valuer   r   r   ZalphaZbetarc   r   Zr1Zr2r   r   r   $test_gammavariate_full_code_coverageK  s.    

!	z6TestDistributions.test_gammavariate_full_code_coveragezrandom.Random.gammavariatec             C   s   d|_ | dtdd d S )Ng        g	@gn!	@)rB   r   r   r   )r   Zgammavariate_mockr   r   r   test_betavariate_return_zero  s    z.TestDistributions.test_betavariate_return_zeroN)r"   r#   r$   r   r   r   r   r   r   rC   rD   r   r   r   r   r   r   r   r     s    Wr   c               @   s>   e Zd Zdd Zdd Zdd Zeee	ddd	d
 Z
dS )
TestModulec             C   s<   |  tjd |  tjd |  tjd |  tjd d S )Ng3?r?g.DT!@g9B.?gtY@)r   r   ZNV_MAGICCONSTr   ZLOG4ZSG_MAGICCONST)r   r   r   r   testMagicConstants  s    zTestModule.testMagicConstantsc             C   s    |  ttjtttk d S )N)r?   r>   r   __all__dir)r   r   r   r   test__all__  s    zTestModule.test__all__c             C   s    G dd dt j}|dd d S )Nc               @   s   e Zd ZdddZdS )z=TestModule.test_random_subclass_with_kwargs.<locals>.SubclassNc             S   s   t j|  d S )N)r   r   __init__)r   newargr   r   r   r    s    zFTestModule.test_random_subclass_with_kwargs.<locals>.Subclass.__init__)N)r"   r#   r$   r  r   r   r   r   Subclass  s   r  r&   )r  )r   r   )r   r  r   r   r    test_random_subclass_with_kwargs  s    z+TestModule.test_random_subclass_with_kwargsforkzfork() requiredc          	   C   s   t  \}}t  }|dkr^z2td}t|d}|t| W d Q R X W d t d X n^t 	| td}t|d}t
| }W d Q R X | || t |d\}}| |d d S )Nr   r   wr   )ospiper  r   r   rw   writerZ   _exitry   evalreadr   waitpidr   )r   r   r  pidvalr~   Z	child_valstatusr   r   r   test_after_fork  s    


zTestModule.test_after_forkN)r"   r#   r$   r   r   r  rC   
skipUnlesshasattrr  r  r   r   r   r   r     s   r   __main__)!rC   Zunittest.mockr   r  r   rp   r   	functoolsr   r   r   r   r   r   r   r   Ztestr	   Z	fractionsr
   r   r   r6   ZSystemRandom_availabler  ZTestCaser   r   r   r   r   r"   mainr   r   r   r   <module>   s:      
u  @ A+
