o
    1ήcJ                     @   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mZ ddl	m
Z
 ddlmZ dZedjedjkr@e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      g | ]	}t t|qS  typenpdtype.0cr
   r
   N/tmp/pip-target-vg8gfxp4/lib/python/numpy/core/tests/test_casting_unittests.py
<listcomp>       r   c                  c   sP    t 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)dtype_classdtr
   r
   r   simple_dtype_instances   s   
r   c                 C   s   | t jkrdS | jdv r>| jdkrd}n| jdkrd}n| jdkr$d}n| jdkr,d	}ntd
|  | jdkr<|d7 }|S | jdkrEdS | jdkrLdS | jdkrSdS | jdkrZdS td
|  )zHReturns the string length when casting the basic dtypes to strings.
       iu            
         z!did not find expected length for ig0   G`   f    r   @   )r   bool_kinditemsizeAssertionErrorchar)r   lengthr
   r
   r   get_expected_stringlength&   s0   










r7   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   r8   I   s    r8   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 ]\}}|| || |< qAq/|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   r	   r
   r   r   r
   r
   r   r   m   r   z&_get_cancast_table.<locals>.<listcomp>r   r$   ).~=# r"   )
textwrapdedentstripsplitr8   r@   r?   r>   r=   zip)tabledtypesconvert_castcancastfrom_dtrowto_dtr   r
   r
   r   _get_cancast_tableQ   s   
rT   c                   @   sD   e Zd ZdZejdddgejdg d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J t || dsJ d S )N100r   can_cast)selfrY   rV   r
   r
   r   test_float_to_string   s   z TestChanges.test_float_to_stringc                 C   s   t ddsJ t ddsJ t ddrJ t ddr J t ddr(J t jdddds2J t jd	dd
ds<J t jddd
dsFJ d S )Nr[   VS20V1U1zd,ir?   castingV3r<   V0r]   r_   r
   r
   r   test_to_void   s   zTestChanges.test_to_voidN)	r9   r:   r;   __doc__r   markparametrizer`   rj   r
   r
   r
   r   rU   ~   s    rU   c                   @   s  e Zd ZdZdd Zdnd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g d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g d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g d%fgd&d' Zd(d) Zejd*e	ejd+d,d-gd.d/ Zejd+d,d-gejd0e d1d2 Zejd0g d3ejd+d,d-gd4d5 Zejd6d7d8gejd9d7d8gd:d; Zd<d= Zd>d? Z ejd@e dAdB Z!ejd@e dCdD Z"ejdEdFdGgdHdI Z#ejdJdKgedLdMgdNdOgddPgdQdRfedMdLgdNdOgddPgdQdRfedMdLgdNdOgddSgdQdfgdTdU Z$ejdVdWdXdYdZe%dLgdZgdRgdQdfe%dLgdZgdRgdQdZdRfdZe%dLdMgdZdZgdRdRgdQdfd[d\d]d^d_d`dadbdcddd[gdedf Z&ejd@ej'dg dhdi Z(ejd@ej'dj ej'dk  dldm Z)dS )oTestCastingi  c           	      C   s
  |d u s
