o
    8ήcI                     @   s|  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 Z0dEd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r<eF  dS dS )F    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   </tmp/pip-target-vg8gfxp4/lib/python/numba/tests/test_sort.pymake_temp_list      r   c                 C   s   t || jS N)npemptydtyper   r   r   r   make_temp_array   r   r    c                 C   s   |    d S r   sortvalr   r   r   sort_usecase*      r%   c                 C      |   S r   argsortr#   r   r   r   argsort_usecase-      r*   Fc                 C   s   |r| j ddS | j ddS N	mergesortkind	quicksortr(   r$   	is_stabler   r   r   argsort_kind_usecase0   s   r3   c                 C   s   t | S r   sortedr#   r   r   r   sorted_usecase6   r+   r6   c                 C   s   t | |dS )Nreverser4   )r$   br   r   r   sorted_reverse_usecase9   r&   r:   c                 C   
   t | S r   )r   r"   r#   r   r   r   np_sort_usecase<      
r<   c                 C   r;   r   r   r)   r#   r   r   r   np_argsort_usecase?   r=   r?   c                 C   s    |r	t j| ddS t j| ddS r,   r>   r1   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   rK   c                 C   sN   t jd g }t| D ]
}|t j  q|d d  }|j|d ||fS )NrB   r7   rC   )r   r9   rH   rI   rJ   r   r   r   list_sort_reverse_usecaseQ   s   rL   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 rA   )rD   rE   listrF   shuffle)selfr   offsetrH   r   r   r   random_list]   s   

zBaseSortingTest.random_listc                 C   s   t t||| S r   rO   rF   rQ   r   rR   r   r   r   sorted_listc      zBaseSortingTest.sorted_listc                 C   s   t t||| d d d S )NrT   rU   r   r   r   revsorted_listf   s   zBaseSortingTest.revsorted_listNc                 C   s>   |d u r|d }|  ||}|| j|| |d | d7 }|S )N   rX   rR   )rV   rS   )rQ   r   mrR   rH   r   r   r   initially_sorted_listi   s
   z%BaseSortingTest.initially_sorted_listc                 C   sh   t d |d u rtt|}tt||||  d |d  d | }t||ks-J t | |S )NrB      )	rD   rE   intmathsqrtrO   rF   lenrP   rQ   r   factorrR   rH   r   r   r   duprandom_listp   s   
