o
    ^Zh=                     @   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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dlmZ ddlmZ ddlmZmZ dd	lmZmZmZmZ d
d Zdd Zdd ZG dd deZ G dd deZ!dddddddddddgddgddiddiddidej"dgj#e$de$ddddddddid gZ%ed!e%ed"e%d#d$ Z&d%d& Z'ed'd(ed)d* Z(d+d, Z)d-d. Z*eed/d0d1gd2d3 Z+ed4d5 Z,eed6d7d8gd9d: Z-eeej.d;kd<d=d>d? Z/d@dA Z0dBdC Z1edDdE Z2edFdG Z3dHdI Z4dJdK Z5dLdM Z6dNdO Z7edPdQ Z8edRdSdTdUdVdW e9dXD dYfdZg d[d\d]fgd^d_ Z:ed`da Z;edbdc Z<eddde Z=edfdg Z>dhdi Z?djdk Z@dS )lz
Test the hashing module.
    N)ProcessPoolExecutor)Decimal)filter_argshash)Memory)np
with_numpy)fixtureparametrizeraisesskipifc                 C   s   | S N )sr   r   G/var/www/auris/lib/python3.10/site-packages/joblib/test/test_hashing.pyunicode      r   c                 G   s>   t  }tdD ]}t }| |  |t |  qt|S )zTime function func on *args.   )listrangetimeappendmin)funcargstimes_t1r   r   r   	time_func"   s   r   c                 G   s<   t | g|R  }t |g|R  }dt|| ||   }|S )zKReturn the relative time between func1 and func2 applied on
    *args.
          ?)r   abs)Zfunc1Zfunc2r   Z
time_func1Z
time_func2relative_diffr   r   r   relative_time,   s   r#   c                   @   s   e Zd Zdd ZdS )Klassc                 C      |S r   r   selfxr   r   r   f7   r   zKlass.fN)__name__
__module____qualname__r)   r   r   r   r   r$   6   s    r$   c                   @   s   e Zd Zdd Zdd ZdS )KlassWithCachedMethodc                 C   s   t |d}|| j| _d S )N)location)r   cacher)   )r'   ZcachedirZmemr   r   r   __init__<   s   
zKlassWithCachedMethod.__init__c                 C   r%   r   r   r&   r   r   r   r)   @   r   zKlassWithCachedMethod.fN)r*   r+   r,   r0   r)   r   r   r   r   r-   ;   s    r-         g      ?       @y      ?      ?y       @      ?abr1   )r1   r1   r4   r1   )r4   r1   r7   )r4   r1   dobj1obj2c                 C   s(   t | t |k}| |u }||ksJ dS )z!Smoke test hash on various types.Nr   )r9   r:   are_hashes_equalZare_objs_identicalr   r   r   test_trivial_hashm   s   r<   c                  C   s^   t td} t| jt| jksJ ttd}ttd}t|jt|jks-J d S )Nr4   
   	   )	ioStringIOr   r   flushcollectionsdequer   extend)r4   Za1Za2r   r   r   test_hash_methodsw   s
   rE   function)scopec                  C   s@   t jd} | d}| }| }|d  d7  < |||fS )Nr   r=   r=   r1   )r   randomRandomStaterandom_samplecopy)rndarr1arr2arr3r   r   r   three_np_arrays   s   

rQ   c                 C   sf   | \}}}t j| ddD ]\}}t|t|k}t||k}||ks%J qt|t|jks1J d S )Nr2   )repeat)	itertoolsproductr   r   allT)rQ   rN   rO   rP   r9   r:   r;   Zare_arrays_equalr   r   r   test_hash_numpy_arrays   s   
rW   c                 C   sT   | \}}}||d}||d}||d}t |t |ksJ t |t |ks(J d S )N)r1   r2   r   )rQ   rN   rO   rP   Zd1Zd2Zd3r   r   r   test_hash_numpy_dict_of_arrays   s   



rX   dtypezdatetime64[s]ztimedelta64[D]c                 C   s2   t td}tjdd| d}t ||ksJ d S )Nr=   r   rY   )r   r   arange)rY   Za_hasharrayr   r   r   test_numpy_datetime_array   s   r]   c                  C   sp   t jt ddddd d d dd d f } t | }t| t|ks'J t | }t| t|ks6J d S )Nip  )  r2   r   F)orderr1   )r   asarrayr[   reshapeZascontiguousarrayr   asfortranarray)r4   r5   cr   r   r   test_hash_numpy_noncontiguous   s
   0

