U
    #c                     @   s   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m	Z	 ddl
mZ ddlmZ dZedjedjkredd	d
d	Zdd eD Zdd Zdd ZG dd dejZdd Ze ZG dd dZG dd dZdS )z
The tests exercise the casting machinery in a more low-level manner.
The reason is mostly to test a new implementation of the casting machinery.

Unlike most tests in NumPy, these are closer to unit-tests rather
than integration tests.
    N)
as_strided)assert_array_equal)_get_castingimplz?bhilqBHILQefdFDlq Lc                 C   s   g | ]}t t|qS  typenpdtype.0cr	   r	   K/tmp/pip-unpacked-wheel-b2rbor69/numpy/core/tests/test_casting_unittests.py
<listcomp>   s     r   c                  c   sN   t D ]D} |  }tj|t|dV  |jdkr| }tj|t|dV  qd S )N)id|)simple_dtypespytestparamstr	byteordernewbyteorder)Zdtype_classdtr	   r	   r   simple_dtype_instances   s    
r   c                 C   s   | t jkrdS | jdkr|| jdkr(d}n>| jdkr8d}n.| jdkrHd}n| jdkrXd	}ntd
|  | jdkrx|d7 }|S | jdkrdS | jdkrdS | jdkrdS | jdkrdS td
|  dS )zHReturns the string length when casting the basic dtypes to strings.
       iu            
         z!did not find expected length for ig0   G`   f    r   @   N)r   Zbool_kinditemsizeAssertionErrorchar)r   lengthr	   r	   r   get_expected_stringlength'   s0    










r3   c                   @   s    e Zd ZdZdZdZdZdZdS )Castingr   r   r!   r    r"   N)__name__
__module____qualname__noequivsafe	same_kindunsafer	   r	   r	   r   r4   J   s
   r4   c                  C   s   t d d} dd | d dd d D }tjtjtjtjdd}i }t	|| d	d  D ]>\}}i ||< t	||dd d D ]\}}|| || |< qq^|S )
Na  
        X ? b h i l q B H I L Q e f d g F D G S U V O M m
        ? # = = = = = = = = = = = = = = = = = = = = = . =
        b . # = = = = . . . . . = = = = = = = = = = = . =
        h . ~ # = = = . . . . . ~ = = = = = = = = = = . =
        i . ~ ~ # = = . . . . . ~ ~ = = ~ = = = = = = . =
        l . ~ ~ ~ # # . . . . . ~ ~ = = ~ = = = = = = . =
        q . ~ ~ ~ # # . . . . . ~ ~ = = ~ = = = = = = . =
        B . ~ = = = = # = = = = = = = = = = = = = = = . =
        H . ~ ~ = = = ~ # = = = ~ = = = = = = = = = = . =
        I . ~ ~ ~ = = ~ ~ # = = ~ ~ = = ~ = = = = = = . =
        L . ~ ~ ~ ~ ~ ~ ~ ~ # # ~ ~ = = ~ = = = = = = . ~
        Q . ~ ~ ~ ~ ~ ~ ~ ~ # # ~ ~ = = ~ = = = = = = . ~
        e . . . . . . . . . . . # = = = = = = = = = = . .
        f . . . . . . . . . . . ~ # = = = = = = = = = . .
        d . . . . . . . . . . . ~ ~ # = ~ = = = = = = . .
        g . . . . . . . . . . . ~ ~ ~ # ~ ~ = = = = = . .
        F . . . . . . . . . . . . . . . # = = = = = = . .
        D . . . . . . . . . . . . . . . ~ # = = = = = . .
        G . . . . . . . . . . . . . . . ~ ~ # = = = = . .
        S . . . . . . . . . . . . . . . . . . # = = = . .
        U . . . . . . . . . . . . . . . . . . . # = = . .
        V . . . . . . . . . . . . . . . . . . . . # = . .
        O . . . . . . . . . . . . . . . . . . . . = # . .
        M . . . . . . . . . . . . . . . . . . . . = = # .
        m . . . . . . . . . . . . . . . . . . . . = = . #
        
c                 S   s   g | ]}t t|qS r	   r
   r   r	   r	   r   r   n   s     z&_get_cancast_table.<locals>.<listcomp>r   r!   ).~=# r   )
textwrapdedentstripsplitr4   r<   r;   r:   r9   zip)tableZdtypesZconvert_castZcancastfrom_dtrowto_dtr   r	   r	   r   _get_cancast_tableR   s     rM   c                   @   sH   e Zd ZdZejdddgejddddd	gd
d Zdd ZdS )TestChangesz:
    These test cases exercise some behaviour changes
    stringSUfloatinger+   dr'   c                 C   s*   t ||stt || ds&td S )NZ100r   can_castr0   )selfrR   rO   r	   r	   r   test_float_to_string   s    z TestChanges.test_float_to_stringc                 C   s   t ddstt dds tt ddr0tt ddr@tt ddrPtt jddddsdtt jd	dd
dsxtt jddd
dstd S )NrT   VZS20ZV1ZU1zd,ir;   castingV3r8   ZV0rU   rW   r	   r	   r   test_to_void   s    zTestChanges.test_to_voidN)	r5   r6   r7   __doc__r   markparametrizerX   r^   r	   r	   r	   r   rN      s
   rN   c                   @   s2  e Zd ZdZdd ZdvddZejde	dd	 Z
ejd
ejde dd Zejde	dd Zejddddddgddejdddfeed dejdddfddejdddfddejdddfddejdddfddejdddfddejdddfddejdddfdd ejddd!dd"d#d$d%gfd&dejdddfeed& dejdddfd'd(ejdddfd(d'ejdddfd)d)ejdddfd&d(ejdddfd(d&ejdddfd(d*ejdddfd+d,ejddd!ddd#d$d%gfgd-d. Zd/d0 Zejd1e	ejd2d3d4gd5d6 Zejd2d3d4gejd7e d8d9 Zejd7d:d;d<gejd2d3d4gd=d> Zejd?d@dAgejdBd@dAgdCdD ZdEdF ZdGdH Z ejdIe dJdK Z!ejdIe dLdM Z"ejdNdOdPgdQdR Z#ejddSgedTdUgdVdWgddXgdYdZfedUdTgdVdWgddXgdYdZfedUdTgdVdWgdd[gdYdfgd\d] Z$ejd^d_d`dadbe%dTgdbgdZgdYdfe%dTgdbgdZgdYdbdZfdbe%dTdUgdbdbgdZdZgdYdfdcdddedfdgdhdidjdkdldcgdmdn Z&ejdIej'do dpdq Z(ejdIej'dr ej'ds  dtdu Z)dS )wTestCastingi  c           	      C   s`  |d ks|j |j kr"| j|j  }n| j|j  }tj||d}|jjsHt|jjsTtdd t|D }t	|D ]6\}}|dk r|j
dkr|t|j d }|||< qn|d kr|jdkrdd |D }||fS |jdkrd	d |D }tj||d}|jjst|jjstt	|D ]<\}}|dk rJ|j
dkrJ|t|j d }|||< q|||fS )
Nr   c                 S   s   g | ]}t d dqS )i   )random	randrange)r   _r	   r	   r   r      s     z(TestCasting.get_data.<locals>.<listcomp>r   ur   ?c                 S   s   g | ]}t |qS r	   boolr   vr	   r	   r   r      s     c                 S   s   g | ]}t |qS r	   rj   rl   r	   r	   r   r      s     )r/   sizer   emptyflagsc_contiguousr0   alignedrange	enumerater.   Ziinfomaxr1   )	rW   dtype1dtype2r2   arr1valuesr&   valuearr2r	   r	   r   get_data   s2    