*
zBaseSortingTest.duprandom_listc                 C   sh   |d u rt t|}tt||||  d |d  d | }t||ks.J t||f|  |S Nr^   )r_   r`   ra   rO   rF   rb   r"   rc   r   r   r   dupsorted_listy   s   *zBaseSortingTest.dupsorted_listc                 C   s,   |  t|t| |  t|t| d S r   )assertEqualrb   rO   r5   rQ   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 ]!}|| ||d  \}}	\}
}||
krU| ||	|| q4d S )Nc                 S   s   | d S r   r   xr   r   r   <lambda>       z4BaseSortingTest.assertSortedValues.<locals>.<lambda>keyr^   )rh   rb   rO   r5   ziprF   
assertLessindex)rQ   rj   orig_valuesrk   result_values
zip_sorted
zip_resultrI   k1v1k2v2r   r   r   assertSortedValues   s   z"BaseSortingTest.assertSortedValuesc                 c   s"    d}d}	 |V  ||| }}qrf   r   )rQ   ar9   r   r   r   fibo   s   zBaseSortingTest.fiboc                 C   s6   g }dD ]}| | || | | || q|S N)   x   )rG   rV   rg   rQ   r   listsrR   r   r   r   make_sample_sorted_lists   s
   z(BaseSortingTest.make_sample_sorted_listsc                 C   sZ   g }dD ]&}| | || | | || | | || | | || q|S r   )rG   rV   rg   rY   re   r   r   r   r   make_sample_lists   s   z!BaseSortingTest.make_sample_lists)rN   )NrN   )__name__
__module____qualname__rS   rV   rY   r]   re   rg   rl   r~   r   r   r   r   r   r   r   rM   [   s    





	rM   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)rQ   r   fr   r   r   r      s   zBaseTimsortTest.merge_initc                    s   d}d fdd	}j j |}||| ||||d  |}||| ||d }||| ||||d  |}||| |}||| |}||| d S )Nr   r   c                    s*    | } ||d|| | | d S r   )array_factoryrl   )rH   r   startresr   rQ   r   r   check   s   
z.BaseTimsortTest.test_binarysort.<locals>.checkrZ   r   )r   
binarysortrV   rY   r]   rS   re   rQ   r   r   rH   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 )Nr   d   r   c                    s8    | } } ||d|| | || d S r   )r   r~   )rH   r   r   r   res_vr   rQ   vr   r   r      s   

z:BaseTimsortTest.test_binarysort_with_values.<locals>.checkrZ   r   )	rO   rF   r   r   rV   rY   r]   rS   re   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 ]}|||| qRj|dd}tt|d D ]}|||| qjd S )	N   c                    s     | ||\}}|rDt||| d D ]}| | | |d  }}|| q|| |k rB| || d  | ||   d S d S t||| d D ]}| | | |d  }}|| qM|| |k rz| || d  | ||  |  d S d S rf   )r   rF   assertGreaterassertLessEqual)rH   lohir   desckr   r9   r   r   r   r      s   $&z-BaseTimsortTest.test_count_run.<locals>.checkr   r[   r   r^   rZ   )r   	count_runrV   rY   rS   rF   rb   re   )rQ   r   r   rH   rI   r   r   r   test_count_run   s$   zBaseTimsortTest.test_count_runc                    f   dj jfdd  fddfdd}jdd	}|| jdd	}|| d S )
Nr   c                    sd    || |||} || || ||kr"| |d  | ||k r0 | | | d S d S rf   )assertGreaterEqualr   rt   rH   rr   r   stophintr   r   r   r   r   
     z/BaseTimsortTest.test_gallop_left.<locals>.checkc                    $   t ||D ]
} | |||| qd S r   rF   rH   rr   r   r   r   r   r   r   check_all_hints     z9BaseTimsortTest.test_gallop_left.<locals>.check_all_hintsc                    f    | } | d | d | d d| d dfD ]} | |d  | |dd   | |dd  qd S 	N      r   irX     r^      r   rH   rr   r   r   rQ   r   r   check_sorted_list     
$z;BaseTimsortTest.test_gallop_left.<locals>.check_sorted_listr   r[   )r   gallop_leftrV   rg   rQ   r   rH   r   r   r   r   r   rQ   r   test_gallop_left     
z BaseTimsortTest.test_gallop_leftc                    r   )
Nr   c                    sd    || |||} || || ||kr"| |d  | ||k r0| | | d S d S rf   )r   r   r   r   r   r   r   r   (  r   z0BaseTimsortTest.test_gallop_right.<locals>.checkc                    r   r   r   r   r   r   r   r   2  r   z:BaseTimsortTest.test_gallop_right.<locals>.check_all_hintsc                    r   r   r   r   r   r   r   r   6  r   z<BaseTimsortTest.test_gallop_right.<locals>.check_sorted_listr   r[   )r   gallop_rightrV   rg   r   r   r   r   test_gallop_right$  r   z!BaseTimsortTest.test_gallop_rightc                 C   s   | j j}tddD ]
}| ||| q	tddD ]}d| tjkr$ n| |d| d q|  D ]<}|dk r:q3|tjkrB d S ||}| |d | |d |dkro|| }dt	
| }| || | |d|  q3d S )	Nr   @      ?   rZ         g?)r   merge_compute_minrunrF   rh   sysmaxsizer   r   r   r   
bit_lengthrt   )rQ   r   rI   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 NrB   r^   r   rX   )rb   r   r   rh   rl   r   r   valuesassertIspending
min_gallop)rQ   funcr   r9   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 ]#\}}t| || |D ]\}}| ||| | ||| qq
d S )N)   r   (   r   r   n   )r   iL  )r   merge_lomerge_hi	itertoolsproductr   r   )rQ   f_lof_hir   r   r   r9   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 )NrB   r   r^   c                    s    | |||} |d d   |d d  dd |dd  |j| j  | j|  f  | jd  |S )Nr   rX   r^   )rh   rl   r   r   )r   r   rI   r   r   r   r   r   rQ   r   stack_sentinelr   r   run_merge_at  s   z4BaseTimsortTest.check_merge_at.<locals>.run_merge_atrZ   )
r   merge_atrb   r   r   r   merge_appendr   rh   r   )	rQ   r   r9   r   r   r   r   rI   last_runr   r   r   check_merge_at{  s6   




zBaseTimsortTest.check_merge_atc                 C   sL   dD ]!\}}t | || |D ]\}}| || | || qqd S )N)r   r   r   )r   i  )r   r   r   r   )rQ   r   r   r   r9   r   r   r   test_merge_at  s   zBaseTimsortTest.test_merge_atc           
   
      s   j j}dg}||d d d d  |D ]j}tj fdd|D  D ][}t|g } |} |}d}|D ]}	 j |t	|t
|	}|t
|	7 }q8 t|j|jd  t
| ||||} |jd  |jd t	dt
|  || q#qd S )N)r   rN   r   r   r   rX   c                 3   s    | ]}  |V  qd S r   )r   .0r   rQ   r   r   	<genexpr>  s    z<BaseTimsortTest.test_merge_force_collapse.<locals>.<genexpr>r^   )r   merge_force_collapserG   r   r   sumr   r   r   r   rb   rh   r   r   rl   )
rQ   r   
sizes_listsizeschunksr   r   r   poscr   r   r   test_merge_force_collapse  s*   


 z)BaseTimsortTest.test_merge_force_collapsec                    f    j j}dD ]*d} fdd|D }tj| D ]}t|g } |}||  || qqd S )N)r^   rN   )r      r   c                       g | ]	}  | qS r   r   r   rQ   size_factorr   r   
<listcomp>      z4BaseTimsortTest.test_run_timsort.<locals>.<listcomp>)r   run_timsortr   r   r   r   rl   rQ   r   r   	all_listsr   r   r   r   r  r   test_run_timsort     

z BaseTimsortTest.test_run_timsortc                 C   s   | j j}dD ]=}d| }| |}| |}| |}|| | }ttddt| }| |}	| |}
||	|
 | 	|||	|
 qd S )Nr^   r   P   r   )
r   run_timsort_with_valuesrg   re   rY   rO   rF   rb   r   r~   )rQ   r   r  
chunk_sizer   r9   r   r   rv   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   rO   r   r   r   r   r   r    s    r  c                   @   s   e Zd ZeZdd ZdS )TestTimsortArraysPurePythonc                 C      t j|t jdS Nr   r   arrayint32rQ   lstr   r   r   r        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}||v 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    | } || |g|R  }|S r   r   )r   r   argsr   r   r   r   r   r   wrapper  s   z5JITTimsortMixin.wrap_with_mergestate.<locals>.wrapper)r   compile)rQ   r   r   _cacherr   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   r  r  r  r  r   r   r   r      r  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   )rb   r%  r   r   rh   rl   )rQ   r   r   r9   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                       d} fdd}j j |}||| |}||| ||d }||| |}||| |}||| |}||| d S )Nr   c                    s\    dg|  dg } ||d| |d d |d d | |dd  d S Ni'  ir^   r   rX   )r   rh   rl   )rH   r   r   r   r   r   r   <  s
   z4BaseQuicksortTest.test_insertion_sort.<locals>.checkrZ   )r0   insertion_sortrV   rY   r]   rS   re   r   r   r   r   test_insertion_sort:  s   









z%BaseQuicksortTest.test_insertion_sortc                    r)  )Nr   c                    s    dg|  dg } ||d|}|d d |d d || }td|D ]
}|| | q+t|d |D ]
}|| | q=d S r*  )r   rh   rF   r   r   )rH   r   r   ru   pivotrI   r   r   r   r   S  s   z/BaseQuicksortTest.test_partition.<locals>.checkrZ   )r0   	partitionrV   rY   r]   rS   re   r   r   r   r   test_partitionQ  s   









z BaseQuicksortTest.test_partitionc                    r)  )Nr   c                    s    dg|  dg } |d|\}}|d d |d d || }td|D ]
}|| | q,t||d D ]
}|| | q>t|d |D ]
}|| | qPd S r*  )r   rh   rF   r   r   )rH   r   r   ltgtr-  rI   r   r   r   r   o  s   z0BaseQuicksortTest.test_partition3.<locals>.checkrZ   )r0   
partition3rV   rY   r]   rS   re   r   r   r   r   test_partition3l  s   









z!BaseQuicksortTest.test_partition3c                    r   )Nr  r   r   c                    r  r   r  r   r  r   r   r    r  z8BaseQuicksortTest.test_run_quicksort.<locals>.<listcomp>)r0   run_quicksortr   r   r   r   rl   r	  r   r  r   test_run_quicksort  r  z$BaseQuicksortTest.test_run_quicksortc              	      s  dd } j |dj}dD ]/d} fdd|D }tj| D ]}t|g } |}||  ||d d d  q qd	d
 } j |dj}tj	d dD ]9}	tjj|	dd }
t
d|
tjj|	ddk < t|
} |}|| |
t|
  } ||d t|  qPd S )Nc                 S      | |kS r   r   r   r9   r   r   r   r0    r+   z3BaseQuicksortTest.test_run_quicksort_lt.<locals>.lt)r0  r  r4  c                    r  r   r  r   r  r   r   r    r  z;BaseQuicksortTest.test_run_quicksort_lt.<locals>.<listcomp>rX   c                 S      t |p| |k S r   r`   isnanr8  r   r   r   	lt_floats  rW   z:BaseQuicksortTest.test_run_quicksort_lt.<locals>.lt_floatsrB   r   r   2   r   sizer   nan皙?)make_quicksortr5  r   r   r   r   rl   r   rD   rE   floatrO   r;  rb   )rQ   r0  r   r   r
  r   r   r   r<  r@  rj   non_nansr   r  r   test_run_quicksort_lt  s.   

	