re   coerce_mmapTFc                 C   s   |  dj}z.tj|ddd}t|}t||dt||dk}||ks&J W dt v r3~t  dS dS dt v r@~t  w w )zECheck that memmap and arrays hash identically if coerce_mmap is True.Zmemmap_temprH   zw+)shapemode)rf   mN)	joinstrpathr   Zmemmapra   r   localsgccollect)tmpdirrf   filenameri   r4   r;   r   r   r   test_hash_memmap   s   


rq   win32z5This test is not stable under windows for some reason)reasonc                  C   s|   t jd} | d}dd }t|t|}|dk sJ dt|| }tt|||f}dt|| ||   }|dk s<J dS )	a  Check the performance of hashing numpy arrays:

    In [22]: a = np.random.random(1000000)

    In [23]: %timeit hashlib.md5(a).hexdigest()
    100 loops, best of 3: 20.7 ms per loop

    In [24]: %timeit hashlib.md5(pickle.dumps(a, protocol=2)).hexdigest()
    1 loops, best of 3: 73.1 ms per loop

    In [25]: %timeit hashlib.md5(cPickle.dumps(a, protocol=2)).hexdigest()
    10 loops, best of 3: 53.9 ms per loop

    In [26]: %timeit hash(a)
    100 loops, best of 3: 20.8 ms per loop
    r   i@B c                 S   s   t t|  S r   )hashlibmd5
memoryview	hexdigest)r(   r   r   r   md5_hash   s   z-test_hash_numpy_performance.<locals>.md5_hashg333333?r   r    N)r   rI   rJ   rK   r#   r   r   r!   )rM   r4   rx   r"   Ztime_hashlibZ	time_hashr   r   r   test_hash_numpy_performance   s   
ry   c                  C   s8   t  } t  }tt| jg dtt|jg dksJ dS )z}Make sure that calling the same method on two different instances
    of the same class does resolve to the same hashes.
    r6   N)r$   r   r   r)   r4   r5   r   r   r   test_bound_methods_hash   s   ,r{   c                 C   sD   t | j}t | j}tt|jjg dtt|jjg dks J dS )zMake sure that calling the same _cached_ method on two different
    instances of the same class does resolve to the same hashes.
    r6   N)r-   rk   r   r   r)   r   )ro   r4   r5   r   r   r   test_bound_cached_methods_hash   s
   

