a
    zeޕ                    @   sv  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rre>dQdR dS )T    N)support)*)Decimal)Fraction)reduce   c                  G   s   t t|  S N)listzipargs r   )/usr/lib/python3.9/test/test_itertools.pylzip   s    r   c                 C   s   d|  S )zTest function of one argument   r   xr   r   r   onearg   s    r   c                  G   s   t dS )z"Test function that raises an errorN)
ValueErrorr   r   r   r   errfunc   s    r   c                  c   s   dD ]
} | V  qdS )zNon-restartable source sequencer   r   r   Nr   ir   r   r   gen3   s    r   c                 C   s   | d dkS )Test predicater   r   r   r   r   r   r   isEven$   s    r   c                 C   s   | d dkS )r   r   r   r   r   r   r   r   isOdd(   s    r   c                  G   s   | S r   r   r   r   r   r   tupleize,   s    r   c                 c   s   t | D ]
}|V  qd S r   rangenr   r   r   r   irange/   s    r"   c                   @   s    e Zd ZdZdd Zdd ZdS )StopNowz"Class emulating an empty iterable.c                 C   s   | S r   r   selfr   r   r   __iter__5   s    zStopNow.__iter__c                 C   s   t d S r   StopIterationr$   r   r   r   __next__7   s    zStopNow.__next__N)__name__
__module____qualname____doc__r&   r)   r   r   r   r   r#   3   s   r#   c                 C   s   t t|| S )zHConvenience function for partially consuming a long of infinite iterabler	   islice)r!   seqr   r   r   take:   s    r1   c                 C   s   t tj| dS Nr   )r   operatormuliterabler   r   r   prod>   s    r7   c                 C   s   t td| d S )Z	Factorialr   )r7   r   r!   r   r   r   factA   s    r9   c                 C   s   | d S Nr   r   rr   r   r   testRF   s    r=   c                 C   s   | d S Nr   r   r;   r   r   r   testR2I   s    r?   c                 C   s   | dk S )N
   r   r   r   r   r   undertenL   s    rA   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       z<listcomp>.<lambda>r   ).0rF   r   r   r   
<listcomp>O   s   rJ   c                   @   s(  e Zd ZdoddZdd Zdd	 Zd
d Zdd Zdd Zdd Z	e
jdd Ze
ddd Zdd Ze
jdd Ze
ddd Zdd Ze
jdd  Ze
dd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Ze
dd7d8 Zd9d: Z e
dd;d< Z!d=d> Z"d?d@ Z#dAdB Z$dCdD Z%e
jdEdF Z&e
ddGdH Z'dIdJ Z(dKdL Z)dMdN Z*dOdP Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1d]d^ Z2d_d` Z3dadb Z4dcdd Z5e
j6dedf Z7e
j6dgdh Z8e
j6didj Z9e
j6dkdl Z:e
j6dmdn Z;dS )pTestBasicOps   r   Nc                    s  d fdd	 t ||}t |}| t|t|  | | }}	| ||	 |rp |}
| ||
 t |}d}z"t|D ]}t| |d7 }qW n ty   Y n0 t ||}t |} | | }}	| ||	 |r
 ||d }
| ||
 dS )zITest that an iterator is the same after pickling, also when part-consumedr   c                    s\    dkrt dt| tr| S ztt| }W n tyF   |  Y S 0  fdd|D S )Nr@   zinfinite recursion encounteredc                    s   g | ]} |d  qS )r   r   rI   e)expandr   r   r   rJ   `   rH   z;TestBasicOps.pickletest.<locals>.expand.<locals>.<listcomp>)RuntimeError
isinstancestrr	   r/   	TypeError)itr   lrO   stopr   r   rO   V   s    

z'TestBasicOps.pickletest.<locals>.expandr   N)r   )rB   rD   rC   assertEqualtyper   nextr(   )r%   protocolrT   rW   r1   comparedumpZi2abcZi3Ztookr   Zi4r   rV   r   
pickletestT   s0    


zTestBasicOps.pickletestc              	   C   sx  |  tttdg d |  tttddg d ttttfD ]0}|  ttt|tdtt|g d qF|  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 s0    Y  ttjd
 D ]2}| |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    n1 sj0    Y  d S )Nr@   )
r   r         r@            $   -   r5   abc)r^   abri         rc   r   r   )
r      	   rl   rk   r   rb   rL   r   rc   )
r   r   r   r   r   r   r   r   r   r   )
r   rm   rn   rn   rn   rn   rn   rn   rn   rn   )
r         i  i  r   r   r   r   r   )initial)r@   rl   r   )r@   rd   ro   d   )rr   n   s   t      )rX   r	   
accumulater   intcomplexr   r   mapassertRaisesrS   minmaxr3   r4   chrrB   HIGHEST_PROTOCOLra   )r%   typrE   rF   r   r   r   test_accumulate}   sL    .  zTestBasicOps.test_accumulatec                 C   s   dd }t |fD ]z}| t|ddtd | t|dtd | t|dg  | td|ddtd | tt|d	d
 qd S )Nc                  w   s   | D ]}|D ]
}|V  qqdS )zPure python version in the docsNr   )	iterablesrT   elementr   r   r   chain2   s    z'TestBasicOps.test_chain.<locals>.chain2ri   defabcdef rL   abcdr   rb   )chainrX   r	   r1   r{   rS   )r%   r   r`   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 )	Nri   r   r   r   rL   r   r   rb   )rX   r	   r   from_iterabler1   r{   rS   r$   r   r   r   test_chain_from_iterable   s
     z%TestBasicOps.test_chain_from_iterablec              	   C   s   t jgt D ]}tdd}| t||td | t|d | t||td | t|tdg  | td|tddtd | t	t|td	d
 qt
tjd D ]}| j|tddtdd qd S )Nri   r   r   r^   Zbcdefr   rL   r   r   rb   r   r\   )copydeepcopypicklecopiersr   rX   r	   rZ   r1   r{   rS   r   rB   r   ra   )r%   ZoperrT   rF   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   ri   r   )r^   r_   r`   drN   fghi)r   r^   r_   r`   r   rN   r   )r{   rS   r   __setstate__iterrX   r	   r%   rT   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 qLdd }dd }dd }tdD ]}dd t|D t|d D ]Z}tt|}| t	|||krtdnt
|t
| t
||   | t	|t	t| | |t| |D ]x | 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qHq$d S )Nri   r   r   c                 S   s   | S r   r   r^   r   r   r   rG      rH   z0TestBasicOps.test_combinations.<locals>.<lambda>    ABCDABr   Cr   Dr   r   r   r   r   r   )r   r   r   r   r   rL   rb   r   r   r   rb   r   r   rb   r   r   rb   )r   r   r   c                 3   s   t |  t }||krdS tt|}t  fdd|D V  tt|D ]}|| || | krL qnqLdS ||  d7  < t|d |D ]}||d  d ||< qt  fdd|D V  q@dS )%Pure python version shown in the docsNc                 3   s   | ]} | V  qd S r   r   rI   r   poolr   r   	<genexpr>   rH   zHTestBasicOps.test_combinations.<locals>.combinations1.<locals>.<genexpr>r   c                 3   s   | ]} | V  qd S r   r   r   r   r   r   r      rH   tuplelenr	   r   reversed)r6   r<   r!   indicesr   jr   r   r   combinations1   s    z5TestBasicOps.test_combinations.<locals>.combinations1c                 3   sP   t |  t }tt||D ],}t|t|krt  fdd|D V  qdS )r   c                 3   s   | ]} | V  qd S r   r   r   r   r   r   r     rH   zHTestBasicOps.test_combinations.<locals>.combinations2.<locals>.<genexpr>N)r   r   permutationsr   sortedr	   r6   r<   r!   r   r   r   r   combinations2   s
    z5TestBasicOps.test_combinations.<locals>.combinations2c                 3   sP   t |  t }tt||D ],}tt||krt  fdd|D V  qdS )zPure python version from cwr()c                 3   s   | ]} | V  qd S r   r   r   r   r   r   r   	  rH   zHTestBasicOps.test_combinations.<locals>.combinations3.<locals>.<genexpr>N)r   r   combinations_with_replacementr   setr   r   r   r   combinations3  s
    z5TestBasicOps.test_combinations.<locals>.combinations3rk   c                 S   s   g | ]}d | d qS rl      r   rI   r   r   r   r   rJ     rH   z2TestBasicOps.test_combinations.<locals>.<listcomp>r   c                 3   s   | ]}| v V  qd S r   r   rM   valuesr   r   r     rH   z1TestBasicOps.test_combinations.<locals>.<genexpr>c                    s   g | ]}| v r|qS r   r   rM   r`   r   r   rJ     rH   )r{   rS   combinationsr   r   rX   r	   rZ   r   r   r9   r   r   
assertTrueallrB   r   ra   )
r%   optestIntermediater   r   r   r!   r<   resultrF   r   r`   r   r   test_combinations   sZ    
6
zTestBasicOps.test_combinationsc                 C   s<   |  ttf tdd W d    n1 s.0    Y  d S )NAAi    )r{   OverflowErrorMemoryErrorr   r$   r   r   r   test_combinations_overflow   s    z'TestBasicOps.test_combinations_overflowz"tuple reuse is specific to CPythonc              
   C   sH   |  tttttddd | ttttttddd d S Nabcderb   r   )rX   r   r   rz   idr   assertNotEqualr	   r$   r   r   r   test_combinations_tuple_reuse&  s     z*TestBasicOps.test_combinations_tuple_reusec              	      sf  t }| t|d | t|ddd | t|d  | t|dd dd gt D ]L}| t||ddg d |dd}t| | t||g d	 qPd
