B
     \=0                 @   s   d Z ddlmZ ddlmZ ddlZddlmZ ddl	Z
ddlmZmZmZ ddlmZ ddlmZmZmZ ddlmZ ejrdd	lmZ d
d ZG dd deZG dd deZG dd deZeejdG dd deZedkre  dS )z*
Test hashing of various supported types.
    )print_functionN)defaultdict)jittypesutils   )TestCasetagCompilationCache)hashing)compile_time_get_string_datac             C   s   t | S )N)hash)x r   7lib/python3.7/site-packages/numba/tests/test_hashing.pyhash_usecase   s    r   c               @   s:   e Zd Zdd Zdd ZejfddZdd Zd	d
 Z	dS )BaseTestc             C   s   t ddt| _d S )NT)nopython)r   r   cfunc)selfr   r   r   setUp   s    zBaseTest.setUpc             C   s"  | j }xt|D ]}||}| |tj tj}|st|ttfst	
| oht|tjpht| }|dk}|dk ot||k}t|dk }	|o|o| o|	}|ry| |t| W q tk
r }
 z<td t||t| td tt|tjd  |
W d d }
~
X Y qX qW d S )Nr   izval, nb_hash, hash(val)z%abs(val), hashing._PyHASH_MODULUS - 1r   )r   listZassertIsInstancer   Z	INT_TYPESIS_PY3
isinstancestrtuplenpZiscomplexobjfloat
is_integerabsassertEqualr   AssertionErrorprintr   Z_PyHASH_MODULUS)r   valuesr   valZnb_hashZproceedZintinputZnonzeroZintminZnotlonger   r   r   check_hash_values    s.    zBaseTest.check_hash_valuesc             c   s   xzdD ]r}t |}|j|  kr,|jks0q qd}t||| V  t||d|  dV  t||d|  dV  dgV  qW d S )N)r   ii`  l        d      )r   iinfominmaxrange)r   typstartinfonr   r   r   int_samples<   s    

zBaseTest.int_samplesc          	   c   s   t |}xdd|jd |jd fD ]x}d}t|j||j }x\d|d |fD ]J}||k r\qNt ||||  |}||}|V  | V  ||  V  qNW q$W |dddd	td
td
 tdg}|V  d S )Nr   
   g      ?g     @@r'   g333333?g        g       g      infnan)	r   Zfinfor,   ZtinyZ
resolutionZlinspaceZastypeZmeanr   )r   r.   r0   r/   r1   Zmin_stepstepar   r   r   float_samplesG   s    

zBaseTest.float_samplesc             c   sb   x\|  |D ]N}xH|  |D ]:}|d t| }|d t| }||d|  }|V  qW qW d S )Ny              ?)r8   len)r   r.   float_tyrealimagr7   r   r   r   complex_samples[   s    zBaseTest.complex_samplesN)
__name__
__module____qualname__r   r&   r   int64r2   r8   r=   r   r   r   r   r      s
   r   c               @   sf   e Zd ZdZdd Zdd Zeddd Zedd	d
 Zdd Z	dd Z
eejddd ZdS )TestNumberHashingz'
    Test hashing of number types.
    c             C   s6   x0|  |D ]"}| |jt| | | qW d S )N)r8   r    dtyper   r&   )r   r.   r7   r   r   r   check_floatsj   s    zTestNumberHashing.check_floatsc             C   s8   x2|  ||D ]"}| |jt| | | qW d S )N)r=   r    rC   r   r&   )r   r.   r:   r7   r   r   r   check_complexo   s    zTestNumberHashing.check_complex	importantc             C   s   |  tj |  tj d S )N)rD   r   float32float64)r   r   r   r   test_floatst   s    zTestNumberHashing.test_floatsc             C   s$   |  tjtj |  tjtj d S )N)rE   r   	complex64rG   
complex128rH   )r   r   r   r   test_complexy   s    zTestNumberHashing.test_complexc             C   s   |  ddg d S )NFT)r&   )r   r   r   r   	test_bool~   s    zTestNumberHashing.test_boolc             C   s  g }xlt jt jt jt jt jt jt jt jgD ]B}x| 	|D ]}| 
| q>W t |}| 
|dg | 
|dg dt|k}| j}x|j|jgD ]}d| }|}	xt|D ]}
d}d}|	g}x,||gD ] }|	|@ }|tjk r|| qW x|D ]}| 
||g qW |rHx,|D ]$}||jkr | 
|| g q W |dkr`|	dB d> }	q|	d? }	qW qW q,W | 
t dg | 
t d	g | 
t dg | 
t d	g d S )
Nr)   r   Zuint   l   *UU*UU
 l   UU*UU* r   l    l    )r   int8uint8int16uint16int32uint32rA   uint64r2   r&   r*   r   itemsizer+   r,   r-   sysmaxsizeappend)r   Zminmaxtyr7   r0   signedZszr   ZshiftsyiZtwiddle1Ztwiddle2ZvalsZtwr$   vr   r   r   	test_ints   sD    




zTestNumberHashing.test_intszPython 2 only testc                s    fdd}d}d}d}xDt jt jt jt jt jt jt jt jgD ]}||||||| q>W d}d}d}x,t j	t j
gD ]}||||||| qxW d}d}d}x,t jt jgD ]}||||||| qW d S )	Nc                s<     | }  |}  |} ||  ||k d S )N)r   r    assertFalse)Zval1Zval2Zval3a1_hasha2_hasha3_hash)r   r   r   check   s
    


