B
    u9aE                 @   sP  d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
G dd dZG dd deZG dd deZG d	d
 d
ZG dd dZG dd dZG dd dejeeZG dd dejeeZG dd dejeeZG dd dejeeZG dd dejeeZG dd dejeeZG dd dejZedkrLe  dS )zUnit tests for the memoryview

   Some tests are in test_bytes. Many tests that require _testbuffer.ndarray
   are in test_buffer.
    Nc               @   s   e Zd ZdZedd Ze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d6S )7AbstractMemoryTestss   abcdefc             C   s   | j S )N)source_bytes)self r   %/usr/lib/python3.7/test_memoryview.py_source   s    zAbstractMemoryTests._sourcec             C   s   t d | j| jgS )N)filterro_typerw_type)r   r   r   r   _types   s    zAbstractMemoryTests._typesc                s$  || j }t|}| | |  d td |  d t |  d td |  d td |  d td | t	 fdd | t	 fd	d | t	 fd
d | t	 fdd | t
 fdd | t
 fdd | t
 fdd d  | t|| d S )Nr      a      fic                  s    d S )N   r   r   )mr   r   <lambda>'       z=AbstractMemoryTests.check_getitem_with_type.<locals>.<lambda>c                  s    d S )Nir   r   )r   r   r   r   (   r   c                  s
    t j S )N)sysmaxsizer   )r   r   r   r   )   r   c                  s    t j  S )N)r   r   r   )r   r   r   r   *   r   c                  s    d  S )Nr   r   )r   r   r   r   ,   r   c                  s    d S )Ng        r   r   )r   r   r   r   -   r   c                  s    d S )Nar   r   )r   r   r   r   .   r   )r   r   getrefcount_viewassertEqualordassertIsInstanceintassertRaises
IndexError	TypeError)r   tpboldrefcountr   )r   r   check_getitem_with_type   s"    


z+AbstractMemoryTests.check_getitem_with_typec             C   s   x| j D ]}| | qW d S )N)r   r#   )r   r    r   r   r   test_getitem2   s    z AbstractMemoryTests.test_getitemc                sN   xH| j D ]>}|| j}| | | t  fddtt D  qW d S )Nc                s   g | ]} | qS r   r   ).0i)r   r   r   
<listcomp>:   s    z1AbstractMemoryTests.test_iter.<locals>.<listcomp>)r   r   r   r   listrangelen)r   r    r!   r   )r   r   	test_iter6   s    

zAbstractMemoryTests.test_iterc                s   | j s| d |  | j}t|}| |  fdd}| t|d | t|d | t|td d  | 	t|| d S )Nzno read-only type to testc                s   |  d< d S )Nr   r   )value)r   r   r   setitemB   s    z:AbstractMemoryTests.test_setitem_readonly.<locals>.setitemr   A   )
r	   skipTestr   r   r   r   r   r   
memoryviewr   )r   r!   r"   r-   r   )r   r   test_setitem_readonly<   s    


z)AbstractMemoryTests.test_setitem_readonlyc          	      s  | j s| d | j |  | j}t|}| | td d< | |d d dd< | |d d dd	< | |d
 d dd< | |d
 d d d < | |d  dd  dd	< | |d d d d <  dd	  dd< | |d  fdd}| t	|dd | t	|dd | t	|tj
d | t	|tj
 d | t|dd | t|dd | t|tddddfd | t|dtdddfd | t|dd | t|dd tdddtdddf}| t||d  jdkr tnt}| ||dd | ||dd | t|tddd | t|tddd d  | t|| d S )Nzno writable type to test   1r   s   1bcdef   0   s   0bcdefs   12   s   012defr   s   abcdef   r   s   cdedefs   ababcfc                s   | | < d S )Nr   )keyr,   )r   r    r   r   r-   c   s    z:AbstractMemoryTests.test_setitem_writable.<locals>.setitemr   r   ig        )r   r   cs   ab)r
   r/   r   r   r   r   r   _check_contentsr   r   r   r   sliceNotImplementedErrorformat
ValueErrorr   )r   r!   r"   r-   Zslicesexcr   )r   r    r   test_setitem_writableJ   sR    


z)AbstractMemoryTests.test_setitem_writablec          
   C   sd   x^| j D ]T}|| j}| |}| t |d= W d Q R X | t |dd= W d Q R X qW d S )Nr4      )r   r   r   r   r   )r   r    r!   r   r   r   r   test_delitem~   s    

