o
    HXh                    @   sz  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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Z0G d8d9 d9Z1G d:d; d;Z2G d<d= d=Z3G d>d? d?Z4d@dA Z5G dBdC dCe j(Z6G dDdE dEe j(Z7G dFdG dGe j(Z8G dHdI dIe j(Z9ej:G dJdK dKe j(Z;dLZ<dMe<iZ=dSdNdOZ>e?dPkr;e>dQdR dS dS )T    N)support)*)Decimal)Fraction)reduce   c                  G   s   t t|  S N)listzipargs r   =/opt/python-3.10.19/usr/lib/python3.10/test/test_itertools.pylzip      r   c                 C   s
   	 d|  S N   r   xr   r   r   onearg   s   r   c                  G   s   	 t r   )
ValueErrorr   r   r   r   errfunc   s   r   c                  c   s    	 dD ]} | V  qd S )Nr   r   r   r   ir   r   r   gen3   
   r   c                 C      	 | d dkS )Nr   r   r   r   r   r   r   isEven$      r   c                 C   r   )Nr   r   r   r   r   r   r   isOdd(   r   r    c                  G      | S r   r   r   r   r   r   tupleize,      r"   c                 c   s    t | D ]}|V  qd S r   rangenr   r   r   r   irange/   s   r(   c                   @      e Zd Z	 dd Zdd ZdS )StopNowc                 C   r!   r   r   selfr   r   r   __iter__5   r#   zStopNow.__iter__c                 C      t r   StopIterationr+   r   r   r   __next__7   r#   zStopNow.__next__N__name__
__module____qualname__r-   r1   r   r   r   r   r*   3       r*   c                 C   s   	 t t|| S r   r	   islice)r'   seqr   r   r   take:   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 r;   )r@   r%   r'   r   r   r   factA   s   rB   c                 C      | d S Nr   r   rr   r   r   testRF      rG   c                 C   rC   r   r   rE   r   r   r   testR2I   rH   rI   c                 C      | dk S )N
   r   r   r   r   r   undertenL   rH   rL   c                 C   s   g | ]}|fd dqS )c                 S   s   t t | |S r   )pickleloadsdumps)sprotor   r   r   <lambda>O   s    z<listcomp>.<lambda>r   ).0rQ   r   r   r   
<listcomp>O   s    rT   c                   @   s@  e Zd Zdud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d7d8 Zd9d: Z e
dd;d< Z!d=d> Z"e
dd?d@ Z#dAdB Z$dCdD Z%dEdF Z&dGdH Z'dIdJ Z(e
jdKdL Z)e
ddMdN Z*dOdP Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1d]d^ Z2d_d` Z3dadb Z4dcdd Z5dedf Z6dgdh Z7didj Z8e
j9dkdl Z:e
j9dmdn Z;e
j9dodp Z<e
j9dqdr Z=e
j9dsdt Z>dS )vTestBasicOps   r   Nc                    s  	 d fdd	 t ||}t |}| t|t|  | |}}	| ||	 |r9 |}
| ||
 t |}d}zt|D ]
}t| |d7 }qEW n	 tyZ   Y nw t ||}t |} | |}}	| ||	 |r ||d  }
| ||
 d S d S )Nr   c                    s\    dkrt dt| tr| S z	tt| }W n ty#   |  Y S w  fdd|D S )NrK   zinfinite recursion encounteredc                    s   g | ]	} |d  qS )r   r   rS   e)expandr   r   r   rT   `   s    z;TestBasicOps.pickletest.<locals>.expand.<locals>.<listcomp>)RuntimeError
isinstancestrr	   r8   	TypeError)itr   lrY   stopr   r   rY   V   s   
z'TestBasicOps.pickletest.<locals>.expandr   r   )rM   rO   rN   assertEqualtyper%   nextr0   )r,   protocolr^   ra   r:   comparedumpZi2abcZi3Ztookr   Zi4r   r`   r   
pickletestT   s8   



zTestBasicOps.pickletestc              	   C   sv  |  tttdg d |  tttddg d ttttfD ]}|  ttt|tdtt|g d q#|  ttdg d |  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 g d}|  tt|tg d |  tt|tg d |  tt|tjg d | 	t
 tt|t W d    n1 sw   Y  ttjd
 D ]}| |ttd | |ttddd q|  ttg dd dg d |  ttg dddg d |  ttg dddg | 	t
 ttddgd W d    d S 1 s4w   Y  d S )NrK   )
r   r         rK            $   -   r>   abc)ri   abrt         rn   r   r   )
r      	   rw   rv   r   rm   rV   r   rn   )
r   r   r   r   r   r   r   r   r   r   )
r   rx   ry   ry   ry   ry   ry   ry   ry   ry   )
r         i  i  r   r   r   r   r   )initial)rK   rw   r   )rK   ro   rz   d   )r}   n   s   t      )rc   r	   
accumulater%   intcomplexr   r   mapassertRaisesr]   minmaxr<   r=   chrrM   HIGHEST_PROTOCOLrl   )r,   typrP   rQ   r   r   r   test_accumulate}   sP     $zTestBasicOps.test_accumulatec                 C   s   dd }t |fD ]=}| t|ddtd | t|dtd | t|dg  | td|ddtd | tt|d	d
 qd S )Nc                  w   s"    	 | D ]
}|D ]}|V  qqd S r   r   )	iterablesr^   elementr   r   r   chain2   s   z'TestBasicOps.test_chain.<locals>.chain2rt   defabcdef rV   abcdr   rm   )chainrc   r	   r:   r   r]   )r,   r   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   r   r   r   rV   r   r   rm   )rc   r	   r   from_iterabler:   r   r]   r+   r   r   r   test_chain_from_iterable   s
    z%TestBasicOps.test_chain_from_iterablec              	   C   s   t jgt D ]O}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t
tjd D ]}| j|tddtdd q]d S )Nrt   r   r   ri   Zbcdefr   rV   r   r   rm   r   rg   )copydeepcopypicklecopiersr   rc   r	   re   r:   r   r]   r%   rM   r   rl   )r,   Zoperr^   rQ   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|g d t }|tddgtdgf | t|g d d S )Nr   r   rt   r   )ri   rj   rk   drX   fghi)r   ri   rj   rk   r   rX   r   )r   r]   r   __setstate__iterrc   r	   r,   r^   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 dd gt D ][}| t|tddg  | t|tddg d	 tdd}t| | t||g d
 | t|ttddg d ttdd}t| | t||g d q&dd }dd }dd }tdD ]}dd t|D t|d D ]}tt|}| t	|||krdnt
|t
| t
||   | t	|t	t| | |t| |D ]; | t	 | | t	t | | t t  | tfdd D  | t  fddD  q| |t|| | |t|| | |t|| ttjd D ]}	| |	t| q@qqd S )Nrt   r   r   c                 S   r!   r   r   ri   r   r   r   rR          z0TestBasicOps.test_combinations.<locals>.<lambda>    ABCDABr   Cr   Dr   r   r   r   r   r   )r   r   r   r   r   rV   rm   r   r   r   rm   r   r   rm   r   r   rm   )r   r   r   c                 3   s    	 t |  t }||krd S tt|}t  fdd|D V  	 tt|D ]}|| || | kr7 nq)d S ||  d7  < t|d |D ]}||d  d ||< qIt  fdd|D V  q#)Nc                 3       | ]} | V  qd S r   r   rS   r   poolr   r   	<genexpr>       zHTestBasicOps.test_combinations.<locals>.combinations1.<locals>.<genexpr>r   c                 3   r   r   r   r   r   r   r   r      r   tuplelenr	   r%   reversed)r?   rF   r'   indicesr   jr   r   r   combinations1   s&   z5TestBasicOps.test_combinations.<locals>.combinations1c                 3   sT    	 t |  t }tt||D ]}t|t|kr't  fdd|D V  qd S )Nc                 3   r   r   r   r   r   r   r   r     r   zHTestBasicOps.test_combinations.<locals>.combinations2.<locals>.<genexpr>)r   r   permutationsr%   sortedr	   r?   rF   r'   r   r   r   r   combinations2      z5TestBasicOps.test_combinations.<locals>.combinations2c                 3   sT    	 t |  t }tt||D ]}tt||kr't  fdd|D V  qd S )Nc                 3   r   r   r   r   r   r   r   r   	  r   zHTestBasicOps.test_combinations.<locals>.combinations3.<locals>.<genexpr>)r   r   combinations_with_replacementr%   setr   r   r   r   combinations3  r   z5TestBasicOps.test_combinations.<locals>.combinations3rv   c                 S      g | ]}d | d qS rw      r   rS   r   r   r   r   rT         z2TestBasicOps.test_combinations.<locals>.<listcomp>r   c                 3       | ]}| v V  qd S r   r   rW   valuesr   r   r     r   z1TestBasicOps.test_combinations.<locals>.<genexpr>c                       g | ]}| v r|qS r   r   rW   rk   r   r   rT     r   )r   r]   combinationsr   r   rc   r	   re   r%   r   rB   r   r   
assertTrueallrM   r   rl   )
r,   optestIntermediater   r   r   r'   rF   resultrQ   r   rk   r   r   test_combinations   s`   
4
zTestBasicOps.test_combinationsc                 C   >   |  ttf tdd W d    d S 1 sw   Y  d S )NAAi    )r   OverflowErrorMemoryErrorr   r+   r   r   r   test_combinations_overflow      "z'TestBasicOps.test_combinations_overflowz"tuple reuse is specific to CPythonc              
   C   H   |  tttttddd | ttttttddd d S Nabcderm   r   )rc   r   r   r   idr   assertNotEqualr	   r+   r   r   r   test_combinations_tuple_reuse&      (z*TestBasicOps.test_combinations_tuple_reusec              	      s\  t }| t|d | t|ddd | t|d  | t|dd dd gt D ]&}| t||ddg d |dd}t| | t||g d	 q(d
d }dd }dd }tdD ]}dd t|D t|d D ]}t||}	| t	|	||| | t	|	t	t
|	 | |	t|	 tt|}
|dks|dkr| |	|
 n| t
