B
    u9aå ã               @   sf  d dl Z d dlmZ d dlT d dlZd dlmZ d dlm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Zd dlZejZe 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G dd dZdd Zdd Zdd Zd d! Z d"d# Z!d$d% Z"d&d' e#ej$d D Z%G d(d) d)e j&Z'G d*d+ d+e j&Z(G d,d- d-e j&Z)G d.d/ d/e j&Z*d0d1 Z+G d2d3 d3Z,G d4d5 d5Z-G d6d7 d7Z.G d8d9 d9Z/G d:d; d;Z0G d<d= d=Z1G d>d? d?Z2d@dA Z3G dBdC dCe j&Z4G dDdE dEe j&Z5G dFdG dGe j&Z6G dHdI dIe j&Z7ej8G dJdK dKe j&Z9dLZ:dMe:iZ;dSdNdOZ<e=dPkrbe<dQdR dS )Té    N)Úsupport)Ú*)ÚDecimal)ÚFraction)Úreduceé   c              G   s   t t|  S )N)ÚlistÚzip)Úargs© r   ú$/usr/lib/python3.7/test_itertools.pyÚlzip   s    r   c             C   s   d|  S )Né   r   )Úxr   r   r   Úonearg   s    r   c              G   s   t d S )N)Ú
ValueError)r
   r   r   r   Úerrfunc   s    r   c              c   s   xdD ]
} | V  qW d S )N)r   r   r   r   )Úir   r   r   Úgen3   s    
r   c             C   s   | d dkS )Nr   r   r   )r   r   r   r   ÚisEven!   s    r   c             C   s   | d dkS )Nr   r   r   )r   r   r   r   ÚisOdd%   s    r   c              G   s   | S )Nr   )r
   r   r   r   Útupleize)   s    r   c             c   s   xt | D ]
}|V  q
W d S )N)Úrange)Únr   r   r   r   Úirange,   s    r   c               @   s   e Zd Zdd Zdd ZdS )ÚStopNowc             C   s   | S )Nr   )Úselfr   r   r   Ú__iter__2   s    zStopNow.__iter__c             C   s   t d S )N)ÚStopIteration)r   r   r   r   Ú__next__4   s    zStopNow.__next__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   0   s   r   c             C   s   t t|| S )N)r   Úislice)r   Úseqr   r   r   Útake7   s    r%   c             C   s   t tj| dS )Nr   )r   ÚoperatorÚmul)Úiterabler   r   r   Úprod;   s    r)   c             C   s   t td| d S )Nr   )r)   r   )r   r   r   r   Úfact>   s    r*   c             C   s   | d S )Nr   r   )Úrr   r   r   ÚtestRC   s    r,   c             C   s   | d S )Nr   r   )r+   r   r   r   ÚtestR2F   s    r-   c             C   s   | dk S )Né
   r   )r   r   r   r   ÚundertenI   s    r/   c             C   s   g | ]}|fd dqS )c             S   s   t  t  | |¡¡S )N)ÚpickleÚloadsÚdumps)ÚsÚprotor   r   r   Ú<lambda>L   ó    z<listcomp>.<lambda>r   )Ú.0r4   r   r   r   ú
<listcomp>L   s   r8   c               @   sÊ  e Zd Zd_ddZdd Zdd	 Zd
d Zdd Zdd Zdd Z	e
jdd Ze
 d¡dd Zdd Ze
jdd Ze
 d¡dd Zdd Ze
jdd  Ze
 d¡d!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Ze
 d¡d7d8 Zd9d: Z e
 d¡d;d< Z!d=d> Z"d?d@ Z#dAdB Z$e
jdCdD Z%e
 d¡dEdF Z&dGdH Z'dIdJ Z(dKdL Z)dMdN Z*dOdP Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1d]d^ Z2dS )`ÚTestBasicOpsé   r   Nc                s  d fdd	 t  ||¡}t  |¡}|  t|t|¡  | | }}	|  ||	¡ |rp |}
|  ||
¡ t  |¡}d}y&x t|D ]}t| |d7 }qW W n tk
rž   Y nX t  ||¡}t  |¡} | | }}	|  ||	¡ |r ||d  }
|  ||
¡ d S )Nr   c                sZ    dkrt dt| tr| S ytt| }W n tk
rD   | S X  fdd|D S )Nr.   zinfinite recursion encounteredc                s   g | ]} |d  qS )r   r   )r7   Úe)Úexpandr   r   r   r8   ]   s    z;TestBasicOps.pickletest.<locals>.expand.<locals>.<listcomp>)ÚRuntimeErrorÚ
isinstanceÚstrr   r#   Ú	TypeError)Úitr   Úl)r<   Ústop)r   r   r<   S   s    
z'TestBasicOps.pickletest.<locals>.expandr   )r   )r0   r2   r1   ÚassertEqualÚtyper   Únextr   )r   ZprotocolrA   rC   r%   ÚcompareÚdumpZi2ÚaÚbÚcZi3Ztookr   Zi4r   )r<   rC   r   Ú
pickletestQ   s0    


zTestBasicOps.pickletestc             C   s,  |   tttdddddddddd	d
g
¡ |   tttddddddddddd	d
g
¡ xPttttfD ]@}|   ttt|tdtt|ddddddddd	d
g
¡ qhW |   ttddddg¡ |   ttg g ¡ |   ttdgdg¡ |  	t
ttddd¡ |  	t
t¡ | j	t
ttdd |  	t
ttdg g¡ ddddddddddg
}|   tt|tddddddddddg
¡ |   tt|tddddddddddg
¡ |   tt|tjddddddddddg
¡ |  	t
¡ tt|t W d Q R X x,ttjd D ]}|  |ttd¡ q
W d S )Nr.   r   r   é   é   é   é   é   é$   é-   )r(   ÚabcrI   Úabé   é   )r   r   é   é	   r:   é   é   iÐ  i°  )rD   r   Ú
accumulater   ÚintÚcomplexr   r   ÚmapÚassertRaisesr@   ÚminÚmaxr&   r'   Úchrr0   ÚHIGHEST_PROTOCOLrL   )r   Útypr3   r4   r   r   r   Útest_accumulatez   s4    (zTestBasicOps.test_accumulatec             C   s   dd }xt |fD ]z}|  t|ddtd¡ |  t|dtd¡ |  t|dg ¡ |  td|ddtd¡ |  tt|d	d
¡ qW d S )Nc              w   s&   x | D ]}x|D ]
}|V  qW qW d S )Nr   )Ú	iterablesrA   Úelementr   r   r   Úchain2   s    

z'TestBasicOps.test_chain.<locals>.chain2rT   ÚdefÚabcdefÚ r:   Úabcdr   rM   )ÚchainrD   r   r%   r`   r@   )r   ri   rK   r   r   r   Ú
test_chain   s    zTestBasicOps.test_chainc             C   s   |   tt ddg¡td¡ |   tt dg¡td¡ |   tt dg¡g ¡ |   tdt ddg¡td¡ |  ttt ddg¡¡ d S )	NrT   rj   rk   rl   r:   rm   r   rM   )rD   r   rn   Úfrom_iterabler%   r`   r@   )r   r   r   r   Útest_chain_from_iterableŠ   s
     z%TestBasicOps.test_chain_from_iterablec          	   C   sæ   x®t jgt D ]}tdd}|  t||td¡ |  t|d¡ |  t||td¡ |  t|tdg ¡ |  td|tddtd¡ |  t	t|td	d
¡ qW x0t
tjd D ]}| j|tddtdd qÀW d S )NrT   rj   rk   rI   Zbcdefrl   r:   rm   r   rM   r   )rG   )ÚcopyÚdeepcopyÚpicklecopiersrn   rD   r   rF   r%   r`   r@   r   r0   rd   rL   )r   ZoperrA   r4   r   r   r   Útest_chain_reducible­   s    
 z!TestBasicOps.test_chain_reduciblec          
   C   sÞ   |   tt jd¡ |   tt jg ¡ |   tt jd¡ |   tt jg f¡ |   tt jtg g f¡ t }| tddgf¡ |  t|ddddd	d
g¡ t }| tddgtdgf¡ |  t|dddddd	d
g¡ d S )Nr   r   rT   rj   rI   rJ   rK   Údr;   ÚfZghi)r`   r@   rn   Ú__setstate__ÚiterrD   r   )r   rA   r   r   r   Útest_chain_setstateº   s    z TestBasicOps.test_chain_setstatec       
   
      sÚ  |   ttd¡ |   ttddd¡ |   ttd ¡ |   ttdd¡ xÜdd gt D ]Ê}|  t|tddg ¡ |  t|tddd	d
ddddg¡ tdd}t| |  t||d
ddddg¡ |  t|ttddddddg¡ ttdd}t| |  t||dddg¡ qNW dd }dd }dd }xtdD ]}dd t|D xtt|d D ]b}tt|}|  t	|||krdnt
|t
| t
||  ¡ |  t	|t	t|¡ |  |t|¡ x|D ]x |  t	 |¡ |  t	t |¡ |  t t ¡ |  tfdd  D ¡ |  t  fd!dD ¡ qàW |  |t||¡ |  |t||¡ |  |t||¡ x*ttjd D ]}	|  |	t|¡ q®W qhW q@W d S )"NrT   r   r   éþÿÿÿc             S   s   | S )Nr   )rI   r   r   r   r5   Í   r6   z0TestBasicOps.test_combinations.<locals>.<lambda>é    ÚABCD)ÚAÚB)r~   ÚC)r~   ÚD)r   r   )r   r   )r   r   r:   rM   )r   r   r   )r   r   rM   )r   r   rM   )r   r   rM   c             3   sÎ   t |  t }||krd S tt|}t  fdd|D V  xx.tt|D ]}|| || | krPP qPW d S ||  d7  < x*t|d |D ]}||d  d ||< qW t  fdd|D V  qBW d S )Nc             3   s   | ]} | V  qd S )Nr   )r7   r   )Úpoolr   r   ú	<genexpr>æ   s    zHTestBasicOps.test_combinations.<locals>.combinations1.<locals>.<genexpr>r   c             3   s   | ]} | V  qd S )Nr   )r7   r   )r   r   r   r   ð   s    )ÚtupleÚlenr   r   Úreversed)r(   r+   r   Úindicesr   Újr   )r   r   Úcombinations1ß   s    z5TestBasicOps.test_combinations.<locals>.combinations1c             3   sT   t |  t }x>tt||D ],}t|t|kr t  fdd|D V  q W d S )Nc             3   s   | ]} | V  qd S )Nr   )r7   r   )r   r   r   r   ø   s    zHTestBasicOps.test_combinations.<locals>.combinations2.<locals>.<genexpr>)r   r   Úpermutationsr   Úsortedr   )r(   r+   r   r   r   )r   r   Úcombinations2ò   s
    z5TestBasicOps.test_combinations.<locals>.combinations2c             3   sT   t |  t }x>tt||D ],}tt||kr t  fdd|D V  q W d S )Nc             3   s   | ]} | V  qd S )Nr   )r7   r   )r   r   r   r      s    zHTestBasicOps.test_combinations.<locals>.combinations3.<locals>.<genexpr>)r   r   Úcombinations_with_replacementr   Úset)r(   r+   r   r   r   )r   r   Úcombinations3ú   s
    z5TestBasicOps.test_combinations.<locals>.combinations3rV   c             S   s   g | ]}d | d qS )rW   é   r   )r7   r   r   r   r   r8     s    z2TestBasicOps.test_combinations.<locals>.<listcomp>r   c             3   s   | ]}| kV  qd S )Nr   )r7   r;   )Úvaluesr   r   r     s    z1TestBasicOps.test_combinations.<locals>.<genexpr>c                s   g | ]}| kr|qS r   r   )r7   r;   )rK   r   r   r8     s    )r`   r@   Úcombinationsr   rt   rD   r   rF   r   r   r*   r   r   Ú
assertTrueÚallr0   rd   rL   )
r   ÚopÚtestIntermediater   r   r   r   r+   Úresultr4   r   )rK   r   r   Útest_combinationsÇ   sP    
6

