B
    u9a                  @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZmZ d dlm	Z	 dd Z
dd ZG dd	 d	eZG d
d dejZedkre  dS )    N)loadsdumps)supportc             C   s    t | drt| S tdd S )N	__index__zBslice indices must be integers or None or have an __index__ method)hasattroperatorindex	TypeError)arg r    /usr/lib/python3.7/test_slice.pyevaluate_slice_index   s    

r   c             C   s   t |}| jd krdnt| j}|dk r2td|dkrBtd|dk rNdnd}|dk rb|d n|}| jd kr|dk r||n|}n*t| j}|dk rt|| |nt||}| jd kr|dk r|n|}n*t| j}|dk rt|| |nt||}|||fS )N   r   zlength should not be negativezslice step cannot be zero)	r   r   stepr   
ValueErrorstartmaxminstop)slicelengthr   lowerupperr   r   r   r   r   slice_indices   s"    


 

 r   c               @   s   e Zd Zdd Zdd ZdS )MyIndexablec             C   s
   || _ d S )N)value)selfr   r   r   r   __init__A   s    zMyIndexable.__init__c             C   s   | j S )N)r   )r   r   r   r   r   D   s    zMyIndexable.__index__N)__name__
__module____qualname__r   r   r   r   r   r   r   @   s   r   c               @   s\   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZdS )	SliceTestc             C   s$   |  tt |  ttdddd d S )Nr            )assertRaisesr	   r   )r   r   r   r   test_constructorJ   s    zSliceTest.test_constructorc             C   s   |  ttdddd d S )Nr   r#   r$   zslice(1, 2, 3))assertEqualreprr   )r   r   r   r   	test_reprN   s    zSliceTest.test_reprc          	   C   s8   |  tttd |  t td  W d Q R X d S )N   )r&   r	   hashr   __hash__)r   r   r   r   	test_hashQ   s    zSliceTest.test_hashc                s6  t dddt dddt ddd}|  | | | d  | d | d G dd dt G  fd	d
d
t}t | t | |  |  fdd t d| t d| |  |  fdd t dd| t dd| |  |  fdd d S )Nr   r#   r$   r%   )r   r#   r$    c               @   s   e Zd ZdS )zSliceTest.test_cmp.<locals>.ExcN)r   r    r!   r   r   r   r   Exca   s   r0   c                   s   e Zd Z fddZdS )z"SliceTest.test_cmp.<locals>.BadCmpc                s    d S )Nr   )r   other)r0   r   r   __eq__e   s    z)SliceTest.test_cmp.<locals>.BadCmp.__eq__N)r   r    r!   r2   r   )r0   r   r   BadCmpd   s   r3   c                  s    kS )Nr   r   )s1s2r   r   <lambda>k       z$SliceTest.test_cmp.<locals>.<lambda>c                  s    kS )Nr   r   )r4   r5   r   r   r6   p   r7   c                  s    kS )Nr   r   )r4   r5   r   r   r6   u   r7   )r   r(   assertNotEqual	Exceptionobjectr&   )r   Zs3r3   r   )r0   r4   r5   r   test_cmpW   s,    

zSliceTest.test_cmpc             C   s   t d}| |jd  | |jd | |jd  t dd}| |jd | |jd | |jd  t ddd}| |jd | |jd | |jd G dd d}| }t |}| |j|k d S )Nr   r#   r$   c               @   s   e Zd ZdS )z(SliceTest.test_members.<locals>.AnyClassN)r   r    r!   r   r   r   r   AnyClass   s   r<   )r   r(   r   r   r   Z
assertTrue)r   sr<   objr   r   r   test_membersw   s     
zSliceTest.test_membersc             C   s   y| |}W n tk
r&   d}Y nX yt||}W n tk
rN   d}Y nX | || |dkr|jdkrt| | }t|| }| || d S )NZ
valueerrorr   )indicesr   r   r(   r   range)r   r   r   actualZexpectedr   r   r   check_indices   s    

zSliceTest.check_indicesc             C   s\  |  td dd |  td d ddd |  tdd ddd |  td d ddd |  td d d	dd
 |  tdd d	dd |  td ddd |  td ddd |  td ddd |  td dddd |  td dddd |  td dddd |  td ddd |  td ddd |  td ddd |  td dddd |  td dddd |  td dddd |  tdddtd d |  tddddtd d dd |  tddddd |  ttdd d tjd  dg d dd  ddddddd d!d"dd g}ddd d!d"dd g}x<tj|dd#D ]*}t| }x|D ]}| 	|| qW qW | 	tdddd$ | 
t td d W d Q R X | 
t tdddd% W d Q R X | 
t td&ddd% W d Q R X | 
t tdd'dd% W d Q R X | 
t tddd(d% W d Q R X | 
t tdddd) W d Q R X |  tdddd%d* |  ttdddd%d* |  tdtddd%d* |  tddtdd%d* |  tdddtd%d* d S )+N
   )r   rD   r   r#   )r   rD   r#   r   )r   rD   r#   r   )	   r   r   )rE   r   rF   r$   )r$   r   rF   i)r   r   r   i)r   r   r   i)rE   r   r   irE   )r   rE   r         )rE   rH   r   )rE   rE   r   id   r   i   ii   5   i   @)repeatr+   g        g      $@g      ?g      @)r   r+   r   )r(   r   r@   listrA   sysmaxsize	itertoolsproductrC   r&   r   r	   r   )r   ZvalsZlengthsZ
slice_argsr=   r   r   r   r   test_indices   sd    &&
zSliceTest.test_indicesc                sF   g  G  fdddt }| }d|dd< |  tdddfg d S )Nc                   s   e Zd Z fddZdS )z3SliceTest.test_setslice_without_getslice.<locals>.Xc                s     ||f d S )N)append)r   ik)tmpr   r   __setitem__   s    z?SliceTest.test_setslice_without_getslice.<locals>.X.__setitem__N)r   r    r!   rX   r   )rW   r   r   X   s   rY   *   r   r#   )r:   r(   r   )r   rY   xr   )rW   r   test_setslice_without_getslice   s
    z(SliceTest.test_setslice_without_getslicec             C   sd   t ddd}xRdD ]J}tt||}| || | |d|d | t|t| qW d S )NrD      r$   )r   r   r#      )r   r   r   r(   r@   r8   id)r   r=   Zprotocoltr   r   r   test_pickle   s    
zSliceTest.test_picklec             C   sD   G dd d}| }t ||_t|}d }t  | |  d S )Nc               @   s   e Zd ZdS )z#SliceTest.test_cycle.<locals>.myobjN)r   r    r!   r   r   r   r   myobj   s    rb   )r   r=   weakrefrefr   Z
gc_collectZassertIsNone)r   rb   owr   r   r   
test_cycle   s    

zSliceTest.test_cycleN)r   r    r!   r'   r*   r.   r;   r?   rC   rS   r\   ra   rg   r   r   r   r   r"   H   s    E
r"   __main__)rQ   r   rO   Zunittestrc   pickler   r   Ztestr   r   r   r:   r   ZTestCaser"   r   mainr   r   r   r   <module>   s   & 7