o
    8ήcjP                     @   s  d Z ddlZddlm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  m  mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlT ddlT ddl m!Z! e	"de	j#fde	j$fde	j%fde	j&dffgZ'e	"de	j(fde	j#fgZ)e	j"de	j(fde	j#fgddZ*eddZ+eddZ,G dd de-Z.G dd deeZ/G dd de-Z0G d d! d!eZ1e2d"kre
3  dS dS )#z#
Tests for the typeof() machinery.
    N)
namedtuple)types)NumbaValueError)typeof)
OmittedArgcompute_fingerprint)TestCasetag)ValueTypingTestBase)*)numpy_supportabcd   efTalignPoint)xyRect)widthheightc                   @   s   e Zd Zedd ZdS )Customc                 C   s   t t jdS )zn
        Magic attribute expected by Numba to get the numba type that
        represents this object.
        *   )r   UniTuplebooleanself r#   >/tmp/pip-target-vg8gfxp4/lib/python/numba/tests/test_typeof.py_numba_type_-   s   zCustom._numba_type_N)__name__
__module____qualname__propertyr%   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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eejd(d)d* Zd+d, Zd-d. Zd/d0 Zd1S )2
TestTypeofzL
    Test typeof() and, implicitly, typing.Context.get_argument_type().
    c                 C   sz   |  t | tdtj | tdtj | tdtj | tdtj | tdtj | tdtj dS )zB
        Test special.typeof() with scalar number values.
           l        l            l    l         N)check_number_valuesr   assertEqualr   intpint64uint64r!   r#   r#   r$   test_number_values;   s   
zTestTypeof.test_number_valuesc                 C      |  t dS zC
        Test special.typeof() with np.timedelta64 values.
        N)check_datetime_valuesr   r!   r#   r#   r$   test_datetime_valuesH      zTestTypeof.test_datetime_valuesc                 C   r3   r4   )check_timedelta_valuesr   r!   r#   r#   r$   test_timedelta_valuesN   r7   z TestTypeof.test_timedelta_valuesc                    s   fdd}t d}||dddd t ddd}||dddd ||jdd	dd t d
ddd d}||dddd t dd}||dddd d|j_||dddd ||j } 	t
}t| W d   n1 szw   Y   d|jf t|j dS )z<
        Test special.typeof() with ndarray values.
        c                    R   t | } |tj  |j|  |j|  |j|  |j| d S N)	r   assertIsInstancer   Arrayr.   ndimlayoutmutablealigned)arrr>   r?   r@   rA   tyr!   r#   r$   checkX      z+TestTypeof.test_array_values.<locals>.check
   r+   CT   r   F<   N)rH   r      rK   Ar#   r   FzUnsupported array dtype: %s)nparangereshapeTflags	writeableastypedtypenewbyteorderassertRaisesr   r   assertInstr	exception)r"   rD   a1a2a3a4a5raisesr#   r!   r$   test_array_valuesT   s&   

zTestTypeof.test_array_valuesc                    s    fdd}t dt jfdg}t|}t jd|d}|||ddd	 t jd|d}|||ddd	 t jdt jfdgd
d}t|}t jd|d}|||ddd
 t jd|d}|||ddd
 d S )Nc                    r:   r;   )	r   r<   r   r=   r.   rT   r>   r?   rA   )rB   rT   r>   r?   rA   rC   r!   r#   r$   rD   t   rE   z0TestTypeof.test_structured_arrays.<locals>.checkmnS5   rT   r+   rG   FTr   )rM   rT   int32r   from_struct_dtypeemptyrecarray)r"   rD   rT   rec_tyrB   r#   r!   r$   test_structured_arrayss   s   