zTestBasicOps.test_combinationsc          	   C   s(   |   ttf¡ tdd W d Q R X d S )NÚAAi    )r`   ÚOverflowErrorÚMemoryErrorr   )r   r   r   r   Útest_combinations_overflow  s    z'TestBasicOps.test_combinations_overflowz"tuple reuse is specific to CPythonc          
   C   sH   |   tttttddd¡ |  ttttttddd¡ d S )NÚabcderM   r   )rD   r   r   r_   Úidr   ÚassertNotEqualr   )r   r   r   r   Útest_combinations_tuple_reuse  s     z*TestBasicOps.test_combinations_tuple_reusec          
      s  t }|  t|d¡ |  t|ddd¡ |  t|d ¡ |  t|dd¡ xldd gt D ]Z}|  t||dddd	d
dddg¡ |dd}t| |  t||d	d
dddg¡ qRW dd }dd }dd }xŒtdD ]®}dd t|D xt|d D ]}t||}	|  t	|	|||¡ |  t	|	t	t
|	¡ |  |	t|	¡ tt|}
|dksp|dkr~|  |	|
¡ n|  t
|	t
|
k¡ x|	D ] |  t	 |¡ dd t D }|  t	|t	t
|¡ |  t t ¡ |  tfdd D ¡ |  | fddD ¡ qW |  |	t||¡ |  |	t||¡ x*ttjd D ]}|  |||¡ qdW qüW qÔW d S )NrT   r   r   r{   c             S   s   | S )Nr   )rI   r   r   r   r5   )  r6   zATestBasicOps.test_combinations_with_replacement.<locals>.<lambda>ÚABC)r~   r~   )r~   r   )r~   r   )r   r   )r   r   )r   r   c             3   sª   t |  t }|s|rd S dg| }t  fdd|D V  xfx*tt|D ]}|| |d krNP qNW d S || d g||  ||d < t  fdd|D V  q@W d S )Nr   c             3   s   | ]} | V  qd S )Nr   )r7   r   )r   r   r   r   :  s    zPTestBasicOps.test_combinations_with_replacement.<locals>.cwr1.<locals>.<genexpr>r   c             3   s   | ]} | V  qd S )Nr   )r7   r   )r   r   r   r   B  s    )r   r   r   r   )r(   r+   r   r   r   r   )r   r   Úcwr12  s    
z=TestBasicOps.test_combinations_with_replacement.<locals>.cwr1c             3   sV   t |  t }x@tt||dD ],}t|t|kr"t  fdd|D V  q"W d S )N)Úrepeatc             3   s   | ]} | V  qd S )Nr   )r7   r   )r   r   r   r   J  s    zPTestBasicOps.test_combinations_with_replacement.<locals>.cwr2.<locals>.<genexpr>)r   r   Úproductr   r   r   )r(   r+   r   r   r   )r   r   Úcwr2D  s
    z=TestBasicOps.test_combinations_with_replacement.<locals>.cwr2c             S   s4   | s|rdS dS t | | d t | t | d  S )Nr   r   )r*   )r   r+   r   r   r   ÚnumcombsL  s    zATestBasicOps.test_combinations_with_replacement.<locals>.numcombsrV   c             S   s   g | ]}d | d qS )rW   r   r   )r7   r   r   r   r   r8   R  s    zCTestBasicOps.test_combinations_with_replacement.<locals>.<listcomp>r   c             S   s   g | ]\}}|qS r   r   )r7   ÚkÚvr   r   r   r8   b  s    c             3   s   | ]}| kV  qd S )Nr   )r7   r;   )r   r   r   r   e  s    zBTestBasicOps.test_combinations_with_replacement.<locals>.<genexpr>c                s   g | ]}| kr|qS r   r   )r7   r;   )rK   r   r   r8   g  s    )r   r`   r@   r   rt   rD   r   rF   r   r   r   r   r   r   Úgroupbyr   r0   rd   rL   )r   Úcwrr   r   r¢   r¥   rŠ   r   r+   r   Zregular_combsZnorunsr4   r   )rK   r   r   Ú"test_combinations_with_replacement"  sL    

z/TestBasicOps.test_combinations_with_replacementc          	   C   s(   |   ttf¡ tdd W d Q R X d S )Nr   i   @)r`   r   r   r   )r   r   r   r   Ú+test_combinations_with_replacement_overflown  s    z8TestBasicOps.test_combinations_with_replacement_overflowc          
   C   sL   t }|  tttt|ddd¡ |  ttttt|ddd¡ d S )Nr   rM   r   )r   rD   r   r   r_   r   r   r   )r   rª   r   r   r   Ú.test_combinations_with_replacement_tuple_reuset  s     z;TestBasicOps.test_combinations_with_replacement_tuple_reusec          	      s   |   tt¡ |   ttddd¡ |   ttd ¡ |   ttdd¡ |  ttddg ¡ |   ttdd¡ |  tttdddd	d
dddg¡ ddd}ddd}x|tdD ]n}dd t|D  xTt|d D ]B}tt |}|  t|||krúdnt|t||  ¡ |  t|tt	|¡ |  |t
|¡ xN|D ]F}|  t||¡ |  tt	||¡ |  t fdd|D ¡ q>W |  |t| |¡ |  |t| |¡ ||krè|  |tt d ¡ |  |tt ¡ x*ttjd D ]}|  |t |¡ qøW qÐW qšW d S )NrT   r   r   r{   r|   r3   rM   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   c             3   s@  t |  t }|d kr|n|}||kr,d S tt|}tt|| d |d d d d }t  fdd|d | D V  xŸ|r:x²tt|D ]}||  d8  < || dkrè||d d  |||d   ||d < || ||< q|| }||  ||  ||< || < t  fdd|d | D V  P qW d S q~W d S )Nr   éÿÿÿÿc             3   s   | ]} | V  qd S )Nr   )r7   r   )r   r   r   r     s    zHTestBasicOps.test_permutations.<locals>.permutations1.<locals>.<genexpr>r   c             3   s   | ]} | V  qd S )Nr   )r7   r   )r   r   r   r     s    )r   r   r   r   r   )r(   r+   r   r   Zcyclesr   r   r   )r   r   Úpermutations1  s&    $ ( z5TestBasicOps.test_permutations.<locals>.permutations1c             3   sf   t |  t }|d kr|n|}x@tt||dD ],}tt||kr2t  fdd|D V  q2W d S )N)r£   c             3   s   | ]} | V  qd S )Nr   )r7   r   )r   r   r   r   £  s    zHTestBasicOps.test_permutations.<locals>.permutations2.<locals>.<genexpr>)r   r   r€   r   r   )r(   r+   r   r   r   )r   r   Úpermutations2  s    z5TestBasicOps.test_permutations.<locals>.permutations2rV   c             S   s   g | ]}d | d qS )rW   r   r   )r7   r   r   r   r   r8   Š  s    z2TestBasicOps.test_permutations.<locals>.<listcomp>r   c             3   s   | ]}| kV  qd S )Nr   )r7   r;   )r   r   r   r   ¯  s    z1TestBasicOps.test_permutations.<locals>.<genexpr>)N)N)r`   r@   r   r   rD   r   r   r   r*   r   r   r   r   r0   rd   rL   )r   r¯   r°   r   r+   r   Úpr4   r   )r   r   Útest_permutationsz  s8    

	,
"
zTestBasicOps.test_permutationsc          	   C   s(   |   ttf¡ tdd W d Q R X d S )Nr~   i   @)r`   r   r   r   )r   r   r   r   Útest_permutations_overflow¹  s    z'TestBasicOps.test_permutations_overflowc          
   C   sH   |   tttttddd¡ |  ttttttddd¡ d S )Nr   rM   r   )rD   r   r   r_   r   r   r   r   )r   r   r   r   Útest_permutations_tuple_reuseŸ  s     z*TestBasicOps.test_permutations_tuple_reusec          	      s(  x t dD ]}dd | }xþt dD ]ð tt| d}tt| }tt| }tt| }|  t||  ¡ |  t||r°t|  d t  t|d  n  ¡ |  t| |krÎdnt|t|   ¡ |  t| |krúdnt|t  t|   ¡ |  |t	t
|¡ |  |t	t
|¡ |  |t	t
|¡ |  |t	t
|¡ |  |dd |D ¡ |  | fd	d|D ¡ |  |d
d |D ¡ |  | fdd|D ¡ |  |ttt
|j|¡ |  |ttt
|j|¡ |  |t	t
|t
|@ ¡ q*W qW d S )NrN   ÚABCDEFGrX   )r£   r   r   c             S   s    g | ]}t |t|kr|qS r   )r   r   )r7   Útr   r   r   r8   Ü  s    z3TestBasicOps.test_combinatorics.<locals>.<listcomp>c                s    g | ]}t t| kr|qS r   )r   r   )r7   r¶   )r+   r   r   r8   Ý  s    c             S   s    g | ]}t |t|kr|qS r   )r   r   )r7   r¶   r   r   r   r8   Þ  s    c                s    g | ]}t t| kr|qS r   )r   r   )r7   r¶   )r+   r   r   r8   ß  s    )r   r   r€   r   r   r   rD   r   r*   r   r   ÚfilterÚ__contains__)r   r   r3   r)   rª   ZpermZcombr   )r+   r   Útest_combinatoricsÃ  s,    :,4zTestBasicOps.test_combinatoricsc             C   s  |   ttdddddddgdtd¡ |   ttdddddddgtd¡ |   ttdddddddgtd¡ |   ttdddddddgtd¡ |   ttddddgtd¡ |   ttdddddddgtd	¡ d
}t ttd|¡}t td¡}|   tt||dddg| ¡ |  ttd td¡ |  tttdd ¡ |  tttd¡ |  tttdd ¡ xdd dd gt	 D ]ô}xìdddddddgddfdddddddgddfdddddddgddfddddgddfdddddddgd	dfgD ]|\}}}}|   t|t||dt|¡ |   t|t||t|¡ t||}|rþt
| |   t||t|¡ qþW qW d S )NÚABCDEFr   r   )ÚdataÚ	selectorsÚACEFrl   ZACr¡   ZBCi'  rN   )r   r   rM   rW   c             S   s
   t   | ¡S )N)rr   )rI   r   r   r   r5   õ  r6   z,TestBasicOps.test_compress.<locals>.<lambda>c             S   s
   t  | ¡S )N)rr   rs   )rI   r   r   r   r5   õ  r6   ZCEFZBCDEFr   )rD   r   Úcompressrn   rp   r£   r   r`   r@   rt   rF   )r   r   r»   rŒ   r   Zresult1Zresult2r   r   r   r   Útest_compressä  s6    (&&& & & 
zTestBasicOps.test_compressc          	   C   sn  |   tdt dddg¡ |   tdtddddg¡ |   td	tdtdddg¡ |   td	tdtd
ddg¡ |   td	tdtdddg¡ |  ttd	dd¡ |  ttd¡ |   tdttd tt	td td ¡ |   tdtt d tt	t d t d ¡ |   tdtddddg¡ |   tdtddddg¡ |   tdtt
dt
dt
dt
dg¡ |   tdttd	dtd	dtddtddg¡ dd> }|   tdt|||d |d	 g¡ td}|   t|d ¡ t| |   t|d!¡ td"}|   t|d#¡ t| |   t|d$¡ |   ttd%d&¡ |   ttd'd(¡ |   tttd't¡ x\tj d tj d d)d
d*dtjd tjd fD ](}tt|}d+ |¡}|   ||¡ qªW x|ddtd td fD ]d}t|}|   tt |¡|¡ |   tt |¡|¡ x(t	tjd D ]}|  |t|¡ q4W qìW tdtd  t ¡  d S ),NrT   )rI   r   )rJ   r   )rK   r   rM   )rI   rM   )rJ   r:   )rK   rW   r   r®   )rI   r®   )rJ   r   éýÿÿÿ)rI   rÀ   )rJ   r{   r:   rI   r.   rW   g      
@g      @g      @y      
@      Ày      @      Ày      @      Àz1.1z2.1z3.1rX   r   iè  zcount(3)zcount(4)i÷ÿÿÿz	count(-9)iøÿÿÿg     $@zcount(10.25)g      $@zcount(10.0)iöÿÿÿr   z	count(%r))rD   r   Úcountr%   r	   r`   r@   Úmaxsizer   r   r   r   ÚreprrF   rE   ÚfloatÚsysÚ__mod__rr   rs   r0   rd   rL   Úexc_info)r   ÚBIGINTrK   r   Úr1Úr2Úvaluer4   r   r   r   Ú
test_count  sT       $6
 zTestBasicOps.test_countc             C   sv  |   tdtdddddg¡ |   tdtddddddg¡ |   tdtdd	d
ddg¡ |  ttdd¡ |   tdtdddddg¡ |   tdtdddddg¡ |   tdtdddddg¡ |   tdttd dtdttd td d¡ |   tdtt d dtdtt d t d d¡ |   tdtdtd ttdddtd   td ¡ |   tdtdddddg¡ |   tdtdddddg¡ |   tdtt	d t	d!t	d t	d"t	d#g¡ |   tdtt
ddt
dd$t
ddt
d%d&t
dd&g¡ dd'> }|   tdt|d	d|d| g¡ |   ttdtdd(tdd)d*g¡ tdd}|   t|d+¡ t| |   t|d,¡ td-d}|   t|d.¡ t| |   t|d.¡ td-d/}|   t|d0¡ t| |   t|d1¡ |   t|d1¡ |   ttd2dd3¡ |   ttd2dd4¡ |   ttd2d5d6¡ |   ttdd5d7¡ tdd5}|   tt|t¡ |   tt|t¡ xÞtj d tj d d8dddtjd tjd fD ]ª}x¢tj d tj d d8ddddtjd tjd f	D ]l}tt||}|dkr"d9| }nd:||f }|   ||¡ x*ttjd D ]}|  |t||¡ qJW qüW qÄW d S );NrT   r   rM   )rI   r   )rJ   rW   )rK   rX   )ÚstartÚstepr®   )rÎ   )rI   r   )rJ   r®   )rK   r{   rI   rJ   r   )rJ   r   )rK   r   r   )rJ   rM   )rK   r:   é   rO   éd   r.   rW   g      ô?g      
@g      @y      
@      Ày      @      Ày      !@       Àz1.1z.1z1.2z1.3rV   é   rP   iè  g      @g      )@g      .@zcount(3, 5)zcount(8, 5)i÷ÿÿÿzcount(-9, 0)rÀ   zcount(-9, -3)zcount(-12, -3)g      %@zcount(10.5, 1.25)zcount(10.5)g      ð?zcount(10.5, 1.0)zcount(10, 1.0)iöÿÿÿz	count(%r)zcount(%r, %r))rD   r   rÁ   r`   r@   r%   rÂ   r   r   r   r   rÃ   rF   rE   r]   rÄ   rÅ   r0   rd   rL   )r   rÈ   rK   r   r   rÉ   rÊ   r4   r   r   r   Útest_count_with_stride6  sf    28""&