|	t
|
k |	D ]D | t	 | dd t D }| t	|t	t
| | t t  | tfdd D  | | fddD  q| |	t|| | |	t|| ttjd D ]}| ||| qqpq_d S )Nrt   r   r   r   c                 S   r!   r   r   r   r   r   r   rR   2  r   zATestBasicOps.test_combinations_with_replacement.<locals>.<lambda>ABC)r   r   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  	 tt|D ]}|| |d kr4 nq(d S || d g||  ||d < t  fdd|D V  q")Nr   c                 3   r   r   r   r   r   r   r   r   C  r   zPTestBasicOps.test_combinations_with_replacement.<locals>.cwr1.<locals>.<genexpr>r   c                 3   r   r   r   r   r   r   r   r   K  r   )r   r   r   r%   )r?   rF   r'   r   r   r   r   r   cwr1;  s"   
z=TestBasicOps.test_combinations_with_replacement.<locals>.cwr1c                 3   sV    	 t |  t }tt||dD ]}t|t|kr(t  fdd|D V  qd S )Nrepeatc                 3   r   r   r   r   r   r   r   r   S  r   zPTestBasicOps.test_combinations_with_replacement.<locals>.cwr2.<locals>.<genexpr>)r   r   productr%   r   r	   r   r   r   r   cwr2M  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   )rB   )r'   rF   r   r   r   numcombsU  s   $zATestBasicOps.test_combinations_with_replacement.<locals>.numcombsrv   c                 S   r   r   r   r   r   r   r   rT   [  r   zCTestBasicOps.test_combinations_with_replacement.<locals>.<listcomp>r   c                 S      g | ]\}}|qS r   r   )rS   kvr   r   r   rT   k      c                 3   r   r   r   rW   r   r   r   r   n  r   zBTestBasicOps.test_combinations_with_replacement.<locals>.<genexpr>c                    r   r   r   rW   r   r   r   rT   p  r   )r   r   r]   r   r   rc   r	   re   r%   r   r   r   r   r   groupbyr   rM   r   rl   )r,   cwrr   r   r   r   r   r'   rF   r   Zregular_combsZnorunsrQ   r   r   r   "test_combinations_with_replacement+  sX   
z/TestBasicOps.test_combinations_with_replacementc                 C   r   )Nr      @)r   r   r   r   r+   r   r   r   +test_combinations_with_replacement_overfloww  r   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 r   )r   rc   r   r   r   r   r   r	   )r,   r   r   r   r   .test_combinations_with_replacement_tuple_reuse}  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g d dd	d
}ddd}tdD ]}dd t|D  t|d D ]}tt |}| t|||krsdn	t|t||   | t|tt	| | |t
| |D ]"}| t|| | tt	|| | t fdd|D  q| |t| | | |t| | ||kr| |tt d  | |tt  ttjd D ]}| |t | qq_qNd S )Nrt   r   r   r   r   rP   rm   )r   r   r   r   r   r   r   r   )r   r   )r   r   c                 3   s@   	 t |  t }|d u r|n|}||krd S tt|}tt|| d |d d d d }t  fdd|d | D V  |rtt|D ]O}||  d8  < || dkrs||d d  |||d   ||d < || ||< qH|| }||  || ||< || < t  fdd|d | D V   nd S |sBd S d S )Nr   c                 3   r   r   r   r   r   r   r   r     r   zHTestBasicOps.test_permutations.<locals>.permutations1.<locals>.<genexpr>r   c                 3   r   r   r   r   r   r   r   r     r   r   )r?   rF   r'   r   Zcyclesr   r   r   r   r   permutations1  s,   $ ( z5TestBasicOps.test_permutations.<locals>.permutations1c                 3   sf    	 t |  t }|d u r|n|}tt||dD ]}tt||kr0t  fdd|D V  qd S )Nr   c                 3   r   r   r   r   r   r   r   r     r   zHTestBasicOps.test_permutations.<locals>.permutations2.<locals>.<genexpr>)r   r   r   r%   r   r   r   r   r   permutations2  s   z5TestBasicOps.test_permutations.<locals>.permutations2rv   c                 S   r   r   r   r   r   r   r   rT     r   z2TestBasicOps.test_permutations.<locals>.<listcomp>r   c                 3   r   r   r   rW   r   r   r   r     r   z1TestBasicOps.test_permutations.<locals>.<genexpr>r   )r   r]   r   r   rc   r	   r%   r   rB   r   r   r   r   rM   r   rl   )r,   r  r  r'   rF   r   prQ   r   r   r   test_permutations  s@   

	,zTestBasicOps.test_permutationsc                 C   r   )Nr   r   )r   r   r   r   r+   r   r   r   test_permutations_overflow  r   z'TestBasicOps.test_permutations_overflowc              
   C   r   r   )rc   r   r   r   r   r   r   r	   r+   r   r   r   test_permutations_tuple_reuse  r   z*TestBasicOps.test_permutations_tuple_reusec              	      s  t dD ]}dd | }t dD ] tt| d}tt| }tt| }tt| }| t||   | t||rSt|  d t  t|d  n   | t| |krbdn	t|t|    | t| |krxd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qd S )Nrn   ABCDEFGrx   r   r   r   c                 S       g | ]}t |t|kr|qS r   r   r	   rS   tr   r   r   rT          z3TestBasicOps.test_combinatorics.<locals>.<listcomp>c                        g | ]}t t| kr|qS r   r   r   r  rE   r   r   rT     r  c                 S   r  r   r  r  r   r   r   rT     r  c                    r  r   r  r  rE   r   r   rT     r  )r%   r	   r   r   r   r   rc   r   rB   r   r   filter__contains__)r,   r'   rP   r@   r   permcombr   rE   r   test_combinatorics  s0   :,4zTestBasicOps.test_combinatoricsc              
   C   s,  |  ttdg ddtd |  ttdg dtd |  ttdg dtd |  ttdg dtd |  ttdg dtd	 |  ttd
g dtd d}tttd|}ttd}|  tt||g d|  | ttd td | tttdd  | tttd | tttdd  dd dd gt	 D ]e}dg dddfdg dddfdg dddfdg dd	dfd
g dddffD ]=\}}}}|  t|t||dt| |  t|t||t| t||}|rt
| |  t||t| qqd S )NABCDEFr   r   r   r   r   r   )data	selectorsACEF)r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   ZACr   )r   r   r   r   r   r   ZBC'  rn   r  r   rm   rw   c                 S   s
   t  | S r   )r   r   r   r   r   rR        
 z,TestBasicOps.test_compress.<locals>.<lambda>c                 S   s
   t | S r   )r   r   r   r   r   r   rR     r!  ZCEFZBCDEFr   )rc   r	   compressr   r   r   r%   r   r]   r   re   )r,   r'   r  r  r   Zresult1Zresult2r   r   r   r   test_compress  s<     
zTestBasicOps.test_compressc              	   C   sZ  |  tdt g d |  tdtdg d |  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g d |  tdtdg d |  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 tj d tj d d%dd&dtjd tjd fD ]}tt|}d'|}|  || qPddtd td fD ]0}t|}|  tt|| |  tt|| t	tjd D ]}| |t| qqotdtd  t  d S )(Nrt   ri   r   rj   r   rk   r   rm   )ri   rm   rj   rV   )rk   rw   r   r(  r)  r  )ri   r  )rj   r   )ri   r*  )rj   r   rV   ri   rK   rw         
@)r+  g      @g      @      
@      )r,  y      @            @      1.1z2.1z3.1rx   r     zcount(3)zcount(4)z	count(-9)ig     $@zcount(10.25)g      $@zcount(10.0)r   	count(%r))rc   r   countr:   r
   r   r]   maxsizer	   r%   r   r   reprre   rd   floatsys__mod__r   r   rM   r   rl   exc_info)r,   BIGINTrk   r   r1r2valuerQ   r   r   r   
test_count  s\      $4
zTestBasicOps.test_countc                 C   sX  |  tdtddg d |  tdtdddg d |  tdtddg d | ttd	d
 |  tdtddg d |  tdtddg d |  tdtddg d |  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g d |  tdtddg d |  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g d! tdd}|  t|d" t| |  t|d# td$d}|  t|d% t| |  t|d% td$d&}|  t|d' t| |  t|d( |  t|d( |  ttd)dd* |  ttd)dd+ |  ttd)d,d- |  ttdd,d. tdd,}|  tt|t |  tt|t tj d tj d d/dddtjd tjd fD ]Q}tj d tj d d/ddddtjd tjd f	D ]4}tt||}|dkrd0| }nd1||f }|  || ttjd D ]}| |t|| qqqd S )2Nrt   r   rm   )ri   r   )rj   rw   )rk   rx   )startstepr  )rA  )r%  )rj   r  )rk   r   ri   rj   r   )r?  )rj   r   r'  r   )r?  )rj   rm   )rk   rV   r   ro   r}   rK   rw   g      ?)r   r+  g      @r,  )r   r-  y      !@       r.  z.1z1.2z1.3rv      rp   r/  g      @)rK   g      )@g      .@zcount(3, 5)zcount(8, 5)r0  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)r1  r2  zcount(%r, %r))rc   r   r3  r   r]   r:   r4  r%   r	   r   r   r5  re   rd   r   r6  r7  rM   r   rl   )r,   r:  rk   r   r   r;  r<  rQ   r   r   r   test_count_with_stride?  sv   28"$



46