z!TestTypeof.test_structured_arraysc              	   C   s   d}t |}| |ttjdd | |j t t|}| |tjtjdddd | |j t t		dg d}| |t
tjdd | |j td}t |}| |ttjdd | |j d S )	N   xxr+   rG   T)readonlyir   r+   rH   rF   )r   r.   r   Bytesuint8assertFalser@   
memoryview
MemoryViewarrayPyArrayrg   
assertTrue	bytearray	ByteArray)r"   r   rC   r#   r#   r$   test_buffers   s    zTestTypeof.test_buffersc                 C   s   t d }| |tj d S r;   )r   r.   r   noner"   rC   r#   r#   r$   	test_none      zTestTypeof.test_nonec                 C   s   t t}| |tj d S r;   )r   Ellipsisr.   r   ellipsisr}   r#   r#   r$   test_ellipsis   r   zTestTypeof.test_ellipsisc                 C   s   t d}| |tj d S )Nabc)r   r.   r   stringr}   r#   r#   r$   test_str   r   zTestTypeof.test_strc                 C   sL   dD ]}t | }| t|tj qdD ]}t | }| t|tj qd S )N)r+   r+   rH   )r+   rH   r+   )r+   rH   N))r+   rH   rH   )r+   rH   r,   )NN)sliceassertIsr   r   slice2_typeslice3_type)r"   argsvr#   r#   r$   test_slices   s   zTestTypeof.test_slicesc              
   C   sP   d}|  t|ttjd d}|  t|ttjttjtjff d S )Nr   rH   )r+   )       @rK   )r.   r   r   r   r/   Tuplefloat64)r"   r   r#   r#   r$   test_tuples   s   
zTestTypeof.test_tuplesc                 C   sx   dgd }|  t|tjtjdd ddig}| t}t| W d    n1 s,w   Y  | dt|j	 d S )N      ?d   T	reflectedr+   rK   zCannot type list element type)
r.   r   r   Listr   rV   
ValueErrorrW   rX   rY   )r"   r   bad_vr_   r#   r#   r$   
test_lists   s   


zTestTypeof.test_listsc                 C   sx   t g d}| t|tjtjdd t|}| t}t| W d    n1 s,w   Y  | 	dt
|j d S )N)r   r   g      @Tr   zCannot determine Numba type of)setr.   r   r   Setr   	frozensetrV   r   rW   rX   rY   )r"   r   r_   r#   r#   r$   	test_sets   s   
zTestTypeof.test_setsc                 C   s   t dd}t|}| |ttjdt  t dd}| t|ttjtjgt  tdd}t|}| |ttjdt | 	|| | 	|t
|j|j d S )Nr+   rH   r   rK   re   )r   r   r.   r   NamedUniTupler/   
NamedTupler   r   assertNotEqualr   rT   count)r"   r   tp_pointwtp_rectr#   r#   r$   test_namedtuple   s    



zTestTypeof.test_namedtuplec                 C   s   t tj}| |tttj | |t tj t tj	}| |tttj | |t tj
 | || t tj}| |tttj | |t tj | t}t tj W d    n1 sfw   Y  | t|jd d S NzCCannot type heterogeneous enum: got value types complex128, float64)r   Colorredr.   r   
EnumMemberr/   blueShake	chocolatemintr   RequestError	not_foundIntEnumMemberinternal_errorrV   r   HeterogeneousEnumrX   rY   )r"   tp_redtp_choctp_404r_   r#   r#   r$   	test_enum   s    


zTestTypeof.test_enumc                 C   s   t t}| |tttj t t}| |tttj | || t t}| |t	ttj t t
}| |t	t
tj | || | t}t t W d    n1 s\w   Y  | t|jd d S r   )r   r   r.   r   	EnumClassr/   r   r   ShapeIntEnumClassr   rV   r   r   rX   rY   )r"   tp_colortp_shaketp_shapetp_errorr_   r#   r#   r$   test_enum_class   s$   
zTestTypeof.test_enum_classc                 C   sZ   t d}| t|ttj t dt jfdg}t	|}| t|t| d S )Nr0   ra   rb   )
rM   rT   r.   r   r   DTyper0   rg   r   rh   )r"   rT   rk   r#   r#   r$   
test_dtype   s
   

zTestTypeof.test_dtypec                 C   sl   |  ttjttj |  ttjttj |  ttjttj |  ttjttj d S r;   )	r.   r   rM   r0   r   NumberClassr   rg   int8r!   r#   r#   r$   test_dtype_values  s   zTestTypeof.test_dtype_valuesc                 C   sl   t t}t t}| |tj | |jjtj	f | |jj
tj	 | || | |t|t d S r;   )r   c_cosc_sinr<   r   ExternalFunctionPointerr.   sigr   r   return_typer   get_pointer)r"   ty_costy_sinr#   r#   r$   test_ctypes  s   zTestTypeof.test_ctypeszCFFI not supportedc                 C   s   ddl m} |  t|j}t|j}t|j}| |tj	 | 
|jjtj | 
|jjtjf | 
|jjtj | 
|| tt}| || | ||j||j | 
||j|t d S )Nr   )cffi_usecases)numba.testsr   initr   cffi_coscffi_sin	cffi_boolr<   r   r   r.   r   r   r    r   r   r   r   r   )r"   modty_cffi_costy_cffi_sinty_cffi_booleanty_ctypes_cosr#   r#   r$   	test_cffi  s$   