z'BaseQuicksortTest.test_run_quicksort_ltN)r   r   r   r,  r/  r3  r6  rF  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_quicksortr0   staticmethodr   rC  rO   r   r   r   r   r   rG    s    rG  c                   @   s    e Zd ZeZeeZdd ZdS )TestQuicksortArraysc                 C   r  r  )r   r  float64r  r   r   r   r     r  z!TestQuicksortArrays.array_factoryN)	r   r   r   jit_quicksortr0   rI  r   rC  r   r   r   r   r   rJ    s    rJ  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   )rh   shapeassertPreciseEqualri   r   r   r   rl     s   z0TestQuicksortMultidimensionalArrays.assertSortedNc                 C   s4   t j|t jd}|d u r|d|jd S ||S )Nr  rX   r   )r   r  rK  reshaperO  )rQ   r  rO  r  r   r   r   r     s   
z1TestQuicksortMultidimensionalArrays.array_factoryc                 C   s   g }|dkr|S t dtt|d D ]6}|| dkrJ||| |f |||| f | || }|D ]}||f|  |||f  q7q|S )Nr^   rZ   r   )rF   r_   r`   ra   rG   
get_shapes)rQ   r   shapesrI   _shapes_shaper   r   r   rR    s   z.TestQuicksortMultidimensionalArrays.get_shapesc           
         s    j j}dD ]Hd} fdd|D }tj| D ]4}t|g } t|}|d  |D ]} j||d} j||d}	|| |		   
|	| q.qqd S )Nr  r4  c                    r  r   r  r   r  r   r   r    r  zJTestQuicksortMultidimensionalArrays.test_run_quicksort.<locals>.<listcomp>rO  )r0   r5  r   r   r   rR  rb   rG   r   r"   rl   )
rQ   r   r   r
  r   r   
shape_listrO  r   	keys_copyr   r  r   r6    s"   

z6TestQuicksortMultidimensionalArrays.test_run_quicksortc                    sv  dd } j |ddj}dD ]Ld} fdd|D }tj| D ]8}t|g } t|}|d  |D ]"} j||d	}	 j||d	 }
||	 |
	  |
 }
 
|
|	 q6q!qd
d } j |ddj}tjd dD ]I}tjj|dd }td|tjj|ddk < t|} t|}|d  |D ]} j||d	}	 j||d	}
||	 |
	   
|
|	 qqod S )Nc                 S   r7  r   r   r8  r   r   r   r0    r+   zETestQuicksortMultidimensionalArrays.test_run_quicksort_lt.<locals>.ltT)r0  rN  r  r4  c                    r  r   r  r   r  r   r   r    r  zMTestQuicksortMultidimensionalArrays.test_run_quicksort_lt.<locals>.<listcomp>rV  c                 S   r9  r   r:  r8  r   r   r   r<    rW   zLTestQuicksortMultidimensionalArrays.test_run_quicksort_lt.<locals>.lt_floatsrB   r=  r?  r   rA  rB  )rC  r5  r   r   r   rR  rb   rG   r   r"   rl   r   rD   rE   rD  rO   )rQ   r0  r   r   r
  r   r   rW  rO  r   rX  r<  r@  rj   r   r  r   rF    sF   


z9TestQuicksortMultidimensionalArrays.test_run_quicksort_ltr   )r   r   r   r   r0   rI  rC  rl   r   rR  r6  rF  r   r   r   r   rM    s    

rM  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S )TestNumpySortc                 C      t jd d S rA   r   rD   rE   r   r   r   r   setUp3  r  zTestNumpySort.setUpc                 c   s"    dD ]}t jjd|dV  qd S )Nr=  c   r?  )r   rD   randint)rQ   r@  r   r   r   
int_arrays6  s   zTestNumpySort.int_arraysc                 c   s`    dD ]}t jj|dd V  qdD ]}t jj|dd }td|t jj|ddk < |V  qd S )Nr=  r?  r   rA  rB  )r   rD   rD  )rQ   r@  rj   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.
        r^   TF)r   count_nonzeror;  uniquer@  )rQ   arrr   r   r   has_duplicatesC  s
   zTestNumpySort.has_duplicatesc                 C   s4   t  |}t  |}|| || | || d S r   copyrP  )rQ   pyfunccfuncr$   expectedgotr   r   r   check_sort_inplaceM  s
   