z#TestBasicOps.test_count_with_stridec                 C   sl   |  tdtdtd |  ttdg  | tt | ttd |  tttt dg d d S )NrK   rt   Z
abcabcabcar   rw   )
r   r   r   r   r   r   r   r   r   r   )rc   r:   cycler	   r   r]   r8   r   r+   r   r   r   
test_cyclev  s
   $zTestBasicOps.test_cyclec                    s  t d | t d | tdt td ttj	d D ]2}| tdt
t |td t  | tdt
t |td t  t  q!ttj	d D ]
}| |t d q[ttj	d D ]T}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 qmd S )Nrt   ri   rK   Z
bcabcabcabr   Z
cabcabcabcr   c                       g | ]}t  qS r   re   r   r   r   r   rT     r   z7TestBasicOps.test_cycle_copy_pickle.<locals>.<listcomp>r   r   Zcdeabcdeabcdeabcdeabc                    rF  r   rG  r   r   r   r   rT     r   rv   )rD  rc   re   r:   r   r   r	   r%   rM   r   rN   rO   rl   r   )r,   rQ   r^   _r  r   r   r   r   test_cycle_copy_pickle}  s:   


z#TestBasicOps.test_cycle_copy_picklec                 C   s6   g d}|D ]}t |}| td|g d qd S )N)sH   citertools
cycle
(c__builtin__
iter
((lI1
aI2
aI3
atRI1
btR((lI1
aI0
tb.s?   citertools
cycle
(c__builtin__
iter
(](KKKetRKbtR(]KaK tb.s>   citertools
cycle
c__builtin__
iter
](KKKeRKbR]KaK b.s;   citertools
cycle
cbuiltins
iter
](KKKeRKbR]KaK b.sH   =       	itertoolscyclebuiltinsiter](KKKeRKbR]KaK b.sH   citertools
cycle
(c__builtin__
iter
((lp0
I1
aI2
aI3
atRI1
btR(g0
I1
tb.s?   citertools
cycle
(c__builtin__
iter
(]q (KKKetRKbtR(h Ktb.s>   citertools
cycle
c__builtin__
iter
]q (KKKeRKbRh Kb.s;   citertools
cycle
cbuiltins
iter
]q (KKKeRKbRh Kb.sG   <       	itertoolscyclebuiltinsiter](KKKeRKbRh Kb.sI   citertools
cycle
(c__builtin__
iter
((lI1
aI2
aI3
atRI1
btR((lI1
aI00
tb.sA   citertools
cycle
(c__builtin__
iter
(](KKKetRKbtR(]KaI00
tb.s=   citertools
cycle
c__builtin__
iter
](KKKeRKbR]Kab.s:   citertools
cycle
cbuiltins
iter
](KKKeRKbR]Kab.sG   <       	itertoolscyclebuiltinsiter](KKKeRKbR]Kab.sI   citertools
cycle
(c__builtin__
iter
((lp0
I1
aI2
aI3
atRI1
btR(g0
I01
tb.sA   citertools
cycle
(c__builtin__
iter
(]q (KKKetRKbtR(h I01
tb.s=   citertools
cycle
c__builtin__
iter
]q (KKKeRKbRh b.s:   citertools
cycle
cbuiltins
iter
]q (KKKeRKbRh b.sF   ;       	itertoolscyclebuiltinsiter](KKKeRKbRh b.rK   )
r   rm   r   r   rm   r   r   rm   r   r   )rM   rN   rc   r:   )r,   Z	testcasesr  r^   r   r   r   test_cycle_unpickle_compat  s
   
z'TestBasicOps.test_cycle_unpickle_compatc                 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    n1 sKw   Y  | t t d}|tddf W d    n1 smw   Y  td| | t t dtddf W d    n1 sw   Y  | tt d	jd
 | tt d	jg f d S )Ndefgrt   r   r   Zdefgabcdefgabcdefgababcdefgr   r   r   r   )rD  r   r	   rc   r:   r   r]   r   )r,   rk   r   r   r   test_cycle_setstate  s&   
z TestBasicOps.test_cycle_setstatec              
      s  |  g ttg  |  g ttg td | tttdg  | ttd  | ttddd d g d}g }t|dd D ]\}}|D ]}|  ||d  || qEq?|  || ttj	d	 D ]-}g }t
tt|t|D ]\}}|D ]}|  ||d  || qyqs|  || qcg }t|tD ](\}}t|tD ]\}}|D ]}|  ||d  |  ||d
  || qqq|  || ttj	d	 D ]F}g }t
tt|t|D ]/\}}t
tt|t|D ]\}}|D ]}|  ||d  |  ||d
  || qqq|  || qd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  ttj	d	 D ]%}t|t}t|\}}t| t| |  tt
t||g  qd}dd tt|D }|  |g d dd tt|D }|  |g d dd tt|D }|  |g d tdd tt|D ddd d }|  |g d G dd dt d( fdd 	}d tfd!d"}|  ||d |  ||d	 G  fd#d$d$}| | d g}| j ||td% |  ||  fd&d'd_|  |d g d	_|  |d d g d S ))Nkeyrt   c                 S   r!   r   r   r   r   r   r   rR     r   z+TestBasicOps.test_groupby.<locals>.<lambda>rK   ))r   rK   r   )r      rp   )r   r   rp   )r      rp   )r         )r   ro   rS  )rm   rz      )rm   rB  rT  c                 S   rC   rD   r   rE   r   r   r   rR         r   r   r   c                 S   r   r   r   rS   r   gr   r   r   rT     r   z-TestBasicOps.test_groupby.<locals>.<listcomp>c                 S   s   g | ]}|d  qS rb   r   )rS   rF   r   r   r   rT     r   Z	AABBBAAAAry   )r   rw   Zabracadabrac                 S   r   r   r   rV  r   r   r   rT   2  r   )ri   rj   rk   r   rF   c                 S   s$   g | ]\}}t t|d dr|qS r  r7   rV  r   r   r   rT   5     $ )ri   rj   rF   c                 S       g | ]\}}t t||fqS r   r   r	   rV  r   r   r   rT   8  r  )rw   ri   r   rj   )r   rk   )r   r   r   rF   c                 S   rY  r   rZ  rV  r   r   r   rT   ;  r  T)reverserm   )r[  r]  r\  c                   @   s   e Zd ZdS )z0TestBasicOps.test_groupby.<locals>.ExpectedErrorN)r3   r4   r5   r   r   r   r   ExpectedError?  s    r_  c                 3   s    t | D ]}dV  q )NZyor$   r&   r_  r   r   delayed_raiseA  s   z0TestBasicOps.test_groupby.<locals>.delayed_raisec                    s    fddt | |D S )Nc                    s   g | ]\}} |qS r   r   rV  funcr   r   rT   F  r   z;TestBasicOps.test_groupby.<locals>.gulp.<locals>.<listcomp>)r   )r?   Zkeyprc  r   rb  r   gulpE  s   z'TestBasicOps.test_groupby.<locals>.gulpc                          e Zd Z fddZdS )z+TestBasicOps.test_groupby.<locals>.DummyCmpc                        r   r   )r,   dstr`  r   r   __eq__O  r#   z2TestBasicOps.test_groupby.<locals>.DummyCmp.__eq__N)r3   r4   r5   rh  r   r`  r   r   DummyCmpN      ri  rb  c                    s    j dkr j d8  _ | S  r   )skip)objr_  keyfuncr   r   rn  Y  s   
z*TestBasicOps.test_groupby.<locals>.keyfuncrb   )rc   r	   r   r   r   r]   appendr%   rM   r   rN   rO   rG   rI   r   r   r
   re   r   	Exceptionrk  )r,   rP   dupr   rW  elemrQ   ZikZigkeysZexpectedkeysr^   rH  Zg1Zg2Zg3rF   ra  rd  ri  r   rm  r   test_groupby  s      

"&zTestBasicOps.test_groupbyc              
   C   s  |  ttttdg d |  ttd g dddg |  tttg dddg |  tdttt g d | t	t | t	tdd	  | t	td
d	 tdd | t	ttd | t	t
ttdtd g d}tttd}|  tt|| tttd}|  tt|| ttjd D ]/}tttd}|  ttt||| t
| |  ttt|||dd   qttjd D ]}tttd}| || qd S )Nrn   )r   r   rV   r   r   r   r   r   r   r   rV   )r   r   rV   rn   c                 S   r!   r   r   r   r   r   r   rR   l  r   z*TestBasicOps.test_filter.<locals>.<lambda>c                 S   r!   r   r   r   r   r   r   rR   m  r   rv   rm   )rc   r	   r  r   r%   boolr:   r3  r   r]   re   r   r   rM   r   rN   rO   rl   r,   ansrk   rQ   r   r   r   test_filterf  s.   (zTestBasicOps.test_filterc                 C   s
  |  ttttdg d |  ttd g dg d |  tttg dg d |  tdttt g d | t	t | t	tdd  | t	td	d tdd
 | t	ttd | t	t
ttdtd ttjd D ]}| |tttd qud S )Nrn   r   ru  r   r   r   rV   )r   rm   rw   rv   c                 S   r!   r   r   r   r   r   r   rR     r   z/TestBasicOps.test_filterfalse.<locals>.<lambda>c                 S   r!   r   r   r   r   r   r   rR     r   rv   rm   r   )rc   r	   filterfalser   r%   rv  r:   r3  r   r]   re   rM   r   rl   r,   rQ   r   r   r   test_filterfalse  s   zTestBasicOps.test_filterfalsec                 C   s(  dd t dt D }| |g d | 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      g | ]\}}||fqS r   r   rS   r   yr   r   r   rT     r   z)TestBasicOps.test_zip.<locals>.<listcomp>rt   r$  rn   r   rm   c                 S      g | ]}t t|qS r   r   r	   rS   Zpairr   r   r   rT     r   r   c                 S      g | ]}|qS r   r   r  r   r   r   rT         )	r
   r3  rc   r	   r%   r   r:   r   r]   )r,   rx  r   r   r   test_zip  s   $$$zTestBasicOps.test_zipc              	   C   sz  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 }| |g d dd t