d }dd }dd }tdD ]}dd t|D t|d D ]z}t||}	| t	|	||| | t	|	t	t
|	 | |	t|	 tt|}
|dksV|dkrd| |	|
 n| t
|	t
|
k |	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 ]}| ||| qDqqd S )Nri   r   r   r   c                 S   s   | S r   r   r   r   r   r   rG   2  rH   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rJ qhqJdS || d g||  ||d< t  fdd|D V  q>dS )r   Nr   c                 3   s   | ]} | V  qd S r   r   r   r   r   r   r   C  rH   zPTestBasicOps.test_combinations_with_replacement.<locals>.cwr1.<locals>.<genexpr>r   c                 3   s   | ]} | V  qd S r   r   r   r   r   r   r   K  rH   )r   r   r   r   )r6   r<   r!   r   r   r   r   r   cwr1;  s    
z=TestBasicOps.test_combinations_with_replacement.<locals>.cwr1c                 3   sR   t |  t }tt||dD ],}t|t|kr t  fdd|D V  q dS )r   repeatc                 3   s   | ]} | V  qd S r   r   r   r   r   r   r   S  rH   zPTestBasicOps.test_combinations_with_replacement.<locals>.cwr2.<locals>.<genexpr>N)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   )r9   )r!   r<   r   r   r   numcombsU  s    zATestBasicOps.test_combinations_with_replacement.<locals>.numcombsrk   c                 S   s   g | ]}d | d qS r   r   r   r   r   r   rJ   [  rH   zCTestBasicOps.test_combinations_with_replacement.<locals>.<listcomp>r   c                 S   s   g | ]\}}|qS r   r   )rI   kvr   r   r   rJ   k  rH   c                 3   s   | ]}| v V  qd S r   r   rM   r   r   r   r   n  rH   zBTestBasicOps.test_combinations_with_replacement.<locals>.<genexpr>c                    s   g | ]}| v r|qS r   r   rM   r   r   r   rJ   p  rH   )r   r{   rS   r   r   rX   r	   rZ   r   r   r   r   r   r   groupbyr   rB   r   ra   )r%   cwrr   r   r   r   r   r!   r<   r   Zregular_combsZnorunsrF   r   r   r   "test_combinations_with_replacement+  sR    
z/TestBasicOps.test_combinations_with_replacementc                 C   s<   |  ttf tdd W d    n1 s.0    Y  d S )Nr      @)r{   r   r   r   r$   r   r   r   +test_combinations_with_replacement_overfloww  s    z8TestBasicOps.test_combinations_with_replacement_overflowc              
   C   sL   t }| tttt|ddd | ttttt|ddd d S r   )r   rX   r   r   rz   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rdnt|t||   | t|tt	| | |t
| |D ]F}| t|| | tt	|| | t fdd|D  q,| |t| | | |t| | ||kr| |tt d  | |tt  ttjd D ]}| |t | qqqd S )Nri   r   r   r   r   rE   rb   )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  |r6tt|D ]}||  d8  < || dkr||d d |||d   ||d< || ||< q|| }||  ||  ||< || < t  fdd|d| D V   q|qdS q|dS )r   Nr   c                 3   s   | ]} | V  qd S r   r   r   r   r   r   r     rH   zHTestBasicOps.test_permutations.<locals>.permutations1.<locals>.<genexpr>r   c                 3   s   | ]} | V  qd S r   r   r   r   r   r   r     rH   r   )r6   r<   r!   r   Zcyclesr   r   r   r   r   permutations1  s&    $ ( z5TestBasicOps.test_permutations.<locals>.permutations1c                 3   sb   t |  t }|du r|n|}tt||dD ],}tt||kr0t  fdd|D V  q0dS )r   Nr   c                 3   s   | ]} | V  qd S r   r   r   r   r   r   r     rH   zHTestBasicOps.test_permutations.<locals>.permutations2.<locals>.<genexpr>)r   r   r   r   r   r   r   r   r   permutations2  s    z5TestBasicOps.test_permutations.<locals>.permutations2rk   c                 S   s   g | ]}d | d qS r   r   r   r   r   r   rJ     rH   z2TestBasicOps.test_permutations.<locals>.<listcomp>r   c                 3   s   | ]}| v V  qd S r   r   rM   r   r   r   r     rH   z1TestBasicOps.test_permutations.<locals>.<genexpr>)N)N)r{   rS   r   r   rX   r	   r   r   r9   r   r   r   r   rB   r   ra   )r%   r   r   r!   r<   r   prF   r   r   r   test_permutations  s:    

	, 
zTestBasicOps.test_permutationsc                 C   s<   |  ttf tdd W d    n1 s.0    Y  d S )Nr   r   )r{   r   r   r   r$   r   r   r   test_permutations_overflow  s    z'TestBasicOps.test_permutations_overflowc              
   C   sH   |  tttttddd | ttttttddd d S r   )rX   r   r   rz   r   r   r   r	   r$   r   r   r   test_permutations_tuple_reuse  s     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||rt|  d t  t|d  n   | t| |krdnt|t|    | t| |krd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 )Nrc   ABCDEFGrm   r   r   r   c                 S   s    g | ]}t |t|kr|qS r   r   r	   rI   tr   r   r   rJ     rH   z3TestBasicOps.test_combinatorics.<locals>.<listcomp>c                    s    g | ]}t t| kr|qS r   r   r   r   r;   r   r   rJ     rH   c                 S   s    g | ]}t |t|kr|qS r   r   r   r   r   r   rJ     rH   c                    s    g | ]}t t| kr|qS r   r   r   r;   r   r   rJ     rH   )r   r	   r   r   r   r   rX   r   r9   r   r   filter__contains__)r%   r!   rE   r7   r   permcombr   r;   r   test_combinatorics  s,    :,4zTestBasicOps.test_combinatoricsc              
   C   s0  |  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 ]}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||}|rt