zTestTypeof.test_cffic                 C   s$   t t }| |ttjd d S )Nr   )r   r   r.   r   r   r    r}   r#   r#   r$   test_custom)  s   
zTestTypeof.test_customc                 C   s   t td}t td}t td}t td}| |td | |td | |td | t|||hd | || d S )N        r+   r   rK   )r   r   r.   r   Omittedlen)r"   ty0ty1ty2ty3r#   r#   r$   test_omitted_args-  s   zTestTypeof.test_omitted_argsc                 C   s<   t j }t|}t|j}| |tj | |tj d S r;   )	rM   randomdefault_rngr   bit_generatorr.   r   npy_rng
npy_bitgen)r"   rngty_rng	ty_bitgenr#   r#   r$   test_np_random8  s
   

zTestTypeof.test_np_randomN)r&   r'   r(   __doc__r2   r6   r9   r`   rl   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   unittest
skipUnlesscffi_support	SUPPORTEDr   r   r   r   r#   r#   r#   r$   r*   6   s4    		

r*   c                   @   s   e Zd Zdd Zdd ZdS )DistinctCheckerc                 C   s   t  | _d S r;   )r   	_distinctr!   r#   r#   r$   __init__C     zDistinctChecker.__init__c                 C   s,   || j v rtd|| j f | j | d S )Nz%r already in %r)r   AssertionErroradd)r"   objr#   r#   r$   r   F  s   
zDistinctChecker.addN)r&   r'   r(   r   r   r#   r#   r#   r$   r   A  s    r   c                   @   s   e Zd 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d Zdd Zdd Zdd Zdd Zd d! Zd"S )#TestFingerprinta  
    Tests for _dispatcher.compute_fingerprint()

    Each fingerprint must denote values of only one Numba type (this is
    the condition for correctness), but values of a Numba type may be
    denoted by several distinct fingerprints (it only makes the cache
    less efficient).
    c                 C   sR   t d}| t d| t t }| t td| | t t | d S )Nr   r   )r   r.   rM   float32r   r   r"   sr#   r#   r$   test_floatsV  
   zTestFingerprint.test_floatsc                 C   sb   t d}dD ]
}| t || qt }dD ]}tt|}|t |  q| t|d| d S )Nr+   )r,   l            )r   int16rg   r0   rr   uint16uint32r1      )r   r.   r   getattrrM   r   r   )r"   r   r   distincttpr#   r#   r$   	test_ints]  s   
zTestFingerprint.test_intsc                 C   s,   t d}| t d| | t d| d S )NTFr+   )r   r.   r   r   r#   r#   r$   	test_booli  s   zTestFingerprint.test_boolc                 C   sR   t d}| |t d t t }| t td| | t t | d S )N              ?y      ?        r   )r   r.   rM   	complex64r   
complex128r   r#   r#   r$   test_complexn  r   zTestFingerprint.test_complexc                 C   s   t d  d S r;   r   r!   r#   r#   r$   r~   u  r   zTestFingerprint.test_nonec                 C   sn   |  t ttj W d    n1 sw   Y  |  t ttj W d    d S 1 s0w   Y  d S r;   )rV   NotImplementedErrorr   r   r   r   r   r!   r#   r#   r$   
test_enumsx  s   "zTestFingerprint.test_enumsc                 C   sp   t dt jfdt jfg}t dt jfdt jfg}t jd|dd }t jd|dd }| t|t| d S )Nra   rc   r+   rf   r   )rM   rT   rg   r0   r   ri   r   r   )r"   d1d2v1v2r#   r#   r$   test_records  s   
zTestFingerprint.test_recordsc                 C   sr   t dd}t dd}t dd}t dd}| t|t| tdd |||fD }| t|d| d S )Nr+   YrH   r   c                 s   s    | ]}t |V  qd S r;   r   ).0r   r#   r#   r$   	<genexpr>  s    z0TestFingerprint.test_datetime.<locals>.<genexpr>rK   )rM   
datetime64timedelta64r.   r   r   r   )r"   r   r   r   r   r  r#   r#   r$   test_datetime  s   
zTestFingerprint.test_datetimec                 C   s  t  }tjdtjd}t|}|| | t|d d | |t|d d d  |t|tj |j	dd |t| tjdtjd}|t| |t|j
 |t|d d d  tjdtjd}|t| tjd	td}t|}|| | t|d d | tjd	td}|t| tjd	td}|t| tjdtd}tjdtd}| t|t| d S )
