B
    u9a-                 @   s  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 ejdkZ	d8ddZ
dd	 Zd
d ZG dd dejZejZG dd deZdZG dd deZG dd deZG dd deZG dd deeZG dd deeZG dd deZG dd dejZG dd  d eZG d!d" d"ejZG d#d$ d$ZG d%d& d&eZG d'd( d(eejZG d)d* d*eejZ G d+d, d,eejZ!G d-d. d.eZ"G d/d0 d0e"ejZ#G d1d2 d2e"ejZ$G d3d4 d4e"ejZ%G d5d6 d6ejZ&e'd7kre(  dS )9    N)assert_python_ok)Hashablel           c             C   sR   | dkrt |S t|}x0t|D ]$}d|  d d@ } | d? d@ ||< q"W t |S )zLinear congruential generatorr   iC iÞ& ir      )bytes	bytearrayrange)xlengthouti r   /usr/lib/python3.7/test_hash.pylcg   s    r   c             C   s^   d|   krdk sn t | dkr,| d }n| }| | d? A d@ }|dkrR|d }n|}||fS )z*Convert SipHash24 output to Py_hash_t
    r   l            l        l    il        )AssertionError)Zuint64Zint64Zuint32Zint32r   r   r   	pysiphash   s    

r   c             C   s&   t jjdk}d}|r| S t|| S )z8Skip decorator for tests that depend on SipHash24 or FNV>   fnv	siphash24zRequires SipHash24 or FNV)sys	hash_info	algorithmunittestskip)Ztestokmsgr   r   r   skip_unless_internalhash+   s    r   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )HashEqualityTestCasec             G   sD   t tt|}x0|dd  D ] }||d kr| d|f  qW d S )N   r   zhashed values differ: %r)listmaphashZfail)selfZobjlistZhashedhr   r   r   	same_hash4   s    zHashEqualityTestCase.same_hashc             C   s>   |  dddd |  ddd |  ddd	 |  d
dd d S )Nr   g      ?y      ?        r   g        y                g      y              g       y               )r$   )r"   r   r   r   test_numeric_literals<   s    z*HashEqualityTestCase.test_numeric_literalsc          	   C   s   |  tdtdtdtdtdtd |  tdtd |  tdtd |  tdtd |  tdtd |  tdtd |  td	td	 d S )
Nr   1z1.0i   i  il        l         l            )r$   intfloatcomplex)r"   r   r   r   test_coerced_integersB   s    z*HashEqualityTestCase.test_coerced_integersc             C   s.   |  tdtd |  tdtdd d S )Ng쏈b=~g      ?g        )r$   r)   r*   r+   )r"   r   r   r   test_coerced_floatsM   s    z(HashEqualityTestCase.test_coerced_floatsc             C   sh   dd }xZt dD ]N}xHt dD ]<}||d|  }t||d|  }| t|t| q W qW d S )Ns#   123456789abcdefghijklmnopqrstuvwxyz   r   )r   
memoryviewassertEqualr!   )r"   br   jZalignedZ	unalignedr   r   r   test_unaligned_buffersQ   s    z+HashEqualityTestCase.test_unaligned_buffersN)__name__
__module____qualname__r$   r'   r,   r-   r3   r   r   r   r   r   2   s
   r   c               @   s   e Zd ZdS )DefaultHashN)r4   r5   r6   r   r   r   r   r7   ]   s    r7   *   c               @   s   e Zd Zdd ZdS )	FixedHashc             C   s   t S )N)_FIXED_HASH_VALUE)r"   r   r   r   __hash__a   s    zFixedHash.__hash__N)r4   r5   r6   r;   r   r   r   r   r9   `   s   r9   c               @   s   e Zd Zdd ZdS )OnlyEqualityc             C   s   | |kS )Nr   )r"   otherr   r   r   __eq__e   s    zOnlyEquality.__eq__N)r4   r5   r6   r>   r   r   r   r   r<   d   s   r<   c               @   s   e Zd Zdd ZdS )OnlyInequalityc             C   s   | |k	S )Nr   )r"   r=   r   r   r   __ne__i   s    zOnlyInequality.__ne__N)r4   r5   r6   r@   r   r   r   r   r?   h   s   r?   c               @   s   e Zd ZdS )InheritedHashWithEqualityN)r4   r5   r6   r   r   r   r   rA   l   s    rA   c               @   s   e Zd ZdS )InheritedHashWithInequalityN)r4   r5   r6   r   r   r   r   rB   m   s    rB   c               @   s   e Zd ZdZdS )NoHashN)r4   r5   r6   r;   r   r   r   r   rC   o   s   rC   c               @   s`   e Zd Ze e e gZe e e	 gZ