tdt D }| |g d ttjd D ]}dd tttdt |D }| |g d q`ttjd D ]"}tdt }t| d	d tt||D }| |d
dg qttjd D ]}| |tdt  qd S )Nrt   r   c                 S   r~  r   r   r  r   r   r   rT     r   z5TestBasicOps.test_zip_tuple_reuse.<locals>.<listcomp>r$  c                 S   r~  r   r   r  r   r   r   rT     r   r   c                 S   r~  r   r   r  r   r   r   rT     r   c                 S   r~  r   r   r  r   r   r   rT     r   r&  r'  )r	   r   r   r
   rc   r   r   r   dictfromkeysr   r3  r   r%   rM   r   rN   rO   re   rl   )r,   idsrx  rQ   r   r   r   r   test_zip_tuple_reuse  s&   $z!TestBasicOps.test_zip_tuple_reusec                    sT  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fD ]@  fddt ttt D }| tt  | | tt i i | dd |D }| tt i tdd| q=| 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 i tt
tdd g d | ttd | ttt dd dD ]}z
t|t t  W n	 ty   Y qw | d|  q| dd tddD tt
dd | dd tddD tt
dd d S )Nrt   rn   r/    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 r   r   )rS   argr   r   r   rT     rX  z;TestBasicOps.test_ziplongest.<locals>.<listcomp>.<listcomp>r   )rS   r   r   r   rT     s    z0TestBasicOps.test_ziplongest.<locals>.<listcomp>c                 S   s   g | ]}t d d |D qS )c                 s   s     | ]}|d u r
dp|V  qd S )NXr   rW   r   r   r   r     s    z:TestBasicOps.test_ziplongest.<locals>.<listcomp>.<genexpr>r  r  r   r   r   rT     s    r  	fillvaluerm   r   rK  )zzip_longest('abc', fv=1)z3zip_longest('abc', fillvalue=1, bogus_keyword=None)zDid not raise Type in:  c                 S   r  r   r  r  r   r   r   rT     r   r   c                 S   r  r   r   r  r   r   r   rT     r  )rt   rK  )r%   r   r   r   rc   r	   zip_longestr  r:   r3  r
   r   r]   evalglobalslocalsZfail)r,   targetZstmtr   r   r   test_ziplongest  sF   

"(
"(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   r   )
r	   r   r   r  rc   r   r   r   r  r  )r,   r  r   r   r   test_zip_longest_tuple_reuse  s   z)TestBasicOps.test_zip_longest_tuple_reusec              	   C   sd   t tjd D ](}| |tdd | |tdd | |tdddd | |tdd qd S )Nr   rt   r   Zdefghr  r   )r%   rM   r   rl   r  r|  r   r   r   test_zip_longest_pickling  s   z&TestBasicOps.test_zip_longest_picklingc                    s^   t   G  fddd}| t }t|  W d    n1 s!w   Y  | |j  d S )Nc                       re  )z?TestBasicOps.test_zip_longest_bad_iterable.<locals>.BadIterablec                    rf  r   r   r+   	exceptionr   r   r-     r#   zHTestBasicOps.test_zip_longest_bad_iterable.<locals>.BadIterable.__iter__N)r3   r4   r5   r-   r   r  r   r   BadIterable  rj  r  )r]   r   r  ZassertIsr  )r,   r  cmr   r  r   test_zip_longest_bad_iterable  s   z*TestBasicOps.test_zip_longest_bad_iterablec                 C   s   G dd d}|ddt }|ddt }dd }| |||g d	 |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 r   )or   r  rX   )r,   r  r  rX   r   r   r   __init__  s   

z5TestBasicOps.test_bug_7244.<locals>.Repeater.__init__c                 S   r!   r   r   r+   r   r   r   r-     r#   z5TestBasicOps.test_bug_7244.<locals>.Repeater.__iter__c                 S   s$   | j dkr|  j d8  _ | jS | jr   )r  r  rX   r+   r   r   r   r1     s   
z5TestBasicOps.test_bug_7244.<locals>.Repeater.__next__Nr3   r4   r5   r  r-   r1   r   r   r   r   Repeater  s    r  r   rm   r   rV   c              	   S   sd   g }t | |ddD ]&\}}td t||f W d    n1 s#w   Y  |||f q	|S )Nr   r  stdout)r  r   Zcaptured_outputprintro  )r;  r<  r   r   r   r   r   r   run  s   z'TestBasicOps.test_bug_7244.<locals>.run)r  r  r  r  r   r  r  )r0   rc   rZ   r  re   r   )r,   r  r;  r<  r  r^   r   r   r   test_bug_7244  s   zTestBasicOps.test_bug_7244c              	   C   sT  |  ttdg  |  ttdg  |  ttddgf |  ttdg d |  tttdtttdtdd | t t  W d    n1 sUw   Y  | t td	d
 W d    n1 sow   Y  | t td	d W d    n1 sw   Y  | t td  W d    d S 1 sw   Y  d S )Nr   ri   ru   ri   rj   r   )r  )rj   rk   )rk   r   )r   rX   r  r   rt   rK   r>   )rc   r	   pairwiser%   r
   r   r]   r+   r   r   r   test_pairwise%  s.   
"zTestBasicOps.test_pairwisec                    s  g dgfdgddgft dt dgg d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fD ]+\}}| tt| | t d	D ]}| tt||  tt|i t|d
 qMq;| 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	 t dD ]P} fddt t
dD }ttt|}| ttt| | | tt| t||  | tt| t||  tt|}| ttt| | qd S )Nr   ru   r   rj   r   rm   )r   r   r  r  r  )r   r   r  r   rV   r   rv   rn   i c                     s    t tt| |dd }t|}|dkrdV  d S tdd |D r&d S dg| }tdd t||D V  	 tt|D ]3}|| t|| d krNq?||  d7  < t|d |D ]}d||< q]tdd t||D V   nd S q9)	Nr   r   r   r   c                 s   s    | ]	}t |d kV  qdS )r   Nr  )rS   r   r   r   r   r   N      z>TestBasicOps.test_product.<locals>.product1.<locals>.<genexpr>c                 s       | ]	\}}|| V  qd S r   r   rS   r   r   r   r   r   r   Q  r  c                 s   r  r   r   r  r   r   r   r   Y  r  )	r	   r   r   getr   anyr
   r   r%   )r   kwdspoolsr'   r   r   r   r   r   r   product1H  s*   

z+TestBasicOps.test_product.<locals>.product1c                  ?   sX    	 t tt| |dd }g g}|D ]  fdd|D }q|D ]}t|V  q"d S )Nr   r   c                    s    g | ]} D ]}||g qqS r   r   r  r   r   r   rT   c  r  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>.product2r   rt   r   )ri   rj   rk   rL  rP  rQ  r}   c                    s   g | ]}t  qS r   )randomchoice)rS   r   argtypesr   r   rT   j  s    z-TestBasicOps.test_product.<locals>.<listcomp>rw   )r%   rc   r	   r   r  r   r   r]   r   r   r  	randranger@   r   r   )r,   r   r   rF   r  r  r   expected_lenr   r  r   test_product8  s:   "	
zTestBasicOps.test_productc                 C   sH   |  ttf tdgd ddi W d    d S 1 sw   Y  d S )Nru   r   r   i   )r   r   r   r   r+   r   r   r   test_product_overflowr  s   "z"TestBasicOps.test_product_overflowc              
   C   r   )Nrt   r   r   )rc   r   r   r   r   r   r   r	   r+   r   r   r   test_product_tuple_reusew  r   z%TestBasicOps.test_product_tuple_reusec              	   C   s   g dgfdgddgft dt dgg d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fD ]0\}}| ttt| | | ttt| | t tjd	 D ]
}| |t|  q`q;d S )
Nr   ru   r   r  r   rm   r  r   r   )	r%   rc   r	   r   r   r   rM   r   rl   )r,   r   r   rQ   r   r   r   test_product_pickling|  s   z"TestBasicOps.test_product_picklingc                 C   sL   t dd}|d | t|d t ddd}|d | tt| d S )Nr  )rm   )r      )r   rm   r   )r   r   r  )r   r   rc   re   r   r0   )r,   r  r   r   r   test_product_issue_25021  s   