z*TestNumberHashing.test_py27.<locals>.checkr      g82?gG`	@y82?G`@yG`	@#0@)r   rO   rP   rQ   rR   rS   rT   rA   rU   rG   rH   rJ   rK   )r   rd   a1a2a3rZ   r   )r   r   	test_py27   s"    zTestNumberHashing.test_py27N)r>   r?   r@   __doc__rD   rE   r	   rI   rL   rM   r_   unittestskipIfr   r   ri   r   r   r   r   rB   e   s   .rB   c               @   sF   e Zd ZdZdd Zdd Zeddd Ze	e
jd	d
d ZdS )TestTupleHashingz!
    Test hashing of tuples.
    c                s.   x(|D ] } fdd|D }|  | qW d S )Nc                s   g | ]} |qS r   r   ).0r7   )splitr   r   
<listcomp>   s    z1TestTupleHashing.check_tuples.<locals>.<listcomp>)r&   )r   Zvalue_generatorro   r#   Ztuplesr   )ro   r   check_tuples   s    
zTestTupleHashing.check_tuplesc                sB   t j  fdd} fdd}| |  | | |  | d S )Nc                s     | } |  d@ |  d@ fS )z9
            Split i's bits into 2 integers.
            l   UU*UU* l   *UU*UU
 r   )r]   )r.   r   r   split2   s    
z8TestTupleHashing.test_homogeneous_tuples.<locals>.split2c                s*    | } |  d@ |  d@ |  d@ fS )z9
            Split i's bits into 3 integers.
            l   $$$$ l   $I$I$I$I l   IIII	 r   )r]   )r.   r   r   split3   s    

z8TestTupleHashing.test_homogeneous_tuples.<locals>.split3)r   rU   rq   r2   )r   rr   rs   r   )r.   r   test_homogeneous_tuples   s
    	
z(TestTupleHashing.test_homogeneous_tuplesrF   c             C   s    d}dd }|  |  | d S )Nl            c             S   s4   | d@ }| d@ | d? d@ A }t |t |d fS )Nl   UU*UU* l   *UU     g-C6?)r   rA   rH   )r]   r7   br   r   r   ro      s    z9TestTupleHashing.test_heterogeneous_tuples.<locals>.split)rq   r2   )r   Zmoduloro   r   r   r   test_heterogeneous_tuples   s    z*TestTupleHashing.test_heterogeneous_tupleszPython 2 only testc             C   sH   d}d}d}|  |}|  |}|  |}| || | ||k d S )N)r      re   )r   rx      )r   r    r`   )r   rf   rg   rh   ra   rb   rc   r   r   r   ri      s    


zTestTupleHashing.test_py27N)r>   r?   r@   rj   rq   rt   r	   rw   rk   rl   r   r   ri   r   r   r   r   rm      s
   rm   z$unicode hash tests are Python 3 onlyc               @   s>   e Zd Zdd Zdd Zdd Zeddd	 Zd
d Z	dS )TestUnicodeHashingc             C   s   d}x(t t|D ]}| |d | g qW d}|t|}x(t t|D ]}| |d | g qNW d}|t|}x(t t|D ]}| |d | g qW d}| | d S )Nabcdefghijklmnopqrstuvwxyzu   眼u   🐍⚡ )r-   r9   r&   joinr   )r   kind1_stringr]   sepZkind2_stringZkind4_stringZempty_stringr   r   r   test_basic_unicode  s    z%TestUnicodeHashing.test_basic_unicodec             C   sD   d}t dddd }t|d }| |dk | ||| d S )Nr{   T)r   c             S   s   | j S )N)_hash)r   r   r   r   fn&  s    z4TestUnicodeHashing.test_hash_passthrough.<locals>.fnr)   )r   r   
assertTruer    )r   r~   r   
hash_valuer   r   r   test_hash_passthrough!  s
    z(TestUnicodeHashing.test_hash_passthroughc             C   sH   d}t dddd }t|d }| |dk | ||||f d S )Nr{   T)r   c             S   s   | j t| fS )N)r   r   )r   r   r   r   r   2  s    z9TestUnicodeHashing.test_hash_passthrough_call.<locals>.fnr)   )r   r   r   r    )r   r~   r   r   r   r   r   test_hash_passthrough_call.  s
    z-TestUnicodeHashing.test_hash_passthrough_callz/Needs hash computation at const unpickling timec             C   s8   t dddd }| }td}| |t|d  d S )NT)r   c              S   s   d} | S )Nr{   r   )r   r   r   r   r   >  s    z0TestUnicodeHashing.test_hash_literal.<locals>.fnr{   r)   )r   r   r    r   )r   r   r$   Ztmpr   r   r   test_hash_literal:  s    z$TestUnicodeHashing.test_hash_literalc             C   s   dd }t dd|}d}||}||}t|}t|}| |d d |d d  | |d |d k d}||}||}t|}t|}| || d S )Nc             S   s    d}d}|| }| rt | |S )NZaaaau   眼眼眼眼)r   )Zdo_hashZconst1Zconst2newr   r   r   implI  s    z:TestUnicodeHashing.test_hash_on_str_creation.<locals>.implT)r   Fr)   )r   r   r    r   )r   r   ZjittedZcompute_hashZexpectedZgotr7   rv   r   r   r   test_hash_on_str_creationF  s    z,TestUnicodeHashing.test_hash_on_str_creationN)
r>   r?   r@   r   r   r   rk   skipr   r   r   r   r   r   rz     s
   rz   __main__) rj   Z
__future__r   Znumba.unittest_supportZunittest_supportrk   rW   collectionsr   Znumpyr   Znumbar   r   r   Zsupportr   r	   r
   Znumba.targetsr   r   Znumba.unicoder   r   r   rB   rm   Z
skipUnlessrz   r>   mainr   r   r   r   <module>   s&   Jk<Z