z TestNumpySort.check_sort_inplacec                 C   s6   t  |}||}||}| || | || d S r   re  )rQ   rg  rh  r$   rj   ri  rj  r   r   r   check_sort_copyT  s
   
zTestNumpySort.check_sort_copyc                 C   sj   t  |}||fi |}||fi |}| j|| t|dd | |s-| || | || d S )Nzthe array wasn't argsorted)msg)rf  rP  r   r"   rd  )rQ   rg  rh  r$   kwargsrj   ri  rj  r   r   r   check_argsort\  s   

zTestNumpySort.check_argsortc                 C   2   t }tdd|}|  D ]	}| ||| qd S NTnopython)r%   r   r_  rk  rQ   rg  rh  rj   r   r   r   test_array_sort_inti  
   z!TestNumpySort.test_array_sort_intc                 C   rp  rq  )r%   r   r`  rk  rt  r   r   r   test_array_sort_floatp  rv  z#TestNumpySort.test_array_sort_floatc                 C   rp  rq  )r<   r   r_  rl  rt  r   r   r   test_np_sort_intw  rv  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 )NTrr  r=  r?  r   rA  rB  )r<   r   r   rD   rD  rl  )rQ   rg  rh  r@  rj   r   r   r   test_np_sort_float~  s   z TestNumpySort.test_np_sort_floatc                         fdd}|t  |t d S )Nc                    .   t dd| }  D ]	} | || qd S rq  )r   r_  ro  rg  rh  rj   r   r   r   r        z-TestNumpySort.test_argsort_int.<locals>.checkr*   r?   rQ   r   r   r   r   test_argsort_int     zTestNumpySort.test_argsort_intc                    @    fdd}|t dd |tdd |t dd |tdd d S )Nc              	      6   t dd| }  D ]} | ||t|d qd S NTrr  r2   )r   r_  ro  dictrg  r2   rh  rj   r   r   r   r        
z2TestNumpySort.test_argsort_kind_int.<locals>.checkTr  Fr3   r@   r  r   r   r   test_argsort_kind_int  
   z#TestNumpySort.test_argsort_kind_intc                    rz  )Nc                    r{  rq  )r   r`  ro  r|  r   r   r   r     r}  /TestNumpySort.test_argsort_float.<locals>.checkr~  r  r   r   r   test_argsort_float  r  z TestNumpySort.test_argsort_floatc                    r  )Nc              	      r  r  )r   r`  ro  r  r  r   r   r   r     r  r  Tr  Fr  r  r   r   r   r    r  N)r   r   r   r\  r_  r`  rd  rk  rl  ro  ru  rw  rx  ry  r  r  r  r   r   r   r   rY  1  s     	
			rY  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 )NTrr  r   r>  r   )rK   r   rh   r5   assertNotEqual)rQ   rg  rh  r@  rj   retr   r   r   test_list_sort  s   zTestPythonSort.test_list_sortc                 C   sX   t }tdd|}dD ]}dD ]}|||\}}| t||d| | || qqd S )NTrr  r  FTr7   )rL   r   rh   r5   r  )rQ   rg  rh  r@  r9   rj   r  r   r   r   test_list_sort_reverse  s   z%TestPythonSort.test_list_sort_reversec                 C   s^   t }tdd|}dD ]!}tjj|dd }t|}||}| || | t|| qd S )NTrr  r  r?  r   )r6   r   r   rD   r5   rP  r  rO   )rQ   rg  rh  r@  rj   ri  rj  r   r   r   test_sorted  s   zTestPythonSort.test_sortedc                 C   sh   t }tdd|}d}tjj|dd }dD ]}t||d}|||}| || | t|| qd S )NTrr  r   r?  r   r  r7   )r:   r   r   rD   r5   rP  r  rO   )rQ   rg  rh  r@  rj   r9   ri  rj  r   r   r   test_sorted_reverse  s   