e e gZdd Zdd Zdd Zdd Zd	d
 ZdS )HashInheritanceTestCasec             C   s(   x"| j D ]}| t|t| qW d S )N)default_expectedr0   r!   _default_hash)r"   objr   r   r   test_default_hash   s    z)HashInheritanceTestCase.test_default_hashc             C   s$   x| j D ]}| t|t qW d S )N)fixed_expectedr0   r!   r:   )r"   rG   r   r   r   test_fixed_hash   s    z'HashInheritanceTestCase.test_fixed_hashc             C   s"   x| j D ]}| tt| qW d S )N)error_expectedZassertRaises	TypeErrorr!   )r"   rG   r   r   r   test_error_hash   s    z'HashInheritanceTestCase.test_error_hashc             C   s*   | j | j }x|D ]}| |t qW d S )N)rE   rI   ZassertIsInstancer   )r"   ZobjectsrG   r   r   r   test_hashable   s    
z%HashInheritanceTestCase.test_hashablec             C   s    x| j D ]}| |t qW d S )N)rK   ZassertNotIsInstancer   )r"   rG   r   r   r   test_not_hashable   s    z)HashInheritanceTestCase.test_not_hashableN)r4   r5   r6   objectr7   r?   rE   r9   rA   rB   rI   rC   r<   rK   rH   rJ   rM   rN   rO   r   r   r   r   rD   r   s   rD   c               @   s$   e Zd ZedZdd Zdd ZdS )DefaultIterSeq
   c             C   s
   t | jS )N)lenseq)r"   r   r   r   __len__   s    zDefaultIterSeq.__len__c             C   s
   | j | S )N)rT   )r"   indexr   r   r   __getitem__   s    zDefaultIterSeq.__getitem__N)r4   r5   r6   r   rT   rU   rW   r   r   r   r   rQ      s   rQ   c               @   s6   e Zd Zeedee edd dgZdd ZdS )HashBuiltinsTestCaserR   c               C   s   dS )Nr   r   r   r   r   r   <lambda>       zHashBuiltinsTestCase.<lambda>r   c             C   s.   t j}x"| jD ]}| t||| qW d S )N)rP   r;   hashes_to_checkr0   r!   )r"   rF   rG   r   r   r   test_hashes   s    z HashBuiltinsTestCase.test_hashesN)	r4   r5   r6   	enumerater   iterrQ   r[   r\   r   r   r   r   rX      s   