r|   c                  C   sP   t jdd tdD td} t jdd tdD td}t| t|ks&J dS )z;Make sure that ndarrays with dtype `object' hash correctly.c                 S      g | ]}t |qS r   r   r[   .0ir   r   r   
<listcomp>	      z*test_hash_object_dtype.<locals>.<listcomp>   rZ   c                 S   r}   r   r~   r   r   r   r   r   
  r   N)r   r\   r   objectr   rz   r   r   r   test_hash_object_dtype  s   r   c                  C   s,   t d} t d}t| t|ksJ d S )Nr3   g      @)r   float64r   rz   r   r   r   test_numpy_scalar  s   

r   c                 C   sp   t | j}dgdgdgdgdgdgdgdgdgdgdgdgdgd}||}||}t|t|ks6J d S )N!   z#s12069__c_maps.nii.gzz#s12158__c_maps.nii.gzz#s12258__c_maps.nii.gzz#s12277__c_maps.nii.gzz#s12300__c_maps.nii.gzz#s12401__c_maps.nii.gzz#s12430__c_maps.nii.gzz#s13817__c_maps.nii.gzz#s13903__c_maps.nii.gzz#s13916__c_maps.nii.gzz#s13981__c_maps.nii.gzz#s13982__c_maps.nii.gzz#s13983__c_maps.nii.gz)r-   rk   r)   r   )ro   kr8   r4   r5   r   r   r   test_dict_hash  s$   


r   c                 C   sB   t | j}tg d}||}||}t|t|ksJ d S )Nr   )r-   rk   setr)   r   )ro   r   r   r4   r5   r   r   r   test_set_hash3  s   


r   c                   C   s8   t ttdtdgt ttdtdgksJ d S )Nr   NaN)r   r   r   r   r   r   r   test_set_decimal_hashP  s   r   c                  C   sD   d} | di}| di}t t |}t||gt||gks J d S )Nfoobar)pickleloadsdumpsr   )stringr4   r5   rd   r   r   r   test_stringX  s
    r   c                  C   sX  t d} t d}| |u sJ t| t|ksJ tt| }| |us(J t| t|ks2J t| | gt||gks@J t| | gt| |gksNJ t dt jdfdt jdfg}t dt jdfdt jdfg}t|t|ksvJ tt|}||usJ t|t|ksJ t||gt||gksJ t||gt||gksJ d S )NZf4name   Zgrades)r2   )r   rY   r   r   r   r   Zstr_r   )Zdt1Zdt2Zdt1_roundtrippedZcomplex_dt1Zcomplex_dt2Zcomplex_dt1_roundtrippedr   r   r   test_numpy_dtype_picklingb  s*   
	

r   zto_hash,expected)zThis is a string to hashZ 71b3f47df22cb19431d85d92d0b230b2)u   C'est létéZ 2d8d189e9b2b0b2e384d93c868c0e576))i@ i1  i3~Z e205227dd82250871fa25aa0ec690aa3c                 C   s   g | ]	}t d   qS )*   )rI   Randomr   r   r   r   r   r     s    r      Z a11ffad81f9682a7d901e6edc3d16c84{   )ir2   r   )ZabcdeZsadfasZ aeda150553d4bb5c69f0e69d51b0e2efc                 C   s   t | |ksJ d S r   r   )Zto_hashexpectedr   r   r   test_hashes_stay_the_same  s   r   c                  C   s8   t jd} | d}t |}t|t|ksJ d S )Nr   rH   )r   rI   rJ   rK   rc   r   )rngZarr_cZarr_fr   r   r   Atest_hashes_are_different_between_c_and_fortran_contiguous_arrays  s   

r   c                   C   s   t td d S Nr   r   r   r\   r   r   r   r   test_0d_array  s   r   c                   C   s&   t tdt tdgksJ d S r   r   r   r   r   r   )test_0d_and_1d_array_hashing_is_different  s   &r   c            
      C   s   dd } |  }|  }t dd}t dd}z9t||D ](\}}|t| }|t| }||ks4J |t| }	||	ksBJ qW |  |  d S |  |  w )Nc                     s   t jd  jdddddt fddtd	D  fd
dtd	D  dd	d jdddd ddgdt jddd	dt 
t jddd	dt jddd	dd d d df g} | S )Nr   ir^   2   )highsizez<i8c                 3   s     | ]}  d dV  qdS )r   <f4Nrandnastyper   r   r   r   	<genexpr>  s    z_test_hashes_stay_the_same_with_numpy_objects.<locals>.create_objects_to_hash.<locals>.<genexpr>r   c                    s   g | ]
}  d dqS )r   r   r   r   r   r   r   r     s    z`test_hashes_stay_the_same_with_numpy_objects.<locals>.create_objects_to_hash.<locals>.<listcomp>r   r   r=      )r   )ir   d   rZ   rH   r2   )r   rI   rJ   randintr   tupler   r   r[   rb   rc   )Zto_hash_listr   r   r   create_objects_to_hash  s   	"zLtest_hashes_stay_the_same_with_numpy_objects.<locals>.create_objects_to_hashr1   )max_workers)r   zipsubmitr   resultshutdown)
r   Zto_hash_list_oneZto_hash_list_twoe1e2Zobj_1Zobj_2Zhash_1Zhash_2Zhash_3r   r   r   ,test_hashes_stay_the_same_with_numpy_objects  s"   


r   c                  C   sH   dd } t tj}t|  W d    n1 sw   Y  |d d S )Nc                   S   s   dS )Nr   r   r   r   r   r   non_picklable  r   z2test_hashing_pickling_error.<locals>.non_picklablezPicklingError while hashing)r   r   PicklingErrorr   match)r   excinfor   r   r   test_hashing_pickling_error  s
   
r   c                  C   sJ   d} t t| d ddi}t|dd W d    d S 1 sw   Y  d S )Nz!Valid options for 'hash_name' are)r   r   r   invalid)	hash_name)r   
ValueErrorr   )msgdatar   r   r   test_wrong_hash_name  s
   "r   )A__doc__rB   rm   rt   r?   rS   r   rI   sysr   concurrent.futuresr   decimalr   Zjoblib.func_inspectr   Zjoblib.hashingr   Zjoblib.memoryr   Zjoblib.test.commonr   r	   Zjoblib.testingr
   r   r   r   r   r   r#   r   r$   r-   rn   r   r   Z
input_listr<   rE   rQ   rW   rX   r]   re   rq   platformry   r{   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s    

&		
	"	
	


(
	
	


E	