B
    u9ag]                 @   sV   d dl Z d dlZd dlZd dlZdd Zdd ZG dd de jZedkrRe 	  dS )	    Nc             c   s@   | | | dk r<|| | | 7 }x| |kr:| V  | |7 } q"W d S )Nr    )startstopstepr   r    /usr/lib/python3.7/test_range.pypyrange	   s
    
r   c             C   s&   || | | 7 }t || | | | S )N)r   )r   r   r   r   r   r   pyrange_reversed   s    r   c               @   s   e Zd Zd6d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d0d1 Zd2d3 Zd4d5 ZdS )7	RangeTestNc       
   	   C   s   |d k	r t ||}t ||}t }t j|||d}xvt|D ]j\}\}}	||	krXq@q@||krv| d|||	 q@|	|kr| d||| q@| d|||	| q@W d S )N)	fillvaluez;{}: iterator ended unexpectedly at position {}; expected {}z/{}: unexpected excess element {} at position {}z5{}: wrong element at position {}; expected {}, got {})	itertoolsisliceobjectzip_longest	enumerateZfailformat)
selfZxsZystest_idlimitsentinelZpairsixyr   r   r   assert_iterators_equal   s     z RangeTest.assert_iterators_equalc             C   s  |  ttddddg |  ttddddddg |  ttdg  |  ttdg  |  ttdddddd	g |  ttdd
dddddg d}d}d}|  tt||d ||d g |  tt|d |d|d |d g |  tt|d |d|d |d g tt|||}| || | || |  t|d tt||| }| || | || |  t|d tt| | | }| | | | | | |  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dd | ttdd | ttddd |  ttdt	j
t	j
d d tt	j
 t	j
d}|  t|t	j
 d S )N   r               
      d   2   g        g       @g      ?g}Ô%ITg\nTspam*   )assertEquallistrangeassertInassertNotInlenassertRaises	TypeError
ValueErrorsysmaxsize)r   abcseqrr   r   r   
test_range-   sH     "(( zRangeTest.test_rangec             C   s  t ddd}| t|d | tt|d t ddd}| t|d | tt|d | | t ddd}| t|d | tt|d | | t ddd}| t|d | tt|d | | xPt dd  t ddd  t ddd d	gD ] }| t|g  | | qW td
tj }tdtj }tdtj }| tt ||d ||d g | tt |d |d	|d |d g | tt |d |d|d |d g tt |||}| 	|| | 
|| | t|d | |d | | |d	 ||  tt ||| }| 	|| | 
|| | t|d | |d | | |d	 ||  tt | | | }| 	| | | 
| | | t|d | |d |  | |d	 | |  d S )Nl      Fx:^V l   
  Fx:^V r   r   r   r   r   r$   r"   r   r%   r   r&   )r+   r)   r.   r*   ZassertFalse
assertTrueintr2   r3   r,   r-   )r   r   r4   r5   r6   r7   r   r   r   test_large_operands]   sX    


"((zRangeTest.test_large_operandsc          	   C   s  dd }t j }t j}|| }t||}| || | || | tt| | | | 	||| | 	|d | t jd }| 	|| ||  | 	|||d  d ||  | t
 || d   W d Q R X | t
 ||  W d Q R X d}dt j }|| }t||}| || | || | tt| | | | 	||| | 	|d | t jd }| 	|| ||  | 	|||d  d ||  | t
 || d   W d Q R X | t
 ||  W d Q R X d}t jd }dt j }d|| |  }t|||}| || | || | tt| | | | 	||| | 	|d | t jd }| 	|| |||   | 	|||d  d |||   | t
 || d   W d Q R X | t
 ||  W d Q R X t jd }d}dt j }d|| |  }t|||}| || | || | tt| | | | 	||| | 	|d | t jd }| 	|| |||   | 	|||d  d |||   | t
 || d   W d Q R X | t
 ||  W d Q R X d S )Nc             S   sN   yt | }W n< tk
rH   | d | d  }d| d | d  |  }Y nX |S )Nr   r   r"   )r.   OverflowError)r   lengthr   r   r   r   
_range_len   s    z.RangeTest.test_large_range.<locals>._range_lenr   r   r   r   r&   )r2   r3   r+   r,   r-   r/   r=   r.   r:   r)   
IndexError)r   r?   r4   r5   Zexpected_lenr   idxr6   r   r   r   test_large_range   s    


 



 



$



