U
    ?h                      @   sx  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Zd dl	m
Z
mZ d dlmZmZ d dlmZmZmZ d dlZd dlmZ d dlmZmZmZ d dlmZmZ d d	lmZ d d
lmZm Z m!Z! dd Z"dd Z#ee"Z$ee#Z%e e"Z&e e#Z'e Z(e Z)dd Z*dd Z+dEddZ,dd Z-dd Z.dd Z/dd Z0dFddZ1d d! Z2d"d# Z3G d$d% d%e4Z5G d&d' d'e5Z6G d(d) d)e6eZ7G d*d+ d+e6eZ8G d,d- d-e4Z9G d.d/ d/e9e6eZ:G d0d1 d1e5Z;G d2d3 d3e;eZ<G d4d5 d5e;eZ=G d6d7 d7e5eZ>G d8d9 d9eZ?G d:d; d;eZ@G d<d= d=eZAd>d? ZBG d@dA dAeeZCG dBdC dCeeZDeEdDkrteF  dS )G    N)KeysView)compile_isolatedFlags)jitnjit)typesutilserrors)testing)TestCaseMemoryLeakMixintag)make_py_quicksortmake_jit_quicksort)make_jit_mergesort)make_py_timsortmake_jit_timsortMergeRunc                 C   s   | d g| S Nr    keysnr   r   G/var/www/html/venv/lib/python3.8/site-packages/numba/tests/test_sort.pymake_temp_list   s    r   c                 C   s   t || jS N)npemptydtyper   r   r   r   make_temp_array   s    r   c                 C   s   |    d S r   sortvalr   r   r   sort_usecase*   s    r$   c                 C   s   |   S r   argsortr"   r   r   r   argsort_usecase-   s    r'   Fc                 C   s    |r| j ddS | j ddS d S N	mergesortkind	quicksortr%   r#   	is_stabler   r   r   argsort_kind_usecase0   s    r/   c                 C   s   t | S r   sortedr"   r   r   r   sorted_usecase6   s    r2   c                 C   s   t | |dS )Nreverser0   )r#   br   r   r   sorted_reverse_usecase9   s    r6   c                 C   s
   t | S r   )r   r!   r"   r   r   r   np_sort_usecase<   s    r7   c                 C   s
   t | S r   r   r&   r"   r   r   r   np_argsort_usecase?   s    r9   c                 C   s$   |rt j| ddS t j| ddS d S r(   r8   r-   r   r   r   np_argsort_kind_usecaseB   s    r:   c                 C   sJ   t jd g }t| D ]}|t j  q|d d  }|  ||fS N*   r   randomseedrangeappendr!   )r   lillr   r   r   list_sort_usecaseH   s    rE   c                 C   sN   t jd g }t| D ]}|t j  q|d d  }|j|d ||fS )Nr<   r3   r=   )r   r5   rB   rC   rD   r   r   r   list_sort_reverse_usecaseQ   s    rF   c                   @   sp   e Zd ZdddZdddZdddZdd	d
ZdddZdddZdd Z	dd Z
dd Zdd Zdd ZdS )BaseSortingTest
   c                 C   s*   t d tt||| }t | |S r;   )r>   r?   listr@   shuffle)selfr   offsetrB   r   r   r   random_list]   s    

zBaseSortingTest.random_listc                 C   s   t t||| S r   rI   r@   rK   r   rL   r   r   r   sorted_listc   s    zBaseSortingTest.sorted_listc                 C   s   t t||| d d d S )NrN   rO   r   r   r   revsorted_listf   s    zBaseSortingTest.revsorted_listNc                 C   s>   |d kr|d }|  ||}|| j|| |d | d7 }|S )N   rQ   rL   )rP   rM   )rK   r   mrL   rB   r   r   r   initially_sorted_listi   s
    z%BaseSortingTest.initially_sorted_listc                 C   sh   t d |d kr tt|}tt||||  d |d  d | }t||ksZtt 	| |S )Nr<      )