| |  t||t| qq\d 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'  rc   r   r   rb   rl   c                 S   s
   t  | S r   )r   r   r   r   r   rG     rH   z,TestBasicOps.test_compress.<locals>.<lambda>c                 S   s
   t | S r   )r   r   r   r   r   r   rG     rH   ZCEFZBCDEFr   )rX   r	   compressr   r   r   r   r{   rS   r   rZ   )r%   r!   r   r  r   Zresult1Zresult2r   r   r   r   test_compress  s6      
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'|}|  || qddtd td fD ]`}t|}|  tt|| |  tt|| t	tjd D ]}| |t| q$qtdtd  t  d S )(Nri   r^   r   r_   r   r`   r   rb   )r^   rb   r_   rL   )r`   rl   r   r  r  r   )r^   r   )r_   r   )r^   r  )r_   r   rL   r^   r@   rl         
@)r  g      @g      @      
@      )r  y      @            @      1.1z2.1z3.1rm   r     zcount(3)zcount(4)z	count(-9)ig     $@zcount(10.25)g      $@zcount(10.0)r   	count(%r))rX   r   countr1   r
   r{   rS   maxsizer	   r   r   r   reprrZ   rY   floatsys__mod__r   r   rB   r   ra   exc_info)r%   BIGINTr`   r   r1r2valuerF   r   r   r   
test_count  sZ       $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 ]}tj d tj d d/ddddtjd tjd f	D ]h}tt||}|dkrd0| }nd1||f }|  || ttjd D ]}| |t|| q2qqd S )2Nri   r   rb   )r^   r   )r_   rl   )r`   rm   )startstepr   )r%  )r	  )r_   r   )r`   r   r^   r_   r   )r#  )r_   r   r  r   )r#  )r_   rb   )r`   rL   rv   rd   rr   r@   rl   g      ?)r   r  g      @r  )r   r  y      !@       r  z.1z1.2z1.3rk      re   r  g      @)r@   g      )@g      .@zcount(3, 5)zcount(8, 5)r  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)r  r  zcount(%r, %r))rX   r   r  r{   rS   r1   r  r   r	   r   r   r  rZ   rY   rx   r  r  rB   r   ra   )r%   r  r`   r   r   r  r   rF   r   r   r   test_count_with_stride?  sp    28"$



46

z#TestBasicOps.test_count_with_stridec                    s  |  tdtdtd |  ttdg  | tt | ttd |  tttt dg d td |  t d |  tdt	
 td ttjd	 D ]d}|  tdtt |td t  |  tdtt |td
 t  t  qttjd	 D ]}| |td qttjd	 D ]}td}t|  fddtdD }t |}t|}|  td|td td}t|  fddtdD }t |}t|}|  td|td qDd S )Nr@   ri   Z
abcabcabcar   rl   )
r   r   r   r   r   r   r   r   r   r   r^   Z
bcabcabcabr   Z
cabcabcabcr   c                    s   g | ]}t  qS r   rZ   r   r   r   r   rJ     rH   z+TestBasicOps.test_cycle.<locals>.<listcomp>r   rv   Zcdeabcdeabcdeabcdeabc                    s   g | ]}t  qS r   r(  r   r   r   r   rJ     rH   rk   )rX   r1   cycler	   r{   rS   r/   r   rZ   r   r   r   rB   r   rC   rD   ra   r   )r%   rF   rT   _r   r   r   r   r   
test_cyclev  sB     


zTestBasicOps.test_cyclec                 C   s^  t d}|tddf | td|td t d}|tddf | td|td | t& t dtddg W d    n1 s0    Y  | t* t d}|tddf W d    n1 s0    Y  td| | t& t dtddf W d    n1 s&0    Y  | tt d	jd
 | tt d	jg f d S )Ndefgri   r   rv   Zdefgabcdefgabcdefgababcdefgr   r   r   r   )r)  r   r	   rX   r1   r{   rS   r   )r%   r`   r   r   r   test_cycle_setstate  s     40
6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  || qq~|  || ttj	d	 D ]Z}g }t
tt|t|D ],\}}|D ]}|  ||d  || qq|  || qg }t|tD ]V\}}t|tD ]@\}}|D ]0}|  ||d  |  ||d
  || qNqBq0|  || ttj	d	 D ]}g }t
tt|t|D ]d\}}t
tt|t|D ]@\}}|D ]0}|  ||d  |  ||d
  || qqq|  || 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 ]J}t|t}t|\}}t| t| |  tt