z"TestPythonSort.test_sorted_reverseN)r   r   r   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S )TestMergeSortc                 C   rZ  )NiA  r[  r   r   r   r   r\    r  zTestMergeSort.setUpc                 C   s8   t j|||}t j|dd}||}t j|| d S )Nr-   r.   )r   rD   r^  r)   r
   assert_equal)rQ   sorterlowhighcountdataexpectrj  r   r   r   check_argsort_stable  s   z"TestMergeSort.check_argsort_stablec                    sH   g d}t dd}|j t fdd}|D ]}| j|g|R   qd S )N))rZ   r   )r   rN   )r   rN   e   )r   r   i  T)
is_argsortc                    s    | S r   r   )rc  toplevelr   r   ro     rp   z3TestMergeSort.test_argsort_stable.<locals>.<lambda>)r   run_mergesortr   r  )rQ   arglistimpr  r   r   r  r   test_argsort_stable  s   
z!TestMergeSort.test_argsort_stableN)r   r   r   r\  r  r  r   r   r   r   r    s    r  c                 C   s   | S r   r   rm   r   r   r   ro     s    ro   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   v   g d}t dd }t ddd}| ||d d  ||d d   | ||d d  |||d d  | d S )N   r^      r^   r   	   c                 S      d|  S Ng      ?r   zr   r   r   external_key     z8TestSortSlashSortedWithKey.test_01.<locals>.external_keyc                 S   0   | d d  }|j |d t| d d  |d|fS Nrq   r"   r5   rn   rr   new_xr   r   r   foo     z/TestSortSlashSortedWithKey.test_01.<locals>.foor   r   rP  py_funcrQ   r   r  r  r   r   r   test_01     
&z"TestSortSlashSortedWithKey.test_01c                 C   s>   g d}t dd }| ||d d  ||d d   d S )Nr  c                 S   s8   dd }| d d  }|j |d t| d d  |d|fS )Nc                 S   r  r  r   r  r   r   r   closure_key  r+   zDTestSortSlashSortedWithKey.test_02.<locals>.foo.<locals>.closure_keyrq   r  )rn   r  r  r   r   r   r    s   z/TestSortSlashSortedWithKey.test_02.<locals>.foor  )rQ   r   r  r   r   r   test_02  s   
*z"TestSortSlashSortedWithKey.test_02c                 C   s@   g d}dd }|  |t|d d  |t|d d   d S )Nr  c                    s    | dd  |  fdd}|S )Nc                 S   r  r  r  )rn   r   r  r   r   r   bar#  r  z<TestSortSlashSortedWithKey.test_03.<locals>.gen.<locals>.barc                    s   dd } | |S )Nc                 S   r  r  r   r  r   r   r   closure_escapee_key+  r+   zYTestSortSlashSortedWithKey.test_03.<locals>.gen.<locals>.foo.<locals>.closure_escapee_keyr   )rn   r  r  r   r   r  )  s   
z<TestSortSlashSortedWithKey.test_03.<locals>.gen.<locals>.foor   )compilerr  r   r  r   gen!  s
   