z AbstractMemoryTests.test_delitemc                s\   xV j D ]L} | j}| }d fdddD } ||  |t qW d S )Nr   c             3   s   | ]}  t|gV  qd S )N)getitem_typebytes)r%   r8   )r   r   r   	<genexpr>   s    z3AbstractMemoryTests.test_tobytes.<locals>.<genexpr>s   abcdef)r   r   r   tobytesjoinr   r   rC   )r   r    r   r!   Zexpectedr   )r   r   test_tobytes   s    z AbstractMemoryTests.test_tobytesc             C   s<   x6| j D ],}| || j}| }| |td qW d S )Ns   abcdef)r   r   r   tolistr   r(   )r   r    r   lr   r   r   test_tolist   s    zAbstractMemoryTests.test_tolistc                s  x| j D ]}| || jxz| j D ]p}| |dk | |dk | |dk | |dk | |dk | |dk q(W | k | d d  k | dd d d  k | dd k | dk | dk | dk | dk xjdfD ]^ | t fdd	 | t fd
d	 | t fdd	 | t fdd	 q6W q
W d S )Ns   abcdefs   abcdes   abcde1r   r   r   Zabcdefc                  s    k S )Nr   r   )r8   r   r   r   r      r   z2AbstractMemoryTests.test_compare.<locals>.<lambda>c                  s    kS )Nr   r   )r8   r   r   r   r      r   c                  s    kS )Nr   r   )r8   r   r   r   r      r   c                  s    kS )Nr   r   )r8   r   r   r   r      r   )r   r   r   
assertTrueZassertFalser   r   )r   r    Ztp_compr   )r8   r   r   test_compare   s,    z AbstractMemoryTests.test_comparec             C   s   |  || j}| |j| j | |j| j | |jd | |jd | t|d | |j| jf | |j	d |S )Nr4   )r   r   r   )
r   r   r   r<   itemsizendimshaper*   strides
suboffsets)r   r    r   r   r   r   check_attributes_with_type   s    z.AbstractMemoryTests.check_attributes_with_typec             C   s.   | j s| d | | j }| |jd d S )Nzno read-only type to testT)r	   r/   rR   r   readonly)r   r   r   r   r   test_attributes_readonly   s    
z,AbstractMemoryTests.test_attributes_readonlyc             C   s.   | j s| d | | j }| |jd d S )Nzno writable type to testF)r
   r/   rR   r   rS   )r   r   r   r   r   test_attributes_writable   s    
z,AbstractMemoryTests.test_attributes_writablec             C   s   x|| j D ]r}|| j}t|}| |}t|}t|d}| |||d | t|| d }| t|| qW d S )Nzutf-8)	r   r   r   r   r   strr9   encoder   )r   r    r!   r"   r   Zoldviewrefcountsr   r   r   test_getbuffer   s    




z"AbstractMemoryTests.test_getbufferc       	      C   s   x| j D ]}t|tsqG dd d}G dd d|}G dd d}||d}| |}| }||_||_t|}d  } }}t	  | 
| d k|  ||d}| }||_||_t|}d  }}t	  | 
| d k|  qW d S )Nc               @   s   e Zd Zdd ZdS )z+AbstractMemoryTests.test_gc.<locals>.MyViewc             S   s   t || _d S )N)r0   r   )r   baser   r   r   __init__   s    z4AbstractMemoryTests.test_gc.<locals>.MyView.__init__N)__name__
__module____qualname__r[   r   r   r   r   MyView   s   r_   c               @   s   e Zd ZdS )z-AbstractMemoryTests.test_gc.<locals>.MySourceN)r\   r]   r^   r   r   r   r   MySource   s   r`   c               @   s   e Zd ZdS )z-AbstractMemoryTests.test_gc.<locals>.MyObjectN)r\   r]   r^   r   r   r   r   MyObject   s   ra   s   abc)r   
isinstancetyper   r   oweakrefrefgcZcollectrK   x)	r   r    r_   r`   ra   r!   r   rd   wrr   r   r   test_gc   s.    



zAbstractMemoryTests.test_gcc          
   C   s  |  td}| t| W d Q R X | |  W d Q R X | |  W d Q R X | |d  W d Q R X | d|d< W d Q R X | t| W d Q R X | |j W d Q R X | |j W d Q R X | |j W d Q R X | |j	 W d Q R X | |j
 W d Q R X | |j W d Q R X | | W d Q R X W d Q R X | dt| | dt| | || | |t|| j | ||| j d S )NZreleasedr      xzreleased memory)ZassertRaisesRegexr=   rC   rE   rH   r*   r<   rM   rN   rS   rO   rP   ZassertInrV   reprr   ZassertNotEqualr0   r   )r   r   r    Zcheckr   r   r   _check_released   sB                z#AbstractMemoryTests._check_releasedc          
   C   sr   xl| j D ]b}|| j}| |}|}| || W d Q R X | || | |}| |  W d Q R X qW d S )N)r   r   r   assertIsrm   release)r   r    r!   r   cmr   r   r   test_contextmanager  s    