t||g  qd}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keyri   c                 S   s   | S r   r   r   r   r   r   rG     rH   z+TestBasicOps.test_groupby.<locals>.<lambda>r@   ))r   r@   rv   )r      re   )r   r   re   )r      re   )r         )r   rd   r4  )rb   ro      )rb   r&  r5  c                 S   s   | d S r:   r   r;   r   r   r   rG     rH   r   r   r   c                 S   s   g | ]\}}|qS r   r   rI   r   gr   r   r   rJ     rH   z-TestBasicOps.test_groupby.<locals>.<listcomp>c                 S   s   g | ]}|d  qS )r   r   )rI   r<   r   r   r   rJ     rH   Z	AABBBAAAArn   )r   rl   Zabracadabrac                 S   s   g | ]\}}|qS r   r   r6  r   r   r   rJ     rH   )r^   r_   r`   r   r<   c                 S   s$   g | ]\}}t t|d dr|qS r   r.   r6  r   r   r   rJ     rH   )r^   r_   r<   c                 S   s    g | ]\}}t t||fqS r   r   r	   r6  r   r   r   rJ     rH   )rl   r^   r   r_   )r   r`   )r   r   r   r<   c                 S   s    g | ]\}}t t||fqS r   r8  r6  r   r   r   rJ     rH   T)reverserb   )r9  r;  r:  c                   @   s   e Zd ZdS )z0TestBasicOps.test_groupby.<locals>.ExpectedErrorN)r*   r+   r,   r   r   r   r   ExpectedError  s   r=  c                 3   s   t | D ]
}dV  q d S )NZyor   r    r=  r   r   delayed_raise!  s    z0TestBasicOps.test_groupby.<locals>.delayed_raisec                    s    fddt | |D S )Nc                    s   g | ]\}} |qS r   r   r6  funcr   r   rJ   &  rH   z;TestBasicOps.test_groupby.<locals>.gulp.<locals>.<listcomp>)r   )r6   ZkeyprA  r   r@  r   gulp%  s    z'TestBasicOps.test_groupby.<locals>.gulpc                       s   e Zd Z fddZdS )z+TestBasicOps.test_groupby.<locals>.DummyCmpc                    s    d S r   r   )r%   dstr>  r   r   __eq__/  s    z2TestBasicOps.test_groupby.<locals>.DummyCmp.__eq__N)r*   r+   r,   rD  r   r>  r   r   DummyCmp.  s   rE  r@  c                    s$   j dkr j d8  _ | S  d S r   )skip)objr=  keyfuncr   r   rI  9  s    
z*TestBasicOps.test_groupby.<locals>.keyfunc)r   )rX   r	   r   r   r{   rS   appendr   rB   r   rC   rD   r=   r?   r   r   r
   rZ   r   	ExceptionrF  )r%   rE   dupr   r7  elemrF   ZikZigkeysZexpectedkeysrT   r*  Zg1Zg2Zg3r<   r?  rB  rE  r   rH  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   q:ttjd D ] }tttd}| || qd S )Nrc   )r   r   rL   r   r   r   r   r   r   r   rL   )r   r   rL   rc   c                 S   s   | S r   r   r   r   r   r   rG   L  rH   z*TestBasicOps.test_filter.<locals>.<lambda>c                 S   s   | S r   r   r   r   r   r   rG   M  rH   rk   rb   )rX   r	   r   r   r   boolr1   r  r{   rS   rZ   r   r   rB   r   rC   rD   ra   r%   ansr`   rF   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 qd S )Nrc   r  rP  r   r   r   rL   )r   rb   rl   rk   c                 S   s   | S r   r   r   r   r   r   rG   g  rH   z/TestBasicOps.test_filterfalse.<locals>.<lambda>c                 S   s   | S r   r   r   r   r   r   rG   h  rH   rk   rb   r   )rX   r	   filterfalser   r   rQ  r1   r  r{   rS   rZ   rB   r   ra   r%   rF   r   r   r   test_filterfalsea  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   s   g | ]\}}||fqS r   r   rI   r   yr   r   r   rJ   p  rH   z)TestBasicOps.test_zip.<locals>.<listcomp>ri   r  rc   r   rb   c                 S   s   g | ]}t t|qS r   r   r	   rI   Zpairr   r   r   rJ   y  rH   r   c                 S   s   g | ]}|qS r   r   r\  r   r   r   rJ   {  rH   )	r
   r  rX   r	   r   r   r1   r{   rS   )r%   rS  r   r   r   test_zipn  s    $$$zTestBasicOps.test_zipc              	   C   s~  t tttdd}| t|t| t ttt tdd}| tt	|t| dd t

tdt D }| |g d dd t
tdt D }| |g d ttjd D ]8}dd tttdt |D }| |g d qttjd D ]F}tdt }t| d	d tt||D }| |d
dg qttjd D ]}| |tdt  q^d S )Nri   r   c                 S   s   g | ]\}}||fqS r   r   rY  r   r   r   rJ     rH   z5TestBasicOps.test_zip_tuple_reuse.<locals>.<listcomp>r  c                 S   s   g | ]\}}||fqS r   r   rY  r   r   r   rJ     rH   r   c                 S   s   g | ]\}}||fqS r   r   rY  r   r   r   rJ     rH   c                 S   s   g | ]\}}||fqS r   r   rY  r   r   r   rJ     rH   r
  r  )r	   rz   r   r
   rX   r|   r}   r   dictfromkeysr   r  r   r   rB   r   rC   rD   rZ   ra   )r%   idsrS  rF   r   r   r   r   test_zip_tuple_reuse~  s$    $z!TestBasicOps.test_zip_tuple_reusec                    sX  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| qz| 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 n0 | d|  q| dd tddD tt
dd | dd tddD tt
dd d S )Nri   rc   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   )rI   argr   r   r   rJ     rH   z;TestBasicOps.test_ziplongest.<locals>.<listcomp>.<listcomp>r   )rI   r   r   r   rJ     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   rM   r   r   r   r     rH   z:TestBasicOps.test_ziplongest.<locals>.<listcomp>.<genexpr>re  r   r   r   r   rJ     rH   rf  	fillvaluerb   r   r,  )zzip_longest('abc', fv=1)z3zip_longest('abc', fillvalue=1, bogus_keyword=None)zDid not raise Type in:  c                 S   s   g | ]}t t|qS r   r[  r\  r   r   r   rJ     rH   r   c                 S   s   g | ]}|qS r   r   r\  r   r   r   rJ     rH   )ri   r,  )r   r}   rz   r   rX   r	   zip_longestr^  r1   r  r
   r{   rS   evalglobalslocalsZfail)r%   targetZstmtr   r   r   test_ziplongest  sD    

"(
"(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 )Nri   r   )
r	   rz   r   ri  rX   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 ]P}| |tdd | |tdd | |tdddd | |tdd qd S )Nr   ri   r   Zdefghrg  r   )r   rB   r   ra   ri  rW  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 sB0    Y  | |j  d S )Nc                       s   e Zd Z fddZdS )z?TestBasicOps.test_zip_longest_bad_iterable.<locals>.BadIterablec                    s    d S r   r   r$   	exceptionr   r   r&     s    zHTestBasicOps.test_zip_longest_bad_iterable.<locals>.BadIterable.__iter__N)r*   r+   r,   r&   r   rq  r   r   BadIterable  s   rs  )rS   r{   ri  ZassertIsrr  )r%   rs  cmr   rq  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   )orx   r   rN   )r%   rv  r   rN   r   r   r   __init__  s    
z5TestBasicOps.test_bug_7244.<locals>.Repeater.__init__c                 S   s   | S r   r   r$   r   r   r   r&     s    z5TestBasicOps.test_bug_7244.<locals>.Repeater.__iter__c                 S   s(   | j dkr|  j d8  _ | jS | jd S r   )r   rv  rN   r$   r   r   r   r)     s    
z5TestBasicOps.test_bug_7244.<locals>.Repeater.__next__N)r*   r+   r,   rw  r&   r)   r   r   r   r   Repeater  s   rx  r   rb   r   rL   c              	   S   sd   g }t | |ddD ]L\}}td t||f W d    n1 sF0    Y  |||f q|S )Nr   rg  stdout)ri  r   Zcaptured_outputprintrJ  )r  r   r   r   r   r   r   r   run  s    *z'TestBasicOps.test_bug_7244.<locals>.run)r   r   r   r   r   rg  r   )r(   rX   rP   ri  rZ   r{   )r%   rx  r  r   r{  rT   r   r   r   test_bug_7244  s    zTestBasicOps.test_bug_7244c                    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 ]V\}}| tt| | t d	D ]0}| tt||  tt|i t|d
 qqv| 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 ]} fddt t
dD }ttt|}| ttt| | | tt| t||  | tt| t||  tt|}| ttt| | qTd S )Nr   rj   r   r_   r   rb   )r   r   r   r   r   )r   r   r   r   rL   r   rk   rc   i c                     s   t tt| |dd }t|}|dkr4dV  d S tdd |D rJd S dg| }tdd t||D V  tt|D ]h}|| t|| d krqz||  d7  < t|d |D ]}d||< qtdd t||D V   qnqzd S qnd S )	Nr   r   r   r   c                 s   s   | ]}t |d kV  qdS )r   Nrc  )rI   r   r   r   r   r     rH   z>TestBasicOps.test_product.<locals>.product1.<locals>.<genexpr>c                 s   s   | ]\}}|| V  qd S r   r   rI   r   r   r   r   r   r     rH   c                 s   s   | ]\}}|| V  qd S r   r   r  r   r   r   r   &  rH   )	r	   rz   r   getr   anyr
   r   r   )r   kwdspoolsr!   r   r   r   r   r   r   product1  s$    

z+TestBasicOps.test_product.<locals>.product1c                  ?   sT   t tt| |dd }g g}|D ]  fdd|D }q$|D ]}t|V  q@dS )z Pure python version used in docsr   r   c                    s    g | ]} D ]}||g qqS r   r   rY  r   r   r   rJ   0  rH   z?TestBasicOps.test_product.<locals>.product2.<locals>.<listcomp>N)r	   rz   r   r  )r   r  r  r   r7   r   r   r   product2+  s    z+TestBasicOps.test_product.<locals>.product2r   ri   r   )r^   r_   r`   r-  r1  r2  rr   c                    s   g | ]}t  qS r   )randomchoice)rI   r   argtypesr   r   rJ   7  rH   z-TestBasicOps.test_product.<locals>.<listcomp>rl   )r   rX   r	   r   r^  r   r{   rS   r   r   r  	randranger7   rz   r   )r%   r   r   r<   r  r  r   Zexpected_lenr   r  r   test_product  s6    "	
zTestBasicOps.test_productc                 C   sF   |  ttf$ tdgd ddi W d    n1 s80    Y  d S )Nrj   r   r   i   )r{   r   r   r   r$   r   r   r   test_product_overflow?  s    z"TestBasicOps.test_product_overflowc              
   C   sH   |  tttttddd | ttttttddd d S )Nri   r   r   )rX   r   r   rz   r   r   r   r	   r$   r   r   r   test_product_tuple_reuseD  s     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 ]`\}}| ttt| | | ttt| | t tjd	 D ]}| |t|  qqvd S )
Nr   rj   r   r}  r   rb   r~  r   r   )	r   rX   r	   r   r   r   rB   r   ra   )r%   r   r   rF   r   r   r   test_product_picklingI  s    z"TestBasicOps.test_product_picklingc                 C   sL   t dd}|d | t|d t ddd}|d | tt| d S )Nr   )rb   )r      )r   rb   r   )r   r   r  )r   r   rX   rZ   r{   r(   )r%   r   r   r   r   test_product_issue_25021X  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 qtd S )Nr^   rb   )objecttimes)r^   r^   r^   ))r   r^   )r   r^   )r   r^   r   r  rL   y      ?        zrepeat((1+0j))rl   zrepeat((1+0j), 5)zrepeat((1+0j), 0)r@   r   Zaar   )rX   r	   r   r   r   r1   r{   rS   r  rZ   r   r   rB   r   ra   )r%   r<   r`   rF   r   r   r   test_repeatb  s.    
zTestBasicOps.test_repeatc                 C   s`   |  ttddd |  ttddd |  ttdddd |  ttdddd d S )Nr^   r   zrepeat('a', 0)r   r  )rX   r  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 )Nrb   r   rk   r   r   rm   ri   rl   r  r   r	  r
  r@   rL   )rX   r	   rz   r3   powr   r   r  r1   r{   rS   negrZ   r   r   r   r   r   rB   r   ra   rR  r   r   r   test_map  s6    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 )Nrb   r   rk   r  rL   rl   i   rL   rl   extrar@   )rX   r	   starmapr3   r  r
   r   r1   r  r   r{   rS   rZ   r   r   r   r   r   rB   r   ra   rR  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 ]0\}}|  tttdg|R  tt|  q6|  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 ]}|  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   qBqdd dD }t|}t|d}| |  t| t  | |  G dd dt}|  tttd|dttd |  tttd|d|dttdd |  tttd|d|d|dttddd d S )N)r@   rv   rb   r@   rb   rv   r@   rv   )r@   r@   r@   rb   rv   rr   )))r@   rs   rb   )r@   rr   rb   ))r@   rs   )r@   rr   ))rs   )rr   r@   r   r   rb   rL   r   r   r^   2   )r  r  r  r  r  c                 s   s   | ]
}|V  qd S r   r   r   r   r   r   r     rH   z+TestBasicOps.test_islice.<locals>.<genexpr>r   c                   @   s   e Zd Zdd Zdd ZdS )z)TestBasicOps.test_islice.<locals>.IntLikec                 S   s
   || _ d S r   valr%   r  r   r   r   rw    s    z2TestBasicOps.test_islice.<locals>.IntLike.__init__c                 S   s   | j S r   r  r$   r   r   r   	__index__  s    z3TestBasicOps.test_islice.<locals>.IntLike.__index__N)r*   r+   r,   rw  r  r   r   r   r   IntLike  s   r  rl   )rX   r	   r/   r   r   r{   rS   r   r   r  r  rZ   r   r   rB   r   ra   weakrefrefZassertIsNotNoner   
gc_collectZassertIsNoner  )	r%   r   ZtgtargsrT   Zrar`   rF   wrr  r   r   r   test_islice  sv    