rX   c               @   s&   e Zd Zdd ZdddZdd ZdS )	HashRandomizationTestsc             C   s   d| S )Nzprint(hash(eval(%a)))r   )r"   repr_r   r   r   get_hash_command   s    z'HashRandomizationTests.get_hash_commandNc             C   s\   t j }d|d< |d k	r(t||d< n|dd  td| |f|}|d  }t|S )NTZ
__cleanenvZPYTHONHASHSEEDz-cr   )	osenvironcopystrpopr   ra   stripr)   )r"   r`   seedenvr   stdoutr   r   r   get_hash   s    
zHashRandomizationTests.get_hashc             C   s0   | j | jdd}| j | jdd}| || d S )NZrandom)rh   )rk   r`   assertNotEqual)r"   Zrun1Zrun2r   r   r   test_randomized_hash   s    z+HashRandomizationTests.test_randomized_hash)N)r4   r5   r6   ra   rk   rm   r   r   r   r   r_      s   
r_   c            
   @   s   e Zd ZdZdZddddgddddggddddgd	d
d	d
gddddgddddgddddggddddgddddgddddgdd d!d"gd#d$d%d&ggd'Zd(d) Zd*d+ Zed,d- Z	ed.d/ Z
dS )0 StringlikeHashRandomizationTestsNi\iL̇l   LL//+?iTSlqU:~@[ ipGl   nYD~ iSljxY_oWs i l    /l iJkl   J:8`~ il)wJ iQ	l .GUJG iO{l]D] iГl   P'ABS! ilm]]Ri ilTe] iX0l'W=>0 iel;u il   g,g i5$fl`bTNVi l9\R>| iQlkT<h4 )djba33xr   r   c             C   s^   |t jjk rd}nt jj}t jdkr2tr,dnd}nt jdks@ttrHdnd}| j| | | S )Nro   littler   r   Zbig      )r   r   cutoffr   	byteorderIS_64BITr   known_hashes)r"   Zpositionr
   r   platformr   r   r   get_expected_hash   s    
z2StringlikeHashRandomizationTests.get_expected_hashc             C   s<   |  dd}| | | j| | | j| jdd| d S )Nr   rq   )rh   )rx   rl   rk   r`   r0   )r"   Zknown_hash_of_objr   r   r   test_null_hash   s    z/StringlikeHashRandomizationTests.test_null_hashc             C   s(   |  dd}| | j| jdd| d S )Nr   rq   r8   )rh   )rx   r0   rk   r`   )r"   r#   r   r   r   rJ     s    z0StringlikeHashRandomizationTests.test_fixed_hashc             C   s6   | j d krd S | dd}| | j| j dd| d S )Nrr      r8   )rh   )	repr_longrx   r0   rk   )r"   r#   r   r   r   test_long_fixed_hash  s    
z5StringlikeHashRandomizationTests.test_long_fixed_hash)r4   r5   r6   r`   r{   rv   rx   ry   r   rJ   r|   r   r   r   r   rn      s.   





rn   c               @   s<   e Zd ZedZedZedZedd Zedd Z	dS )	StrHashRandomizationTestsabcZabcdefghijku
   äú∑ℇc             C   s   |  tdd d S )N r   )r0   r!   )r"   r   r   r   test_empty_string  s    z+StrHashRandomizationTests.test_empty_stringc             C   sL   |  dd}| | j| jdd| |  dd}| | j| jdd| d S )Nrq      r   )rh      r8   )rx   r0   rk   	repr_ucs2)r"   r#   r   r   r   test_ucs2_string   s    z*StrHashRandomizationTests.test_ucs2_stringN)
r4   r5   r6   reprr`   r{   r   r   r   r   r   r   r   r   r}     s
   r}   c               @   s(   e Zd ZedZedZedd ZdS )BytesHashRandomizationTestss   abcs   abcdefghijkc             C   s   |  tdd d S )NrZ   r   )r0   r!   )r"   r   r   r   r   ,  s    z-BytesHashRandomizationTests.test_empty_stringN)r4   r5   r6   r   r`   r{   r   r   r   r   r   r   r   '  s   r   c               @   s    e Zd ZdZdZedd ZdS ) MemoryviewHashRandomizationTestszmemoryview(b'abc')zmemoryview(b'abcdefghijk')c             C   s   |  ttdd d S )NrZ   r   )r0   r!   r/   )r"   r   r   r   r   5  s    z2MemoryviewHashRandomizationTests.test_empty_stringN)r4   r5   r6   r`   r{   r   r   r   r   r   r   r   0  s   r   c               @   s   e Zd Zdd ZdS )DatetimeTestsc             C   s   d| S )Nz import datetime; print(hash(%s))r   )r"   r`   r   r   r   ra   :  s    zDatetimeTests.get_hash_commandN)r4   r5   r6   ra   r   r   r   r   r   9  s   r   c               @   s   e Zd ZeedddZdS )DatetimeDateTestsi*  rR      N)r4   r5   r6   r   datetimeZdater`   r   r   r   r   r   =  s   r   c            
   @   s&   e Zd ZeedddddddZdS )	DatetimeDatetimeTestsr   rr   rq   r      r      N)r4   r5   r6   r   r   r`   r   r   r   r   r   @  s   r   c               @   s   e Zd ZeedZdS )DatetimeTimeTestsr   N)r4   r5   r6   r   r   Ztimer`   r   r   r   r   r   C  s   r   c               @   s   e Zd Zdd ZdS )HashDistributionTestCasec          
   C   s   d}xt dt|D ]}|d | }| j|dt t }t }x<t dD ]0}t|t| }||d@  ||d@  qHW | t|d| | t|d| W d Q R X qW d S )	NZabcdefghabcdefgr   )prefix      r      r.   )r   rS   ZsubTestsetr!   chraddZassertGreater)r"   baser   r   Zs15Zs255cr#   r   r   r   test_hash_distributionI  s    z/HashDistributionTestCase.test_hash_distributionN)r4   r5   r6   r   r   r   r   r   r   G  s   r   __main__)r   ))r   rb   r   r   Ztest.support.script_helperr   Zcollections.abcr   maxsizeru   r   r   r   ZTestCaser   rP   r;   rF   r7   r:   r9   r<   r?   rA   rB   rC   rD   rQ   rX   r_   rn   r}   r   r   r   r   r   r   r   r4   mainr   r   r   r   <module>   sJ   


*%Q