z/TestSortSlashSortedWithKey.test_03.<locals>.gen)rP  r   nop_compiler)rQ   r   r  r   r   r   test_03  s   0z"TestSortSlashSortedWithKey.test_03c                 C   r  )Nr   r9   Br9   CAc                 S   r'   r   upperr  r   r   r   r  7  r  z8TestSortSlashSortedWithKey.test_04.<locals>.external_keyc                 S   r  r  r  r  r   r   r   r  ;  r  z/TestSortSlashSortedWithKey.test_04.<locals>.foor   r  r  r   r   r   test_043  r  z"TestSortSlashSortedWithKey.test_04c              	   C   sn   g d}t dd }t ddd}td |fdD ]\}}| ||d d  ||||d d  || qd S )	Nr  c                 S   r'   r   r  r  r   r   r   r  I  r  z8TestSortSlashSortedWithKey.test_05.<locals>.external_keyFc                 S   s4   | d d  }|j ||d t| d d  ||d|fS Nrr   r8   r  rn   rr   r8   r  r   r   r   r  M  s   z/TestSortSlashSortedWithKey.test_05.<locals>.foo)TFr^   ir   NF)r   r   r   rP  r  )rQ   r   r  r  rr   revr   r   r   test_05E  s   

z"TestSortSlashSortedWithKey.test_05c                 C   sp   g d}t dd }| tj}d}||d d  | W d    n1 s&w   Y  d}| |t|j d S )Nr  c                 S   sB   |rdd }nd }| d d  }|j |d t| d d  |d|fS )Nc                 S   r  r  r   r  r   r   r   r  ^  r+   zQTestSortSlashSortedWithKey.test_optional_on_key.<locals>.foo.<locals>.closure_keyrq   r  )rn   	predicater  r  r   r   r   r  [  s   
z<TestSortSlashSortedWithKey.test_optional_on_key.<locals>.fooTz<Key must concretely be None or a Numba JIT compiled functionr   assertRaisesr	   TypingErrorassertInstr	exception)rQ   r   r  raisesTFrm  r   r   r   test_optional_on_keyX  s   
z/TestSortSlashSortedWithKey.test_optional_on_keyc              	   C   s   t ddd}t ddd}t dd }g d}||fD ]Q}| tj}||d	d
 W d    n1 s5w   Y  d}| |t|j | tj}|||dd W d    n1 s]w   Y  d}| |t|j qd S )NFc                 S   s   t | d d  ||dS r  r4   )rn   rr   r8   r   r   r   