"$&&*  
 
$

&$zTestBasicOps.test_islicec                 C   s2  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| qd S )	Nr   rb   rl   rv   r   rL   rc   rm   r  r  r  r@   )r   r   r   r   r   r   r   r   r   r   )rX   r	   	takewhilerA   r{   rS   r3   r  rZ   r   r   rQ  r(   r   r   r   rB   r   ra   )r%   r   r   rF   r   r   r   test_takewhile  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| qd S )Nr  )rv   r   rL   rc   rm   r  r  r@   r   )rX   r	   	dropwhilerA   r{   rS   r3   r  rZ   r   r   r   r   r   rB   r   ra   )r%   r   rF   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 ]}dg| dg|  }t| g g f}t t|}|D ] }t|| }	|| 	|	 qv| |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|| qX| 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 ]P}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 ]F}| |tt d	 t d	\}}| j|||d | j|||d qd S )N   rr   rl   r   r   rb   r   r   ri   r   rb  invalidr   c                 S   s   g | ]}t |qS r   r	   r   r   r   r   rJ   ~  rH   z)TestBasicOps.test_tee.<locals>.<listcomp>r@   	__class__r  <   r   )teerX   r	   r"   r   r   rZ   r  shufflerJ  r{   rS   rY   r   r   r   r   r  proxygetattrr   r  ReferenceErrorr   r1   r   rB   r   ra   r   )r%   r!   r^   r_   r   r   orderZlistsZitsr!  r`   r   r   t1t2ZtnewZt3r   rS  Zlong_ansrF   r   r   r   test_tee>  s     
 
$<








zTestBasicOps.test_teec                 C   s8   t td d\}}zt| ~W n   ~~ Y n0 d S )Ni -1)r  r   r  )r%   ZforwardZbackwardr   r   r   test_tee_del_backward  s    z"TestBasicOps.test_tee_del_backwardc                    sX   G  fddd}t | \} | td t| W d    n1 sJ0    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   s   | S r   r   r$   r   r   r   r&     s    z1TestBasicOps.test_tee_reenter.<locals>.I.__iter__c                    s   | j }d| _ |rt S d S NF)firstrZ   )r%   r  r}  r   r   r)     s    z1TestBasicOps.test_tee_reenter.<locals>.I.__next__N)r*   r+   r,   r  r&   r)   r   r}  r   r   I  s   r  r  )r  assertRaisesRegexrP   rZ   )r%   r  r^   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}|  zP  | td t| W d    n1 s~0    Y  W  	  |
  n 	  |
  0 d S )Nc                       s"   e Zd Zdd Z fddZdS )z+TestBasicOps.test_tee_concurrent.<locals>.Ic                 S   s   | S r   r   r$   r   r   r   r&     s    z4TestBasicOps.test_tee_concurrent.<locals>.I.__iter__c                    s         d S r   )r   waitr$   Zfinishr$  r   r   r)     s    z4TestBasicOps.test_tee_concurrent.<locals>.I.__next__N)r*   r+   r,   r&   r)   r   r  r   r   r    s   r  )rm  r   r  )	threadingEventr  ThreadrZ   r$  r  r  rP   r   join)r%   r  r^   r_   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 ]6}|  tt|dd g  |  tt|dd t  qd S )Nr   c                 S   s   | S r   r   r   r   r   r   rG     rH   z1TestBasicOps.test_StopIteration.<locals>.<lambda>c                 S   s   | S r   r   r   r   r   r   rG     rH   )r{   r(   rZ   r
   r   r)  r   r#   r/   r  r   r   rV  rz   r  r  r  )r%   r   r   qr   r   r   test_StopIteration  s     zTestBasicOps.test_StopIterationc                 C   s6   t d g gd}t| t  | tt| d S r2   )r   rZ   gccollectr   
is_trackedr   r   r   r   test_combinations_result_gc  s    z(TestBasicOps.test_combinations_result_gcc                 C   s6   t d g gd}t| t  | tt| d S r2   )r   rZ   r  r  r   r  r   r   r   r   ,test_combinations_with_replacement_result_gc%  s    z9TestBasicOps.test_combinations_with_replacement_result_gcc                 C   s6   t d g gd}t| t  | tt| d S r2   )r   rZ   r  r  r   r  r   r   r   r   test_permutations_result_gc-  s    z(TestBasicOps.test_permutations_result_gcc                 C   s4   t d g g}t| t  | tt| d S r   )r   rZ   r  r  r   r  r   r   r   r   test_product_result_gc5  s    z#TestBasicOps.test_product_result_gcc                 C   s*   t g g}t  | tt| d S r   )ri  r  r  r   r  rZ   r   r   r   r   test_zip_longest_result_gc=  s    
z'TestBasicOps.test_zip_longest_result_gc)rL   r   N)<r*   r+   r,   ra   r   r   r   r   r   r   r   Zbigaddrspacetestr   Zimpl_detailr   r   r   r   r   r   r   r   r  r"  r'  r+  r.  rO  rT  rX  r]  ra  rn  ro  rp  ru  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   rK   R   s   
)#P

L

?

!"07*$ 
*
(:


^ 	



rK   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   rb   rL   rl   r   rb   rc   r@   rd   )rX   r	   rw   r$   r   r   r   r   G  s    zTestExamples.test_accumulatec              
   C   s   g d}g d}t tjd D ]h}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   rB   r   rw   rX   r	   rC   rD   rZ   r   r   )r%   r   ZaccumulatedrF   rT   r   r   r   test_accumulate_reducibleJ  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)rw   r3   is_rX   rZ   r   rB   r   rC   rD   r	   r   r   )r%   rT   rF   Zit_copyr   r   r   test_accumulate_reducible_noneY  s    z+TestExamples.test_accumulate_reducible_nonec                 C   s   |  dtddd d S Nr   r   ZDEFr   )rX   r  r   r$   r   r   r   r   c  s    zTestExamples.test_chainc                 C   s    |  dtddgd d S r  )rX   r  r   r   r$   r   r   r   r   f  s    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   rL   rb   r   )rX   r	   r   r   r$   r   r   r   r   i  s    zTestExamples.test_combinationsc                 C   s   |  ttddg d d S )Nr   r   r   )rX   r	   r   r$   r   r   r   r   o  s    z/TestExamples.test_combinations_with_replacementc                 C   s"   |  ttdg dtd d S )Nr   r   r  )rX   r	   r  r$   r   r   r   r  s  s    zTestExamples.test_compressc                 C   s"   |  tttddg d d S )Nr@   rl   )r@   r1  r   r2  r3  )rX   r	   r/   r  r$   r   r   r   r"  v  s    zTestExamples.test_countc                 C   s"   |  tttddtd d S )Nr   r   ZABCDABCDABCD)rX   r	   r/   r)  r$   r   r   r   r+  y  s    zTestExamples.test_cyclec                 C   s&   |  ttdd g dg d d S )Nc                 S   s   | dk S Nrl   r   r   r   r   r   rG   }  rH   z-TestExamples.test_dropwhile.<locals>.<lambda>r   rL   rc   rL   r   )rc   rL   r   )rX   r	   r  r$   r   r   r   r  |  s    zTestExamples.test_dropwhilec                 C   sT   |  dd tdD td |  dd tdD tdtdtd	td