zTestCasting.get_dataTc                 C   s>  |r|j j}|j j}n6|r4d|j j }d|j j }n|j jd }|j jd }t|d |j j d }t|d |j j d }tj|tjd}	tj|tjd}
|t| |	jkst|t| |
jkst|rt|	dd 	|j |j
|f}t|
dd 	|j |j
|f}n@t|	dd 	|j |j
|f}t|
dd 	|j |j
|f}||d< |s|rl|j jnd}|	|d|  }|d	t|kst|r|jjst|jjstn|jjrt|jjrt|r|jjst|jjs6tn8|jjr|j jdkst|jjr6|j jdks6t||fS )
z
        Returns a copy of arr1 that may be non-contiguous or unaligned, and a
        matching array for arr2 (although not a copy).
        r!   r   r    rc   Nr>   .r       )r   r/   lenr   zerosuint8nbytesr0   r   viewshapetobytescountrp   rq   rr   	alignment)rW   rx   r{   rr   contigZstride1Zstride2Z	max_size1Z	max_size2
from_bytesto_bytesZnew1Znew2offsetbufr	   r	   r   get_data_variation   s^    
    zTestCasting.get_data_variationfrom_Dtc                 C   s4  t D ](}t||}| |  fD ]}||d fd d }|| ksNt~| |  fD ]}|||f\}\}}	}