Nre   rf   r+   rH   F)writere   re   r#   r   )r   rM   ri   r   r   r   r.   rS   r	  setflagsrP   
recordtyperecordtype2recordtype3rj   )r"   r  rB   r   r   r   r#   r#   r$   test_arrays  s8   


zTestFingerprint.test_arraysc                 C   s  t  }td}| td| || |tt  |ttd ttt }|| tddg}|t| | tt|| dD ]}t|g d}|t| |tt| qKtjdtj	d}|t| | tt|| |
d	}|t| |tt| |j}|t| |tt| |d d d
 }|t| |tt| tdd}|t| | tt|| d S )N    rm   Br   birp      rf   r  rH   r,   i @  )r   r   r.   r   ry   rt   rv   rM   ri   rr   rO   rP   mmap)r"   r  r   
m_uint8_1drB   
array_codera   r#   r#   r$   r{     s<   


zTestFingerprint.test_buffersc                 C   s   t  }ttd}| ttd| || dD ]
}|t| q|t |t tjdtd}tjdtd}| t|jt|j d S )Nr0   )rg   zm8[s]zm8[W]zM8[s]r+   rf   )	r   r   rM   rT   r.   r   r  r  rj   )r"   r  r   descrr   r   r#   r#   r$   r     s   


zTestFingerprint.test_dtypec                 C   s   t  }td}| td| || |td |td |td |tddtdf |tddtdf d S )	Nr   )rH   r#   )r+   rH   rK   )r  rH   rK   r+   r   )r   r+   )r   r   r.   r   rM   ri   r"   r  r   r#   r#   r$   r     s   
zTestFingerprint.test_tuplesc                 C   s   t  }tdg}| tddg| || |tdg |tddg |tdg | t tg  W d    d S 1 sEw   Y  d S Nr+   rH   rK   r  g      @g@r   )r   r   r.   r   rV   r   r(  r#   r#   r$   r     s   


"zTestFingerprint.test_listsc                 C   s   t  }ttdg}| ttddg| || |tdg |ttdg |ttddg |ttdg | t tt  W d    n1 sWw   Y  | t ttddg W d    d S 1 suw   Y  d S r)  )	r   r   r   r.   r   rV   r   r  r   r(  r#   r#   r$   r     s   
"zTestFingerprint.test_setsc                 C   sn   t  }td}td}td}t|}| t|| || |t| |td |td d S )Nr   r   r+   )r   r   r   r.   r   )r"   r  v0r  r  r   r#   r#   r$   r   "  s   
z!TestFingerprint.test_omitted_argsc                 C   s$   d }t dD ]}|f}qt|}d S )Ni  )ranger   )r"   tro   r   r#   r#   r$   test_complicated_type0  s   z%TestFingerprint.test_complicated_typeN)r&   r'   r(   r   r   r  r  r  r~   r  r  r  r  r{   r   r   r   r   r   r-  r#   r#   r#   r$   r   L  s$    	
-$r   __main__)4r   rv   collectionsr   enumr$  typing	py_typingnumpyrM   r   numba.core.typing.cffi_utilscore
cffi_utilsr   
numba.corer   numba.core.errorsr   numba.misc.specialr   numba.core.dispatcherr   numba._dispatcherr   numba.tests.supportr	   r
   numba.tests.test_numpy_supportr   numba.tests.ctypes_usecasesnumba.tests.enum_usecasesnumba.npr   rT   r   rg   r	  str_r  r   r  r  r   r   objectr   r*   r   r   r&   mainr#   r#   r#   r$   <module>   sX    

   m