68

z#TestBasicOps.test_count_with_stridec                s  |   tdtdtd¡ |   ttdg ¡ |  tt¡ |  ttd¡ |   tttt dddddddddddg
¡ td |   t d	¡ |   tdt	 
 ¡td
¡ xvttjd D ]d}|   tdt t  |¡¡td
¡ t  |   tdt t  |¡¡td¡ t  t  qŒW x(ttjd D ]}|  |td¡ q4W xŒttjd D ]ª}td}t|  fddtdD }t  |¡}t |¡}|   td|td¡ td}t|  fddtdD }t  |¡}t |¡}|   td|td¡ q^W d S )Nr.   rT   Z
abcabcabcarl   rW   r   r   r   rI   Z
bcabcabcabZ
cabcabcabcr   c                s   g | ]}t  qS r   )rF   )r7   r   )rK   r   r   r8     s    z+TestBasicOps.test_cycle.<locals>.<listcomp>rÏ   Zcdeabcdeabcdeabcdeabc                s   g | ]}t  qS r   )rF   )r7   r   )rK   r   r   r8     s    rV   )rD   r%   Úcycler   r`   r@   r#   r   rF   rr   rs   r   r0   rd   r1   r2   rL   ry   )r   r4   rA   Ú_r±   rv   r   )rK   r   Ú
test_cyclem  s>    0



zTestBasicOps.test_cyclec          	   C   s   t d}| tddf¡ |  td|td¡ t d}| tddf¡ |  td|td¡ |  t¡ t d tddg¡ W d Q R X |  t¡  t d}| tddf¡ W d Q R X td| |  t¡ t d tddf¡ W d Q R X |  tt d	jd
¡ |  tt d	jg f¡ d S )NÚdefgrT   r   rÏ   ZdefgabcdefgabcdefgabÚabcdefgr   r   rl   r   )rÓ   rx   r   rD   r%   r`   r@   r   )r   rK   r   r   r   Útest_cycle_setstate  s      
 z TestBasicOps.test_cycle_setstatec          
      s6  |   g ttg ¡ |   g ttg td¡ |  tttdg ¡ |  ttd ¡ |  ttddd d¡ dddd	d
dddg}g }xBt|dd D ]0\}}x&|D ]}|   ||d ¡ | |¡ qW qW |   ||¡ xvttj	d D ]d}g }xNt 
t t|t|¡¡D ]2\}}x(|D ] }|   ||d ¡ | |¡ qW qþW |   ||¡ qÜW g }xlt|tD ]^\}}xRt|tD ]D\}}x8|D ]0}|   ||d ¡ |   ||d ¡ | |¡ qvW qhW qTW |   ||¡ x²ttj	d D ] }g }xt 
t t|t|¡¡D ]l\}}x`t 
t t|t|¡¡D ]D\}}x8|D ]0}|   ||d ¡ |   ||d ¡ | |¡ q$W qW qôW |   ||¡ qÒW dd t|tD }	tdd |D }
|   t|	|
¡ |   t|	t|
¡ ttdtd}t|t}t|\}}t|\}}t|\}}|   t|g ¡ |   t|g ¡ |   t|d¡ t| |   t|g ¡ x\ttj	d D ]J}t|t}t|\}}t| t| |   tt 
t ||¡¡g ¡ qXW d}dd tt|D }|   |dddddg¡ dd tt|D }|   |dddg¡ d d tt|D }|   |d!d"d#d$d%g¡ td&d tt|D d'd(d d) }|   |d!d%d"g¡ G d*d+ d+t d5 fd,d-	}d tfd.d/}|   ||d¡ |   ||d¡ G  fd0d1d1}| | d g}| j ||td2 |   ||¡  fd3d4d_|   |d g¡ d_|   |d d g¡ d S )6N)ÚkeyrT   c             S   s   | S )Nr   )r   r   r   r   r5   Á  r6   z+TestBasicOps.test_groupby.<locals>.<lambda>r.   )r   r.   rÏ   )r   é   rP   )r   r   rP   )r   é   rP   )r   é   é   )r   rO   rÝ   )rM   rZ   é   )rM   rÑ   rÞ   c             S   s   | d S )Nr   r   )r+   r   r   r   r5   Ç  r6   r   r   r   c             S   s   g | ]\}}|qS r   r   )r7   r§   Úgr   r   r   r8   í  s    z-TestBasicOps.test_groupby.<locals>.<listcomp>c             S   s   g | ]}|d  qS )r   r   )r7   r+   r   r   r   r8   î  s    Z	AABBBAAAArY   )r~   rW   Zabracadabrac             S   s   g | ]\}}|qS r   r   )r7   r§   rß   r   r   r   r8   	  s    rI   rJ   rK   rv   r+   c             S   s$   g | ]\}}t t|d dr|qS )r   r   )r   r#   )r7   r§   rß   r   r   r   r8     s    c             S   s    g | ]\}}t t||fqS r   )r   r   )r7   r§   rß   r   r   r   r8     s    )rW   rI   )r   rJ   )r   rK   )r   rv   )r   r+   c             S   s    g | ]\}}t t||fqS r   )r   r   )r7   r§   rß   r   r   r   r8     s    T)ÚreverserM   c               @   s   e Zd ZdS )z0TestBasicOps.test_groupby.<locals>.ExpectedErrorN)r    r!   r"   r   r   r   r   ÚExpectedError  s   rá   c             3   s    xt | D ]
}dV  q
W  d S )NZyo)r   )r   r   )rá   r   r   Údelayed_raise  s    
z0TestBasicOps.test_groupby.<locals>.delayed_raisec                s    fddt | |D S )Nc                s   g | ]\}} |qS r   r   )r7   r§   rß   )Úfuncr   r   r8     s    z;TestBasicOps.test_groupby.<locals>.gulp.<locals>.<listcomp>)r©   )r(   Zkeyprã   r   )rã   r   Úgulp  s    z'TestBasicOps.test_groupby.<locals>.gulpc                   s   e Zd Z fddZdS )z+TestBasicOps.test_groupby.<locals>.DummyCmpc                s    d S )Nr   )r   Údst)rá   r   r   Ú__eq__&  s    z2TestBasicOps.test_groupby.<locals>.DummyCmp.__eq__N)r    r!   r"   ræ   r   )rá   r   r   ÚDummyCmp%  s   rç   )rã   c                s$   j dkr j d8  _ | S  d S )Nr   r   )Úskip)Úobj)rá   Úkeyfuncr   r   rê   0  s    
z*TestBasicOps.test_groupby.<locals>.keyfunc)r   )rD   r   r©   r   r`   r@   Úappendr   r0   rd   r1   r2   r,   r-   r   r   r	   rF   r   Ú	Exceptionrè   )r   r3   Údupr§   rß   Úelemr4   ZikZigÚkeysZexpectedkeysrA   rÔ   Zg1Zg2Zg3r+   râ   rä   rç   r   )rá   rê   r   Útest_groupby»  s     


"

""


$&zTestBasicOps.test_groupbyc          
   C   sì  |   ttttddddg¡ |   ttd dddddgddg¡ |   tttdddddgddg¡ |   tdttt ddddg¡ |  t	t¡ |  t	tdd ¡ |  t	tdd tdd	¡ |  t	ttd
¡ |  t	t
ttdtd¡ dddg}tttd}|   tt |¡|¡ tttd}|   tt |¡|¡ xrttjd D ]`}tttd}|   tt t ||¡¡|¡ t
| |   tt t ||¡¡|dd  ¡ qPW x2ttjd D ] }tttd}|  ||¡ qÄW d S )NrN   r   r   r:   r   c             S   s   | S )Nr   )r   r   r   r   r5   C  r6   z*TestBasicOps.test_filter.<locals>.<lambda>c             S   s   | S )Nr   )r   r   r   r   r5   D  r6   rV   rM   )rD   r   r·   r   r   Úboolr%   rÁ   r`   r@   rF   rr   rs   r0   rd   r1   r2   rL   )r   ÚansrK   r4   r   r   r   Útest_filter=  s,     $$"
,zTestBasicOps.test_filterc          
   C   s&  |   ttttddddg¡ |   ttd dddddgdddg¡ |   tttdddddgdddg¡ |   tdttt ddddg¡ |  t	t¡ |  t	td	d
 ¡ |  t	tdd
 tdd¡ |  t	ttd¡ |  t	t
ttdtd¡ x.ttjd D ]}|  |tttd¡ qW d S )NrN   r   rM   rW   r   r   r:   rV   c             S   s   | S )Nr   )r   r   r   r   r5   ^  r6   z/TestBasicOps.test_filterfalse.<locals>.<lambda>c             S   s   | S )Nr   )r   r   r   r   r5   _  r6   )rD   r   Úfilterfalser   r   rñ   r%   rÁ   r`   r@   rF   r0   rd   rL   )r   r4   r   r   r   Útest_filterfalseX  s     &&"zTestBasicOps.test_filterfalsec             C   s*  dd t dt D }|  |dddg¡ |  tt dtdtdtd¡ |  tt dtd	tdtd	¡ |  td	t dt tdtd	¡ |  tt dtd¡ |  tt  t ¡ |  tt d	¡ |  tt td	d	¡ |  d
d t ddD tdd¡ |  dd t ddD tdd¡ d S )Nc             S   s   g | ]\}}||fqS r   r   )r7   r   Úyr   r   r   r8   g  s    z)TestBasicOps.test_zip.<locals>.<listcomp>rT   )rI   r   )rJ   r   )rK   r   rN   rk   rM   c             S   s   g | ]}t t|qS r   )r   r   )r7   Úpairr   r   r   r8   p  s    rj   c             S   s   g | ]}|qS r   r   )r7   r÷   r   r   r   r8   r  s    )	r	   rÁ   rD   r   r   r   r%   r`   r@   )r   rò   r   r   r   Útest_zipe  s    $$$zTestBasicOps.test_zipc          	   C   s  t tttdd}|  t|t|¡ t ttt tdd}|  tt 	|¡t|¡ dd t
 
tdt ¡D }|  |dddg¡ dd t
 tdt ¡D }|  |dddg¡ xLttjd	 D ]:}d
d t t tdt |¡¡D }|  |dddg¡ qÆW xXttjd	 D ]F}tdt }t| dd t t ||¡¡D }|  |ddg¡ qW x,ttjd	 D ]}|  |tdt ¡ qnW d S )NrT   rj   c             S   s   g | ]\}}||fqS r   r   )r7   r   rö   r   r   r   r8   }  s    z5TestBasicOps.test_zip_tuple_reuse.<locals>.<listcomp>)rI   r   )rJ   r   )rK   r   c             S   s   g | ]\}}||fqS r   r   )r7   r   rö   r   r   r   r8     s    r   c             S   s   g | ]\}}||fqS r   r   )r7   r   rö   r   r   r   r8     s    c             S   s   g | ]\}}||fqS r   r   )r7   r   rö   r   r   r   r8     s    )r   r_   r   r	   rD   ra   rb   r   ÚdictÚfromkeysrr   rÁ   rs   r   r0   rd   r1   r2   rF   rL   )r   Úidsrò   r4   r   r   r   r   Útest_zip_tuple_reuseu  s$    $z!TestBasicOps.test_zip_tuple_reusec                sV  xödt dgt ddgt dt ddt ddgt dt dt ddt d	t d
gt dt dt ddt d	t d
t dggD ]x  fddt ttt D }|  tt  |¡ |  tt i |¡ dd |D }|  tt tdd|¡ q|W |  tdtdt	 tt
dt d¡ |  tt tt
 ¡ |  ttg tt