z'AbstractMemoryTests.test_contextmanagerc             C   sP   xJ| j D ]@}|| j}| |}|  | || |  | || qW d S )N)r   r   r   ro   rm   )r   r    r!   r   r   r   r   test_release#  s    

z AbstractMemoryTests.test_releasec             C   sJ   | j }|d kr| d || j}| |}td}| t|j| d S )Nzno read-only type to tests   ZZZZ)	r	   r/   r   r   ioBytesIOr   r   readinto)r   r    r!   r   r&   r   r   r   test_writable_readonly-  s    



z*AbstractMemoryTests.test_writable_readonlyc             C   s   |  t| ji  d S )N)r   r   r   )r   r   r   r   test_getbuf_fail8  s    z$AbstractMemoryTests.test_getbuf_failc             C   s   | j }|d kr| d || j}| |}| t|td |  | t|td | |}|  | tt| d S )Nzno read-only type to tests   abcdef)	r	   r/   r   r   r   hashro   r   r=   )r   r    r!   r   r   r   r   	test_hash;  s    



zAbstractMemoryTests.test_hashc             C   s>   | j }|d kr| d || j}| |}| tt| d S )Nzno writable type to test)r
   r/   r   r   r   r=   rx   )r   r    r!   r   r   r   r   test_hash_writableM  s    


z&AbstractMemoryTests.test_hash_writablec                s   xz| j D ]p}|| j}| |}g  |f fdd	}t||}| | | ~tj  | | d  |  d | qW d S )Nc                s     | d S )N)append)ri   r!   )Lr   r   callback\  s    z2AbstractMemoryTests.test_weakref.<locals>.callbackr   )	r   r   r   re   rf   rn   testsupportZ
gc_collect)r   r    r!   r   r}   ri   r   )r|   r   test_weakrefV  s    


z AbstractMemoryTests.test_weakrefc          	   C   sn   xh| j D ]^}|| j}| |}tt| }| tt|| | tt|t|d d d  qW d S )Nr   )r   r   r   r(   reversedrH   r   )r   r    r!   r   Zaslistr   r   r   test_reversede  s    

z!AbstractMemoryTests.test_reversedc             C   s   t  dddddg}t|}|d}|d}|dd }t|}~| |d d | |d d | |jd | |jd |d}| |d d | |d d | |jd | |jd d S )NH   Br   r6   I)arrayr0   castr   r<   )r   r   rh   r   r!   r8   d_r   r   r   test_issue22668m  s     


z#AbstractMemoryTests.test_issue22668N)r\   r]   r^   r   propertyr   r   r#   r$   r+   r1   r?   rA   rG   rJ   rL   rR   rT   rU   rY   rj   rm   rq   rr   rv   rw   ry   rz   r   r   r   r   r   r   r   r      s6   4	
&
	r   c               @   s    e Zd ZeZeZeZdZdZ	dS )BaseBytesMemoryTestsr4   r   N)
r\   r]   r^   rC   r	   	bytearrayr
   rB   rM   r<   r   r   r   r   r     s
   r   c               @   sT   e Zd ZdZdd Zdd ZedjZdZe	