foo_sortedq     zETestSortSlashSortedWithKey.test_exceptions_sorted.<locals>.foo_sortedc                 S   s   | d d  }|j ||d |S r  r!   r  r   r   r   foo_sortu  s   zCTestSortSlashSortedWithKey.test_exceptions_sorted.<locals>.foo_sortc                 S   r  r  r   r  r   r   r   r  {  r  zGTestSortSlashSortedWithKey.test_exceptions_sorted.<locals>.external_keyr  illegalrq   z1Key must be None or a Numba JIT compiled functionzgo backwardsr  z$an integer is required for 'reverse'r  r  )rQ   r  r  r  r   implr  r  r   r   r   test_exceptions_sortedo  s&   
z1TestSortSlashSortedWithKey.test_exceptions_sortedN)
r   r   r   r  r  r  r  r  r  r  r   r   r   r   r    s    r  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 d    n1 sw   Y  d}| |t|j t dd }| tj}|  W d    n1 sEw   Y  d}| |t|j d S )Nc                 S   s   t dj| d d S )Nr   r.   r   aranger)   r.   r   r   r   nonliteral_kind  r  z9TestArrayArgsort.test_exceptions.<locals>.nonliteral_kindr0   z"kind" must be a string literalc                   S   s   t djdd d S )Nr    )r  r  r   r   r   r   unsupported_kwarg  r  z;TestArrayArgsort.test_exceptions.<locals>.unsupported_kwargzUnsupported keywords: ['foo']r  )rQ   r  r  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)Grf  r   r`   rD   r   typingr   numpyr   numba.core.compilerr   r   numbar   r   
numba.corer   r   r	   unittestr
   numba.tests.supportr   r   r   numba.misc.quicksortr   r   numba.misc.mergesortr   numba.misc.timsortr   r   r   r   r    r  r  jit_list_timsortr&  rH  rL  r%   r*   r3   r6   r:   r<   r?   r@   rK   rL   objectrM   r   r  r  r  r'  r(  rG  rJ  rM  rY  r  r  r  r  r  r   mainr   r   r   r   <module>   st    

	
P  I 		b - 