g ¡ |  ttdtt
d¡ |  ttdi tt
tdd g d¡ |  ttd¡ |  ttt dd¡ xHdD ]@}yt|t t  W n tk
rð   Y nX |  d| ¡ qÂW |  dd tddD tt
dd¡ |  dd tddD tt
dd¡ d S )NrT   rN   iè  iÐ  i4  iž  iê  r   i°  iÜ  c                s"   g | ] t  fd dD qS )c                s$   g | ]} t |k r|  nd qS )N)r   )r7   Úarg)r   r   r   r8     s    z;TestBasicOps.test_ziplongest.<locals>.<listcomp>.<listcomp>)r   )r7   )r
   )r   r   r8     s   z0TestBasicOps.test_ziplongest.<locals>.<listcomp>c             S   s   g | ]}t d d |D qS )c             s   s   | ]}|d krdp|V  qd S )NÚXr   )r7   r;   r   r   r   r     s    z:TestBasicOps.test_ziplongest.<locals>.<listcomp>.<genexpr>)r   )r7   r¶   r   r   r   r8     s    rþ   )Ú	fillvaluerM   rk   rÖ   )zzip_longest('abc', fv=1)z3zip_longest('abc', fillvalue=1, bogus_keyword=None)zDid not raise Type in:  c             S   s   g | ]}t t|qS r   )r   r   )r7   r÷   r   r   r   r8   µ  s    rj   c             S   s   g | ]}|qS r   r   )r7   r÷   r   r   r   r8   ·  s    )rT   rÖ   )r   rb   r_   r   rD   r   Úzip_longestrù   r%   rÁ   r	   r`   r@   ÚevalÚglobalsÚlocalsZfail)r   ÚtargetZstmtr   )r
   r   Útest_ziplongest  s<    

"0
 (
zTestBasicOps.test_ziplongestc             C   s^   t tttdd}|  t|t|¡ t ttt tdd}|  tt 	|¡t|¡ d S )NrT   rj   )
r   r_   r   r   rD   ra   rb   r   rù   rú   )r   rû   r   r   r   Útest_zip_longest_tuple_reuseº  s    z)TestBasicOps.test_zip_longest_tuple_reusec          	   C   sh   xbt tjd D ]P}|  |tdd¡ |  |tdd¡ |  |tdddd¡ |  |tdd¡ qW d S )Nr   rT   rj   Zdefgh)rÿ   rl   )r   r0   rd   rL   r   )r   r4   r   r   r   Útest_zip_longest_picklingÁ  s
    z&TestBasicOps.test_zip_longest_picklingc             C   s°   G dd d}|ddt }|ddt }dd }|  |||d	d	d	d
g¡ |ddt}|ddt }t||dd}|  t|d	¡ |  t|d	¡ |  t|d	¡ |  tt|¡ d S )Nc               @   s$   e Zd Zdd Zdd Zdd ZdS )z,TestBasicOps.test_bug_7244.<locals>.Repeaterc             S   s   || _ t|| _|| _d S )N)Úor]   r¶   r;   )r   r  r¶   r;   r   r   r   Ú__init__Ì  s    
z5TestBasicOps.test_bug_7244.<locals>.Repeater.__init__c             S   s   | S )Nr   )r   r   r   r   r   Ð  s    z5TestBasicOps.test_bug_7244.<locals>.Repeater.__iter__c             S   s(   | j dkr|  j d8  _ | jS | jd S )Nr   r   )r¶   r  r;   )r   r   r   r   r   Ò  s    
z5TestBasicOps.test_bug_7244.<locals>.Repeater.__next__N)r    r!   r"   r	  r   r   r   r   r   r   ÚRepeaterÊ  s   r
  r   rM   r   r:   c          
   S   sT   g }xJt | |ddD ]8\}}t d¡ t||f W d Q R X | ||f¡ qW |S )Nr   )rÿ   Ústdout)r   r   Zcaptured_outputÚprintrë   )rÉ   rÊ   r   r   r   r   r   r   ÚrunÝ  s    z'TestBasicOps.test_bug_7244.<locals>.run)r   r   )r   r   r   )rÿ   )r   rD   r=   r   rF   r`   )r   r
  rÉ   rÊ   r  rA   r   r   r   Útest_bug_7244È  s    zTestBasicOps.test_bug_7244c                s  xØg dgfdgddgft dt dgddd	d
ddgft dt dt dgg ft dt dt dgg ft dt dt dgg fgD ]V\}}|  tt| |¡ x8t dD ],}|  tt||  tt|t|d¡ qŠW qW |  tttt dgd  d¡ |  ttt dd ¡ dd }dd }dddt dt dtddddtdt dt	t dg	 x®t dD ]¢} fdd t t
 d!¡D }ttt|}|  ttt| |¡ |  tt| t|| ¡ |  tt| t|| ¡ tt|}|  ttt| |¡ qbW d S )"Nr   rU   )rI   )rJ   r   rM   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   r   r:   )r£   rV   rN   iË c                 sø   t tt| | dd¡ }t|}|dkr4dV  d S tdd |D rJd S dg| }tdd t||D V  xx~tt|D ]j}|| t|| d krq~||  d7  < xt|d |D ]}d||< qŒW tdd t||D V  P q~W d S qpW d S )	Nr£   r   r   r   c             s   s   | ]}t |d kV  qdS )r   N)r   )r7   r   r   r   r   r     s    z>TestBasicOps.test_product.<locals>.product1.<locals>.<genexpr>c             s   s   | ]\}}|| V  qd S )Nr   )r7   r   r   r   r   r   r   	  s    c             s   s   | ]\}}|| V  qd S )Nr   )r7   r   r   r   r   r   r     s    )	r   r_   r   Úgetr   Úanyr	   r   r   )r
   ÚkwdsÚpoolsr   r   r   r   r   r   r   Úproduct1   s&    
z+TestBasicOps.test_product.<locals>.product1c              ?   s\   t tt| | dd¡ }g g}x|D ]  fdd|D }q&W x|D ]}t|V  qFW d S )Nr£   r   c                s    g | ]} D ]}||g qqS r   r   )r7   r   rö   )r   r   r   r8     s    z?TestBasicOps.test_product.<locals>.product2.<locals>.<listcomp>)r   r_   r   r  )r
   r  r  r   r)   r   )r   r   Úproduct2  s    

z+TestBasicOps.test_product.<locals>.product2rl   rT   r   )rI   rJ   rK   r×   rÚ   rÛ   rÐ   c                s   g | ]}t   ¡qS r   )ÚrandomÚchoice)r7   r   )Úargtypesr   r   r8   "  s    z-TestBasicOps.test_product.<locals>.<listcomp>rW   )r   rD   r   r€   rù   r   r`   r@   r   r   r  Z	randranger)   r_   ry   )r   r
   r   r+   r  r  r   Zexpected_lenr   )r  r   Útest_productð  s2    $"	
zTestBasicOps.test_productc          	   C   s2   |   ttf¡ tdgd ddi W d Q R X d S )NrU   r|   r£   i   )r`   r   r   r€   )r   r   r   r   Útest_product_overflow*  s    z"TestBasicOps.test_product_overflowc          
   C   sH   |   tttttddd¡ |  ttttttddd¡ d S )NrT   rj   r   )rD   r   r   r_   r   r€   r   r   )r   r   r   r   Útest_product_tuple_reuse/  s     z%TestBasicOps.test_product_tuple_reusec          	   C   sì   xæg dgfdgddgft dt dgddd	d
ddgft dt dt dgg ft dt dt dgg ft dt dt dgg fgD ]d\}}|  tt t| ¡|¡ |  tt t| ¡|¡ x&t tjd D ]}|  |t| ¡ qÌW qW d S )Nr   rU   )rI   )rJ   r   rM   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   r   r   )	r   rD   r   rr   r€   rs   r0   rd   rL   )r   r
   r   r4   r   r   r   Útest_product_pickling4  s    $z"TestBasicOps.test_product_picklingc             C   sL   t dd}| d¡ |  t|d¡ t ddd}| d¡ |  tt|¡ d S )N)r   r   )rM   )r   i   )r   rM   r   )r   r   i   )r€   rx   rD   rF   r`   r   )r   r±   r   r   r   Útest_product_issue_25021C  s    


z%TestBasicOps.test_product_issue_25021c             C   s   |   ttddddddg¡ |   ttdtddddg¡ |   ttdddddg¡ |   tdtddddg¡ |   ttddg ¡ |   ttddg ¡ |  tt¡ |  ttd dd	¡ |  ttd d¡ td
}|   t|d¡ td
d}|   t|d¡ t| |   t|d¡ tddd}|   t	|d¡ |   tdt
 
|¡td¡ |   tdt
 |¡td¡ x,ttjd D ]}|  |tddd¡ q~W d S )NrI   rM   )ÚobjectÚtimes)r   rI   )r   rI   )r   rI   r   rÀ   r:   y      ð?        zrepeat((1+0j))rW   zrepeat((1+0j), 5)zrepeat((1+0j), 0)r.   r   Zaar   )rD   r   r£   r   r   r%   r`   r@   rÃ   rF   rr   rs   r0   rd   rL   )r   r+   rK   r4   r   r   r   Útest_repeatM  s,    
zTestBasicOps.test_repeatc             C   s`   |   ttddd¡ |   ttddd¡ |   ttdddd¡ |   ttdddd¡ d S )NrI   r®   zrepeat('a', 0)r{   )r  )rD   rÃ   r£   )r   r   r   r   Útest_repeat_with_negative_timesg  s    z,TestBasicOps.test_repeat_with_negative_timesc          	   C   sŽ  |   tttjtdtdddddg¡ |   tttdtddd	d
g¡ |   tttdt dd	d
g¡ |   tdttdt dd	g¡ |   tttjg g ¡ |  	t
t¡ |  	t
ttd tdtd¡ |  	t
ttj¡ |  	t
ttdtd¡ |  	ttttdgdg¡ |  	t
tttdgdg¡ dd	d
g}ttdt }|   tt |¡|¡ ttdt }|   tt |¡|¡ x2ttjd D ] }ttdt }|  ||¡ qW d S )NrM   r   rV   r   rX   rT   rW   )rI   r   )rJ   r   )rK   r   r   r.   r:   )rD   r   r_   r&   Úpowr   r   rÁ   r%   r`   r@   ÚnegrF   r   r   r   rr   rs   r0   rd   rL   )r   rò   rK   r4   r   r   r   Útest_mapm  s.    

zTestBasicOps.test_mapc          
   C   sÆ  |   tttjttdtdddddg¡ |   tdttjtt tddddg¡ |   tttjg g ¡ |   tttjt	ddggdg¡ |  
ttttjd g¡ |  
tt¡ |  
tttjd	gd
¡ |  
tttdd	g¡ |  
ttttd	g¡ |  
ttttd	g¡ dddg}ttjttdtdd}|   tt |¡|¡ ttjttdtdd}|   tt |¡|¡ x@ttjd D ].}ttjttdtdd}|  ||¡ qW d S )NrM   r   rV   r   rX   r:   rW   i   )r:   rW   Úextrar.   )rD   r   Ústarmapr&   r!  r	   r   r%   rÁ   ry   r`   r@   rF   r   r   r   rr   rs   r0   rd   rL   )r   rò   rK   r4   r   r   r   Útest_starmap  s(    " $
zTestBasicOps.test_starmapc       	   	   C   sX  x2dD ]*}|   tttdf| tt| ¡ qW x6dD ].\}}|   tttdf| tt| ¡ q:W |   tttdd ttd¡ |   tttdd d ttd¡ |   tttdd d d ttd¡ |   tttddd ttdd¡ |   tttddd dttddd¡ ttd}|   tt|dttd¡ |   t|ttdd¡ ttd}|   tt|ddg ¡ |   t|ttdd¡ td}|  tt|¡ |  tt|dddd¡ |  tt|d	dd¡ |  tt|dd	d
¡ |  tt|ddd
¡ |  tt|ddd¡ |  tt|d¡ |  tt|dd¡ |  tt|dd¡ |  tt|ddd¡ |  tt|ddd¡ |   tttt	 ddt
d¡ t	 }|   tt|ddddg¡ |   t|d¡ xdD ]}|   tt ttdf| ¡tt| ¡ |   tt ttdf| ¡tt| ¡ x2ttjd D ] }|  |ttdf| ¡ qFW qÚW dd dD }t |¡}t|d}|  | ¡ t| t ¡  |  | ¡ G dd dt}|   tttd|dttd¡ |   tttd|d|dttdd¡ |   tttd|d|d|dttddd¡ d S )N))r.   rÏ   rM   )r.   rM   rÏ   )r.   rÏ   )r.   r.   )r.   rM   )rÏ   rÐ   )))r.   én   rM   )r.   rÐ   rM   ))r.   r'  )r.   rÐ   ))r'  )rÐ   r.   r   r   rM   r:   éûÿÿÿr®   r   rI   é2   ))r.   rÏ   rM   )r.   rM   rÏ   )r.   rÏ   )r.   rM   )rÏ   c             s   s   | ]
}|V  qd S )Nr   )r7   r   r   r   r   r   ï  s    z+TestBasicOps.test_islice.<locals>.<genexpr>)r   r   c               @   s   e Zd Zdd Zdd ZdS )z)TestBasicOps.test_islice.<locals>.IntLikec             S   s
   || _ d S )N)Úval)r   r*  r   r   r   r	  ú  s    z2TestBasicOps.test_islice.<locals>.IntLike.__init__c             S   s   | j S )N)r*  )r   r   r   r   Ú	__index__ü  s    z3TestBasicOps.test_islice.<locals>.IntLike.__index__N)r    r!   r"   r	  r+  r   r   r   r   ÚIntLikeù  s   r,  rW   )rD   r   r#   r   ry   r`   r@   r   r   rÁ   rÂ   rF   rr   rs   r0   rd   rL   ÚweakrefÚrefZassertIsNotNoner   Z