r>   r?   intmathsqrtrI   r@   lenAssertionErrorrJ   rK   r   factorrL   rB   r   r   r   duprandom_listp   s    
*
zBaseSortingTest.duprandom_listc                 C   sh   |d krt t|}tt||||  d |d  d | }t||ks\tt||f|  |S NrW   )rX   rY   rZ   rI   r@   r[   r\   r!   r]   r   r   r   dupsorted_listy   s    *zBaseSortingTest.dupsorted_listc                 C   s,   |  t|t| |  t|t| d S r   )assertEqualr[   rI   r1   rK   origresultr   r   r   assertSorted   s    zBaseSortingTest.assertSortedc                 C   s   |  t|t| |  t|t| tt||dd d}tt||}|  || tt|d D ]B}|| ||d   \}}	\}
}||
krh| ||	|| qhd S )Nc                 S   s   | d S r   r   xr   r   r   <lambda>       z4BaseSortingTest.assertSortedValues.<locals>.<lambda>keyrW   )rb   r[   rI   r1   zipr@   
assertLessindex)rK   rd   orig_valuesre   Zresult_valuesZ
zip_sortedZ
zip_resultrC   Zk1Zv1Zk2Zv2r   r   r   assertSortedValues   s    z"BaseSortingTest.assertSortedValuesc                 c   s"   d}d}|V  |||  }}qd S r`   r   )rK   ar5   r   r   r   fibo   s    zBaseSortingTest.fiboc                 C   s6   g }dD ](}| | || | | || q|S N)   x   )rA   rP   ra   rK   r   listsrL   r   r   r   make_sample_sorted_lists   s
    z(BaseSortingTest.make_sample_sorted_listsc                 C   sZ   g }dD ]L}| | || | | || | | || | | || q|S rt   )rA   rP   ra   rR   r_   rw   r   r   r   make_sample_lists   s    z!BaseSortingTest.make_sample_lists)rH   )rH   )rH   )NrH   )NrH   )NrH   )__name__
__module____qualname__rM   rP   rR   rV   r_   ra   rf   rq   rs   ry   rz   r   r   r   r   rG   [   s   




	
rG   c                   @   s|   e 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S )BaseTimsortTestc                 C   s   | j j}||S r   )timsort
merge_init)rK   r   fr   r   r   r      s    zBaseTimsortTest.merge_initc                    s   d}d fdd	}j j |}||| ||||d  |}||| ||d }||| ||||d  |}||| |}||| |}||| d S )Nru   r   c                    s*    | } ||d|| | | d S r   )array_factoryrf   )rB   r   startresr   rK   r   r   check   s    
z.BaseTimsortTest.test_binarysort.<locals>.checkrS   )r   )r   
binarysortrP   rR   rV   rM   r_   rK   r   r   rB   r   r   r   test_binarysort   s"    









zBaseTimsortTest.test_binarysortc                    s   d}t tdd| d fdd	}jj |}||| ||||d  |}||| ||d }||| ||||d  |}||| |}||| |}||| d S )Nru   d   r   c                    s8    | } } ||d|| | || d S r   )r   rq   )rB   r   r   r   Zres_vr   rK   vr   r   r      s    

z:BaseTimsortTest.test_binarysort_with_values.<locals>.checkrS   )r   )	rI   r@   r   r   rP   rR   rV   rM   r_   r   r   r   r   test_binarysort_with_values   s$    









z+BaseTimsortTest.test_binarysort_with_valuesc                    s   d}j j  fdd}j|dd}||d| ||d|d  ||dd j|dd}||d| ||d|d  ||dd j|dd}tt|d D ]}|||| qj|dd}tt|d D ]}|||| qd S )	N   c                    s     | ||\}}|rt||| d D ]&}| | | |d   }}|| q,|| |k r| || d  | ||   nht||| d D ]&}| | | |d   }}|| q|| |k r| || d  | ||  |  d S r`   )r   r@   assertGreaterassertLessEqual)rB   lohir   desckrr   r5   r   r   r   r      s    "z-BaseTimsortTest.test_count_run.<locals>.checkr   rT   r   rW   rS   )r   Z	count_runrP   rR   rM   r@   r[   r_   )rK   r   r   rB   rC   r   r   r   test_count_run   s"    zBaseTimsortTest.test_count_runc                    sf   dj jfdd  fddfdd}jdd	}|| jdd	}|| d S )
Nru   c                    s`    || |||} || || ||krD| |d  | ||k r\ | | | d S r`   )assertGreaterEqualr   rn   rB   rl   r   stophintr   r   r   r   r   
  s    z/BaseTimsortTest.test_gallop_left.<locals>.checkc                    s$   t ||D ]} | |||| q
d S r   r@   rB   rl   r   r   r   r   r   r   check_all_hints  s    z9BaseTimsortTest.test_gallop_left.<locals>.check_all_hintsc                    sf    | } | d | d | d d| d dfD ]6} | |d  | |dd   | |dd  q*d S 	N      r   irQ     rW      r   rB   rl   r   r   rK   r   r   check_sorted_list  s
    
$z;BaseTimsortTest.test_gallop_left.<locals>.check_sorted_listr   rT   )r   Zgallop_leftrP   ra   rK   r   rB   r   r   r   r   r   rK   r   test_gallop_left  s    
z BaseTimsortTest.test_gallop_leftc                    sf   dj jfdd  fddfdd}jdd	}|| jdd	}|| d S )
Nru   c                    s`    || |||} || || ||krD| |d  | ||k r\| | | d S r`   )r   r   r   r   r   r   r   r   (  s    z0BaseTimsortTest.test_gallop_right.<locals>.checkc                    s$   t ||D ]} | |||| q
d S r   r   r   r   r   r   r   2  s    z:BaseTimsortTest.test_gallop_right.<locals>.check_all_hintsc                    sf    | } | d | d | d d| d dfD ]6} | |d  | |dd   | |dd  q*d S r   r   r   r   r   r   r   6  s
    