z%TestBasicOps.test_product_issue_25021c                 C   s  |  ttdddg d |  ttdtdg d |  ttddg d |  tdtdg d |  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 ttjd D ]}| |tddd qd S )Nri   rm   )objecttimes)ri   ri   ri   ))r   ri   )r   ri   )r   ri   r   r*  rV   y      ?        zrepeat((1+0j))rw   zrepeat((1+0j), 5)zrepeat((1+0j), 0)rK   r   Zaar   )rc   r	   r   r   r%   r:   r   r]   r5  re   r   r   rM   r   rl   )r,   rF   rk   rQ   r   r   r   test_repeat  s0   
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  )rc   r5  r   r+   r   r   r   test_repeat_with_negative_times  s   z,TestBasicOps.test_repeat_with_negative_timesc              	   C   s  |  tttjtdtddg d |  tttdtdg d |  tttdt g d |  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 g d}ttdt }|  tt|| ttdt }|  tt|| ttjd D ]}ttdt }| || qd S )Nrm   r   rv   r   r   rx   rt   rw   r$  r   r%  r&  rK   rV   )rc   r	   r   r<   powr%   r"   r3  r:   r   r]   negre   r   r   r   r   r   rM   r   rl   rw  r   r   r   test_map  s8   zTestBasicOps.test_mapc              
   C   s  |  tttjttdtddg d |  tdttjtt tdg d |  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 g d}ttjttdtdd}|  tt|| ttjttdtdd}|  tt|| ttjd D ]}ttjttdtdd}| || qd S )Nrm   r   rv   r  rV   rw   i   rV   rw   extrarK   )rc   r	   starmapr<   r  r
   r%   r:   r3  r   r   r]   re   r   r   r   r   r   rM   r   rl   rw  r   r   r   test_starmap  s.   " $zTestBasicOps.test_starmapc           	   	   C   sR  dD ]}|  tttdg|R  tt|  qdD ]\}}|  tttdg|R  tt|  q|  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 dD ]J}|  ttttdg|R  tt|  |  ttttdg|R  tt|  ttjd D ]}| |ttdg|R   qqjd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)rK   r   rm   rK   rm   r   rK   r   )rK   rK   rK   rm   r   r}   )))rK   r~   rm   )rK   r}   rm   ))rK   r~   )rK   r}   ))r~   )r}   rK   r   r   rm   rV   r  r   ri   2   )r  r  r  r  r  c                 s   s    | ]}|V  qd S r   r   r   r   r   r   r   7      z+TestBasicOps.test_islice.<locals>.<genexpr>r  c                   @      e Zd Zdd Zdd ZdS )z)TestBasicOps.test_islice.<locals>.IntLikec                 S   
   || _ d S r   valr,   r  r   r   r   r  B     
z2TestBasicOps.test_islice.<locals>.IntLike.__init__c                 S   s   | j S r   r  r+   r   r   r   	__index__D  s   z3TestBasicOps.test_islice.<locals>.IntLike.__index__N)r3   r4   r5   r  r  r   r   r   r   IntLikeA  s    r  rw   )rc   r	   r8   r%   r   r   r]   r   r   r3  r4  re   r   r   rM   r   rl   weakrefrefZassertIsNotNoner   
gc_collectZassertIsNoner  )	r,   r   Ztgtargsr^   Zrark   rQ   wrr  r   r   r   test_islice  sx   

"$&&*  
 
 

&$zTestBasicOps.test_islicec                 C   s0  g d}|  ttt|g d |  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g d}|  t|g d | tt| |  tttt|g d |  tttt|g d ttjd D ]}| |tt| qd S )	Nr   rm   rw   r   r   rV   rn   rx   r   r  r  rK   )r   r   r   r   r   r   r   r   r   r   )rc   r	   	takewhilerL   r   r]   r<   r  re   r   r   rv  r0   r   r   r%   rM   r   rl   )r,   r  r  rQ   r   r   r   test_takewhileL  s$    zTestBasicOps.test_takewhilec                 C   s   g d}|  ttt|g d |  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|g d |  tttt|g d ttjd D ]}| |tt| qrd S )Nr  )r   r   rV   rn   rx   r  r  rK   r   )rc   r	   	dropwhilerL   r   r]   r<   r  re   r   r   r   r   r%   rM   r   rl   )r,   r  rQ   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|\}}tdD ]
}| t|| q\~| t|tt| t t|\}}tdD ]
}| t|| q~| t|ttd| tdD ]E}dg| dg|  }t| g g f}t t|}|D ]}t|| }	|| 	|	 q| |d tt| | |d tt| q| 
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\}}}
tdD ]}| t|| q)| 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 tdD ](}t d	|}| t|t | t|| | dd |D td	g|  qt d	\}}t |\}
}| ||
u  t d	\}}t|}| 
t| | 
t|d ||}| t|t|  kot|  kotd	kn   t td\}}t|}| t|dt| ~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   ttjd D ]#}| |tt d	 t d	\}}| j|||d | j|||d qd S )N   r}   rw   r   r   rm   r   r   rt   r   r  invalidr  c                 S      g | ]}t |qS r   r	   r   r   r   r   rT     r   z)TestBasicOps.test_tee.<locals>.<listcomp>rK   	__class__r  <   r   )teerc   r	   r(   r   r%   re   r  shufflero  r   r]   rd   r   r   r   r   r  proxygetattrr   r  ReferenceErrorr   r:   r   rM   r   rl   r   )r,   r'   ri   rj   r   r   orderZlistsZitsr=  rk   r   r   t1t2ZtnewZt3r  rx  Zlong_ansrQ   r   r   r   test_teeq  s    
 
$<








zTestBasicOps.test_teec                 C   s0   t td d\}}zt| ~W d S    ~~ )Ni -1)r  r   r  )r,   ZforwardZbackwardr   r   r   test_tee_del_backward  s   z"TestBasicOps.test_tee_del_backwardc                    sZ   G  fddd}t | \} | td t| W d    d S 1 s&w   Y  d S )Nc                       s$   e Zd ZdZdd Z fddZdS )z(TestBasicOps.test_tee_reenter.<locals>.ITc                 S   r!   r   r   r+   r   r   r   r-     r#   z1TestBasicOps.test_tee_reenter.<locals>.I.__iter__c                    s   | j }d| _ |rt S d S NF)firstre   )r,   r  r  r   r   r1     s
   z1TestBasicOps.test_tee_reenter.<locals>.I.__next__N)r3   r4   r5   r  r-   r1   r   r  r   r   I  s    r  r  )r  assertRaisesRegexrZ   re   )r,   r  ri   r   r  r   test_tee_reenter  s
   

"zTestBasicOps.test_tee_reenterc                    s   t  t   G  fddd}t| \}}t jt|gd}|  z4  | td t| W d    n1 s?w   Y  W  	  |
  d S W  	  |
  d S  	  |
  w )Nc                       "   e Zd Zdd Z fddZdS )z+TestBasicOps.test_tee_concurrent.<locals>.Ic                 S   r!   r   r   r+   r   r   r   r-   #  r#   z4TestBasicOps.test_tee_concurrent.<locals>.I.__iter__c                    s         d S r   )r   waitr+   Zfinishr@  r   r   r1   %  s   z4TestBasicOps.test_tee_concurrent.<locals>.I.__next__Nr2   r   r   r   r   r  "      r  )r  r   r  )	threadingEventr  Threadre   r@  r  r  rZ   r   join)r,   r  ri   rj   threadr   r   r   test_tee_concurrent  s$   

z TestBasicOps.test_tee_concurrentc                 C   s$  |  ttt  ttttfD ]}|  tt|g  |  tt|t  q|  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 ttttttfD ]}|  tt|dd g  |  tt|dd t  qtd S )Nr   c                 S   r!   r   r   r   r   r   r   rR   H  r   z1TestBasicOps.test_StopIteration.<locals>.<lambda>c                 S   r!   r   r   r   r   r   r   rR   I  r   )r   r0   re   r
   r   rD  r   r*   r8   r  r   r  r{  r   r  r  r  )r,   r   r  qr   r   r   test_StopIteration4  s"   zTestBasicOps.test_StopIterationc                 C   6   t d g gd}t| t  | tt| d S r;   )r   re   gccollectr   