t||kstt|	|kst|
d k	r|tjksttjt	| | kst|j
|	j
kstn.||kr|j
|	j
kst|t	| | kst||kr`||kst||	ks`tq`q$qd S )Nr   )r   get_castingimplr   _resolve_descriptorsr0   r   r4   r8   r9   
CAST_TABLEisnative)rW   r   Zto_DtcastrJ   defaultrL   r[   from_resto_resview_offr	   r	   r   test_simple_cancast  s*    

zTestCasting.test_simple_cancastzignore::numpy.ComplexWarningrJ   c                 C   s  t  D ]x}|jd }tt|t|}|||f\}\}}}||k	sP||k	rV dS |tjk}~~~| ||\}	}
}||	|
f |
	 |kst
| |	|
dd\}}|||f t||
 | |
 kst
|jdkr|jdks|js dS | |	|
dd\}}|||f t||
 | |
 ks8t
| |	|
dd\}}|||f t||
 | |
 kszt
~~~qdS )a  
        This test checks numeric direct casts for dtypes supported also by the
        struct module (plus complex).  It tries to be test a wide range of
        inputs, but skips over possibly undefined behaviour (e.g. int rollover).
        Longdouble and CLongdouble are tested, but only using double precision.

        If this test creates issues, it should possibly just be simplified
        or even removed (checking whether unaligned/non-contiguous casts give
        the same results is useful, though).
        r   NTFr   )r   ry   r   r   r   r4   r:   r|   _simple_strided_calltolistr0   r   r   r   r   _supports_unaligned)rW   rJ   rL   r   r[   r   r   r   r:   rx   r{   ry   arr1_oarr2_or	   r	   r   test_simple_direct_casts#  s<    




z$TestCasting.test_simple_direct_castsc              
   C   s  | }t dt dt dt dt dt dg}|D ]\}tt|t|}|||f\}\}}}	||kszt||kst~~|t| t| @ st|	d kstt t j}
| ||
\}}}|	|}t 
d|d< |t dkr2d	|d
< |||f tt t|d
  W 5 Q R X  d S |||f dd | D |ks\t| ||dd\}}|||f t|| | | ks>tq>d S )NM8M8[ms]M8[4D]m8m8[ms]m8[4D]NaT.r   r>   c                 S   s   g | ]}t |qS r	   )intrl   r	   r	   r   r     s     z5TestCasting.test_numeric_to_times.<locals>.<listcomp>TF)r   r   r   r   r   r0   r   int64r|   r   Z
datetime64r   r   raises
ValueErrorr   r   r   r   r   )rW   r   rJ   Ztime_dtypesZtime_dtr   r[   r   r   r   Zint64_dtrx   r{   ry   r   r   r	   r	   r   test_numeric_to_timesa  s@      


z!TestCasting.test_numeric_to_timesrL   expected_castingexpected_view_offnomdenomzM8[ns]Nr   r   r   r   zM8[5ms]i@B zM8[7ms]   r   zM8[1M]         r>   i"  ii!zm8[ns]r   r   zm8[5ms]zm8[7ms]r   zm8[1M]c              	   C   s  t |}|d k	rt |}t ddddddg}|t d|j}|jj|jks\tt ||d sttt	|}t
||}	|	||f\}
\}}}||kst||ks|d kst|
|kst||kst|d k	r|| | |}d	|d< nt |}||d
< ||}||}t |}|
tjkrr|dksJ|dkrrtt |	||f W 5 Q R X d S dD ]R}dD ]F}| ||||\}}d|d
< |	||f t|d|d q~qvd S )Nr   r   l    i'  il        r   r   r   .r   r   )TT)r   r   arrayastyper   r   r0   Zisnatr   r   r   r   
empty_liker4   r<   r   r   r   r   r   r   )rW   rJ   rL   r   r   r   r   ry   ZDTyper   r[   r   r   r   Zexpected_outorig_arrZorig_outrr   r   Zarroutr	   r	   r   test_time_to_time  sN     







    zTestCasting.test_time_to_timec                 C   s:   |j dkrdnd}|j| | }t|j |j  | S )NrP   r   r"   )r1   r/   r   r   r   )rW   r   change_lengthfactr2   r	   r	   r   string_with_modified_length  s    z'TestCasting.string_with_modified_lengthother_DTstring_charrP   rQ   c                 C   s  |dkrdnd}t t|}t||}| }t|}t| | }||d f\}	\}
}}|j|| ksrt|	tj	kst|d kstt
||stdD ]b}|dkrtj	}ntj}| ||}|||f\}	\}}}||kst|	|kst|d kstqt||}|||f\}	}}|	tjks0t|d ks>tt||}||d f\}	\}}}|	tjkspt|d ks~t||kstd S )NrP   r   r"   r>   r   r   r   )r   r   r   r   r3   r   r/   r0   r4   r:   
isinstancer;   r   r<   )rW   r   r   r   	string_DTr   other_dtexpected_length	string_dtsafetyres_other_dtres_dtr   r   expected_safetyrL   rg   r	   r	   r   test_string_cancast  sF    


zTestCasting.test_string_cancastr   c                 C   s  t t|}tt ||}t|t |}||df\}\}}}||k	r^|j|jksZtdS | |d\}	}
tjt	|	|d}| 
|d}tjt	|	|d}| 
|d}tjt	|	|d}|jrt|jrtdD ]}| |	|d|\}}| |	| d|\}}| |	|d|\}}|||f |||f t||| |||f t|| |jdkrjqd|d	< |||f t|	| d|d	< |||f t|	| qdS )
ah  
        Tests casts from and to string by checking the roundtripping property.

        The test also covers some string to string casts (but not all).

        If this test creates issues, it should possibly just be simplified
        or even removed (checking whether unaligned/non-contiguous casts give
        the same results is useful, though).
        Nrc   r>   r   )TFTbr   .)r   r   r   r   r   r   r0   r|   r   r~   r   r   r   copyr   r   r   r.   )rW   r   r   r   r   Z	cast_backrg   r   r   r   ry   Zstr_arrZstring_dt_shortZstr_arr_shortZstring_dt_longZstr_arr_longr   Z	other_arrr	   r	   r   "test_simple_string_casts_roundtrip  sb    

         

z.TestCasting.test_simple_string_casts_roundtripZS8z<U8z>U8c                 C   s  t |}|dkrdnd}|jdkr(dnd}tt |}tt||}|j| }t | | }||d f\}	\}
}}|j|| kstt||std }|j|kr|j	rt
j}d}qt
j}n|dkrt
j}nt
j}||kst||	kstdD ]}| ||}|||f\}	\}}}||ks,t|dkrF||ksTtn|d ksTt|t
jkrp|	|kstq|dk r|	t
jkstq|dkr|	|kstq|dkr|	t
jkstqd S )NrP   r   r"   r   rQ   r   )r   r   r1   r   r   r/   r   r0   r   r   r4   r8   r9   r:   r<   r   r;   )rW   r   r   r   divr   r   r   r   r   r   r   r   r   r   r   rL   rg   r	   r	   r   test_string_to_string_cancastP  sP    





z)TestCasting.test_string_to_string_cancastorder1><order2c           	      C   s   t | d}t | d}t jdt jddd  |}t jdt jddd  |}|jdkr~|jjrrt|jjr~td}||d< ||	 fD ]0}||d< |d |kst|	 d |kstqd S )NZU30y   rc   r   u   this is a ünicode string‽r	   .)
r   r   ro   r   r   r   rp   rr   r0   r   )	rW   r   r   rv   rw   Zdata1Zdata2elementdatar	   r	   r   test_unicode_byteswapped_cast  s    
z)TestCasting.test_unicode_byteswapped_castc                 C   sD   t jg dddjjdks tt jg dddjjdks@td S )NZV5rc   rP   r   rQ   r%   )r   r   r   r   r/   r0   r]   r	   r	   r    test_void_to_string_special_case  s     z,TestCasting.test_void_to_string_special_casec              	   C   sX   t tt}t tt}t||}tjtdd |	tdd f W 5 Q R X d S )Nz+casting from object to the parametric DType)matchO)
r   r   r   objectr   r   r   r   	TypeErrorr   )rW   object_dtypeZother_dtyper   r	   r	   r   (test_object_to_parametric_internal_error  s    
z4TestCasting.test_object_to_parametric_internal_errorr   c                 C   s   t tt}t|t |}|td|f\}\}}}|tjksHt|d ksTt||ks`t|tdd f\}\}}}|tjkst|d kst||	dkstd S )Nr   rA   )
r   r   r   r   r   r   r4   r<   r0   r   rW   r   r   r   r   rg   r   r   r	   r	   r   !test_object_and_simple_resolution  s    z-TestCasting.test_object_and_simple_resolutionc                 C   sd   t tt}tt ||}||d f\}\}}}|tjksBt|d ksNt|tdks`td S )Nr   )	r   r   r   r   r   r   r4   r:   r0   r   r	   r	   r    test_simple_to_object_resolution  s    z,TestCasting.test_simple_to_object_resolutionr[   r8   r<   c                 C   sH   t dg}|dk}t jd||d|ks,tt j|d|d|ksDtd S )N)Zfooz<f4)r    r!   r<   V4rZ   )r   r   rV   r0   )rW   r[   r   expectedr	   r	   r   &test_void_and_structured_with_subarray  s    z2TestCasting.test_void_and_structured_with_subarrayexpected_offar   i4f4r"   namesformatsoffsetsr!      c                 C   s~   t ddgddgddgd}tt|t|}|||f\}}}|j|jkr`|tjksntn|tj	ksnt||ksztd S )Nr   r   r   r   r!   r   r   )
r   r   r   r   r   r   r4   r9   r0   r:   )rW   rL   r   rJ   r   r   rg   r   r	   r	   r   test_structured_field_offsets  s    
z)TestCasting.test_structured_field_offsets)rJ   rL   r   )r&   (1,1)ir   )r   r&   r   )(2,1)ir   r   r&   )zi,izi,i,iN)r   r\   r   )r   r   r   )r   ZV10N)r   r   N)r   ZV8N)r   r\   r   )r   r   r   )r\   r   N)r   r   Nc                 C   sJ   t |}t |}tt|t|}|||f\}}}||ksFtd S )N)r   r   r   r   r   r0   )rW   rJ   rL   r   r   rg   r   r	   r	   r   'test_structured_view_offsets_paramteric  s
    %