|j |j kr| j|j  }n| j|j  }tj||d}|jjs$J |jjs*J dd t|D }t|D ]\}}|||< q7|d u rT|j	dkrPdd |D }||fS |j	dkr`dd |D }tj||d}|jjsmJ |jjssJ t|D ]\}}|||< qw|||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>?c                 S      g | ]}t |qS r
   boolr   vr
   r
   r   r          c                 S   ru   r
   rv   rx   r
   r
   r   r      rz   )
r3   sizer   emptyflagsc_contiguousalignedrange	enumerater5   )	r_   dtype1dtype2r6   arr1valuesr)   valuearr2r
   r
   r   get_data   s*   




zTestCasting.get_dataTc                 C   s,  |r|j j}|j j}n|rd|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sYJ |t| |
jksdJ |rt|	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|r|j jnd}|	|d| 
 }|d	t|ksJ |r|jjsJ |jjsJ n|jjrJ |jjrJ |r|jjsJ |jjsJ ||fS |jjr|j jdksJ |jjr|j jdksJ ||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#   ro   NrB   .r       )r   r3   lenr   zerosuint8nbytesr   viewshapetobytescountr}   r~   r   	alignment)r_   r   r   r   contigstride1stride2	max_size1	max_size2
from_bytesto_bytesnew1new2offsetbufr
   r
   r   get_data_variation   sX   
zTestCasting.get_data_variationfrom_Dtc                 C   s,  t D ]}t||}| |  fD ]}||d fd d }|| ks%J ~| |  fD ]c}|||f\}\}}	}
t||ksDJ t|	|ksLJ |
d urk|tjksWJ tjt| | ksbJ |j	|	j	ksjJ n||krw|j	|	j	kswJ |t| | ksJ ||u r||u sJ ||	u sJ q.qqd S )Nr"   )
r   get_castingimplr   _resolve_descriptorsr   r8   r<   r=   
CAST_TABLEisnative)r_   r   to_DtcastrQ   defaultrS   rf   from_resto_resview_offr
   r
   r   test_simple_cancast   s2   
zTestCasting.test_simple_cancastzignore::numpy.ComplexWarningrQ   c                 C   s  t  D ]}|jd }tt|t|}|||f\}\}}}||us'||ur* dS |tjk}~~~| ||\}	}
}||	|
f |
	 |ksJJ | 
|	|
dd\}}|||f t||
 | |
 ksjJ |jdkrt|jdksw|jsz dS | 
|	|
dd\}}|||f t||
 | |
 ksJ | 
|	|
dd\}}|||f t||
 | |
 ksJ ~~~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   r   r   r   r   r8   r>   r   _simple_strided_calltolistr   r   r   r   _supports_unaligned)r_   rQ   rS   r   rf   r   r   r   r>   r   r   r   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\}\}}}	||u s<J ||u sBJ ~~|t| t| @ sPJ |	d u sVJ t t j}
| ||
\}}}||}t 	d|d< |t dkrd	|d
< |
||f tt t|d
  W d     d S 1 sw   Y   d S |
||f dd | D |ksJ | ||dd\}}|
||f t|| | | ksJ qd S )NM8M8[ms]M8[4D]m8m8[ms]m8[4D]NaT.r   rB   c                 S   ru   r
   )intrx   r
   r
   r   r     rz   z5TestCasting.test_numeric_to_times.<locals>.<listcomp>TF)r   r   r   r   r   r   int64r   r   
datetime64r   r   raises
ValueErrorr   r   r   r   r   )r_   r   rQ   time_dtypestime_dtr   rf   r   r   r   int64_dtr   r   r   r   r   r
   r
   r   test_numeric_to_timesZ  sD   


z!TestCasting.test_numeric_to_times)rQ   rS   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   rB   "  !zm8[ns]r   r   zm8[5ms]zm8[7ms]r   zm8[1M])r   r   r   r   r   r   c                 C   s  t |}|d urt |}t g d}|t d|j}|jj|jks*J t ||d s6J t|}t	||}	|	
||f\}
\}}}||u sQJ ||u s[|d u s[J |
|ksaJ ||ksgJ |d ury|| | |}d|d< nt |}||d< ||}||}t |}|
tjkr|dks|dkrtt |	||f W d    d S 1 sw   Y  d S dD ]'}dD ]"}| ||||\}}d|d< |	||f t|d|d qqd S )	N)r   r"   l    i'  il        r   r   r   .r   r   TT)r   r   arrayastyper   r   isnatr   r   r   r   
empty_liker8   r@   r   r   r   r   r   r   )r_   rQ   rS   r   r   r   r   r   DTyper   rf   r   r   r   expected_outorig_arrorig_outr   r   arroutr
   r
   r   test_time_to_time  sR   
 







zTestCasting.test_time_to_timec                 C   s:   |j dkrdnd}|j| | }t|j |j  | S )NrW   r"   r%   )r5   r3   r   r   r   )r_   r   change_lengthfactr6   r
   r
   r   string_with_modified_length  s   z'TestCasting.string_with_modified_lengthother_DTstring_charrW   rX   c                 C   s  |dkrdnd}t t|}t||}| }t|}t| | }||d f\}	\}
}}|j|| ks9J |	tjks@J |d u sFJ t	||sMJ dD ]1}|dkrYtj}ntj
}| ||}|||f\}	\}}}||u stJ |	|kszJ |d u sJ qOt||}|||f\}	}}|	tjksJ |d u sJ t||}||d f\}	\}}}|	tjksJ |d u sJ ||u sJ d S )NrW   r"   r%   rB   r   r"   r   )r   r   r   r   r7   r   r3   r8   r>   
isinstancer?   r   r@   )r_   r   r   r   	string_DTr   other_dtexpected_length	string_dtsafetyres_other_dtres_dtr   r   expected_safetyrS   rs   r
   r
   r   test_string_cancast  sF   


zTestCasting.test_string_cancastr   c                 C   s  t t|}tt ||}t|t |}||df\}\}}}||ur/|j|jks-J dS | |d\}	}
tjt|	|d}| 	|d}tjt|	|d}| 	|d}tjt|	|d}|j
rcJ |j
rhJ dD ]j}| |	|d|\}}| |	| d|\}}| |	|d|\}}|||f |||f t||| |||f t|| |jdkrqjd|d	< |||f t|	| d|d	< |||f t|	| qjd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).
        Nro   rB   r"   )TFTbr   .)r   r   r   r   r   r   r   r   r   r   r   r   copyr   r   r   r2   )r_   r   r   r   r   	cast_backrs   r   r   r   r   str_arrstring_dt_shortstr_arr_shortstring_dt_longstr_arr_longr   	other_arrr
   r
   r   "test_simple_string_casts_roundtrip  sR   




z.TestCasting.test_simple_string_casts_roundtrip)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sGJ t||sNJ d }|j|krb|jr^t	j
}d}nt	j}n|dkrjt	j}nt	j}||kssJ ||	ksyJ dD ]Y}| ||}|||f\}	\}}}||u sJ |dkr||ksJ n|d u sJ |t	jkr|	|ksJ q{|dk r|	t	jksJ q{|dkr|	|ksJ q{|dkr|	t	jksJ q{d S )NrW   r"   r%   r   rX   r   )r   r   r5   r   r   r3   r   r   r   r8   r<   r=   r>   r@   r   r?   )r_   r   r   r   divr   r   r   r   r   r   r   r   r   r   r   rS   rs   r
   r
   r   test_string_to_string_cancastI  sT   



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r9J |jjr?J d}||d< || fD ]}||d< |d |ksYJ | d |kscJ qKd S )NU30y   ro   r"   u   this is a ünicode string‽r
   .)	r   r   r|   r   r   r   r}   r   r   )	r_   r   r   r   r   data1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J t jg dddjjdks J d S )NV5ro   rW   r    rX   r(   )r   r   r   r   r3   ri   r
   r
   r    test_void_to_string_special_case  s    $z,TestCasting.test_void_to_string_special_casec                 C   sn   t tt}t tt}t||}tjtdd |	tdd f W d    d S 1 s0w   Y  d S )Nz+casting from object to the parametric DType)matchO)
r   r   r   objectr   r   r   r   	TypeErrorr   )r_   object_dtype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s$J |d u s*J ||u s0J |tdd f\}\}}}|tjksFJ |d u sLJ ||dksUJ d S )Nr  rE   )	r   r   r   r  r   r   r8   r@   r   r_   r   r  r   r   rs   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s!J |d u s'J |tdu s0J d S )Nr  )r   r   r   r  r   r   r8   r>   r  r
   r
   r    test_simple_to_object_resolution  s   z,TestCasting.test_simple_to_object_resolutionrf   r<   r@   c                 C   sH   t dg}|dk}t jd||d|ksJ t j|d|d|ks"J d S )N)fooz<f4)r#   r$   r@   V4re   )r   r   r^   )r_   rf   r   expectedr
   r
   r   &test_void_and_structured_with_subarray  s   z2TestCasting.test_void_and_structured_with_subarrayrS   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r0|tjks/J n|tjks7J ||ks=J d S )Nr  r   r  r  r$   r  r  )	r   r   r   r   r   r  r8   r=   r>   )r_   rS   r  rQ   r   r   rs   r   r
   r
   r   test_structured_field_offsets  s   
z)TestCasting.test_structured_field_offsets)rQ   rS   r  )r)   (1,1)ir   )r  r)   r   )(2,1)ir  r   r)   )zi,izi,i,iN)r  rg   r   )r  r  r   )r  V10N)r  r  N)r  V8N)r  rg   r   )r  r  r   )rg   r  N)r  r  Nc                 C   sJ   t |}t |}tt|t|}|||f\}}}||ks#J d S )N)r   r   r   r   r   )r_   rQ   rS   r  r   rs   r   r
   r
   r   'test_structured_view_offsets_paramteric  s
   
%
z3TestCasting.test_structured_view_offsets_paramtericAllc                 C   s   t d gd }t d gd }| d|j ksJ z||}W n' tyI   tt ||f W d    Y d S 1 sAw   Y  Y d S w t	||| d S )Nr    r   )
r   r   r   r   r   r   r  r   r   r   )r_   r   
arr_normal	arr_NULLsr  r
   r
   r   'test_object_casts_NULL_None_equivalence  s   &z3TestCasting.test_object_casts_NULL_None_equivalence
AllIntegerAllFloatc                 C   s:   t jg dt jdt}||}g d}t|| d S )N)r   r#   iro   )r   r"   r"   )r   r   int8r   rw   r   r   )r_   r   nonstandard_boolsresr  r
   r
   r   test_nonstandard_bool_to_other!  s   
z*TestCasting.test_nonstandard_bool_to_otherr   )*r9   r:   r;   r{   r   r   r   rl   rm   r   r   filterwarningsr   r   r   r8   r<   r   r   r   r   r=   r>   r@   r?   r   r   r   r   r   r   r   r  r  r	  r  r  dictr  	typecodesr   r&  r
   r
   r
   r   rn      s    
#;

!<
.


0.<3






 
rn   )rk   r   rH   enumrq   numpyr   numpy.lib.stride_tricksr   numpy.testingr   numpy.core._multiarray_umathr   r   r   r   r3   replacer   r7   IntEnumr8   rT   r   rU   rn   r
   r
   r
   r   <module>   s(    	#*