g d S )Nc                 S   s   g | ]\}}|qS r   r   r6  r   r   r   rJ     rH   z-TestExamples.test_groupby.<locals>.<listcomp>ZAAAABBBCCDAABBBZABCDABc                 S   s   g | ]\}}t |qS r   r  r6  r   r   r   rJ     rH   Z
AAAABBBCCDZAAAAZBBBZCCr   )rX   r   r	   r$   r   r   r   rO    s    zTestExamples.test_groupbyc                 C   s&   |  ttdd tdg d d S )Nc                 S   s   | d S r>   r   r   r   r   r   rG     rH   z*TestExamples.test_filter.<locals>.<lambda>r@   )r   rb   rl   rk   rn   )rX   r	   r   r   r$   r   r   r   rT    s    zTestExamples.test_filterc                 C   s&   |  ttdd tdg d d S )Nc                 S   s   | d S r>   r   r   r   r   r   rG     rH   z/TestExamples.test_filterfalse.<locals>.<lambda>r@   )r   r   rL   rc   rm   )rX   r	   rV  r   r$   r   r   r   rX    s    zTestExamples.test_filterfalsec                 C   s    |  tttddg d d S )N)r   rb   r@   )rl   r   rb   r   rn   r  )rX   r	   rz   r  r$   r   r   r   r    s    zTestExamples.test_mapc                 C   st   |  ttddtd |  ttdddtd |  ttddd td |  ttddd dtd d S )	Nr   r   ABrL   CDCDEFGr   ACEG)rX   r	   r/   r$   r   r   r   r    s    zTestExamples.test_islicec                 C   s   |  ttddddg d S )Nr   xyr   r   r   rZ  )rX   r	   r
   r$   r   r   r   r]    s    zTestExamples.test_zipc                 C   s"   |  ttddddg d d S )Nr   r  -rg  )r  r  )r   r  )r   r  )rX   r	   ri  r$   r   r   r   test_zip_longest  s    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 DCrb   )r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )rX   r	   r   rz   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   rb   r   )rU  )r   r   r   )r   r   r   )r   r   r   )r   r   r   r  )r   r   r   r  )rX   r	   r   rz   r   r  r   r$   r   r   r   r    s    zTestExamples.test_productc                 C   s   |  ttddg d d S )Nr@   rb   )r@   r@   r@   )rX   r	   r   r$   r   r   r   r    s    zTestExamples.test_repeatc                 C   s"   |  tttg dg d d S )N))r   rl   )rb   r   r  r  )rX   r	   r  r  r$   r   r   r   test_stapmap  s    zTestExamples.test_stapmapc                 C   s&   |  ttdd g dddg d S )Nc                 S   s   | dk S r  r   r   r   r   r   rG     rH   z-TestExamples.test_takewhile.<locals>.<lambda>r  r   rL   )rX   r	   r  r$   r   r   r   r    s    zTestExamples.test_takewhileN)r*   r+   r,   r   r  r  r   r   r   r   r  r"  r+  r  rO  rT  rX  r  r  r]  r  r   r  r  r  r  r   r   r   r   r  E  s.   
r  c                   @   s    e Zd Zedd Zdd ZdS )TestPurePythonRoughEquivalentsc           
      g   s   t | }|jpd|jptj|jp"d  }}}tt|||}zt|}W n, t	yt   t
t|| D ]\}}	qdY d S 0 z,t| D ]\}}	||kr|	V  t|}qW n0 t	y   t
t|d || D ]\}}	qY n0 d S r   )slicer$  rW   r  r  r%  r   r   rZ   r(   r
   	enumerate)
r6   r   rE   r$  rW   r%  rT   Znextir   r   r   r   r   r/     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  rL   r  r  r   r  r@   rb   r   r  )rX   r	   r/   r   r   r  rZ   )r%   rT   r`   r   r   r   test_islice_recipe  s      z1TestPurePythonRoughEquivalents.test_islice_recipeN)r*   r+   r,   staticmethodr/   r  r   r   r   r   r    s   
r  c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/S )0TestGCc                 C   s   | | t| ~~d S r   )rJ  rZ   )r%   iterator	containerr   r   r   	makecycle  s    
zTestGC.makecyclec                 C   s    g }|  tdd|dg| d S Nr   r   rb   )r  rw   r%   r^   r   r   r   r     s    zTestGC.test_accumulatec                 C   s   g }|  t|| d S r   )r  r   r  r   r   r   r     s    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   s"   g }|  tdd|dgd| d S r  )r  r   r  r   r   r   r     s    zTestGC.test_combinationsc                 C   s"   g }|  tdd|dgd| d S r  )r  r   r  r   r   r   r     s    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    s    zTestGC.test_compressc                 C   s6   g }t dtft|d}| t|d|d| d S )NIntr   r   r   )rY   rx   r^  r  r  )r%   r^   r  r   r   r   r"    s    zTestGC.test_countc                 C   s   g }|  t|gd | d S r>   )r  r)  r  r   r   r   r+    s    zTestGC.test_cyclec                 C   s    g }|  ttd||g| d S r:   )r  r  rQ  r  r   r   r   r    s    zTestGC.test_dropwhilec                 C   s$   g }|  t|gd dd | d S )Nr   c                 S   s   | S r   r   r   r   r   r   rG     rH   z%TestGC.test_groupby.<locals>.<lambda>)r  r   r  r   r   r   rO  	  s    zTestGC.test_groupbyc                 C   s>   d}dd }t t||dD ]\}}|jdg | qd S )Nr@   c                 S   s   | S r   r   r   r   r   r   rG     rH   z'TestGC.test_issue2246.<locals>.<lambda>r/  r   )r   r   __dict__
setdefaultrJ  )r%   r!   rI  r   r   r   r   r   test_issue2246  s    zTestGC.test_issue2246c                 C   s$   g }|  tdd |gd | d S )Nc                 S   s   dS )NTr   r   r   r   r   rG     rH   z$TestGC.test_filter.<locals>.<lambda>r   )r  r   r  r   r   r   rT    s    zTestGC.test_filterc                 C   s   g }|  tdd || d S )Nc                 S   s   dS r  r   r   r   r   r   rG     rH   z)TestGC.test_filterfalse.<locals>.<lambda>)r  rV  r  r   r   r   rX    s    zTestGC.test_filterfalsec                 C   s&   g }|  t|gd |gd | d S )Nr   rb   )r  r
   r  r   r   r   r]    s    zTestGC.test_zipc                 C   sP   g }|  t|gd |gd | |d g}|  t|gd |gd |d| d S )Nr   rb   rg  )r  ri  )r%   r^   r_   r   r   r   r     s    zTestGC.test_zip_longestc                 C   s$   g }|  tdd |gd | d S )Nc                 S   s   | S r   r   r   r   r   r   rG   (  rH   z!TestGC.test_map.<locals>.<lambda>r   )r  rz   r  r   r   r   r  &  s    zTestGC.test_mapc                 C   s    g }|  t|gd d | d S r>   )r  r/   r  r   r   r   r  *  s    zTestGC.test_islicec                 C   s"   g }|  tdd|dgd| d S r  )r  r   r  r   r   r   r   .  s    zTestGC.test_permutationsc                 C   s$   g }|  tdd|dgdd| d S )Nr   r   rb   r   )r  r   r  r   r   r   r  2  s    zTestGC.test_productc                 C   s   g }|  t|| d S r   )r  r   r  r   r   r   r  6  s    zTestGC.test_repeatc                 C   s(   g }|  tdd ||fgd | d S )Nc                  W   s   | S r   r   r   r   r   r   rG   <  rH   z%TestGC.test_starmap.<locals>.<lambda>r   )r  r  r  r   r   r   r  :  s    zTestGC.test_starmapc                 C   s"   g }|  ttdd||g| d S )Nr   r   )r  r  rQ  r  r   r   r   r  >  s    zTestGC.test_takewhileN)r*   r+   r,   r  r   r   r   r   r   r  r"  r+  r  rO  r   rT  rX  r]  r  r  r  r   r  r  r  r  r   r   r   r   r    s.   r  c                 c   s   | D ]
}|V  qdS )zRegular generatorNr   seqnr   r   r   r   RB  s    r  c                   @   s    e Zd ZdZdd Zdd ZdS )GzSequence using __getitem__c                 C   s
   || _ d S r   r  r%   r  r   r   r   rw  I  s    z
G.__init__c                 C   s
   | j | S r   r  )r%   r   r   r   r   __getitem__K  s    zG.__getitem__N)r*   r+   r,   r-   rw  r  r   r   r   r   r  G  s   r  c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r  z Sequence using iterator protocolc                 C   s   || _ d| _d S r:   r  r  r   r   r   rw  P  s    z
I.__init__c                 C   s   | S r   r   r$   r   r   r   r&   S  s    z
I.__iter__c                 C   s2   | j t| jkrt| j| j  }|  j d7  _ |S r2   r   r   r  r(   r%   r   r   r   r   r)   U  s    z
I.__next__Nr*   r+   r,   r-   rw  r&   r)   r   r   r   r   r  N  s   r  c                   @   s    e Zd ZdZdd Zdd ZdS )Igz9Sequence using iterator protocol defined with a generatorc                 C   s   || _ d| _d S r:   r  r  r   r   r   rw  ]  s    zIg.__init__c                 c   s   | j D ]
}|V  qd S r   r  r  r   r   r   r&   `  s    
zIg.__iter__Nr*   r+   r,   r-   rw  r&   r   r   r   r   r  [  s   r  c                   @   s    e Zd ZdZdd Zdd ZdS )rf  z Missing __getitem__ and __iter__c                 C   s   || _ d| _d S r:   r  r  r   r   r   rw  f  s    z
X.__init__c                 C   s2   | j t| jkrt| j| j  }|  j d7  _ |S r2   r	  r
  r   r   r   r)   i  s    z