ddd Ze	
d	d
d ZdS )BaseArrayMemoryTestsNc             C   s   t  dt|S )Nr&   )r   r(   )r   r!   r   r   r   r     r   zBaseArrayMemoryTests.<lambda>c             C   s   t  dt| S )Nr&   )r   r(   rE   )r   r!   r   r   r   r     r   r&   z/XXX test should be adapted for non-byte buffersc             C   s   d S )Nr   )r   r   r   r   rY     s    z#BaseArrayMemoryTests.test_getbufferz:XXX NotImplementedError: tolist() only supports byte viewsc             C   s   d S )Nr   )r   r   r   r   rJ     s    z BaseArrayMemoryTests.test_tolist)r\   r]   r^   r	   r
   rB   r   rM   r<   unittestskiprY   rJ   r   r   r   r   r     s   r   c               @   s   e Zd Zdd Zdd ZdS )BaseMemoryviewTestsc             C   s   t |S )N)r0   )r   objr   r   r   r     s    zBaseMemoryviewTests._viewc             C   s   |  ||| d S )N)r   )r   r    r   contentsr   r   r   r9     s    z#BaseMemoryviewTests._check_contentsN)r\   r]   r^   r   r9   r   r   r   r   r     s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	BaseMemorySliceTestss   XabcdefYc             C   s   t |}|dd S )Nr4      )r0   )r   r   r   r   r   r   r     s    zBaseMemorySliceTests._viewc             C   s   |  |dd || d S )Nr4   r   )r   )r   r    r   r   r   r   r   r9     s    z$BaseMemorySliceTests._check_contentsc             C   sJ   xD| j D ]:}t|| j}t|}|dd  | t|| qW d S )Nr4   r6   )r   r0   r   r   r   r   )r   r    r   r"   r   r   r   	test_refs  s
    
zBaseMemorySliceTests.test_refsN)r\   r]   r^   r   r   r9   r   r   r   r   r   r     s   r   c               @   s    e Zd ZdZdd Zdd ZdS )BaseMemorySliceSliceTestss   XabcdefYc             C   s   t |}|d d dd  S )Nr   r4   )r0   )r   r   r   r   r   r   r     s    zBaseMemorySliceSliceTests._viewc             C   s   |  |dd || d S )Nr4   r   )r   )r   r    r   r   r   r   r   r9     s    z)BaseMemorySliceSliceTests._check_contentsN)r\   r]   r^   r   r   r9   r   r   r   r   r     s   r   c               @   s   e Zd Zdd ZdS )BytesMemoryviewTestc             C   sz   xt| j D ]j}|| j}| t| | t|d | tt | tt|| | jtt|d | jtt|dd qW d S )N)object)argumentT)r   r   rK   r0   r   r   )r   r    Zobr   r   r   test_constructor  s    
z$BytesMemoryviewTest.test_constructorN)r\   r]   r^   r   r   r   r   r   r     s   r   c               @   s   e Zd Zdd ZdS )ArrayMemoryviewTestc             C   sH   t  dtd}t|}t  dtddd}||d d < | || d S )Nr&   
   	   r   )r   r)   r0   r   )r   r   r   Znew_ar   r   r   test_array_assign  s
    z%ArrayMemoryviewTest.test_array_assignN)r\   r]   r^   r   r   r   r   r   r     s   r   c               @   s   e Zd ZdS )BytesMemorySliceTestN)r\   r]   r^   r   r   r   r   r     s   r   c               @   s   e Zd ZdS )ArrayMemorySliceTestN)r\   r]   r^   r   r   r   r   r     s   r   c               @   s   e Zd ZdS )BytesMemorySliceSliceTestN)r\   r]   r^   r   r   r   r   r     s   r   c               @   s   e Zd ZdS )ArrayMemorySliceSliceTestN)r\   r]   r^   r   r   r   r   r     s   r   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
	OtherTestc          
   C   s   t jd}t|d}| }t|d}|d d |d d< |dd  |dd < | |jd xdD ]z}| 	|f | }t||}t||d d |d d< t||dd  |dd < | |jd W d Q R X qlW d S )Nctypesg333333?r   r6   ZBbc)
r~   r   import_modulerC   Zc_doubler0   r   r   r,   ZsubTest)r   r   Zp6r   r   r<   r   r   r   test_ctypes_cast  s    
zOtherTest.test_ctypes_castc             C   s6   dd }t |}|d d d }| | dd  d S )Nr3   i@ r   Z30)r0   r   hex)r   rh   Zm1Zm2r   r   r   test_memoryview_hex  s    zOtherTest.test_memoryview_hexc          	   C   s,   t d}| t t| W d Q R X d S )Ns   abc)r0   r   r   copy)r   r   r   r   r   	test_copy  s    zOtherTest.test_copyc          
   C   sF   t d}x8ttjd D ]&}| t t|| W d Q R X qW d S )Ns   abcr4   )r0   r)   pickleZHIGHEST_PROTOCOLr   r   dumps)r   r   protor   r   r   test_pickle  s    zOtherTest.test_pickleN)r\   r]   r^   r   r   r   r   r   r   r   r   r     s   r   __main__)__doc__r   Ztest.supportr~   r   rg   re   r   rs   r   r   r   r   r   r   r   r   ZTestCaser   r   r   r   r   r   r   r\   mainr   r   r   r   <module>   s@     w'