z3TestCasting.test_structured_view_offsets_paramtericZAllc                 C   s   t d gd }t |}t|jjd|j | d|j ksDtz|	|}W n6 t
k
r   tt
 |	|f W 5 Q R X Y nX t||	| d S )Nr   r   r}   )r   r   r   ctypesmemsetr   r   r   r0   r   r   r   r   r   )rW   r   Z
arr_normalZ	arr_NULLsr   r	   r	   r   'test_object_casts_NULL_None_equivalence  s    
z3TestCasting.test_object_casts_NULL_None_equivalenceZ
AllIntegerZAllFloatc                 C   s>   t jdddgt jdt}||}dddg}t|| d S )Nr   r    irc   r   )r   r   Zint8r   rk   r   r   )rW   r   Znonstandard_boolsresr   r	   r	   r   test_nonstandard_bool_to_other)  s    

z*TestCasting.test_nonstandard_bool_to_other)TT)*r5   r6   r7   rn   r|   r   r   r`   ra   r   r   filterwarningsr   r   r   r4   r8   r   r   r   r   r9   r:   r<   r;   r   r   r   r   r   r   r   r   r   r   r   r   dictr   Z	typecodesr   r   r	   r	   r	   r   rb      s   )
;
!
<
.       
0.<3





 

rb   )r_   r   rD   enumre   r   Znumpyr   Znumpy.lib.stride_tricksr   Znumpy.testingr   Znumpy.core._multiarray_umathr   r   r   r   r/   replacer   r3   IntEnumr4   rM   r   rN   rb   r	   r	   r	   r   <module>   s(   	#*