X.__next__N)r*   r+   r,   r-   rw  r)   r   r   r   r   rf  d  s   rf  c                   @   s    e Zd ZdZdd Zdd ZdS )NzIterator missing __next__()c                 C   s   || _ d| _d S r:   r  r  r   r   r   rw  q  s    z
N.__init__c                 C   s   | S r   r   r$   r   r   r   r&   t  s    z
N.__iter__Nr  r   r   r   r   r  o  s   r  c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	EzTest propagation of exceptionsc                 C   s   || _ d| _d S r:   r  r  r   r   r   rw  y  s    z
E.__init__c                 C   s   | S r   r   r$   r   r   r   r&   |  s    z
E.__iter__c                 C   s   dd  d S )Nrb   r   r   r$   r   r   r   r)   ~  s    z
E.__next__Nr  r   r   r   r   r  w  s   r  c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	SzTest immediate stopc                 C   s   d S r   r   r  r   r   r   rw    s    z
S.__init__c                 C   s   | S r   r   r$   r   r   r   r&     s    z
S.__iter__c                 C   s   t d S r   r'   r$   r   r   r   r)     s    z
S.__next__Nr  r   r   r   r   r    s   r  c                 C   s   t tdd ttt| S )z Test multiple tiers of iteratorsc                 S   s   | S r   r   r   r   r   r   rG     rH   zL.<locals>.<lambda>)r   rz   r  r  r  r  r   r   r   L  s    r  c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!S )"TestVariousIteratorArgsc                 C   s   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   r  r  r  r  r  rX   r	   rw   r  r{   rS   rf  r  ZeroDivisionErrorr  )r%   rE   r<   r!   r7  r   r   r   r     s    z'TestVariousIteratorArgs.test_accumulatec              	   C   s   ddt ddt dddfD ]}ttttttfD ]V}| 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?rb    rl   )r   r  r  r  r  r  r  rX   r	   r   r{   rS   rf  r  r  r  r%   rE   r7  r   r   r   r     s     4z"TestVariousIteratorArgs.test_chainc              	   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dt	|| q6| tt
t|td | tt
t|td | tt	t
t|td qd S )	Nr  r   r  r  rb  r  rl   r   )r   r   r  r  r  r  r  r  rX   r	   r  r   r{   rS   rf  r  r  r  )r%   rE   r!   r7  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 r  )r   r{   rS   r   rf  r  r  r  )r%   rE   r   r   r   r    s    z$TestVariousIteratorArgs.test_productc                 C   s   ddt ddt dddfD ]}ttttttfD ]B}t|d }t||d }tt	t
|||}| || q.| tt
t| | tt
t| | ttt
t| qd S )	Nr  r   r  r  rb  r  rl   rb   )r   r  r  r  r  r  r  r   r	   r/   r)  rX   r{   rS   rf  r  r  r  )r%   rE   r7  ZtgtlenZexpectedactualr   r   r   r+    s    z"TestVariousIteratorArgs.test_cyclec                 C   s   t dt dt ddt dddfD ]z}ttttttfD ]*}| dd	 t||D t	|| q6| 
ttt| | 
ttt| | 
tt	tt| q"d S )
Nr@   r   r  rk   r1  rb  r  rl   c                 S   s   g | ]\}}|qS r   r   )rI   r   Zsbr   r   r   rJ     rH   z8TestVariousIteratorArgs.test_groupby.<locals>.<listcomp>)r   r  r  r  r  r  r  rX   r   r	   r{   rS   rf  r  r  r  r  r   r   r   rO    s    &(z$TestVariousIteratorArgs.test_groupbyc              	   C   s   t dt dt ddt dddfD ]}ttttttfD ],}| tt	t
||dd	 ||D  q6| tt	t
t| | tt	t
t| | ttt	t
t| q"d S )
Nr@   r   r  r  rb  r  rl   c                 S   s   g | ]}t |r|qS r   )r   r   r   r   r   rJ     rH   z7TestVariousIteratorArgs.test_filter.<locals>.<listcomp>)r   r  r  r  r  r  r  rX   r	   r   r   r{   rS   rf  r  r  r  r  r   r   r   rT    s    &z#TestVariousIteratorArgs.test_filterc              	   C   s   t dt dt ddt dddfD ]}ttttttfD ],}| tt	t
||dd	 ||D  q6| tt	t
t| | tt	t
t| | ttt	t
t| q"d S )
Nr@   r   r  r  rb  r  rl   c                 S   s   g | ]}t |r|qS r   )r   r   r   r   r   rJ     rH   z<TestVariousIteratorArgs.test_filterfalse.<locals>.<listcomp>)r   r  r  r  r  r  r  rX   r	   rV  r   r{   rS   rf  r  r  r  r  r   r   r   rX    s    &z(TestVariousIteratorArgs.test_filterfalsec              	   C   s   ddt ddt dddfD ]}ttttttfD ]P}| tt	||t
|| | tt	||||t
|||| q.| tt	t| | tt	t| | ttt	t| qd S r  )r   r  r  r  r  r  r  rX   r	   r
   r   r{   rS   rf  r  r  r  r  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fD ]X}| tt	||tt
|| | tt	||||tt
|||| q.| tt	t| | tt	t| | ttt	t| qd S r  )r   r  r  r  r  r  r  rX   r	   ri  r
   r{   rS   rf  r  r  r  r  r   r   r   rn    s    $2z'TestVariousIteratorArgs.test_ziplongestc              
   C   s   t dt dt ddt dddfD ]}ttttttfD ]\}| tt	t