gc_collectZassertIsNoner  )	r   r
   ZtgtargsrA   ZrarK   r4   Zwrr,  r   r   r   Útest_isliceŠ  sj    
"$&&* 
&

&$zTestBasicOps.test_islicec             C   sR  ddddddddg}|   ttt|dddg¡ |   tttg g ¡ |  tt¡ |  tttj¡ |  tttjd	gd
¡ |  tttdd	g¡ |  t	ttt
d	g¡ ttddddddg}|   t|dddg¡ |  tt|¡ |   tt tt|¡dddg¡ |   tt tt|¡dddg¡ x*ttjd D ]}|  |tt|¡ q2W d S )Nr   rM   rW   rÏ   r   r:   rN   rX   )r:   rW   r$  r.   r   )rD   r   Ú	takewhiler/   r`   r@   r&   r!  rF   r   r   rñ   r   rr   rs   r   r0   rd   rL   )r   r»   r¶   r4   r   r   r   Útest_takewhile  s     "zTestBasicOps.test_takewhilec             C   s$  ddddddddg}|   ttt|dddddg¡ |   tttg g ¡ |  tt¡ |  tttj¡ |  tttjd	gd
¡ |  tttdd	g¡ |  t	ttt
d	g¡ |   tt tt|¡dddddg¡ |   tt tt|¡dddddg¡ x*ttjd D ]}|  |tt|¡ qW d S )Nr   rM   rW   rÏ   r   r:   rN   rX   )r:   rW   r$  r.   )rD   r   Ú	dropwhiler/   r`   r@   r&   r!  rF   r   r   rr   rs   r   r0   rd   rL   )r   r»   r4   r   r   r   Útest_dropwhile  s     &zTestBasicOps.test_dropwhilec             C   s  d}t g \}}|  t|g ¡ |  t|g ¡ t t|\}}|  t||tt|t|¡ t t|\}}|  t|tt|¡ |  t|tt|¡ t t|\}}x tdD ]}|  t||¡ qºW ~|  t|tt|¡ t t|\}}x"tdD ]}|  t||¡ qW ~|  t|ttd|¡ xtdD ]}dg| dg|  }t |¡ g g f}t t|}x(|D ] }t|| }	||  	|	¡ qW |  |d tt|¡ |  |d tt|¡ qFW |  
tt ¡ |  
tt d¡ |  
tt ddgd¡ |  
tt ddgdd¡ t d	\}}t|d
}
|  t|
td
¡ t tdd\}}}
x"tdD ]}|  t||¡ qjW |  t|ttd¡ |  t|
t|
gttd¡ |  t|ttdd¡ |  t|
ttdd¡ |  
tt d	d¡ |  
tt g d¡ x\tdD ]P}t d	|}|  t|t¡ |  t||¡ |  dd |D td	g| ¡ qW t d	\}}t |\}
}|  ||
k¡ t d	\}}t|}|  
t|¡ |  
t|d¡ ||}|  t|t|  koþt|  koþtd	kn  ¡ t td\}}t |¡}|  t|dt|¡ ~|  
tt|d¡ td	}ttd}t d	\}}|  tt |¡|¡ |  tt |¡|¡ t ttd\}}|  tt |¡|¡ |  tt |¡|¡ t d	\}}td| td| |  tt |¡|dd  ¡ |  tt |¡|dd  ¡ |  t||dd  ¡ |  t||dd  ¡ t td\}}td| td| |  tt |¡|dd  ¡ |  tt |¡|dd  ¡ |  t||dd  ¡ |  t||dd  ¡ t d	\}}|  tt |¡|¡ |  tt |¡|¡ |  t||¡ |  t||¡ t td\}}|  tt |¡|¡ |  tt |¡|¡ |  t||¡ |  t||¡ t d	\}}td| td| |  tt |¡|dd  ¡ |  tt |¡|dd  ¡ |  t||dd  ¡ |  t||dd  ¡ t td\}}td| td| |  tt |¡|dd  ¡ |  tt |¡|dd  ¡ |  t||dd  ¡ |  t||dd  ¡ xXttjd D ]F}|  |tt d	¡ t d	\}}| j|||d | j|||d qÐW d S )NéÈ   rÐ   rW   r   r   rM   r   r   rT   rj   iÐ  Zinvalidr®   c             S   s   g | ]}t |qS r   )r   )r7   r   r   r   r   r8   i  s    z)TestBasicOps.test_tee.<locals>.<listcomp>r.   Ú	__class__i'  é<   )rG   )ÚteerD   r   r   r   r   rF   r  Zshufflerë   r`   r@   rE   r   r   r   r   r-  ÚproxyÚgetattrÚReferenceErrorrr   r%   rs   r0   rd   rL   ry   )r   r   rI   rJ   r   r   ÚorderZlistsZitsrË   rK   r   rv   Zt1Zt2ZtnewZt3r±   rò   Zlong_ansr4   r   r   r   Útest_tee)  sæ     

 
&<