$zRangeTest.test_large_rangec             C   s`  |  tt |  ttdddd |  ttddd tdtj }|  tt||d td |  ttddd |  ttdd	d	 |  ttdd
 |  ttddd
 |  ttd |  ttdd |  ttdd |  tt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dd |  ttddd |  ttddd d S )Nr   r   r   r   r   r   g      ?g}Ô%ITg\nTr'   r(   g        )r/   r0   r+   r1   r;   r2   r3   )r   r4   r   r   r   test_invalid_invocation   s(    z!RangeTest.test_invalid_invocationc                s  t d}| |dd | |dd | t|jd t dd}| |dd | |dd | t|j G dd dt G  fdd	d	}t d
}|  |j|  t dd}| |dd | t dddd
d | t ddddd | t ddd | t ddd | tt ddd djdd  | t ddd ddd d dd  G dd dt}| }| t d|d d S )Nr   r   r   r&   r   c               @   s   e Zd ZdS )z$RangeTest.test_index.<locals>.BadExcN)__name__
__module____qualname__r   r   r   r   BadExc  s   rG   c                   s   e Zd Z fddZdS )z$RangeTest.test_index.<locals>.BadCmpc                s   |dkr  dS )Nr   Fr   )r   other)rG   r   r   __eq__  s    z+RangeTest.test_index.<locals>.BadCmp.__eq__N)rD   rE   rF   rI   r   )rG   r   r   BadCmp  s   rJ   r   r   ir   r!   l      Fx:^V l   Fx:^V r$   W   V   c               @   s   e Zd Zdd ZdS )z)RangeTest.test_index.<locals>.AlwaysEqualc             S   s   dS )NTr   )r   rH   r   r   r   rI   %  s    z0RangeTest.test_index.<locals>.AlwaysEqual.__eq__N)rD   rE   rF   rI   r   r   r   r   AlwaysEqual$  s   rM   )	r+   r)   indexr/   r1   countr0   	Exceptionr   )r   urJ   r4   rM   always_equalr   )rG   r   
test_index  s.    

 *zRangeTest.test_indexc          	   C   s  dt j }d}G dd d}| tt||||d |g | tt||||d |g G dd d}| tt|  G dd	 d	}| tt|  | td
d |d td | t tdd
d |   W d Q R X | t tdd
d |   W d Q R X d S )Nr   r(   c               @   s   e Zd Zdd Zdd ZdS )z+RangeTest.test_user_index_method.<locals>.Ic             S   s   t || _d S )N)r;   n)r   rT   r   r   r   __init__0  s    z4RangeTest.test_user_index_method.<locals>.I.__init__c             S   s   | j S )N)rT   )r   r   r   r   	__index__2  s    z5RangeTest.test_user_index_method.<locals>.I.__index__N)rD   rE   rF   rU   rV   r   r   r   r   I/  s   rW   r   c               @   s   e Zd Zdd ZdS )z,RangeTest.test_user_index_method.<locals>.IXc             S   s   t d S )N)RuntimeError)r   r   r   r   rV   9  s    z6RangeTest.test_user_index_method.<locals>.IX.__index__N)rD   rE   rF   rV   r   r   r   r   IX8  s   rY   c               @   s   e Zd Zdd ZdS )z,RangeTest.test_user_index_method.<locals>.INc             S   s   dS )Nznot a numberr   )r   r   r   r   rV   ?  s    z6RangeTest.test_user_index_method.<locals>.IN.__index__N)rD   rE   rF   rV   r   r   r   r   IN>  s   rZ   r   r   r   )r2   r3   r)   r*   r+   r/   rX   r0   )r   ZbignumZsmallnumrW   rY   rZ   r   r   r   test_user_index_method*  s    
$$ z RangeTest.test_user_index_methodc             C   sz  |  tddd |  tddd |  tddd |  tddd |  tddd | ttddt | ttddt |  tddd |  tddd |  tddd |  tddd ddd d |  tddd ddd d d G d	d
 d
t}| }|  td|d |  ttt	j
t	j
d d d S )Nr   r"   r   r   r   l      Fx:^V r$   rK   c               @   s   e Zd Zdd ZdS )z)RangeTest.test_count.<locals>.AlwaysEqualc             S   s   dS )NTr   )r   rH   r   r   r   rI   \  s    z0RangeTest.test_count.<locals>.AlwaysEqual.__eq__N)rD   rE   rF   rI   r   r   r   r   rM   [  s   rM   r   )r)   r+   rO   assertIstyper;   rN   r   r.   r2   r3   )r   rM   rR   r   r   r   
test_countM  s     "&zRangeTest.test_countc             C   sF   |  ttdd |  ttddd |  ttdddd d S )Nr   zrange(0, 1)r   zrange(1, 2)r   zrange(1, 2, 3))r)   reprr+   )r   r   r   r   	test_reprc  s    zRangeTest.test_reprc             C   s   dddddddd dd d fg}xht tjd	 D ]V}xP|D ]H}| j||d
0 t | }| ttt||t| W d Q R X q<W q2W d S )N)   )r      )ir   )   r   r"   )ra      r   )r&   r   r   r   A   r   )protoZtest)r+   pickleHIGHEST_PROTOCOLZsubTestr)   r*   loadsdumps)r   	testcasesrf   tr8   r   r   r   test_picklingh  s    
zRangeTest.test_picklingc          
   C   s   dddddddd dd d fg}xt tjd	 D ]}x|D ]}tt |  }}tt | }t||}t|}| t|t| | t|| t|}yt	| W n t
k
r   w<Y nX t||}t|}| t||d	d   q<W q2W d S )
N)ra   )r   rb   )ir   )rc   r   r"   )ra   rd   r   )r&   r   r   r   re   r   )r+   rg   rh   iterr*   rj   ri   r)   r]   nextStopIteration)r   rk   rf   rl   itZitorgdatadr   r   r   test_iterator_picklingr  s$    



z RangeTest.test_iterator_picklingc             C   s   xt tjd D ]x}t dd dd d }t|}xt|}|dd d kr4P q4W t||}t|}| t|g  | t|g  qW d S )Nr   r   re   )	r+   rg   rh   rn   ro   rj   ri   r)   r*   )r   rf   r8   r   rs   i2r   r   r    test_exhausted_iterator_pickling  s    
z*RangeTest.test_exhausted_iterator_picklingc             C   sz   xtt tjd D ]b}t d}t|}xt|}|dkr&P q&W t||}t|}| t|g  | t|g  qW d S )Nr   rc      )	r+   rg   rh   rn   ro   rj   ri   r)   r*   )r   rf   r8   r   rs   ru   r   r   r   &test_large_exhausted_iterator_pickling  s    
z0RangeTest.test_large_exhausted_iterator_picklingc          	   C   s&   |  t tg dd W d Q R X d S )Nr   r"   )r/   r0   r+   )r   r   r   r   test_odd_bug  s    zRangeTest.test_odd_bugc             C   s   |  dtd |  dtd |  dtd G dd d}|  | td G dd d}| | td |  t| td G d	d
 d
t}|  |dtd |  |dttd d S )Ng      ?r   Ty      ?        c               @   s   e Zd Zdd ZdS )z RangeTest.test_types.<locals>.C1c             S   s   dS )NTr   )r   rH   r   r   r   rI         z'RangeTest.test_types.<locals>.C1.__eq__N)rD   rE   rF   rI   r   r   r   r   C1  s   r{   c               @   s   e Zd Zdd Zdd ZdS )z RangeTest.test_types.<locals>.C2c             S   s   dS )Nr   r   )r   r   r   r   __int__  rz   z(RangeTest.test_types.<locals>.C2.__int__c             S   s   dS )Nr   r   )r   r   r   r   rV     rz   z*RangeTest.test_types.<locals>.C2.__index__N)rD   rE   rF   r|   rV   r   r   r   r   C2  s   r}   c               @   s   e Zd Zdd ZdS )z RangeTest.test_types.<locals>.C3c             S   s   dS )NTr   )r   rH   r   r   r   rI     rz   z'RangeTest.test_types.<locals>.C3.__eq__N)rD   rE   rF   rI   r   r   r   r   C3  s   r~   rb   r   )r,   r+   r-   r;   r*   )r   r{   r}   r~   r   r   r   
test_types  s    zRangeTest.test_typesc             C   s   t ddd}| d| | d| | d| | d| | d| | d| t ddd}| d| | d| | d	| | d| t ddd
}| d| | d	| | d| d S )Nr   e   r   r   c   r$   ir"   ir&   i)r+   r,   r-   )r   r8   r   r   r   test_strided_limits  s     zRangeTest.test_strided_limitsc             C   sR   t d}| d| | d| t dd}| d| | d| | d| d S )Nr   r   ir"   )r+   r-   )r   r8   r   r   r   
test_empty  s    
zRangeTest.test_emptyc                s   dd dD   fdd D }x|D ]~\}}}t |||}t|||}d|||}| j|||dd tt |||}t|||}d|||}| j|||dd q&W d S )	Nc             S   s>   g | ]6}| | d  d|d  |fD ]}dD ]}|| q*q"qS )r   r   )r&   r"   r   r   r   r   ).0MbaseZjiggler   r   r   
<listcomp>  s   z2RangeTest.test_range_iterators.<locals>.<listcomp>)l        l            c                s*   g | ]"} D ]}d D ]}|||fqqqS ))l         i   r&   r"   r   r   r   )r   r   endr   )limitsr   r   r     s   zrange({}, {}, {})r$   )r   zreversed(range({}, {}, {})))r+   r   r   r   reversedr   )r   test_rangesr   r   r   Ziter1Ziter2r   r   )r   r   test_range_iterators  s    
zRangeTest.test_range_iteratorsc             C   sL   t ttd}| t|ddd t ttdd> }| t|ddd d S )Nr   r   r   i  )r]   rn   r+   r/   r0   )r   Zrangeiter_typeZlong_rangeiter_typer   r   r   test_range_iterators_invocation  s    z)RangeTest.test_range_iterators_invocationc                s   d fdd	}xt dt dt dddt ddd	t tjd tjd gD ]X |dd
 |dd |dd
 |dd |dd |ddd
 |dd |dd	d qJW d S )Nc                sL   t | ||}t | t |  t | tt |  d S )N)slicer)   r*   r.   )r   r   r   r   )r8   r   r   r   check  s    z#RangeTest.test_slice.<locals>.checkr   r   r   	   r      r   r   rc      iir"   r$   )N)r+   r2   r3   )r   r   r   )r8   r   r   
test_slice  s    







zRangeTest.test_slicec             C   s  t d}| d| | d| | d| | d| | d| | d| | d| t ddd}| d| | d| | d| | d| | d| | d| | d| t ddd	}| d| | d| | d| | d| | d| | d| | d| t ddd
}| d| | d| | d| | d| | d| | d| | d| d S )Nr   r   r   g      @gffffff@r"    r   r   r&   )r+   r,   r-   )r   r8   r   r   r   test_contains  s@    zRangeTest.test_containsc             C   sh   xbt dt dt dddt dddt tjd tjd gD ]&}| tt|t|d d d  q:W d S )	Nr   r   r   r   r   r   r   r"   )r+   r2   r3   r)   r*   r   )r   r8   r   r   r   test_reverse_iteration7  s    

z RangeTest.test_reverse_iterationc             C   s   t tdddd }d ddddddd	d
dddddddddh}x<|D ]4}x.|D ]&}x |dh D ]}||||  q^W qNW qDW d S )Nr      r   rc   r   r"   r&   r   r!   rw   iird   ir   ir   i)r+   r   indices)r   r8   valuesr   jkr   r   r   test_issue11845@  s    

zRangeTest.test_issue11845c                s  t dt ddt dddt dt ddt dddt dddt dt dd	dt dddt dddg ttt  fd
d D }fddD }| ||  fdd D }| |dd |D  x6 D ].}x( D ] }||kr| t|t| qW qW | t ddkd | dt dkd | t dddgkd | t ddd d dt ddd d | tt ddd d dtt ddd d | t ddd dt ddd d d | t dd dd dd  dd t dd dd dd  | tt dd dd dd  dd tt dd dd dd  | t dd dd dd t dd dd d dd  | t	 t dt dk  W d Q R X | t	 t dt dk W d Q R X | t	 t dt dk W d Q R X | t	 t dt dk W d Q R X d S )Nr   r"   r   r   r      r   r    r   c                s   g | ]} D ]}||kqqS r   r   )r   r4   r5   )r   r   r   r   R  s    z-RangeTest.test_comparison.<locals>.<listcomp>c                s   g | ]} D ]}||kqqS r   r   )r   r4   r5   )test_tuplesr   r   r   S  s    c                s   g | ]} D ]}||kqqS r   r   )r   r4   r5   )r   r   r   r   W  s    c             S   s   g | ]
}| qS r   r   )r   r   r   r   r   r   X  s    r   Fr$         r   )
r+   r*   maptupler)   hashr\   ZassertNotEqualr/   r0   )r   Z	ranges_eqZ	tuples_eqZ	ranges_ner4   r5   r   )r   r   r   test_comparisonI  sJ    

"&zRangeTest.test_comparisonc             C   s   |  tdddd |  tdddd |  tdddd |  tdddddd |  tdddddd |  tdddddd |  tdddddd d S )Nr   r   r   ir   r"   r   )assert_attrsr+   )r   r   r   r   test_attributes~  s    zRangeTest.test_attributesc          	   C   s   |  |j| |  |j| |  |j| | t d|_W d Q R X | t d|_W d Q R X | t d|_W d Q R X | t
 |`W d Q R X | t
 |`W d Q R X | t
 |`W d Q R X d S )Nr   r   r   )r)   r   r   r   r/   AttributeError)r   Zrangeobjr   r   r   r   r   r   r     s    zRangeTest.assert_attrs)N)rD   rE   rF   r   r9   r<   rB   rC   rS   r[   r^   r`   rm   rt   rv   rx   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	      s4   
08U(#

"		5
r	   __main__)
Zunittestr2   rg   r   r   r   ZTestCaser	   rD   mainr   r   r   r   <module>   s   	     	