||dd	 ||D  | tt	tj||||d
d	 ||D  q6| tt	t
t| | tt	t
t| | ttt	t
t| q"d S )Nr@   r   rr   r  rv   r  rl   c                 S   s   g | ]}t |qS r   )r   r   r   r   r   rJ     rH   z4TestVariousIteratorArgs.test_map.<locals>.<listcomp>c                 S   s   g | ]}|| qS r   r   r   r   r   r   rJ     rH   )r   r  r  r  r  r  r  rX   r	   rz   r   r3   r  r{   rS   rf  r  r  r  r  r   r   r   r    s    &z TestVariousIteratorArgs.test_mapc              
   C   s   ddt ddt dddfD ]}ttttttfD ]4}| 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  r  rb  r  rl   r   r   r@   )r   r  r  r  r  r  r  rX   r	   r/   r{   rS   rf  r  r  r  r  r   r   r   r    s    2z#TestVariousIteratorArgs.test_islicec              	   C   s   t dt dt ddt dddfD ]}ttttttfD ]8}t||}| t	t
tj||dd	 ||D  q6| tt
tjt| | tt
tjt| | tt	t
tjt| q"d S )
Nr@   r   rr   r  rv   r  rl   c                 S   s   g | ]}|| qS r   r   r   r   r   r   rJ     rH   z8TestVariousIteratorArgs.test_starmap.<locals>.<listcomp>)r   r  r  r  r  r  r  r   rX   r	   r  r3   r  r{   rS   rf  r  r  r  )r%   rE   r7  ssr   r   r   r    s    &
z$TestVariousIteratorArgs.test_starmapc              	   C   s   t dt dt ddt dddfD ]}ttttttfD ]F}g }||D ]}t|sV qb|| qF| 	t
tt||| q6| tttt| | tttt| | tt
ttt| q"d S Nr@   r   r  r  rb  r  rl   )r   r  r  r  r  r  r  r   rJ  rX   r	   r  r{   rS   rf  r  r  r  r%   rE   r7  ZtgtrM  r   r   r   r    s    &z&TestVariousIteratorArgs.test_takewhilec              	   C   s   t dt dt ddt dddfD ]}ttttttfD ]H}g }||D ]}|sXt|rXqF|| qF| 	t
tt||| q6| tttt| | tttt| | tt
ttt| q"d S r  )r   r  r  r  r  r  r  r   rJ  rX   r	   r  r{   rS   rf  r  r  r  r  r   r   r   r    s    &z&TestVariousIteratorArgs.test_dropwhilec                 C   s   ddt ddt dddfD ]}ttttttfD ]D}t||\}}| t	|t	|| | t	|t	|| q.| 
ttt| | 
ttt| | 
tt	tt|d  qd S )	Nr  r   r  r  rb  r  rl   r   )r   r  r  r  r  r  r  r  rX   r	   r{   rS   rf  r  r  r  )r%   rE   r7  Zit1Zit2r   r   r   r     s    z TestVariousIteratorArgs.test_teeN)r*   r+   r,   r   r   r  r  r+  rO  rT  rX  r]  rn  r  r  r  r  r  r  r   r   r   r   r    s    						
r  c                   @   s   e Zd Zdd Zdd ZdS )LengthTransparencyc                 C   sL   |  ttd dd |  ttd dd |  ttd dd d S )Nr  r   r   rX   r3   length_hintr   r$   r   r   r   r  ,  s    zLengthTransparency.test_repeatc                 C   sh   |  ttd dd |  ttd dd |  ttd ddd |  ttd ddd d S )Nr   r   r   r  r  r$   r   r   r   r  1  s    z2LengthTransparency.test_repeat_with_negative_timesN)r*   r+   r,   r  r  r   r   r   r   r  *  s   r  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   r(  )r!  r  r   zr   r   r7  @  s    
z@RegressionTests.test_sf_793826.<locals>.mutatingtuple.<locals>.g)r	   rz   r
   r   rZ   )Ztuple1r   Ztuple2r7  itemsgenr   r"  r   mutatingtuple<  s    
z5RegressionTests.test_sf_793826.<locals>.mutatingtuplec                    s   | a tt  d d < d S r   )Tr	   r  r  r   r   r   K  s    z)RegressionTests.test_sf_793826.<locals>.fr   )rL   rl   rc   )r	   r'  rX   )r%   r&  r   secondr   r(  r   test_sf_7938269  s    zRegressionTests.test_sf_793826c                    s    fdd} fdd}g  |  ttt| |d |  ddg g  |  ttt| |d |  ddg g  |  ttt|  |  ddg d S )	Nc                   3   s,     d dV    d t  d d S )Nr   r   r   )rJ  AssertionErrorr   Zhistr   r   gen1Z  s
    

z,RegressionTests.test_sf_950057.<locals>.gen1c                 3   s     d dV    d d S )Nrb   r   rL   )rJ  r   r,  r   r   gen2a  s    
z,RegressionTests.test_sf_950057.<locals>.gen2Fr   r   T)r{   r+  r	   r   rX   r)  )r%   r-  r.  r   r,  r   test_sf_950057V  s    zRegressionTests.test_sf_950057c                 C   sN   t dd tdD }| t t| W d    n1 s@0    Y  d S )Nc                 s   s   | ]
}d V  qdS )r   Nr   )rI   Zunusedr   r   r   r   w  rH   zERegressionTests.test_long_chain_of_empty_iterables.<locals>.<genexpr>i )r   r   r   r{   r(   rZ   r   r   r   r   "test_long_chain_of_empty_iterablesr  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 )Nrl   rc   r  r8   r}  r   r   r   |  s    z,RegressionTests.test_issue30347_1.<locals>.fr@   )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  q6d S )Nc                       s"   e Zd Zdd Z fddZdS )z,RegressionTests.test_issue30347_2.<locals>.Kc                 S   s   d S r   r   r
  r   r   r   rw    s    z5RegressionTests.test_issue30347_2.<locals>.K.__init__c                    s   d7 dkrt  d  dS )Nr   Tr(  )r%   otherr7  r   r   r   rD    s    
z3RegressionTests.test_issue30347_2.<locals>.K.__eq__N)r*   r+   r,   rw  rD  r   r3  r   r   K  s   r4  r   r@   r   r   )rZ   r   r   )r%   r4  r   r   r3  r   test_issue30347_2  s
    	z!RegressionTests.test_issue30347_2N)
r*   r+   r,   r*  r/  r   Zskip_if_pgo_taskr0  r1  r5  r   r   r   r   r!  7  s   
r!  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yx } z| d|jd  W Y d }~qd }~0 0 q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   )rw  )r%   newargr   clsr   r   rw    s    zKSubclassWithKwargsTest.test_keywords_in_subclass.<locals>.Subclass.__init__)N)r*   r+   r,   rw  r   r8  r   r   Subclass  s   r:  r   )r7  zkeyword argumentsr   )r   r
   r   rV  r   rz   r  r/   r  r  r)  r  rS   ZassertNotInr   )r%   r:  errr   r8  r   test_keywords_in_subclass  s    z0SubclassWithKwargsTest.test_keywords_in_subclassN)r*   r+   r,   r<  r   r   r   r   r6    s   r6  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3Pirj   Z12r   )
ri   ri   ri   ri   ri   ri   ri   ri   ri   ri   r@   )r   calcobjsizecheck_sizeofr   r@  r%   basesizecheckr   r   r   test_product_sizeof  s    
zSizeofTest.test_product_sizeofc                 C   sL   t d}| j}|tdd|d| j   |ttdd|d| j   d S NZ3Pnir   rb   r@   rL   )r   rB  rC  r   r@  r   rD  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 rH  )r   r   rB  rC  r@  r   )r%   r   rE  rF  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   rL   rb   r   rl   r@   )r   rB  rC  r   r@  r   rD  r   r   r   test_permutations_sizeof  s    


z#SizeofTest.test_permutations_sizeofN)
r*   r+   r,   rA  r   rC  rG  rI  rJ  rK  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 pairwise(iterable):
...     "s -> (s0,s1), (s1,s2), (s2, s3), ..."
...     a, b = tee(iterable)
...     try:
...         next(b)
...     except StopIteration:
...         pass
...     return zip(a, b)

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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



libreftestc              	   C   s   t ttttttttf	}t	j
|  | rxttdrxdd l}d gd }tt|D ]"}t	j
|  |  t ||< qLt| t	tjt |  d S )Ngettotalrefcountr   rl   )rK   r  r  r!  r  r6  r  r  r=  r   Zrun_unittesthasattrr  r  r   r   r  rM  rz  Zrun_doctestmodulesr*   )verboseZtest_classesr  countsr   r   r   r   	test_main
  s     


rR  __main__T)rP  )N)@Zunittesttestr   	itertoolsr  decimalr   Z	fractionsr   r3   r  r   rB   	functoolsr   r  r>  r  r  ZMAX_Py_ssize_tr  Zminsizer   r   r   r   r   r   r   r"   r#   r1   r7   r9   r=   r?   rA   r   r   r   ZTestCaserK   r  r  r  r  r  r  r  rf  r  r  r  r  r  r  r!  r6  r  r=  rL  Z__test__rR  r*   r   r   r   r   <module>   s   
           ~n*e	
	 \&  ;