is_trackedr   r   r   r   test_combinations_result_gcK  s   z(TestBasicOps.test_combinations_result_gcc                 C   r
  r;   )r   re   r  r  r   r  r   r   r   r   ,test_combinations_with_replacement_result_gcX     z9TestBasicOps.test_combinations_with_replacement_result_gcc                 C   r
  r;   )r   re   r  r  r   r  r   r   r   r   test_permutations_result_gc`  r  z(TestBasicOps.test_permutations_result_gcc                 C   s4   t d g g}t| t  | tt| d S r   )r   re   r  r  r   r  r   r   r   r   test_product_result_gch  s   z#TestBasicOps.test_product_result_gcc                 C   s*   t g g}t  | tt| d S r   )r  r  r  r   r  re   r   r   r   r   test_zip_longest_result_gcp  s   
z'TestBasicOps.test_zip_longest_result_gc)rV   r   N)?r3   r4   r5   rl   r   r   r   r   r   r   r   Zbigaddrspacetestr   Zimpl_detailr   r   r   r   r	  r
  r  r  r#  r>  rC  rE  rI  rJ  rM  rt  ry  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	  cpython_onlyr  r  r  r  r  r   r   r   r   rU   R   s    
)#P

L

?

!"07$$ 
*
(:


^ 	



rU   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g dg d d S Nr   r   rm   rV   rw   r   rm   rn   rK   ro   )rc   r	   r   r+   r   r   r   r   z      zTestExamples.test_accumulatec              
   C   s   g d}g d}t tjd D ]4}t|}| ttt|||d d   | t|d | ttt|||dd   qt|}| t|d | tt	
||dd   | tt		||dd   d S )Nr  r  r   )r%   rM   r   r   rc   r	   rN   rO   re   r   r   )r,   r  ZaccumulatedrQ   r^   r   r   r   test_accumulate_reducible}  s   &("z&TestExamples.test_accumulate_reduciblec                 C   s   t g dtj}| t|d  ttjd D ]}tt	||}| t
|ddg q| t
t|ddg | t
t|ddg d S )N)NNNr   TF)r   r<   is_rc   re   r%   rM   r   rN   rO   r	   r   r   )r,   r^   rQ   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 Nr   r   ZDEFr  )rc   r  r   r+   r   r   r   r     s   zTestExamples.test_chainc                 C   s    |  dtddgd d S r  )rc   r  r   r   r+   r   r   r   r     r  z%TestExamples.test_chain_from_iterablec                 C   s<   |  ttddg d |  tttddg d d S )Nr   r   r   rV   rm   r   )rc   r	   r   r%   r+   r   r   r   r     s   zTestExamples.test_combinationsc                 C      |  ttddg d d S )Nr   r   r   )rc   r	   r   r+   r   r   r   r     s   z/TestExamples.test_combinations_with_replacementc                 C   s"   |  ttdg dtd d S )Nr  r  r  )rc   r	   r"  r+   r   r   r   r#       "zTestExamples.test_compressc                 C   s"   |  tttddg d d S )NrK   rw   )rK   rP  r   rQ  rR  )rc   r	   r8   r3  r+   r   r   r   r>    r  zTestExamples.test_countc                 C   s"   |  tttddtd d S )Nr   r   ZABCDABCDABCD)rc   r	   r8   rD  r+   r   r   r   rE    r  zTestExamples.test_cyclec                 C   s&   |  ttdd g dg d d S )Nc                 S   rJ   Nrw   r   r   r   r   r   rR     rU  z-TestExamples.test_dropwhile.<locals>.<lambda>r   rV   rn   rV   r   )rn   rV   r   )rc   r	   r  r+   r   r   r   r       &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   r   r   r   rV  r   r   r   rT     r   z-TestExamples.test_groupby.<locals>.<listcomp>ZAAAABBBCCDAABBBZABCDABc                 S   s   g | ]\}}t |qS r   r  rV  r   r   r   rT     r   Z
AAAABBBCCDZAAAAZBBBZCCr   )rc   r   r	   r+   r   r   r   rt    s   zTestExamples.test_groupbyc                 C   &   |  ttdd tdg d d S )Nc                 S      | d S r   r   r   r   r   r   rR     rU  z*TestExamples.test_filter.<locals>.<lambda>rK   )r   rm   rw   rv   ry   )rc   r	   r  r%   r+   r   r   r   ry    r"  zTestExamples.test_filterc                 C   r#  )Nc                 S   r$  r   r   r   r   r   r   rR     rU  z/TestExamples.test_filterfalse.<locals>.<lambda>rK   )r   r   rV   rn   rx   )rc   r	   r{  r%   r+   r   r   r   r}    r"  zTestExamples.test_filterfalsec                 C   s    |  tttddg d d S )N)r   rm   rK   )rw   r   rm   r   ry   r/  )rc   r	   r   r  r+   r   r   r   r    r  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   ABrV   CDCDEFGr   ACEG)rc   r	   r8   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  )rc   r	   r
   r+   r   r   r   r       zTestExamples.test_zipc                 C   s"   |  ttddddg d d S )Nr   r*  -r  )r+  r,  )r   r.  )r   r.  )rc   r	   r  r+   r   r   r   test_zip_longest     zTestExamples.test_zip_longestc                 C   sD   |  ttddtttd  |  tttdg d 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   )rc   r	   r   r   r   splitr%   r+   r   r   r   r	    s   zTestExamples.test_permutationsc                 C   sH   |  ttddtttd  |  tttdddg d d S )Nr   r*  zAx Ay Bx By Cx Cy Dx Dyr   rm   r   )rz  )r   r   r   )r   r   r   )r   r   r   )r   r   r   r  )r   r   r   r  )rc   r	   r   r   r   r1  r%   r+   r   r   r   r    s   zTestExamples.test_productc                 C   r  )NrK   rm   )rK   rK   rK   )rc   r	   r   r+   r   r   r   r    r-  zTestExamples.test_repeatc                 C   s"   |  tttg dg d d S )N))r   rw   )rm   r   r  r%  )rc   r	   r  r  r+   r   r   r   test_stapmap  r0  zTestExamples.test_stapmapc                 C   s&   |  ttdd g dddg d S )Nc                 S   rJ   r   r   r   r   r   r   rR     rU  z-TestExamples.test_takewhile.<locals>.<lambda>r!  r   rV   )rc   r	   r  r+   r   r   r   r    r"  zTestExamples.test_takewhileN)r3   r4   r5   r   r  r  r   r   r   r   r#  r>  rE  r  rt  ry  r}  r  r  r  r/  r	  r  r  r2  r  r   r   r   r   r  x  s0    
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|||}zt|}W n t	y;   t
t|| D ]\}}	q3Y d S w zt| D ]\}}	||krP|	V  t|}qAW d S  t	yl   t
t|d || D ]\}}	qdY d S w r   )slicer@  ra   r7  r4  rA  r   r%   re   r0   r
   	enumerate)
r?   r   rP   r@  ra   rA  r^   Znextir   r   r   r   r   r8     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   r&  rV   r'  r(  r   r)  rK   rm   r   r  )rc   r	   r8   r   r%   r3  re   )r,   r^   rk   r   r   r   test_islice_recipe  s     z1TestPurePythonRoughEquivalents.test_islice_recipeN)r3   r4   r5   staticmethodr8   r6  r   r   r   r   r3    s    
r3  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/d0 Zd1S )2TestGCc                 C   s   | | t| ~~d S r   )ro  re   )r,   iterator	containerr   r   r   	makecycle  s   
zTestGC.makecyclec                 C   s    g }|  tdd|dg| d S Nr   r   rm   )r;  r   r,   ri   r   r   r   r        zTestGC.test_accumulatec                 C      g }|  t|| d S r   )r;  r   r=  r   r   r   r        zTestGC.test_chainc                 C   s   g }|  t|g| d S r   )r;  r   r   r=  r   r   r   r     s   zTestGC.test_chain_from_iterablec                 C   "   g }|  tdd|dgd| d S r<  )r;  r   r=  r   r   r   r   #     zTestGC.test_combinationsc                 C   rA  r<  )r;  r   r=  r   r   r   r   '  rB  z)TestGC.test_combinations_with_replacementc                 C   s   g }|  tdg d| d S )Nr  )r   r   r   r   r   r   )r;  r"  r=  r   r   r   r#  +     zTestGC.test_compressc                 C   s6   g }t dtft|d}| t|d|d| d S )NIntr   r   r   )rd   r   r  r;  r3  )r,   ri   rD  r   r   r   r>  /  s   zTestGC.test_countc                 C      g }|  t|gd | d S r   )r;  rD  r=  r   r   r   rE  4  rC  zTestGC.test_cyclec                 C   s    g }|  ttd||g| d S rD   )r;  r  rv  r=  r   r   r   r  8  r>  zTestGC.test_dropwhilec                 C   s$   g }|  t|gd dd | d S )Nr   c                 S   r!   r   r   r   r   r   r   rR   >  r   z%TestGC.test_groupby.<locals>.<lambda>)r;  r   r=  r   r   r   rt  <      zTestGC.test_groupbyc                 C   s>   d}dd }t t||dD ]\}}|jdg | qd S )NrK   c                 S   r!   r   r   r   r   r   r   rR   C  r   z'TestGC.test_issue2246.<locals>.<lambda>rN  r   )r   r%   __dict__
setdefaultro  )r,   r'   rn  r   r   r   r   r   test_issue2246@  s
   zTestGC.test_issue2246c                 C   $   g }|  tdd |gd | d S )Nc                 S      dS )NTr   r   r   r   r   rR   I  r   z$TestGC.test_filter.<locals>.<lambda>r   )r;  r  r=  r   r   r   ry  G  rF  zTestGC.test_filterc                 C   s   g }|  tdd || d S )Nc                 S   rK  r  r   r   r   r   r   rR   M  r   z)TestGC.test_filterfalse.<locals>.<lambda>)r;  r{  r=  r   r   r   r}  K  rC  zTestGC.test_filterfalsec                 C   s&   g }|  t|gd |gd | d S )Nr   rm   )r;  r
   r=  r   r   r   r  O  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  )r;  r  )r,   ri   rj   r   r   r   r/  S  s   &zTestGC.test_zip_longestc                 C   rJ  )Nc                 S   r!   r   r   r   r   r   r   rR   [  r   z!TestGC.test_map.<locals>.<lambda>r   )r;  r   r=  r   r   r   r  Y  rF  zTestGC.test_mapc                 C   s    g }|  t|gd d | d S r   )r;  r8   r=  r   r   r   r  ]  r>  zTestGC.test_islicec                 C   rE  r   )r;  r  r=  r   r   r   r  a  rC  zTestGC.test_pairwisec                 C   rA  r<  )r;  r   r=  r   r   r   r	  e  rB  zTestGC.test_permutationsc                 C   s$   g }|  tdd|dgdd| d S )Nr   r   rm   r   )r;  r   r=  r   r   r   r  i  rF  zTestGC.test_productc                 C   r?  r   )r;  r   r=  r   r   r   r  m  r@  zTestGC.test_repeatc                 C   s(   g }|  tdd ||fgd | d S )Nc                  W   r!   r   r   r  r   r   r   rR   s  r   z%TestGC.test_starmap.<locals>.<lambda>r   )r;  r  r=  r   r   r   r  q  s   $zTestGC.test_starmapc                 C   s"   g }|  ttdd||g| d S )Nr   r   )r;  r  rv  r=  r   r   r   r  u  rB  zTestGC.test_takewhileN)r3   r4   r5   r;  r   r   r   r   r   r#  r>  rE  r  rt  rI  ry  r}  r  r/  r  r  r  r	  r  r  r  r  r   r   r   r   r8    s2    r8  c                 c   s    	 | D ]}|V  qd S r   r   seqnr   r   r   r   Ry  r   rO  c                   @   r)   )Gc                 C   r  r   rN  r,   rN  r   r   r   r    r  z
G.__init__c                 C   s
   | j | S r   rQ  )r,   r   r   r   r   __getitem__  r  zG.__getitem__N)r3   r4   r5   r  rS  r   r   r   r   rP  ~  r6   rP  c                   @   &   e Zd Z	 dd Zdd Zdd ZdS )r  c                 C      || _ d| _d S rD   rM  rR  r   r   r   r       
z
I.__init__c                 C   r!   r   r   r+   r   r   r   r-     r#   z
I.__iter__c                 C   2   | j t| jkr
t| j| j  }|  j d7  _ |S r;   r   r   rN  r0   r,   r   r   r   r   r1        z
I.__next__Nr  r   r   r   r   r    
    r  c                   @   r)   )Igc                 C   rU  rD   rM  rR  r   r   r   r    rV  zIg.__init__c                 c   s    | j D ]}|V  qd S r   rQ  r  r   r   r   r-     s   
zIg.__iter__Nr3   r4   r5   r  r-   r   r   r   r   r\        r\  c                   @   r)   )r  c                 C   rU  rD   rM  rR  r   r   r   r    rV  z
X.__init__c                 C   rW  r;   rX  rY  r   r   r   r1     rZ  z
X.__next__N)r3   r4   r5   r  r1   r   r   r   r   r    r^  r  c                   @   r)   )Nc                 C   rU  rD   rM  rR  r   r   r   r    rV  z
N.__init__c                 C   r!   r   r   r+   r   r   r   r-     r#   z
N.__iter__Nr]  r   r   r   r   r_    r^  r_  c                   @   rT  )Ec                 C   rU  rD   rM  rR  r   r   r   r    rV  z
E.__init__c                 C   r!   r   r   r+   r   r   r   r-     r#   z
E.__iter__c                 C   s   dd  d S )Nrm   r   r   r+   r   r   r   r1     r   z
E.__next__Nr  r   r   r   r   r`    r[  r`  c                   @   rT  )Sc                 C      d S r   r   rR  r   r   r   r    r#   z
S.__init__c                 C   r!   r   r   r+   r   r   r   r-     r#   z
S.__iter__c                 C   r.   r   r/   r+   r   r   r   r1     r#   z
S.__next__Nr  r   r   r   r   ra    s
    ra  c                 C   s    	 t tdd ttt| S )Nc                 S   r!   r   r   r   r   r   r   rR     r   zL.<locals>.<lambda>)r   r   rO  r\  rP  rQ  r   r   r   L  s   rc  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#S )$TestVariousIteratorArgsc                 C   s   g d}g d}t |}tttttfD ]}| tt||| q| ttt	|g  | 
ttt| | 
ttt| | 
tttt| d S r  )r   rP  r  r\  rc  rO  rc   r	   r   ra  r   r]   r  r_  ZeroDivisionErrorr`  )r,   rP   rF   r'   rW  r   r   r   r     s   z'TestVariousIteratorArgs.test_accumulatec              	   C   s   ddt ddt dddfD ]W}ttttttfD ]+}| tt	||t|| | tt	||||t||t||  q| 
ttt	t| | 
ttt	t| | 
ttt	t| qd S N123r   r/  Zdog333333?r    rw   )r%   rP  r  r\  ra  rc  rO  rc   r	   r   r   r]   r  r_  re  r`  r,   rP   rW  r   r   r   r     s    4z"TestVariousIteratorArgs.test_chainc              	   C   s   ddt ddt dddfD ]J}t|}ttttttfD ]}| t	t
||tdt	|| q| tt
t|td | tt
t|td | tt	t
t|td qd S 	Nrg  r   r/  rh  r  ri  rw   r   )r%   r   rP  r  r\  ra  rc  rO  rc   r	   r"  r   r   r]   r  r_  re  r`  )r,   rP   r'   rW  r   r   r   r#    s   (z%TestVariousIteratorArgs.test_compressc                 C   sZ   ddt ddt dddfD ]}| ttt| | ttt| | ttt| qd S rf  )r%   r   r]   r   r  r_  re  r`  )r,   rP   r   r   r   r    s
   z$TestVariousIteratorArgs.test_productc                 C   s   ddt ddt dddfD ]I}ttttttfD ]!}t|d }t||d }tt	t