zTestBasicOps.test_teec             C   s8   t td d\}}yt| ~W n   ~~ Y nX d S )Ni -1)r7  r£   r  )r   ZforwardZbackwardr   r   r   Útest_tee_del_backwardŸ  s    z"TestBasicOps.test_tee_del_backwardc             C   s,  |   ttt ¡ x:ttttfD ]*}|   tt|g ¡ |   tt|t ¡ qW |   tttg d ¡ |   tttt d ¡ t	g \}}|   tt|¡ |   tt|¡ t	t \}}|   tt|¡ |   tt|¡ |   ttt
d d¡ xJttttttfD ]6}|   tt|dd g ¡ |   tt|dd t ¡ qîW d S )Nr   c             S   s   | S )Nr   )r   r   r   r   r5   Û  r6   z1TestBasicOps.test_StopIteration.<locals>.<lambda>c             S   s   | S )Nr   )r   r   r   r   r5   Ü  r6   )r`   r   rF   r	   rn   rÓ   r©   r   r#   r7  r£   r·   rô   r_   r0  r2  r%  )r   rw   r±   Úqr   r   r   Útest_StopIterationÇ  s     zTestBasicOps.test_StopIteration)r:   r   N)3r    r!   r"   rL   rf   ro   rq   ru   rz   r   r   Zbigaddrspacetestr   Zimpl_detailr    r«   r¬   r­   r²   r³   rŽ   r¹   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  r3  r<  r=  r?  r   r   r   r   r9   O   s^   
)PL?!"07*$ *(:
^ 	r9   c               @   sÄ   e Z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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 )0ÚTestExamplesc          	   C   s,   |   ttdddddgdddddg¡ d S )	Nr   r   rM   r:   rW   rN   r.   rO   )rD   r   r\   )r   r   r   r   rf   à  s    zTestExamples.test_accumulatec          
   C   sð   dddddg}dddddg}xzt tjd D ]h}t|}|  tt t ||¡¡|d d  ¡ |  t|d¡ |  tt t ||¡¡|dd  ¡ q,W t|}|  t|d¡ |  tt	 
|¡|dd  ¡ |  tt	 	|¡|dd  ¡ d S )	Nr   r   rM   r:   rW   rN   r.   rO   )r   r0   rd   r\   rD   r   r1   r2   rF   rr   rs   )r   r»   Zaccumulatedr4   rA   r   r   r   Útest_accumulate_reducibleã  s    &*z&TestExamples.test_accumulate_reduciblec             C   s   t d d d gtj}|  t|d ¡ x<ttjd D ]*}t t 	||¡¡}|  t
|ddg¡ q2W |  t
t |¡ddg¡ |  t
t |¡ddg¡ d S )Nr   TF)r\   r&   Úis_rD   rF   r   r0   rd   r1   r2   r   rr   rs   )r   rA   r4   Zit_copyr   r   r   Útest_accumulate_reducible_noneò  s    z+TestExamples.test_accumulate_reducible_nonec             C   s   |   d tdd¡d¡ d S )Nrl   r¡   ÚDEFrº   )rD   Újoinrn   )r   r   r   r   ro   ü  s    zTestExamples.test_chainc             C   s    |   d t ddg¡¡d¡ d S )Nrl   r¡   rD  rº   )rD   rE  rn   rp   )r   r   r   r   rq   ÿ  s    z%TestExamples.test_chain_from_iterablec          	   C   sH   |   ttddddddddg¡ |   tttd	d
ddddg¡ d S )Nr}   r   )r~   r   )r~   r   )r~   r   )r   r   )r   r   )r   r   r:   rM   )r   r   r   )r   r   rM   )r   r   rM   )r   r   rM   )rD   r   r   r   )r   r   r   r   r     s    zTestExamples.test_combinationsc          	   C   s&   |   ttddddddddg¡ d S )	Nr¡   r   )r~   r~   )r~   r   )r~   r   )r   r   )r   r   )r   r   )rD   r   r   )r   r   r   r   r«     s    z/TestExamples.test_combinations_with_replacementc             C   s*   |   ttdddddddgtd¡ d S )Nrº   r   r   rœ   )rD   r   rŸ   )r   r   r   r   r¿     s    zTestExamples.test_compressc             C   s(   |   tttdddddddg¡ d S )Nr.   rW   rÚ   r   rÛ   rÜ   )rD   r   r#   rÁ   )r   r   r   r   rÌ     s    zTestExamples.test_countc             C   s"   |   tttddtd¡ d S )Nr}   r   ZABCDABCDABCD)rD   r   r#   rÓ   )r   r   r   r   rÕ     s    zTestExamples.test_cyclec          
   C   s.   |   ttdd dddddgdddg¡ d S )Nc             S   s   | dk S )NrW   r   )r   r   r   r   r5     r6   z-TestExamples.test_dropwhile.<locals>.<lambda>r   r:   rN   )rD   r   r2  )r   r   r   r   r3    s    zTestExamples.test_dropwhilec             C   sT   |   dd tdD td¡ |   dd tdD tdtdtd	td
g¡ d S )Nc             S   s   g | ]\}}|qS r   r   )r7   r§   rß   r   r   r   r8     s    z-TestExamples.test_groupby.<locals>.<listcomp>ZAAAABBBCCDAABBBZABCDABc             S   s   g | ]\}}t |qS r   )r   )r7   r§   rß   r   r   r   r8     s    Z
AAAABBBCCDZAAAAZBBBZCCr   )rD   r©   r   )r   r   r   r   rð     s    
zTestExamples.test_groupbyc             C   s,   |   ttdd tddddddg¡ d S )	Nc             S   s   | d S )Nr   r   )r   r   r   r   r5     r6   z*TestExamples.test_filter.<locals>.<lambda>r.   r   rM   rW   rV   rY   )rD   r   r·   r   )r   r   r   r   ró     s    zTestExamples.test_filterc             C   s,   |   ttdd tddddddg¡ d S )	Nc             S   s   | d S )Nr   r   )r   r   r   r   r5   "  r6   z/TestExamples.test_filterfalse.<locals>.<lambda>r.   r   r   r:   rN   rX   )rD   r   rô   r   )r   r   r   r   rõ   !  s    zTestExamples.test_filterfalsec             C   s"   |   tttdddddg¡ d S )N)r   rM   r.   )rW   r   rM   r|   rY   iè  )rD   r   r_   r!  )r   r   r   r   r#  $  s    zTestExamples.test_mapc             C   st   |   ttddtd¡ |   ttdddtd¡ |   ttddd td¡ |   ttddd dtd¡ d S )	Nrµ   r   ÚABr:   ÚCDÚCDEFGr   ÚACEG)rD   r   r#   )r   r   r   r   r/  '  s    zTestExamples.test_islicec             C   s   |   ttddddg¡ d S )Nr}   Úxy)r~   r   )r   rö   )rD   r   r	   )r   r   r   r   rø   -  s    zTestExamples.test_zipc             C   s&   |   ttddddddddg¡ d S )	Nr}   rJ  ú-)rÿ   )r~   r   )r   rö   )r   rK  )r   rK  )rD   r   r   )r   r   r   r   Útest_zip_longest0  s    zTestExamples.test_zip_longestc          	   C   sL   |   ttddtttd ¡ ¡ |   tttdddddd	d
g¡ d S )Nr}   r   z#AB AC AD BA BC BD CA CB CD DA DB DCrM   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )rD   r   r   r_   r   Úsplitr   )r   r   r   r   r²   4  s    zTestExamples.test_permutationsc             C   sT   |   ttddtttd ¡ ¡ |   tttdddddd	d
ddddg¡ d S )Nr}   rJ  zAx Ay Bx By Cx Cy Dx Dyr   rM   )r£   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )rD   r   r€   r_   r   rM  r   )r   r   r   r   r  :  s
    zTestExamples.test_productc             C   s    |   ttdddddg¡ d S )Nr.   rM   )rD   r   r£   )r   r   r   r   r  A  s    zTestExamples.test_repeatc             C   s&   |   tttdddgdddg¡ d S )N)r   rW   )rM   r   )r.   rM   r|   rY   iè  )rD   r   r%  r!  )r   r   r   r   Útest_stapmapD  s    zTestExamples.test_stapmapc          
   C   s,   |   ttdd dddddgddg¡ d S )Nc             S   s   | dk S )NrW   r   )r   r   r   r   r5   I  r6   z-TestExamples.test_takewhile.<locals>.<lambda>r   r:   rN   )rD   r   r0  )r   r   r   r   r1  H  s    zTestExamples.test_takewhileN)r    r!   r"   rf   rA  rC  ro   rq   r   r«   r¿   rÌ   rÕ   r3  rð   ró   rõ   r#  r/  rø   rL  r²   r  r  rN  r1  r   r   r   r   r@  Þ  s.   
r@  c               @   s    e Zd Zedd Zdd ZdS )ÚTestPurePythonRoughEquivalentsc       
      g   sæ   t | }|jpd|jptj|jp"d  }}}tt|||}yt|}W n0 t	k
rx   xt
t|| D ]\}}	qhW d S X y0x*t| D ]\}}	||kr|	V  t|}qW W n6 t	k
rà   x t
t|d || D ]\}}	qÐW Y nX d S )Nr   r   )ÚslicerÍ   rC   rÅ   rÂ   rÎ   ry   r   rF   r   r	   Ú	enumerate)
r(   r
   r3   rÍ   rC   rÎ   rA   Znextir   rh   r   r   r   r#   N  s"    $z%TestPurePythonRoughEquivalents.islicec          	   C   s6  |   t|  dd¡td¡ |   t|  ddd¡td¡ |   t|  ddd ¡td¡ |   t|  ddd d¡td¡ ttd	}|   t|  |d
¡ttd
¡ |   t|ttd
d	¡ ttd	}|   t|  |d
d
¡g ¡ |   t|ttd
d	¡ t }|   t|  |dd
d¡dg¡ |   t|d
¡ d S )Nrµ   r   rF  r:   rG  rH  r   rI  r.   rM   r   r)  )rD   r   r#   ry   r   rÁ   rF   )r   rA   rK   r   r   r   Útest_islice_reciped  s      z1TestPurePythonRoughEquivalents.test_islice_recipeN)r    r!   r"   Ústaticmethodr#   rR  r   r   r   r   rO  L  s   rO  c               @   sÄ   e Z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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 )0ÚTestGCc             C   s   |  |¡ t| ~~d S )N)rë   rF   )r   ÚiteratorÚ	containerr   r   r   Ú	makecyclex  s    
zTestGC.makecyclec             C   s    g }|   tdd|dg|¡ d S )Nr   r   rM   )rW  r\   )r   rI   r   r   r   rf   }  s    zTestGC.test_accumulatec             C   s   g }|   t||¡ d S )N)rW  rn   )r   rI   r   r   r   ro     s    zTestGC.test_chainc             C   s   g }|   t |g¡|¡ d S )N)rW  rn   rp   )r   rI   r   r   r   rq     s    zTestGC.test_chain_from_iterablec             C   s"   g }|   tdd|dgd|¡ d S )Nr   r   rM   )rW  r   )r   rI   r   r   r   r     s    zTestGC.test_combinationsc             C   s"   g }|   tdd|dgd|¡ d S )Nr   r   rM   )rW  r   )r   rI   r   r   r   r«     s    z)TestGC.test_combinations_with_replacementc          
   C   s&   g }|   tdddddddg|¡ d S )Nrº   r   r   )rW  rŸ   )r   rI   r   r   r   r¿     s    zTestGC.test_compressc             C   s6   g }t dtft|d}|  t|d|d|¡ d S )NÚInt)r   r   r   )rE   r]   rù   rW  rÁ   )r   rI   rX  r   r   r   rÌ     s    zTestGC.test_countc             C   s   g }|   t|gd |¡ d S )Nr   )rW  rÓ   )r   rI   r   r   r   rÕ     s    zTestGC.test_cyclec             C   s    g }|   ttd||g|¡ d S )Nr   )rW  r2  rñ   )r   rI   r   r   r   r3    s    zTestGC.test_dropwhilec             C   s$   g }|   t|gd dd |¡ d S )Nr   c             S   s   | S )Nr   )r   r   r   r   r5   €  r6   z%TestGC.test_groupby.<locals>.<lambda>)rW  r©   )r   rI   r   r   r   rð   ¢  s    zTestGC.test_groupbyc             C   sB   d}dd }x0t t||dD ]\}}|j dg ¡ |¡ qW d S )Nr.   c             S   s   | S )Nr   )r   r   r   r   r5   ©  r6   z'TestGC.test_issue2246.<locals>.<lambda>)rÙ   r   )r©   r   Ú__dict__Ú
setdefaultrë   )r   r   rê   r   r   r   r   r   Útest_issue2246Š  s    zTestGC.test_issue2246c             C   s$   g }|   tdd |gd |¡ d S )Nc             S   s   dS )NTr   )r   r   r   r   r5   ¯  r6   z$TestGC.test_filter.<locals>.<lambda>r   )rW  r·   )r   rI   r   r   r   ró   ­  s    zTestGC.test_filterc             C   s   g }|   tdd ||¡ d S )Nc             S   s   dS )NFr   )r   r   r   r   r5   ³  r6   z)TestGC.test_filterfalse.<locals>.<lambda>)rW  rô   )r   rI   r   r   r   rõ   ±  s    zTestGC.test_filterfalsec             C   s&   g }|   t|gd |gd |¡ d S )Nr   rM   )rW  r	   )r   rI   r   r   r   rø   µ  s    zTestGC.test_zipc             C   sP   g }|   t|gd |gd |¡ |d g}|   t|gd |gd |d|¡ d S )Nr   rM   )rÿ   )rW  r   )r   rI   rJ   r   r   r   rL  ¹  s    zTestGC.test_zip_longestc             C   s$   g }|   tdd |gd |¡ d S )Nc             S   s   | S )Nr   )r   r   r   r   r5   Á  r6   z!TestGC.test_map.<locals>.<lambda>r   )rW  r_   )r   rI   r   r   r   r#  ¿  s    zTestGC.test_mapc             C   s    g }|   t|gd d |¡ d S )Nr   )rW  r#   )r   rI   r   r   r   r/  Ã  s    zTestGC.test_islicec             C   s"   g }|   tdd|dgd|¡ d S )Nr   r   rM   )rW  r   )r   rI   r   r   r   r²   Ç  s    zTestGC.test_permutationsc             C   s$   g }|   tdd|dgdd|¡ d S )Nr   r   rM   )r£   )rW  r€   )r   rI   r   r   r   r  Ë  s    zTestGC.test_productc             C   s   g }|   t||¡ d S )N)rW  r£   )r   rI   r   r   r   r  Ï  s    zTestGC.test_repeatc             C   s(   g }|   tdd ||fgd |¡ d S )Nc              W   s   | S )Nr   )r¶   r   r   r   r5   Õ  r6   z%TestGC.test_starmap.<locals>.<lambda>r   )rW  r%  )r   rI   r   r   r   r&  Ó  s    zTestGC.test_starmapc             C   s"   g }|   ttdd||g|¡ d S )Nr   r   )rW  r0  rñ   )r   rI   r   r   r   r1  ×  s    zTestGC.test_takewhileN)r    r!   r"   rW  rf   ro   rq   r   r«   r¿   rÌ   rÕ   r3  rð   r[  ró   rõ   rø   rL  r#  r/  r²   r  r  r&  r1  r   r   r   r   rT  v  s.   rT  c             c   s   x| D ]
}|V  qW d S )Nr   )Úseqnr   r   r   r   ÚRÛ  s    
r]  c               @   s   e Zd Zdd Zdd ZdS )ÚGc             C   s
   || _ d S )N)r\  )r   r\  r   r   r   r	  â  s    z
G.__init__c             C   s
   | j | S )N)r\  )r   r   r   r   r   Ú__getitem__ä  s    zG.__getitem__N)r    r!   r"   r	  r_  r   r   r   r   r^  à  s   r^  c               @   s$   e Zd Zdd Zdd Zdd ZdS )ÚIc             C   s   || _ d| _d S )Nr   )r\  r   )r   r\  r   r   r   r	  é  s    z
I.__init__c             C   s   | S )Nr   )r   r   r   r   r   ì  s    z
I.__iter__c             C   s2   | j t| jkrt| j| j  }|  j d7  _ |S )Nr   )r   r   r\  r   )r   rš   r   r   r   r   î  s
     z
I.__next__N)r    r!   r"   r	  r   r   r   r   r   r   r`  ç  s   r`  c               @   s   e Zd Zdd Zdd ZdS )ÚIgc             C   s   || _ d| _d S )Nr   )r\  r   )r   r\  r   r   r   r	  ö  s    zIg.__init__c             c   s   x| j D ]
}|V  qW d S )N)r\  )r   r*  r   r   r   r   ù  s    zIg.__iter__N)r    r!   r"   r	  r   r   r   r   r   ra  ô  s   ra  c               @   s   e Zd Zdd Zdd ZdS )rþ   c             C   s   || _ d| _d S )Nr   )r\  r   )r   r\  r   r   r   r	  ÿ  s    z
X.__init__c             C   s2   | j t| jkrt| j| j  }|  j d7  _ |S )Nr   )r   r   r\  r   )r   rš   r   r   r   r     s
     z
X.__next__N)r    r!   r"   r	  r   r   r   r   r   rþ   ý  s   rþ   c               @   s   e Zd Zdd Zdd ZdS )ÚNc             C   s   || _ d| _d S )Nr   )r\  r   )r   r\  r   r   r   r	  
  s    z
N.__init__c             C   s   | S )Nr   )r   r   r   r   r     s    z
N.__iter__N)r    r!   r"   r	  r   r   r   r   r   rb    s   rb  c               @   s$   e Zd Zdd Zdd Zdd ZdS )ÚEc             C   s   || _ d| _d S )Nr   )r\  r   )r   r\  r   r   r   r	    s    z
E.__init__c             C   s   | S )Nr   )r   r   r   r   r     s    z
E.__iter__c             C   s   dd  d S )NrM   r   r   )r   r   r   r   r     s    z
E.__next__N)r    r!   r"   r	  r   r   r   r   r   r   rc    s   rc  c               @   s$   e Zd Zdd Zdd Zdd ZdS )ÚSc             C   s   d S )Nr   )r   r\  r   r   r   r	    s    z
S.__init__c             C   s   | S )Nr   )r   r   r   r   r     s    z
S.__iter__c             C   s   t d S )N)r   )r   r   r   r   r      s    z
S.__next__N)r    r!   r"   r	  r   r   r   r   r   r   rd    s   rd  c             C   s   t tdd ttt| S )Nc             S   s   | S )Nr   )r   r   r   r   r5   %  r6   zL.<locals>.<lambda>)rn   r_   r]  ra  r^  )r\  r   r   r   ÚL#  s    re  c               @   s   e Z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d Zdd Zdd  Zd!S )"ÚTestVariousIteratorArgsc             C   sª   dddddg}dddddg}t |}x.tttttfD ]}|  tt|||¡ q4W |  ttt	|g ¡ |  
ttt|¡ |  
ttt|¡ |  
tttt|¡ d S )	Nr   r   rM   r:   rW   rN   r.   rO   )r   r^  r`  ra  re  r]  rD   r   r\   rd  r`   r@   rþ   rb  ÚZeroDivisionErrorrc  )r   r3   r+   r   rß   r   r   r   rf   *  s    z'TestVariousIteratorArgs.test_accumulatec          	   C   sÖ   xÐddt ddt dddfD ]²}xjttttttfD ]V}|  tt	||t||¡ |  tt	||||t||t|| ¡ q2W |  
ttt	t|¡ |  
ttt	t|¡ |  
ttt	t|¡ qW d S )NÚ123rl   iè  )Údog333333ó?iÐ  i  rW   )r   r^  r`  ra  rd  re  r]  rD   r   rn   r`   r@   rþ   rb  rg  rc  )r   r3   rß   r   r   r   ro   5  s      6z"TestVariousIteratorArgs.test_chainc          	   C   sŒ   x¶ddt ddt dddfD ]}t|}x>ttttttfD ]*}|  t	t
||tdt	||¡ q:W |  tt
t|td¡ |  tt
t|td¡ |  tt	t
t|td¡ qW d S )	Nrh  rl   iè  )ri  g333333ó?iÐ  i  rW   r   )r   r   r^  r`  ra  rd  re  r]  rD   r   rŸ   r£   r`   r@   rþ   rb  rg  rc  )r   r3   r   rß   r   r   r   r¿   >  s     *z%TestVariousIteratorArgs.test_compressc             C   s^   xXddt ddt dddfD ]:}|  ttt|¡ |  ttt|¡ |  ttt|¡ qW d S )Nrh  rl   iè  )ri  g333333ó?iÐ  i  rW   )r   r`   r@   r€   rþ   rb  rg  rc  )r   r3   r   r   r   r  G  s     z$TestVariousIteratorArgs.test_productc             C   sº   xŽddt ddt dddfD ]}xVttttttfD ]B}t|d }t||d }tt	t
|||}|  ||¡ q2W |  tt
t|¡ |  tt
t|¡ |  ttt
t|¡ qW d S )	Nrh  rl   iè  )ri  g333333ó?iÐ  i  rW   rM   )r   r^  r`  ra  rd  re  r]  r   r   r#   rÓ   rD   r`   r@   rþ   rb  rg  rc  )r   r3   rß   ZtgtlenZexpectedÚactualr   r   r   rÕ   M  s     z"TestVariousIteratorArgs.test_cyclec             C   sª   x€t dt dt ddt dddfD ]~}x>ttttttfD ]*}|  dd	 t||D t	||¡ q:W |  
ttt|¡ |  
ttt|¡ |  
tt	tt|¡ q$W d S )
Nr.   r   iè  )rV   rÚ   iÐ  i  rW   c             S   s   g | ]\}}|qS r   r   )r7   r§   Zsbr   r   r   r8   [  s    z8TestVariousIteratorArgs.test_groupby.<locals>.<listcomp>)r   r^  r`  ra  rd  re  r]  rD   r©   r   r`   r@   rþ   rb  rg  rc  )r   r3   rß   r   r   r   rð   X  s    (*z$TestVariousIteratorArgs.test_groupbyc          	   C   s²   x¬t dt dt ddt dddfD ]}x@ttttttfD ],}|  tt	t
||dd	 ||D ¡ q:W |  tt	t
t|¡ |  tt	t
t|¡ |  ttt	t
t|¡ q$W d S )
Nr.   r   iè  )rV   rÚ   iÐ  i  rW   c             S   s   g | ]}t |r|qS r   )r   )r7   r   r   r   r   r8   d  s    z7TestVariousIteratorArgs.test_filter.<locals>.<listcomp>)r   r^  r`  ra  rd  re  r]  rD   r   r·   r   r`   r@   rþ   rb  rg  rc  )r   r3   rß   r   r   r   ró   `  s    (z#TestVariousIteratorArgs.test_filterc          	   C   s²   x¬t dt dt ddt dddfD ]}x@ttttttfD ],}|  tt	t
||dd	 ||D ¡ q:W |  tt	t
t|¡ |  tt	t
t|¡ |  ttt	t
t|¡ q$W d S )
Nr.   r   iè  )rV   rÚ   iÐ  i  rW   c             S   s   g | ]}t |r|qS r   )r   )r7   r   r   r   r   r8   m  s    z<TestVariousIteratorArgs.test_filterfalse.<locals>.<listcomp>)r   r^  r`  ra  rd  re  r]  rD   r   rô   r   r`   r@   rþ   rb  rg  rc  )r   r3   rß   r   r   r   rõ   i  s    (z(TestVariousIteratorArgs.test_filterfalsec          	   C   sÈ   xÂddt ddt dddfD ]€}xdttttttfD ]P}|  tt	||t
||¡ |  tt	||||t
||||¡ q2W |  tt	t|¡ |  tt	t|¡ |  ttt	t|¡ qW d S )Nrh  rl   iè  )ri  g333333ó?iÐ  i  rW   )r   r^  r`  ra  rd  re  r]  rD   r   r	   r   r`   r@   rþ   rb  rg  rc  )r   r3   rß   r   r   r   rø   r  s      0z TestVariousIteratorArgs.test_zipc          
   C   sÐ   xÊddt ddt dddfD ]¬}xlttttttfD ]X}|  tt	||tt
||¡ |  tt	||||tt
||||¡ q2W |  tt	t|¡ |  tt	t|¡ |  ttt	t|¡ qW d S )Nrh  rl   iè  )ri  g333333ó?iÐ  i  rW   )r   r^  r`  ra  rd  re  r]  rD   r   r   r	   r`   r@   rþ   rb  rg  rc  )r   r3   rß   r   r   r   r  {  s     $4z'TestVariousIteratorArgs.test_ziplongestc          
   C   sâ   xÜt dt dt ddt dddfD ]¶}xpttttttfD ]\}|  tt	t
||dd	 ||D ¡ |  tt	tj||||d
d	 ||D ¡ q:W |  tt	t
t|¡ |  tt	t
t|¡ |  ttt	t
t|¡ q$W d S )Nr.   r   rÐ   )rV   rÚ   rÏ   r)  rW   c             S   s   g | ]}t |qS r   )r   )r7   r   r   r   r   r8     s    z4TestVariousIteratorArgs.test_map.<locals>.<listcomp>c             S   s   g | ]}|| qS r   r   )r7   r   r   r   r   r8     s    )r   r^  r`  ra  rd  re  r]  rD   r   r_   r   r&   r!  r`   r@   rþ   rb  rg  rc  )r   r3   rß   r   r   r   r#    s    (z TestVariousIteratorArgs.test_mapc          
   C   s²   x¬ddt ddt dddfD ]}xHttttttfD ]4}|  tt	||dd d	t||dd d	 ¡ q2W |  
tt	t|d
¡ |  
tt	t|d
¡ |  
ttt	t|d
¡ qW d S )NZ12345rl   iè  )ri  g333333ó?iÐ  i  rW   r   r   r.   )r   r^  r`  ra  rd  re  r]  rD   r   r#   r`   r@   rþ   rb  rg  rc  )r   r3   rß   r   r   r   r/    s     4z#TestVariousIteratorArgs.test_islicec          	   C   sÄ   xŸt dt dt ddt dddfD ]}xLttttttfD ]8}t||}|  t	t
tj||dd	 ||D ¡ q:W |  tt
tjt|¡ |  tt
tjt|¡ |  tt	t
tjt|¡ q$W d S )
Nr.   r   rÐ   )rV   rÚ   rÏ   r)  rW   c             S   s   g | ]}|| qS r   r   )r7   r   r   r   r   r8     s    z8TestVariousIteratorArgs.test_starmap.<locals>.<listcomp>)r   r^  r`  ra  rd  re  r]  r   rD   r   r%  r&   r!  r`   r@   rþ   rb  rg  rc  )r   r3   rß   Ússr   r   r   r&    s    (
z$TestVariousIteratorArgs.test_starmapc          	   C   sÎ   xÈt dt dt ddt dddfD ]¢}x\ttttttfD ]H}g }x$||D ]}t|sZP | |¡ qLW |  	t
tt|||¡ q:W |  tttt|¡ |  tttt|¡ |  tt
ttt|¡ q$W d S )Nr.   r   iè  )rV   rÚ   iÐ  i  rW   )r   r^  r`  ra  rd  re  r]  r   rë   rD   r   r0  r`   r@   rþ   rb  rg  rc  )r   r3   rß   Útgtrî   r   r   r   r1  ¡  s    ( z&TestVariousIteratorArgs.test_takewhilec          	   C   sÒ   xÌt dt dt ddt dddfD ]Š}x`ttttttfD ]L}g }x(||D ]}|s^t|r^qL| |¡ qLW |  	t
tt|||¡ q:W |  tttt|¡ |  tttt|¡ |  tt
ttt|¡ q$W d S )Nr.   r   iè  )rV   rÚ   iÐ  i  rW   )r   r^  r`  ra  rd  re  r]  r   rë   rD   r   r2  r`   r@   rþ   rb  rg  rc  )r   r3   rß   rl  rî   r   r   r   r3  ­  s    ( z&TestVariousIteratorArgs.test_dropwhilec             C   sÀ   xºddt ddt dddfD ]}xXttttttfD ]D}t||\}}|  t	|t	||¡ |  t	|t	||¡ q2W |  
ttt|¡ |  
ttt|¡ |  
tt	tt|d ¡ qW d S )	Nrh  rl   iè  )ri  g333333ó?iÐ  i  rW   r   )r   r^  r`  ra  rd  re  r]  r7  rD   r   r`   r@   rþ   rb  rg  rc  )r   r3   rß   Zit1Zit2r   r   r   r<  ¹  s     z TestVariousIteratorArgs.test_teeN)r    r!   r"   rf   ro   r¿   r  rÕ   rð   ró   rõ   rø   r  r#  r/  r&  r1  r3  r<  r   r   r   r   rf  (  s    						
rf  c               @   s   e Zd Zdd Zdd ZdS )ÚLengthTransparencyc             C   sL   |   t td d¡d¡ |   t td d¡d¡ |   t td d¡d¡ d S )Nr)  r   r   )rD   r&   Úlength_hintr£   )r   r   r   r   r  Å  s    zLengthTransparency.test_repeatc             C   sh   |   t td d¡d¡ |   t td d¡d¡ |   t td dd¡d¡ |   t td dd¡d¡ d S )Nr®   r   r{   )r  )rD   r&   rn  r£   )r   r   r   r   r   Ê  s    z2LengthTransparency.test_repeat_with_negative_timesN)r    r!   r"   r  r   r   r   r   r   rm  Ã  s   rm  c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )ÚRegressionTestsc                s<   dd } fdd}g  |d|d t t}|   |¡ d S )Nc                sT   dgf fdd	}t |}t | |dd< t||}t|gt|   t d S )Nr   c                s   |r|d d =  t  | S )N)rF   )rË   Úfirst)rw   Úzr   r   rß   Ù  s    
z@RegressionTests.test_sf_793826.<locals>.mutatingtuple.<locals>.g)r   r_   r	   r   rF   )Ztuple1rw   Ztuple2rß   ÚitemsÚgenr   )rw   rq  r   ÚmutatingtupleÕ  s    
z5RegressionTests.test_sf_793826.<locals>.mutatingtuplec                s   | a tt  d d < d S )N)ÚTr   )r¶   )rp  r   r   rw   ä  s    z)RegressionTests.test_sf_793826.<locals>.f)r   r   rM   )r:   rW   rN   )r   ru  rD   )r   rt  rw   Úsecondr   )rp  r   Útest_sf_793826Ò  s    zRegressionTests.test_sf_793826c                s     fdd} fdd}g  |   ttt| |d¡ |   ddg¡ g  |   ttt| |d¡ |   ddg¡ g  |   ttt| ¡ |   ddg¡ d S )	Nc               3   s,      d¡ dV     d¡ t   d¡ d S )Nr   r   r   )rë   ÚAssertionErrorr   )Úhistr   r   Úgen1ó  s
    

z,RegressionTests.test_sf_950057.<locals>.gen1c             3   s      d¡ dV     d¡ d S )NrM   r   r:   )rë   )r   )ry  r   r   Úgen2ú  s    
z,RegressionTests.test_sf_950057.<locals>.gen2Fr   r   T)r`   rx  r   rn   rD   rÓ   )r   rz  r{  r   )ry  r   Útest_sf_950057ï  s    zRegressionTests.test_sf_950057c          	   C   s:   t  dd tdD ¡}|  t¡ t| W d Q R X d S )Nc             s   s   | ]
}d V  qdS )r   Nr   )r7   Zunusedr   r   r   r     s    zERegressionTests.test_long_chain_of_empty_iterables.<locals>.<genexpr>i )rn   rp   r   r`   r   rF   )r   rA   r   r   r   Ú"test_long_chain_of_empty_iterables  s    z2RegressionTests.test_long_chain_of_empty_iterablesc                s4    fdd}x"t td|D ]\} t  qW d S )Nc                s   | dkrt   | dkS )NrW   rN   )r   )r   )rJ   r   r   rw     s    z,RegressionTests.test_issue30347_1.<locals>.fr.   )r©   r   r   )r   rw   r§   r   )rJ   r   Útest_issue30347_1  s    z!RegressionTests.test_issue30347_1c                sN   G  fddd}dt ttd|d  xtdD ]}t  d  q8W d S )Nc                   s"   e Zd Zdd Z fddZdS )z,RegressionTests.test_issue30347_2.<locals>.Kc             S   s   d S )Nr   )r   rš   r   r   r   r	    s    z5RegressionTests.test_issue30347_2.<locals>.K.__init__c                s   d7 dkrt  d  dS )Nr   T)rF   )r   Úother)rß   r   r   r   ræ     s    
z3RegressionTests.test_issue30347_2.<locals>.K.__eq__N)r    r!   r"   r	  ræ   r   )rß   r   r   r   ÚK  s   r  r   r.   r   r   )rF   r©   r   )r   r  r   r   )rß   r   r   Útest_issue30347_2  s
    	z!RegressionTests.test_issue30347_2N)r    r!   r"   rw  r|  r}  r~  r  r   r   r   r   ro  Ð  s
   ro  c               @   s   e Zd Zdd ZdS )ÚSubclassWithKwargsTestc                s   x|t ttttttttt	t
tfD ]\ G  fddd }y|dd W q tk
rx } z|  d|jd ¡ W d d }~X Y qX qW d S )Nc                   s   e Zd Zd fdd	ZdS )zBSubclassWithKwargsTest.test_keywords_in_subclass.<locals>.SubclassNc                s    j | f|  d S )N)r	  )r   Únewargr
   )Úclsr   r   r	  1  s    zKSubclassWithKwargsTest.test_keywords_in_subclass.<locals>.Subclass.__init__)N)r    r!   r"   r	  r   )r  r   r   ÚSubclass0  s   r  r   )r  zkeyword argumentsr   )r£   r	   r·   rô   rn   r_   r%  r#   r0  r2  rÓ   rŸ   r@   ZassertNotInr
   )r   r  Úerrr   )r  r   Útest_keywords_in_subclass,  s    z0SubclassWithKwargsTest.test_keywords_in_subclassN)r    r!   r"   r  r   r   r   r   r  +  s   r  c               @   s:   e Zd Zdd ZejZdd Zdd Zdd Zd	d
 Z	dS )Ú
SizeofTestc             C   s   t  d¡| _d S )Nr   )ÚstructÚcalcsizeÚssize_t)r   r   r   r   ÚsetUp;  s    zSizeofTest.setUpc             C   sF   t  d¡}| j}|tdd|d| j   |td |d| j   d S )NZ3PirU   Z12r   )
rT   rT   rT   rT   rT   rT   rT   rT   rT   rT   r.   )r   ÚcalcobjsizeÚcheck_sizeofr€   r  )r   ÚbasesizeÚcheckr   r   r   Útest_product_sizeof@  s    
zSizeofTest.test_product_sizeofc             C   sL   t  d¡}| j}|tdd|d| j   |ttdd|d| j   d S )NÚ3Pnirm   rM   r.   r:   )r   r  r  r   r  r   )r   r  r  r   r   r   Útest_combinations_sizeofF  s    
z#SizeofTest.test_combinations_sizeofc             C   sP   t }t d¡}| j}||dd|d| j   ||tdd|d| j   d S )Nr  rm   rM   r.   r:   )r   r   r  r  r  r   )r   rª   r  r  r   r   r   Ú)test_combinations_with_replacement_sizeofL  s
    
z4SizeofTest.test_combinations_with_replacement_sizeofc             C   sŠ   t  d¡}| j}|td|d| j  d| j   |tdd|d| j  d| j   |tdd|d| j  d| j   |ttdd|d| j  d| j   d S )NZ4Pnirm   r:   rM   r   rW   r.   )r   r  r  r   r  r   )r   r  r  r   r   r   Útest_permutations_sizeofS  s    


z#SizeofTest.test_permutations_sizeofN)
r    r!   r"   r  r   r  r  r  r  r  r   r   r   r   r  9  s   r  a"   Doctest for examples in the library reference: libitertools.tex


>>> amounts = [120.15, 764.05, 823.14]
>>> for checknum, amount in zip(count(1200), amounts):
...     print('Check %d is for $%.2f' % (checknum, amount))
...
Check 1200 is for $120.15
Check 1201 is for $764.05
Check 1202 is for $823.14

>>> import operator
>>> for cube in map(operator.pow, range(1,4), repeat(3)):
...    print(cube)
...
1
8
27

>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
>>> for name in islice(reportlines, 3, None, 2):
...    print(name.title())
...
Alex
Laura
Martin
Walter
Samuele

>>> from operator import itemgetter
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
>>> di = sorted(sorted(d.items()), key=itemgetter(1))
>>> for k, g in groupby(di, itemgetter(1)):
...     print(k, list(map(itemgetter(0), g)))
...
1 ['a', 'c', 'e']
2 ['b', 'd', 'f']
3 ['g']

# Find runs of consecutive numbers using groupby.  The key to the solution
# is differencing with a range so that consecutive numbers all appear in
# same group.
>>> data = [ 1,  4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
>>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]):
...     print(list(map(operator.itemgetter(1), g)))
...
[1]
[4, 5, 6]
[10]
[15, 16, 17, 18]
[22]
[25, 26, 27, 28]

>>> def take(n, iterable):
...     "Return first n items of the iterable as a list"
...     return list(islice(iterable, n))

>>> def prepend(value, iterator):
...     "Prepend a single value in front of an iterator"
...     # prepend(1, [2, 3, 4]) -> 1 2 3 4
...     return chain([value], iterator)

>>> def enumerate(iterable, start=0):
...     return zip(count(start), iterable)

>>> def tabulate(function, start=0):
...     "Return function(0), function(1), ..."
...     return map(function, count(start))

>>> import collections
>>> def consume(iterator, n=None):
...     "Advance the iterator n-steps ahead. If n is None, consume entirely."
...     # Use functions that consume iterators at C speed.
...     if n is None:
...         # feed the entire iterator into a zero-length deque
...         collections.deque(iterator, maxlen=0)
...     else:
...         # advance to the empty slice starting at position n
...         next(islice(iterator, n, n), None)

>>> def nth(iterable, n, default=None):
...     "Returns the nth item or a default value"
...     return next(islice(iterable, n, None), default)

>>> def all_equal(iterable):
...     "Returns True if all the elements are equal to each other"
...     g = groupby(iterable)
...     return next(g, True) and not next(g, False)

>>> def quantify(iterable, pred=bool):
...     "Count how many times the predicate is true"
...     return sum(map(pred, iterable))

>>> def padnone(iterable):
...     "Returns the sequence elements and then returns None indefinitely"
...     return chain(iterable, repeat(None))

>>> def ncycles(iterable, n):
...     "Returns the sequence elements n times"
...     return chain(*repeat(iterable, n))

>>> def dotproduct(vec1, vec2):
...     return sum(map(operator.mul, vec1, vec2))

>>> def flatten(listOfLists):
...     return list(chain.from_iterable(listOfLists))

>>> def repeatfunc(func, times=None, *args):
...     "Repeat calls to func with specified arguments."
...     "   Example:  repeatfunc(random.random)"
...     if times is None:
...         return starmap(func, repeat(args))
...     else:
...         return starmap(func, repeat(args, times))

>>> def pairwise(iterable):
...     "s -> (s0,s1), (s1,s2), (s2, s3), ..."
...     a, b = tee(iterable)
...     try:
...         next(b)
...     except StopIteration:
...         pass
...     return zip(a, b)

>>> def grouper(n, iterable, fillvalue=None):
...     "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
...     args = [iter(iterable)] * n
...     return zip_longest(*args, fillvalue=fillvalue)

>>> def roundrobin(*iterables):
...     "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
...     # Recipe credited to George Sakkis
...     pending = len(iterables)
...     nexts = cycle(iter(it).__next__ for it in iterables)
...     while pending:
...         try:
...             for next in nexts:
...                 yield next()
...         except StopIteration:
...             pending -= 1
...             nexts = cycle(islice(nexts, pending))

>>> def powerset(iterable):
...     "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
...     s = list(iterable)
...     return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

>>> def unique_everseen(iterable, key=None):
...     "List unique elements, preserving order. Remember all elements ever seen."
...     # unique_everseen('AAAABBBCCDAABBB') --> A B C D
...     # unique_everseen('ABBCcAD', str.lower) --> A B C D
...     seen = set()
...     seen_add = seen.add
...     if key is None:
...         for element in iterable:
...             if element not in seen:
...                 seen_add(element)
...                 yield element
...     else:
...         for element in iterable:
...             k = key(element)
...             if k not in seen:
...                 seen_add(k)
...                 yield element

>>> def unique_justseen(iterable, key=None):
...     "List unique elements, preserving order. Remember only the element just seen."
...     # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
...     # unique_justseen('ABBCcAD', str.lower) --> A B C A D
...     return map(next, map(itemgetter(1), groupby(iterable, key)))

>>> def first_true(iterable, default=False, pred=None):
...     '''Returns the first true value in the iterable.
...
...     If no true value is found, returns *default*
...
...     If *pred* is not None, returns the first item
...     for which pred(item) is true.
...
...     '''
...     # first_true([a,b,c], x) --> a or b or c or x
...     # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
...     return next(filter(pred, iterable), default)

>>> def nth_combination(iterable, r, index):
...     'Equivalent to list(combinations(iterable, r))[index]'
...     pool = tuple(iterable)
...     n = len(pool)
...     if r < 0 or r > n:
...         raise ValueError
...     c = 1
...     k = min(r, n-r)
...     for i in range(1, k+1):
...         c = c * (n - k + i) // i
...     if index < 0:
...         index += c
...     if index < 0 or index >= c:
...         raise IndexError
...     result = []
...     while r:
...         c, n, r = c*r//n, n-1, r-1
...         while index >= c:
...             index -= c
...             c, n = c*(n-r)//n, n-1
...         result.append(pool[-1-n])
...     return tuple(result)


This is not part of the examples but it tests to make sure the definitions
perform as purported.

>>> take(10, count())
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> list(prepend(1, [2, 3, 4]))
[1, 2, 3, 4]

>>> list(enumerate('abc'))
[(0, 'a'), (1, 'b'), (2, 'c')]

>>> list(islice(tabulate(lambda x: 2*x), 4))
[0, 2, 4, 6]

>>> it = iter(range(10))
>>> consume(it, 3)
>>> next(it)
3
>>> consume(it)
>>> next(it, 'Done')
'Done'

>>> nth('abcde', 3)
'd'

>>> nth('abcde', 9) is None
True

>>> [all_equal(s) for s in ('', 'A', 'AAAA', 'AAAB', 'AAABA')]
[True, True, True, False, False]

>>> quantify(range(99), lambda x: x%2==0)
50

>>> a = [[1, 2, 3], [4, 5, 6]]
>>> flatten(a)
[1, 2, 3, 4, 5, 6]

>>> list(repeatfunc(pow, 5, 2, 3))
[8, 8, 8, 8, 8]

>>> import random
>>> take(5, map(int, repeatfunc(random.random)))
[0, 0, 0, 0, 0]

>>> list(pairwise('abcd'))
[('a', 'b'), ('b', 'c'), ('c', 'd')]

>>> list(pairwise([]))
[]

>>> list(pairwise('a'))
[]

>>> list(islice(padnone('abc'), 0, 6))
['a', 'b', 'c', None, None, None]

>>> list(ncycles('abc', 3))
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']

>>> dotproduct([1,2,3], [4,5,6])
32

>>> list(grouper(3, 'abcdefg', 'x'))
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]

>>> list(roundrobin('abc', 'd', 'ef'))
['a', 'd', 'e', 'b', 'f', 'c']

>>> list(powerset([1,2,3]))
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]

>>> all(len(list(powerset(range(n)))) == 2**n for n in range(18))
True

>>> list(powerset('abcde')) == sorted(sorted(set(powerset('abcde'))), key=len)
True

>>> list(unique_everseen('AAAABBBCCDAABBB'))
['A', 'B', 'C', 'D']

>>> list(unique_everseen('ABBCcAD', str.lower))
['A', 'B', 'C', 'D']

>>> list(unique_justseen('AAAABBBCCDAABBB'))
['A', 'B', 'C', 'D', 'A', 'B']

>>> list(unique_justseen('ABBCcAD', str.lower))
['A', 'B', 'C', 'A', 'D']

>>> first_true('ABC0DEF1', '9', str.isdigit)
'0'

>>> population = 'ABCDEFGH'
>>> for r in range(len(population) + 1):
...     seq = list(combinations(population, r))
...     for i in range(len(seq)):
...         assert nth_combination(population, r, i) == seq[i]
...     for i in range(-len(seq), 0):
...         assert nth_combination(population, r, i) == seq[i]


Ú
libreftestc          	   C   s   t ttttttttf	}t	j
|  | r|ttdr|dd l}d gd }x2tt|D ]"}t	j
|  | ¡  t ¡ ||< qNW t| t	 tjt | ¡ d S )NÚgettotalrefcountr   rW   )r9   rf  rT  ro  rm  r  r@  rO  r  r   Zrun_unittestÚhasattrrÅ   Úgcr   r   Zcollectr  r  Zrun_doctestÚmodulesr    )ÚverboseZtest_classesr  Zcountsr   r   r   r   Ú	test_main	  s    


r  Ú__main__T)r  )N)>ZunittestZtestr   Ú	itertoolsr-  Údecimalr   Z	fractionsr   r&   r  rr   r0   Ú	functoolsr   rÅ   r  ZMAX_Py_ssize_trÂ   Zminsizer   r   r   r   r   r   r   r   r   r%   r)   r*   r,   r-   r/   r   rd   rt   ZTestCaser9   r@  rO  rT  r]  r^  r`  ra  rþ   rb  rc  rd  re  rf  rm  ro  r  Zcpython_onlyr  r  Z__test__r  r    r   r   r   r   Ú<module>   s   
           n*e	
	 [
  _