$z<BaseTimsortTest.test_gallop_right.<locals>.check_sorted_listr   rT   )r   Zgallop_rightrP   ra   r   r   r   r   test_gallop_right$  s    
z!BaseTimsortTest.test_gallop_rightc                 C   s   | j j}tddD ]}| ||| qtddD ]*}d| tjkrH q^| |d| d q2|  D ]v}|dk rtqf|tjkr q||}| |d | |d |dkrf|| }dt	
| }| || | |d|  qfd S )	Nr   @      ?   rS         ?)r   Zmerge_compute_minrunr@   rb   sysmaxsizers   r   r   r   
bit_lengthrn   )rK   r   rC   r   quotpr   r   r   test_merge_compute_minrunB  s(    
z)BaseTimsortTest.test_merge_compute_minrunc                 C   s   t |}t |}dg| | dg }| |}| |}d}	|	| }
|||||	||
|}| |d |d  | |d |d  | |dd |dd  | t |jt |j | t |jt |j | |j	|j	 | |j
d d S Nr<   rW   r   rQ   )r[   r   r   rb   rf   r   r   valuesassertIspendingZ
min_gallop)rK   funcrr   r5   nanb	orig_keysr   msssassbnew_msr   r   r   check_merge_lo_hiZ  s    

z!BaseTimsortTest.check_merge_lo_hic                 C   s`   | j j}| j j}dD ]F\}}t| || |D ]$\}}| ||| | ||| q4qd S )N)   r   (   r   r   n   )r   iL  )r   Zmerge_loZmerge_hi	itertoolsproductry   r   )rK   Zf_loZf_hir   r   rr   r5   r   r   r   test_merge_lo_hip  s    z BaseTimsortTest.test_merge_lo_hic           	         sn  j j t|t|dg| | dg d }tdd fdd}}|}j |}|j}j |t}j |t|}||||}|j|d  }|}j |}|j}j |t}j |t|}t| d}j ||}||||}|j|d  |j	|jd  | d S )Nr<   r   rW   c                    s    | |||} |d d   |d d  dd |dd  |j| j  | j|  f  | jd  |S )Nr   rQ   rW   )rb   rf   r   r   )r   r   rC   r   r   r   r   r   rK   r   Zstack_sentinelr   r   run_merge_at  s    z4BaseTimsortTest.check_merge_at.<locals>.run_merge_atrS   )
r   Zmerge_atr[   r   r   r   merge_appendr   rb   r   )	rK   rr   r5   r   r   r   r   rC   Zlast_runr   r   r   check_merge_at{  s6    




zBaseTimsortTest.check_merge_atc                 C   sL   dD ]B\}}t | || |D ] \}}| || | || q$qd S )N)r   r   r   )r   i  )r   r   ry   r   )rK   r   r   rr   r5   r   r   r   test_merge_at  s    zBaseTimsortTest.test_merge_atc           
   
      s   j j}dg}||d d d d  |D ]}tj fdd|D  D ]}t|g } |} |}d}|D ](}	 j |t	|t
|	}|t
|	7 }qp t|j|jd  t
| ||||} |jd  |jd t	dt
|  || qFq*d S )N)r   rH   r   ru   r   rQ   c                 3   s   | ]}  |V  qd S r   )ry   .0r   rK   r   r   	<genexpr>  s   z<BaseTimsortTest.test_merge_force_collapse.<locals>.<genexpr>rW   )r   Zmerge_force_collapserA   r   r   sumr   r   r   r   r[   rb   r   r   rf   )
rK   r   Z
sizes_listsizeschunksr   r   r   poscr   r   r   test_merge_force_collapse  s&    


 z)BaseTimsortTest.test_merge_force_collapsec                    sf    j j}dD ]Td} fdd|D }tj| D ],}t|g } |}||  || q2qd S )N)rW   rH   )r      ru   c                    s   g | ]}  | qS r   rz   r   rK   size_factorr   r   
<listcomp>  s     z4BaseTimsortTest.test_run_timsort.<locals>.<listcomp>)r   Zrun_timsortr   r   r   r   rf   rK   r   r   	all_listsr   r   r   r   r   r   test_run_timsort  s    

z BaseTimsortTest.test_run_timsortc                 C   s   | j j}dD ]z}d| }| |}| |}| |}|| | }ttddt| }| |}	| |}
||	|
 | 	|||	|
 qd S )NrW   r   P   r   )
r   Zrun_timsort_with_valuesra   r_   rR   rI   r@   r[   r   rq   )rK   r   r   
chunk_sizerr   r5   r   r   rp   r   r   r   r   r   test_run_timsort_with_values  s    





z,BaseTimsortTest.test_run_timsort_with_valuesN)r{   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r~      s   $1r~   c                   @   s   e Zd ZeZeZdS )TestTimsortPurePythonN)r{   r|   r}   py_list_timsortr   rI   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZeZdd ZdS )TestTimsortArraysPurePythonc                 C   s   t j|t jdS Nr   r   arrayZint32rK   lstr   r   r   r     s    z)TestTimsortArraysPurePython.array_factoryN)r{   r|   r}   py_array_timsortr   r   r   r   r   r   r     s   r   c                   @   s$   e Zd ZeZdZdZi fddZdS )JITTimsortMixinNc                    s>   | f}||kr|| S |j |j fdd}|||< |S )z
        Wrap *func* into another compiled function inserting a runtime-created
        mergestate as the first function argument.
        c                    s   | } || |f| }|S r   r   )r   r   argsr   r   r   r   r   r   wrapper  s    z5JITTimsortMixin.wrap_with_mergestate.<locals>.wrapper)r   compile)rK   r   r   _cacherl   r   r   r   r   wrap_with_mergestate	  s    z$JITTimsortMixin.wrap_with_mergestate)r{   r|   r}   jit_array_timsortr   r   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd Zdd Zdd ZdS )TestTimsortArraysc                 C   s   t j|t jdS r   r   r   r   r   r   r      s    zTestTimsortArrays.array_factoryc                 C   s   t |}t |}| | j|}dg| | dg }| |}d}|| }	||||||	|}
| |d |d  | |d |d  | |dd |dd  d S r   )r[   r   r   r   rb   rf   )rK   r   rr   r5   r   r   r   r   r   r   r   r   r   r   r   #  s    
z#TestTimsortArrays.check_merge_lo_hiN)r{   r|   r}   r   r   r   r   r   r   r     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 )BaseQuicksortTestc                    s   d} fdd}j j |}||| |}||| ||d }||| |}||| |}||| |}||| d S )Nru   c                    s\    dg|  dg } ||d| |d d |d d | |dd  d S Ni'  irW   r   rQ   )r   rb   rf   )rB   r   r   r   r   r   r   <  s
    z4BaseQuicksortTest.test_insertion_sort.<locals>.checkrS   )r,   Zinsertion_sortrP   rR   rV   rM   r_   r   r   r   r   test_insertion_sort:  s    









z%BaseQuicksortTest.test_insertion_sortc                    s   d} fdd}j j |}||| |}||| ||d }||| |}||| |}||| |}||| d S )Nru   c                    s    dg|  dg } ||d|}|d d |d d || }td|D ]}|| | qVt|d |D ]}|| | qzd S r   )r   rb   r@   r   r   )rB   r   r   ro   pivotrC   r   r   r   r   S  s    z/BaseQuicksortTest.test_partition.<locals>.checkrS   )r,   	partitionrP   rR   rV   rM   r_   r   r   r   r   test_partitionQ  s    









z BaseQuicksortTest.test_partitionc                    s   d} fdd}j j |}||| |}||| ||d }||| |}||| |}||| |}||| d S )Nru   c                    s    dg|  dg } |d|\}}|d d |d d || }td|D ]}|| | qXt||d D ]}|| | q|t|d |D ]}|| | qd S r   )r   rb   r@   r   r   )rB   r   r   ltgtr   rC   r   r   r   r   o  s    z0BaseQuicksortTest.test_partition3.<locals>.checkrS   )r,   Z
partition3rP   rR   rV   rM   r_   r   r   r   r   test_partition3l  s    









z!BaseQuicksortTest.test_partition3c                    sf    j j}dD ]Td} fdd|D }tj| D ],}t|g } |}||  || q2qd S )Nr   r   ru   c                    s   g | ]}  | qS r   r   r   r   r   r   r     s     z8BaseQuicksortTest.test_run_quicksort.<locals>.<listcomp>)r,   run_quicksortr   r   r   r   rf   r   r   r   r   test_run_quicksort  s    

z$BaseQuicksortTest.test_run_quicksortc              	      s  dd } j |dj}dD ]^d} fdd|D }tj| D ]6}t|g } |}||  ||d d d  q@qd	d
 } j |dj}tj	d dD ]r}	tjj|	dd }
t
d|
tjj|	ddk < t|
} |}|| |
t|
  } ||d t|  qd S )Nc                 S   s   | |kS r   r   rr   r5   r   r   r   r    s    z3BaseQuicksortTest.test_run_quicksort_lt.<locals>.lt)r  r   r  c                    s   g | ]}  | qS r   r   r   r   r   r   r     s     z;BaseQuicksortTest.test_run_quicksort_lt.<locals>.<listcomp>rQ   c                 S   s   t |p| |k S r   rY   isnanr  r   r   r   	lt_floats  s    z:BaseQuicksortTest.test_run_quicksort_lt.<locals>.lt_floatsr<   r   ru   2   r   sizer   nan皙?)make_quicksortr  r   r   r   r   rf   r   r>   r?   floatrI   r
  r[   )rK   r  r   r   r   r   r   r   r  r  rd   Znon_nansr   r   r   test_run_quicksort_lt  s*    


z'BaseQuicksortTest.test_run_quicksort_ltN)r{   r|   r}   r   r  r  r  r  r   r   r   r   r   6  s
   r   c                   @   s   e Zd ZeZeeZeZ	dS )TestQuicksortPurePythonN)
r{   r|   r}   py_quicksortr,   staticmethodr   r  rI   r   r   r   r   r   r    s   r  c                   @   s    e Zd ZeZeeZdd ZdS )TestQuicksortArraysc                 C   s   t j|t jdS r   )r   r   float64r   r   r   r   r     s    z!TestQuicksortArrays.array_factoryN)	r{   r|   r}   jit_quicksortr,   r  r   r  r   r   r   r   r   r    s   r  c                   @   sH   e Zd ZeddZeeZdd ZdddZdd	 Z	d
d Z
dd ZdS )#TestQuicksortMultidimensionalArraysT)is_np_arrayc                 C   s    |  |j|j | || d S r   )rb   shapeassertPreciseEqualrc   r   r   r   rf     s    z0TestQuicksortMultidimensionalArrays.assertSortedNc                 C   s8   t j|t jd}|d kr*|d|jd S ||S d S )Nr   rQ   r   )r   r   r  Zreshaper  )rK   r   r  r   r   r   r   r     s    z1TestQuicksortMultidimensionalArrays.array_factoryc                 C   s   g }|dkr|S t dtt|d D ]l}|| dkr(||| |f |||| f | || }|D ]$}||f|  |||f  qnq(|S )NrW   rS   r   )r@   rX   rY   rZ   rA   
get_shapes)rK   r   ZshapesrC   Z_shapes_shaper   r   r   r    s    z.TestQuicksortMultidimensionalArrays.get_shapesc           
         s    j j}dD ]d} fdd|D }tj| D ]h}t|g } t|}|d  |D ]<} j||d} j||d}	|| |		   
|	| q\q2qd S )Nr   r  c                    s   g | ]}  | qS r   r   r   r   r   r   r     s     zJTestQuicksortMultidimensionalArrays.test_run_quicksort.<locals>.<listcomp>r  )r,   r  r   r   r   r  r[   rA   r   r!   rf   )
rK   r   r   r   r   r   
shape_listr  r   	keys_copyr   r   r   r    s    

z6TestQuicksortMultidimensionalArrays.test_run_quicksortc                    sx  dd } j |ddj}dD ]d} fdd|D }tj| D ]p}t|g } t|}|d  |D ]D} j||d	}	 j||d	 }
||	 |
	  |
 }
 
|
|	 qlqBqd
d } j |ddj}tjd dD ]}tjj|dd }td|tjj|ddk < t|} t|}|d  |D ]>} j||d	}	 j||d	}
||	 |
	   
|
|	 q2qd S )Nc                 S   s   | |kS r   r   r  r   r   r   r    s    zETestQuicksortMultidimensionalArrays.test_run_quicksort_lt.<locals>.ltT)r  r  r   r  c                    s   g | ]}  | qS r   r   r   r   r   r   r     s     zMTestQuicksortMultidimensionalArrays.test_run_quicksort_lt.<locals>.<listcomp>r!  c                 S   s   t |p| |k S r   r	  r  r   r   r   r    s    zLTestQuicksortMultidimensionalArrays.test_run_quicksort_lt.<locals>.lt_floatsr<   r  r  r   r  r  )r  r  r   r   r   r  r[   rA   r   r!   rf   r   r>   r?   r  rI   )rK   r  r   r   r   r   r   r"  r  r   r#  r  r  rd   r   r   r   r    s>    


z9TestQuicksortMultidimensionalArrays.test_run_quicksort_lt)N)r{   r|   r}   r   r,   r  r  rf   r   r  r  r  r   r   r   r   r    s   

r  c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zi f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 )!TestNumpySortc                 C   s   t jd d S r;   r   r>   r?   r   r   r   r   setUp3  s    zTestNumpySort.setUpc                 c   s    dD ]}t jjd|dV  qd S )Nr  c   r  )r   r>   randint)rK   r  r   r   r   
int_arrays6  s    zTestNumpySort.int_arraysc                 c   s   dD ]}t jj|dd V  qdD ]6}t jj|dd }td|t jj|ddk < |V  q"dD ]6}t jj|dd }td|t jj|ddk < |V  q^d S )Nr  r  r   r  r  )r  r   r   )r   r>   r  )rK   r  rd   r   r   r   float_arrays:  s    zTestNumpySort.float_arraysc                 C   s2   t t |dkrdS t |j|jk r.dS dS )zM
        Whether the array has duplicates.  Takes NaNs into account.
        rW   TF)r   Zcount_nonzeror
  uniquer  )rK   arrr   r   r   has_duplicatesH  s
    zTestNumpySort.has_duplicatesc                 C   s4   t  |}t  |}|| || | || d S r   copyr  )rK   pyfunccfuncr#   expectedgotr   r   r   check_sort_inplaceR  s
    

z TestNumpySort.check_sort_inplacec                 C   s6   t  |}||}||}| || | || d S r   r.  )rK   r0  r1  r#   rd   r2  r3  r   r   r   check_sort_copyY  s
    
zTestNumpySort.check_sort_copyc                 C   sb   t  |}||f|}||f|}| j|| t|dd | |sR| || | || d S )Nzthe array wasn't argsorted)msg)r/  r  r   r!   r-  )rK   r0  r1  r#   kwargsrd   r2  r3  r   r   r   check_argsorta  s    

zTestNumpySort.check_argsortc                 C   s2   t }tdd|}|  D ]}| ||| qd S NTZnopython)r$   r   r)  r4  rK   r0  r1  rd   r   r   r   test_array_sort_intn  s    z!TestNumpySort.test_array_sort_intc                 C   s2   t }tdd|}|  D ]}| ||| qd S r9  )r$   r   r*  r4  r;  r   r   r   test_array_sort_floatu  s    z#TestNumpySort.test_array_sort_floatc                 C   s2   t }tdd|}|  D ]}| ||| qd S r9  )r7   r   r)  r5  r;  r   r   r   test_np_sort_int|  s    zTestNumpySort.test_np_sort_intc                 C   sZ   t }tdd|}dD ]>}tjj|dd }td|tjj|ddk < | ||| qd S )NTr:  r  r  r   r  r  )r7   r   r   r>   r  r5  )rK   r0  r1  r  rd   r   r   r   test_np_sort_float  s    z TestNumpySort.test_np_sort_floatc                    s     fdd}|t  |t d S )Nc                    s.   t dd| }  D ]} | || qd S r9  )r   r)  r8  r0  r1  rd   r   r   r   r     s    z-TestNumpySort.test_argsort_int.<locals>.checkr'   r9   rK   r   r   r   r   test_argsort_int  s    zTestNumpySort.test_argsort_intc                    s@    fdd}|t dd |tdd |t dd |tdd d S )Nc              	      s6   t dd| }  D ]} | ||t|d qd S NTr:  r.   )r   r)  r8  dictr0  r.   r1  rd   r   r   r   r     s
    
z2TestNumpySort.test_argsort_kind_int.<locals>.checkTrE  Fr/   r:   rB  r   r   r   test_argsort_kind_int  s
    z#TestNumpySort.test_argsort_kind_intc                    s     fdd}|t  |t d S )Nc                    s.   t dd| }  D ]} | || qd S r9  )r   r*  r8  r@  r   r   r   r     s    /TestNumpySort.test_argsort_float.<locals>.checkrA  rB  r   r   r   test_argsort_float  s    z TestNumpySort.test_argsort_floatc                    s@    fdd}|t dd |tdd |t dd |tdd d S )Nc              	      s6   t dd| }  D ]} | ||t|d qd S rD  )r   r*  r8  rF  rG  r   r   r   r     s
    
rJ  TrE  FrH  rB  r   r   r   rK    s
    c              	   C   s8   t ddt}d}| tj|}|d  W 5 Q R X d S )NTr:  z#.*Argument "a" must be array-like.*)r   r7   assertRaisesRegexr	   TypingError)rK   r1  r6  raisesr   r   r   test_bad_array  s    zTestNumpySort.test_bad_arrayN)r{   r|   r}   r&  r)  r*  r-  r4  r5  r8  r<  r=  r>  r?  rC  rI  rK  rO  r   r   r   r   r$  1  s    
			r$  c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestPythonSortc                 C   sH   t }tdd|}dD ],}||\}}| t|| | || qd S )NTr:  ru   r  r   )rE   r   rb   r1   assertNotEqual)rK   r0  r1  r  rd   retr   r   r   test_list_sort  s    zTestPythonSort.test_list_sortc                 C   sX   t }tdd|}dD ]<}dD ]2}|||\}}| t||d| | || qqd S )NTr:  rQ  FTr3   )rF   r   rb   r1   rR  )rK   r0  r1  r  r5   rd   rS  r   r   r   test_list_sort_reverse  s    z%TestPythonSort.test_list_sort_reversec                 C   s^   t }tdd|}dD ]B}tjj|dd }t|}||}| || | t|| qd S )NTr:  rQ  r  r   )r2   r   r   r>   r1   r  rR  rI   )rK   r0  r1  r  rd   r2  r3  r   r   r   test_sorted  s    zTestPythonSort.test_sortedc                 C   sh   t }tdd|}d}tjj|dd }dD ]6}t||d}|||}| || | t|| q,d S )NTr:  ru   r  r   rU  r3   )r6   r   r   r>   r1   r  rR  rI   )rK   r0  r1  r  rd   r5   r2  r3  r   r   r   test_sorted_reverse  s    
z"TestPythonSort.test_sorted_reverseN)r{   r|   r}   rT  rV  rW  rX  r   r   r   r   rP    s   	
rP  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestMergeSortc                 C   s   t jd d S )NiA  r%  r   r   r   r   r&    s    zTestMergeSort.setUpc                 C   s8   t j|||}t j|dd}||}t j|| d S )Nr)   r*   )r   r>   r(  r&   r
   Zassert_equal)rK   sorterlowhighcountdataexpectr3  r   r   r   check_argsort_stable  s    z"TestMergeSort.check_argsort_stablec                    sJ   ddddg}t dd}|j t fdd}|D ]}| j|f|  q0d S )	N)rS   r   )r   rH   )r   rH   e   )r   r   i  T)Z
is_argsortc                    s    | S r   r   )r,  toplevelr   r   ri     rj   z3TestMergeSort.test_argsort_stable.<locals>.<lambda>)r   Zrun_mergesortr   r`  )rK   ZarglistimprZ  r   r   rd  r   test_argsort_stable  s    
z!TestMergeSort.test_argsort_stableN)r{   r|   r}   r&  r`  rg  r   r   r   r   rY    s   rY  c                 C   s   | S r   r   rg   r   r   r   ri     rj   ri   c                   @   sD   e 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S )TestSortSlashSortedWithKeyc                 C   s~   ddddddg}t dd }t d
dd	}| ||d d  ||d d   | ||d d  |||d d  | d S )N   rW      r   	   c                 S   s   d|  S Ng      ?r   zr   r   r   external_key  s    z8TestSortSlashSortedWithKey.test_01.<locals>.external_keyc                 S   s0   | d d  }|j |d t| d d  |d|fS Nrk   r!   r1   rh   rl   new_xr   r   r   foo  s    z/TestSortSlashSortedWithKey.test_01.<locals>.foo)Nr   r  py_funcrK   rr   ro  rt  r   r   r   test_01  s    
&z"TestSortSlashSortedWithKey.test_01c                 C   sF   ddddddg}t dd }| ||d d  ||d d   d S )Nri  rW   rj  r   rk  c                 S   s8   dd }| d d  }|j |d t| d d  |d|fS )Nc                 S   s   d|  S rl  r   rm  r   r   r   closure_key   s    zDTestSortSlashSortedWithKey.test_02.<locals>.foo.<locals>.closure_keyrk   rq  )rh   ry  rs  r   r   r   rt    s    z/TestSortSlashSortedWithKey.test_02.<locals>.fooru  )rK   rr   rt  r   r   r   test_02  s    
z"TestSortSlashSortedWithKey.test_02c                 C   sH   ddddddg}dd }|  |t|d d  |t|d d   d S )Nri  rW   rj  r   rk  c                    s    | dd  |  fdd}|S )Nc                 S   s0   | d d  }|j |d t| d d  |d|fS rp  rq  )rh   r   rs  r   r   r   bar.  s    z<TestSortSlashSortedWithKey.test_03.<locals>.gen.<locals>.barc                    s   dd } | |S )Nc                 S   s   d|  S rl  r   rm  r   r   r   closure_escapee_key6  s    zYTestSortSlashSortedWithKey.test_03.<locals>.gen.<locals>.foo.<locals>.closure_escapee_keyr   )rh   r|  r{  r   r   rt  4  s    z<TestSortSlashSortedWithKey.test_03.<locals>.gen.<locals>.foor   )compilerrt  r   r}  r   gen,  s
    
z/TestSortSlashSortedWithKey.test_03.<locals>.gen)r  r   nop_compiler)rK   rr   r  r   r   r   test_03(  s    z"TestSortSlashSortedWithKey.test_03c                 C   s~   ddddddg}t dd }t d
dd	}| ||d d  ||d d   | ||d d  |||d d  | d S )Nrr   r5   BCAc                 S   s   |   S r   upperrm  r   r   r   ro  B  s    z8TestSortSlashSortedWithKey.test_04.<locals>.external_keyc                 S   s0   | d d  }|j |d t| d d  |d|fS rp  rq  rr  r   r   r   rt  F  s    z/TestSortSlashSortedWithKey.test_04.<locals>.foo)Nru  rw  r   r   r   test_04>  s    
&z"TestSortSlashSortedWithKey.test_04c              	   C   sv   ddddddg}t dd }t dd	d
}td |fdD ]6\}}| ||d d  ||||d d  || q:d S )Nrr   r5   r  r  r  c                 S   s   |   S r   r  rm  r   r   r   ro  T  s    z8TestSortSlashSortedWithKey.test_05.<locals>.external_keyFc                 S   s4   | d d  }|j ||d t| d d  ||d|fS Nrl   r4   rq  rh   rl   r4   rs  r   r   r   rt  X  s    z/TestSortSlashSortedWithKey.test_05.<locals>.foo)TFrW   ir   )NF)r   r   r   r  rv  )rK   rr   ro  rt  rl   revr   r   r   test_05P  s    

z"TestSortSlashSortedWithKey.test_05c              	   C   sd   ddddddg}t dd }| tj}d}||d d  | W 5 Q R X d	}| |t|j d S )
Nri  rW   rj  r   rk  c                 S   sB   |rdd }nd }| d d  }|j |d t| d d  |d|fS )Nc                 S   s   d|  S rl  r   rm  r   r   r   ry  i  s    zQTestSortSlashSortedWithKey.test_optional_on_key.<locals>.foo.<locals>.closure_keyrk   rq  )rh   	predicatery  rs  r   r   r   rt  f  s    
z<TestSortSlashSortedWithKey.test_optional_on_key.<locals>.fooTz<Key must concretely be None or a Numba JIT compiled functionr   ZassertRaisesr	   rM  ZassertInstr	exception)rK   rr   rt  rN  ZTFr6  r   r   r   test_optional_on_keyc  s    
z/TestSortSlashSortedWithKey.test_optional_on_keyc              
   C   s   t ddd}t ddd}t dd }dd	d
d	ddg}||fD ]z}| tj}||dd W 5 Q R X d}| |t|j | tj}|||dd W 5 Q R X d}| |t|j q@d S )NFc                 S   s   t | d d  ||dS r  r0   )rh   rl   r4   r   r   r   
foo_sorted|  s    zETestSortSlashSortedWithKey.test_exceptions_sorted.<locals>.foo_sortedc                 S   s   | d d  }|j ||d |S r  r    r  r   r   r   foo_sort  s    zCTestSortSlashSortedWithKey.test_exceptions_sorted.<locals>.foo_sortc                 S   s   d|  S rl  r   rm  r   r   r   ro    s    zGTestSortSlashSortedWithKey.test_exceptions_sorted.<locals>.external_keyri  rW   rj  r   rk  illegalrk   z1Key must be None or a Numba JIT compiled functionzgo backwardsr  z$an integer is required for 'reverse')NF)NFr  )rK   r  r  ro  rr   implrN  r_  r   r   r   test_exceptions_sortedz  s     
z1TestSortSlashSortedWithKey.test_exceptions_sortedN)
r{   r|   r}   rx  rz  r  r  r  r  r  r   r   r   r   rh    s   rh  c                   @   s   e Zd ZdZdd ZdS )TestArrayArgsortzTests specific to array.argsortc              	   C   s   t dd }| tj}|d W 5 Q R X d}| |t|j t dd }| tj}|  W 5 Q R X d}| |t|j d S )Nc                 S   s   t dj| d d S )Nr   r*   r   Zaranger&   r*   r   r   r   nonliteral_kind  s    z9TestArrayArgsort.test_exceptions.<locals>.nonliteral_kindr,   z"kind" must be a string literalc                   S   s   t djdd d S )Nr    )rt  r  r   r   r   r   unsupported_kwarg  s    z;TestArrayArgsort.test_exceptions.<locals>.unsupported_kwargzUnsupported keywords: ['foo']r  )rK   r  rN  r_  r  r   r   r   test_exceptions  s    

z TestArrayArgsort.test_exceptionsN)r{   r|   r}   __doc__r  r   r   r   r   r    s   r  __main__)F)F)Gr/  r   rY   r>   r   typingr   numpyr   Znumba.core.compilerr   r   Znumbar   r   Z
numba.corer   r   r	   Zunittestr
   Znumba.tests.supportr   r   r   Znumba.misc.quicksortr   r   Znumba.misc.mergesortr   Znumba.misc.timsortr   r   r   r   r   r   r   Zjit_list_timsortr   r  r  r$   r'   r/   r2   r6   r7   r9   r:   rE   rF   objectrG   r~   r   r   r   r   r   r  r  r  r$  rP  rY  r  rh  r  r{   mainr   r   r   r   <module>   sp   

	
P  I 		b - 