|||}| || q| tt
t| | tt
t| | ttt
t| qd S )	Nrg  r   r/  rh  r  ri  rw   rm   )r%   rP  r  r\  ra  rc  rO  r   r	   r8   rD  rc   r   r]   r  r_  re  r`  )r,   rP   rW  Ztgtlenexpectedactualr   r   r   rE    s   z"TestVariousIteratorArgs.test_cyclec                 C   s   t dt dt ddt dddfD ]=}ttttttfD ]}| dd	 t||D t	|| q| 
ttt| | 
ttt| | 
tt	tt| qd S )
NrK   r   r/  rv   rP  r  ri  rw   c                 S   r   r   r   )rS   r   Zsbr   r   r   rT     r   z8TestVariousIteratorArgs.test_groupby.<locals>.<listcomp>)r%   rP  r  r\  ra  rc  rO  rc   r   r	   r   r]   r  r_  re  r`  rj  r   r   r   rt    s   &(z$TestVariousIteratorArgs.test_groupbyc              	   C      t dt dt ddt dddfD ]A}ttttttfD ]}| tt	t
||dd	 ||D  q| tt	t
t| | tt	t
t| | ttt	t
t| qd S )
NrK   r   r/  rn  r  ri  rw   c                 S      g | ]}t |r|qS r   )r   r   r   r   r   rT     r   z7TestVariousIteratorArgs.test_filter.<locals>.<listcomp>)r%   rP  r  r\  ra  rc  rO  rc   r	   r  r   r   r]   r  r_  re  r`  rj  r   r   r   ry       &z#TestVariousIteratorArgs.test_filterc              	   C   ro  )
NrK   r   r/  rn  r  ri  rw   c                 S   rp  r   )r    r   r   r   r   rT     r   z<TestVariousIteratorArgs.test_filterfalse.<locals>.<listcomp>)r%   rP  r  r\  ra  rc  rO  rc   r	   r{  r   r   r]   r  r_  re  r`  rj  r   r   r   r}    rq  z(TestVariousIteratorArgs.test_filterfalsec              	   C   s   ddt ddt dddfD ]P}ttttttfD ](}| tt	||t
|| | tt	||||t
|||| q| tt	t| | tt	t| | ttt	t| qd S rf  )r%   rP  r  r\  ra  rc  rO  rc   r	   r
   r   r   r]   r  r_  re  r`  rj  r   r   r   r    s    .z TestVariousIteratorArgs.test_zipc              
   C   s   ddt ddt dddfD ]T}ttttttfD ],}| tt	||tt
|| | tt	||||tt
|||| q| tt	t| | tt	t| | ttt	t| qd S rf  )r%   rP  r  r\  ra  rc  rO  rc   r	   r  r
   r   r]   r  r_  re  r`  rj  r   r   r   r    s   $2z'TestVariousIteratorArgs.test_ziplongestc              
   C   s   t dt dt ddt dddfD ]Y}ttttttfD ].}| tt	t
||dd	 ||D  | tt	tj||||d
d	 ||D  q| tt	t
t| | tt	t
t| | ttt	t
t| qd S )NrK   r   r}   rn  r   r  rw   c                 S   r  r   )r   r   r   r   r   rT   &  r   z4TestVariousIteratorArgs.test_map.<locals>.<listcomp>c                 S      g | ]}|| qS r   r   r   r   r   r   rT   (  r   )r%   rP  r  r\  ra  rc  rO  rc   r	   r   r   r<   r  r   r]   r  r_  re  r`  rj  r   r   r   r  "  s   &z TestVariousIteratorArgs.test_mapc              
   C   s   ddt ddt dddfD ]E}ttttttfD ]}| tt	||dd d	t||dd d	  q| 
tt	t|d
 | 
tt	t|d
 | 
ttt	t|d
 qd S )NZ12345r   r/  rh  r  ri  rw   r   r   rK   )r%   rP  r  r\  ra  rc  rO  rc   r	   r8   r   r]   r  r_  re  r`  rj  r   r   r   r  -  s   2z#TestVariousIteratorArgs.test_islicec                 C   s   ddt ddt dddfD ]I}ttttttfD ]!}t||}tt||dd  }tt	||}| 
|| q| tt	t| | tt	t| | ttt	t| qd S rk  )r%   rP  r  r\  ra  rc  rO  r	   r
   r  rc   r   r]   r  r_  re  r`  )r,   rP   rW  r9   rl  rm  r   r   r   r  5  s   z%TestVariousIteratorArgs.test_pairwisec              	   C   s   t dt dt ddt dddfD ]J}ttttttfD ]}t||}| t	t
tj||dd	 ||D  q| tt
tjt| | tt
tjt| | tt	t
tjt| qd S )
NrK   r   r}   rn  r   r  rw   c                 S   rr  r   r   r   r   r   r   rT   E  r   z8TestVariousIteratorArgs.test_starmap.<locals>.<listcomp>)r%   rP  r  r\  ra  rc  rO  r   rc   r	   r  r<   r  r   r]   r  r_  re  r`  )r,   rP   rW  ssr   r   r   r  @  s   &
z$TestVariousIteratorArgs.test_starmapc              	   C   s   t dt dt ddt dddfD ]N}ttttttfD ]#}g }||D ]}t|s+ n|| q#| 	t
tt||| q| tttt| | tttt| | tt
ttt| qd S NrK   r   r/  rn  r  ri  rw   )r%   rP  r  r\  ra  rc  rO  r   ro  rc   r	   r  r   r]   r  r_  re  r`  r,   rP   rW  Ztgtrr  r   r   r   r  J  s   &z&TestVariousIteratorArgs.test_takewhilec              	   C   s   t dt dt ddt dddfD ]O}ttttttfD ]$}g }||D ]}|s,t|r,q#|| q#| 	t
tt||| q| tttt| | tttt| | tt
ttt| qd S rt  )r%   rP  r  r\  ra  rc  rO  r    ro  rc   r	   r  r   r]   r  r_  re  r`  ru  r   r   r   r  V  s   &z&TestVariousIteratorArgs.test_dropwhilec                 C   s   ddt ddt dddfD ]L}ttttttfD ]"}t||\}}| t	|t	|| | t	|t	|| q| 
ttt| | 
ttt| | 
tt	tt|d  qd S )	Nrg  r   r/  rh  r  ri  rw   r   )r%   rP  r  r\  ra  rc  rO  r  rc   r	   r   r]   r  r_  re  r`  )r,   rP   rW  Zit1Zit2r   r   r   r  b  s   z TestVariousIteratorArgs.test_teeN)r3   r4   r5   r   r   r#  r  rE  rt  ry  r}  r  r  r  r  r  r  r  r  r  r   r   r   r   rd    s$    						
rd  c                   @   r  )LengthTransparencyc                 C   sL   |  ttd dd |  ttd dd |  ttd dd d S )Nr  r   r   rc   r<   length_hintr   r+   r   r   r   r  n  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  rw  r+   r   r   r   r  s  s   z2LengthTransparency.test_repeat_with_negative_timesN)r3   r4   r5   r  r  r   r   r   r   rv  l  s    rv  c                   @   s:   e Zd Zdd Zdd Zej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 r   rG  )r=  r  r   zr   r   rW    s   
z@RegressionTests.test_sf_793826.<locals>.mutatingtuple.<locals>.g)r	   r   r
   r   re   )Ztuple1r   Ztuple2rW  itemsgenr   rz  r   mutatingtuple~  s   
z5RegressionTests.test_sf_793826.<locals>.mutatingtuplec                    s   | a tt  d d < d S r   )Tr	   rL  r  r   r   r     s   z)RegressionTests.test_sf_793826.<locals>.fr   )rV   rw   rn   )r	   r  rc   )r,   r~  r   secondr   r  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r   )ro  AssertionErrorr   Zhistr   r   gen1  s
   

z,RegressionTests.test_sf_950057.<locals>.gen1c                 3   s       d dV    d d S )Nrm   r   rV   )ro  r   r  r   r   gen2  s   
z,RegressionTests.test_sf_950057.<locals>.gen2Fr   r   T)r   r  r	   r   rc   rD  )r,   r  r  r   r  r   test_sf_950057  s   zRegressionTests.test_sf_950057c                 C   sP   t dd tdD }| t t| W d    d S 1 s!w   Y  d S )Nc                 s   s    | ]}d V  qdS )r   Nr   )rS   Zunusedr   r   r   r     r  zERegressionTests.test_long_chain_of_empty_iterables.<locals>.<genexpr>i )r   r   r%   r   r0   re   r   r   r   r   "test_long_chain_of_empty_iterables  s   
"z2RegressionTests.test_long_chain_of_empty_iterablesc                    s0    fdd}t td|D ]\} t  qd S )Nc                    s   | dkrt   | dkS )Nrw   rn   r  rA   r  r   r   r     s   z,RegressionTests.test_issue30347_1.<locals>.frK   )r   r%   r	   )r,   r   r   r   r  r   test_issue30347_1  s   
z!RegressionTests.test_issue30347_1c                    sJ   G  fddd}dt ttd|d  tdD ]}t  d  qd S )Nc                       r  )z,RegressionTests.test_issue30347_2.<locals>.Kc                 S   rb  r   r   rY  r   r   r   r    r#   z5RegressionTests.test_issue30347_2.<locals>.K.__init__c                    s   d7 dkrt  d  dS )Nr   TrG  )r,   otherrW  r   r   r   rh    s   
z3RegressionTests.test_issue30347_2.<locals>.K.__eq__N)r3   r4   r5   r  rh  r   r  r   r   K  r  r  r   rK   r   r   )re   r   r%   )r,   r  r   r   r  r   test_issue30347_2  s   	z!RegressionTests.test_issue30347_2N)
r3   r4   r5   r  r  r   Zskip_if_pgo_taskr  r  r  r   r   r   r   ry  y  s    
ry  c                   @   s   e Zd Zdd ZdS )SubclassWithKwargsTestc                    s~   t ttttttttt	t
tfD ]. G  fddd }z|dd W q ty< } z| d|jd  W Y d }~qd }~w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 | g|R   d S r   )r  )r,   newargr   clsr   r   r    s   zKSubclassWithKwargsTest.test_keywords_in_subclass.<locals>.Subclass.__init__r   )r3   r4   r5   r  r   r  r   r   Subclass  s    r  r   )r  zkeyword argumentsr   )r   r
   r  r{  r   r   r  r8   r  r  rD  r"  r]   ZassertNotInr   )r,   r  errr   r  r   test_keywords_in_subclass  s   z0SubclassWithKwargsTest.test_keywords_in_subclassN)r3   r4   r5   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_tr+   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   rK   )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 NZ3Pnir   rm   rK   rV   )r   r  r  r   r  r%   r  r   r   r   test_combinations_sizeof  s   
"z#SizeofTest.test_combinations_sizeofc                 C   sP   t }td}| j}||dd|d| j   ||tdd|d| j   d S r  )r   r   r  r  r  r%   )r,   r   r  r  r   r   r   )test_combinations_with_replacement_sizeof  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4Pnir   rV   rm   r   rw   rK   )r   r  r  r   r  r%   r  r   r   r   test_permutations_sizeof  s   


z#SizeofTest.test_permutations_sizeofN)
r3   r4   r5   r  r   r  r  r  r  r  r   r   r   r   r    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 pad_none(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 grouper(iterable, n, *, incomplete='fill', fillvalue=None):
...     "Collect data into non-overlapping fixed-length chunks or blocks"
...     # grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx
...     # grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError
...     # grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
...     args = [iter(iterable)] * n
...     if incomplete == 'fill':
...         return zip_longest(*args, fillvalue=fillvalue)
...     if incomplete == 'strict':
...         return zip(*args, strict=True)
...     if incomplete == 'ignore':
...         return zip(*args)
...     else:
...         raise ValueError('Expected fill, strict, or ignore')

>>> def triplewise(iterable):
...     "Return overlapping triplets from an iterable"
...     # pairwise('ABCDEFG') -> ABC BCD CDE DEF EFG
...     for (a, _), (b, c) in pairwise(pairwise(iterable)):
...         yield a, b, c

>>> import collections
>>> def sliding_window(iterable, n):
...     # sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
...     it = iter(iterable)
...     window = collections.deque(islice(it, n), maxlen=n)
...     if len(window) == n:
...         yield tuple(window)
...     for x in it:
...         window.append(x)
...         yield tuple(window)

>>> 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 partition(pred, iterable):
...     "Use a predicate to partition entries into false entries and true entries"
...     # partition(is_odd, range(10)) --> 0 2 4 6 8   and  1 3 5 7 9
...     t1, t2 = tee(iterable)
...     return filterfalse(pred, t1), filter(pred, t2)

>>> def before_and_after(predicate, it):
...     ''' Variant of takewhile() that allows complete
...         access to the remainder of the iterator.
...
...         >>> all_upper, remainder = before_and_after(str.isupper, 'ABCdEfGhI')
...         >>> str.join('', all_upper)
...         'ABC'
...         >>> str.join('', remainder)
...         'dEfGhI'
...
...         Note that the first iterator must be fully
...         consumed before the second iterator can
...         generate valid results.
...     '''
...     it = iter(it)
...     transition = []
...     def true_iterator():
...         for elem in it:
...             if predicate(elem):
...                 yield elem
...             else:
...                 transition.append(elem)
...                 return
...     def remainder_iterator():
...         yield from transition
...         yield from it
...     return true_iterator(), remainder_iterator()

>>> 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(islice(pad_none('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('abcdefg', 3, fillvalue='x'))
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]

>>> it = grouper('abcdefg', 3, incomplete='strict')
>>> next(it)
('a', 'b', 'c')
>>> next(it)
('d', 'e', 'f')
>>> next(it)
Traceback (most recent call last):
  ...
ValueError: zip() argument 2 is shorter than argument 1

>>> list(grouper('abcdefg', n=3, incomplete='ignore'))
[('a', 'b', 'c'), ('d', 'e', 'f')]

>>> list(triplewise('ABCDEFG'))
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]

>>> list(sliding_window('ABCDEFG', 4))
[('A', 'B', 'C', 'D'), ('B', 'C', 'D', 'E'), ('C', 'D', 'E', 'F'), ('D', 'E', 'F', 'G')]

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

>>> def is_odd(x):
...     return x % 2 == 1

>>> evens, odds = partition(is_odd, range(10))
>>> list(evens)
[0, 2, 4, 6, 8]
>>> list(odds)
[1, 3, 5, 7, 9]

>>> it = iter('ABCdEfGhI')
>>> all_upper, remainder = before_and_after(str.isupper, it)
>>> ''.join(all_upper)
'ABC'
>>> ''.join(remainder)
'dEfGhI'

>>> 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 }tt|D ]}t	j
|  |  t ||< q&t| t	tjt |  d S )Ngettotalrefcountr   rw   )rU   rd  r8  ry  rv  r  r  r3  r  r   Zrun_unittesthasattrr7  r  r%   r   r  r  r  Zrun_doctestmodulesr3   )verboseZtest_classesr  countsr   r   r   r   	test_main
  s    


r  __main__T)r  r   )@Zunittesttestr   	itertoolsr  decimalr   Z	fractionsr   r<   r  r   rM   	functoolsr   r7  r  r  r  ZMAX_Py_ssize_tr4  Zminsizer   r   r   r   r   r    r"   r(   r*   r:   r@   rB   rG   rI   rL   r%   r   r   ZTestCaserU   r  r3  r8  rO  rP  r  r\  r  r_  r`  ra  rc  rd  rv  ry  r  r  r  r  Z__test__r  r3   r   r   r   r   <module>   s    
            2n*i	
	 '\&   


