o
    ۾i&                     @   sL  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	m
Z
 d dlmZmZ d dl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#dBddZ$dd Z%dd Z&dd Z'dd Z(dBddZ)dd Z*dd  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Z0G d)d* d*e,Z1G d+d, d,e1e.eZ2G d-d. d.e-Z3G d/d0 d0e3eZ4G d1d2 d2e3eZ5G d3d4 d4e-eZ6G d5d6 d6eZ7G d7d8 d8eZ8G d9d: d:eZ9d;d< Z:G d=d> d>eeZ;G d?d@ d@eeZ<e=dAkr$e>  dS dS )C    N)jitnjit)utilserrors)TestCaseMemoryLeakMixin)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   I/home/ubuntu/.local/lib/python3.10/site-packages/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_usecase-   s   r-   c                 C   s   t | S r   sortedr   r   r   r   sorted_usecase3   r%   r0   c                 C   s   t | |dS )Nreverser.   )r   br   r   r   sorted_reverse_usecase6   r    r4   c                 C   
   t | S r   )r   r   r   r   r   r   np_sort_usecase9      
r6   c                 C   r5   r   r   r#   r   r   r   r   np_argsort_usecase<   r7   r9   c                 C   s    |r	t j| ddS t j| ddS r&   r8   r+   r   r   r   np_argsort_kind_usecase?   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_usecaseE   s   rE   c                 C   sN   t jd g }t| D ]
}|t j  q|d d  }|j|d ||fS )Nr<   r1   r=   )r   r3   rB   rC   rD   r   r   r   list_sort_reverse_usecaseN   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_listZ   s   

zBaseSortingTest.random_listc                 C   s   t t||| S r   rI   r@   rK   r   rL   r   r   r   sorted_list`      zBaseSortingTest.sorted_listc                 C   s   t t||| d d d S )NrN   rO   r   r   r   revsorted_listc   s   zBaseSortingTest.revsorted_listNc                 C   s>   |d u r|d }|  ||}|| j|| |d | d7 }|S )N   rR   rL   )rP   rM   )rK   r   mrL   rB   r   r   r   initially_sorted_listf   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 )Nr<      )	r>   r?   intmathsqrtrI   r@   lenrJ   rK   r   factorrL   rB   r   r   r   duprandom_listm   s   
*
zBaseSortingTest.duprandom_listc                 C   sh   |d u rt t|}tt||||  d |d  d | }t||ks.J t||f|  |S NrX   )rY   rZ   r[   rI   r@   r\   r   r]   r   r   r   dupsorted_listv   s   *zBaseSortingTest.dupsorted_listc                 C   s,   |  t|t| |  t|t| d S r   )assertEqualr\   rI   r/   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 ]!}|| ||d  \}}	\}
}||
krU| ||	|| q4d S )Nc                 S   s   | d S r   r   xr   r   r   <lambda>       z4BaseSortingTest.assertSortedValues.<locals>.<lambda>keyrX   )rb   r\   rI   r/   zipr@   
assertLessindex)rK   rd   orig_valuesre   result_values
zip_sorted
zip_resultrC   k1v1k2v2r   r   r   assertSortedValues   s   z"BaseSortingTest.assertSortedValuesc                 c   s"    d}d}	 |V  ||| }}qr`   r   )rK   ar3   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 ]&}| | || | | || | | || | | || q|S r{   )rA   rP   ra   rS   r_   r~   r   r   r   make_sample_lists   s   z!BaseSortingTest.make_sample_lists)rH   )NrH   )__name__
__module____qualname__rM   rP   rS   rW   r_   ra   rf   rx   rz   r   r   r   r   r   r   rG   X   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 )Nr|   r   c                    s*    | } ||d|| | | d S r   )array_factoryrf   )rB   r   startresr   rK   r   r   check   s   
z.BaseTimsortTest.test_binarysort.<locals>.checkrT   r   )r   
binarysortrP   rS   rW   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 )Nr|   d   r   c                    s8    | } } ||d|| | || d S r   )r   rx   )rB   r   r   r   res_vr   rK   vr   r   r      s   

z:BaseTimsortTest.test_binarysort_with_values.<locals>.checkrT   r   )	rI   r@   r   r   rP   rS   rW   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 ]}|||| 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 r`   )r   r@   assertGreaterassertLessEqual)rB   lohir   desckry   r3   r   r   r   r      s   $&z-BaseTimsortTest.test_count_run.<locals>.checkr   rU   r   rX   rT   )r   	count_runrP   rS   rM   r@   r\   r_   )rK   r   r   rB   rC   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 r`   )assertGreaterEqualr   rn   rB   rl   r   stophintr   r   r   r   r        z/BaseTimsortTest.test_gallop_left.<locals>.checkc                    $   t ||D ]
} | |||| qd S r   r@   rB   rl   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rR     rX      r   rB   rl   r   r   rK   r   r   check_sorted_list     
$z;BaseTimsortTest.test_gallop_left.<locals>.check_sorted_listr   rU   )r   gallop_leftrP   ra   rK   r   rB   r   r   r   r   r   rK   r   test_gallop_left     
z BaseTimsortTest.test_gallop_leftc                    r   )
Nr|   c                    sd    || |||} || || ||kr"| |d  | ||k r0| | | d S d S r`   )r   r   r   r   r   r   r   r   %  r   z0BaseTimsortTest.test_gallop_right.<locals>.checkc                    r   r   r   r   r   r   r   r   /  r   z:BaseTimsortTest.test_gallop_right.<locals>.check_all_hintsc                    r   r   r   r   r   r   r   r   3  r   z<BaseTimsortTest.test_gallop_right.<locals>.check_sorted_listr   rU   )r   gallop_rightrP   ra   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   @      ?   rT         ?)r   merge_compute_minrunr@   rb   sysmaxsizerz   r   r   r   
bit_lengthrn   )rK   r   rC   r   quotpr   r   r   test_merge_compute_minrun?  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<   rX   r   rR   )r\   r   r   rb   rf   r   r   valuesassertIspending
min_gallop)rK   funcry   r3   nanb	orig_keysr   msssassbnew_msr   r   r   check_merge_lo_hiW  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   )rK   f_lof_hir   r   ry   r3   r   r   r   test_merge_lo_him  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   rX   c                    s    | |||} |d d   |d d  dd |dd  |j| j  | j|  f  | jd  |S )Nr   rR   rX   )rb   rf   r   r   )r   r   rC   r   r   r   r   r   rK   r   stack_sentinelr   r   run_merge_at  s   z4BaseTimsortTest.check_merge_at.<locals>.run_merge_atrT   )
r   merge_atr\   r   r   r   merge_appendr   rb   r   )	rK   ry   r3   r   r   r   r   rC   last_runr   r   r   check_merge_atx  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   )rK   r   r   ry   r3   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   rH   r   r|   r   rR   c                 3   s    | ]}  |V  qd S r   )r   .0r   rK   r   r   	<genexpr>  s    z<BaseTimsortTest.test_merge_force_collapse.<locals>.<genexpr>rX   )r   merge_force_collapserA   r   r   sumr   r   r   r   r\   rb   r   r   rf   )
rK   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)rX   rH   )r      r|   c                       g | ]	}  | qS r   r   r   rK   size_factorr   r   
<listcomp>      z4BaseTimsortTest.test_run_timsort.<locals>.<listcomp>)r   run_timsortr   r   r   r   rf   rK   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rX   r   P   r   )
r   run_timsort_with_valuesra   r_   rS   rI   r@   r\   r   rx   )rK   r   r   
chunk_sizery   r3   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      t j|t jdS Nr   r   arrayint32rK   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)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   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   )r\   r   r   r   rb   rf   )rK   r   ry   r3   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'  irX   r   rR   )r   rb   rf   )rB   r   r   r   r   r   r   9  s
   z4BaseQuicksortTest.test_insertion_sort.<locals>.checkrT   )r*   insertion_sortrP   rS   rW   rM   r_   r   r   r   r   test_insertion_sort7  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   rb   r@   r   r   )rB   r   r   ro   pivotrC   r   r   r   r   P  s   z/BaseQuicksortTest.test_partition.<locals>.checkrT   )r*   	partitionrP   rS   rW   rM   r_   r   r   r   r   test_partitionN  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   rb   r@   r   r   )rB   r   r   ltgtr(  rC   r   r   r   r   l  s   z0BaseQuicksortTest.test_partition3.<locals>.checkrT   )r*   
partition3rP   rS   rW   rM   r_   r   r   r   r   test_partition3i  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>)r*   run_quicksortr   r   r   r   rf   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   ry   r3   r   r   r   r+    r%   z3BaseQuicksortTest.test_run_quicksort_lt.<locals>.lt)r+  r  r/  c                    r   r   r   r   r   r   r   r    r  z;BaseQuicksortTest.test_run_quicksort_lt.<locals>.<listcomp>rR   c                 S      t |p| |k S r   rZ   isnanr3  r   r   r   	lt_floats  rQ   z:BaseQuicksortTest.test_run_quicksort_lt.<locals>.lt_floatsr<   r   r|   2   r   sizer   nan皙?)make_quicksortr0  r   r   r   r   rf   r   r>   r?   floatrI   r6  r\   )rK   r+  r   r   r  r   r   r   r7  r;  rd   non_nansr   r   r   test_run_quicksort_lt  s.   

	
z'BaseQuicksortTest.test_run_quicksort_ltN)r   r   r   r'  r*  r.  r1  rA  r   r   r   r   r#  3  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   rB    s    rB  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_quicksortr*   rD  r	   r>  r   r   r   r   r   rE    s    rE  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   s4   t j|t jd}|d u r|d|jd S ||S )Nr  rR   r   )r   r  rF  reshaperJ  )rK   r  rJ  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 )NrX   rT   r   )r@   rY   rZ   r[   rA   
get_shapes)rK   r   shapesrC   _shapes_shaper   r   r   rM    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  r/  c                    r   r   r   r   r   r   r   r    r  zJTestQuicksortMultidimensionalArrays.test_run_quicksort.<locals>.<listcomp>rJ  )r*   r0  r   r   r   rM  r\   rA   r   r   rf   )
rK   r   r   r  r   r   
shape_listrJ  r   	keys_copyr   r   r   r1    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   r2  r   r   r3  r   r   r   r+    r%   zETestQuicksortMultidimensionalArrays.test_run_quicksort_lt.<locals>.ltT)r+  rI  r  r/  c                    r   r   r   r   r   r   r   r  
  r  zMTestQuicksortMultidimensionalArrays.test_run_quicksort_lt.<locals>.<listcomp>rQ  c                 S   r4  r   r5  r3  r   r   r   r7    rQ   zLTestQuicksortMultidimensionalArrays.test_run_quicksort_lt.<locals>.lt_floatsr<   r8  r:  r   r<  r=  )r>  r0  r   r   r   rM  r\   rA   r   r   rf   r   r>   r?   r?  rI   )rK   r+  r   r   r  r   r   rR  rJ  r   rS  r7  r;  rd   r   r   r   rA     sF   


z9TestQuicksortMultidimensionalArrays.test_run_quicksort_ltr   )r   r   r   r	   r*   rD  r>  rf   r   rM  r1  rA  r   r   r   r   rH    s    

rH  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!d" Zd#d$ Zd%d& Zd'd( Zd)S )*TestNumpySortc                 C      t jd d S r;   r   r>   r?   r   r   r   r   setUp0  r  zTestNumpySort.setUpc                 c   s"    dD ]}t jjd|dV  qd S )Nr8  c   r:  )r   r>   randint)rK   r;  r   r   r   
int_arrays3  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D ]}t jj|dd }td|t jj|ddk < |V  q0d S )Nr8  r:  r   r<  r=  )r9  r   r   )r   r>   r?  )rK   r;  rd   r   r   r   float_arrays7  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.
        rX   TF)r   count_nonzeror6  uniquer;  )rK   arrr   r   r   has_duplicatesE  s
   zTestNumpySort.has_duplicatesc                 C   s4   t  |}t  |}|| || | || d S r   copyrK  )rK   pyfunccfuncr   expectedgotr   r   r   check_sort_inplaceO  s
   

z TestNumpySort.check_sort_inplacec                 C   s6   t  |}||}||}| || | || d S r   r`  )rK   rb  rc  r   rd   rd  re  r   r   r   check_sort_copyV  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)ra  rK  r   r   r_  )rK   rb  rc  r   kwargsrd   rd  re  r   r   r   check_argsort^  s   

zTestNumpySort.check_argsortc                 C   2   t }tdd|}|  D ]	}| ||| qd S NTnopython)r   r   rZ  rf  rK   rb  rc  rd   r   r   r   test_array_sort_intk  
   z!TestNumpySort.test_array_sort_intc                 C   rk  rl  )r   r   r[  rf  ro  r   r   r   test_array_sort_floatr  rq  z#TestNumpySort.test_array_sort_floatc                 C   sd   t }tdd|}|  D ]"}|d d  }tj| tdd t||D }| ||| qd S )NTrm  c                 S      g | ]}t | qS r   complexr   rh   r   r   r   r        z9TestNumpySort.test_array_sort_complex.<locals>.<listcomp>)	r   r   r[  r   r>   rJ   r  rm   rf  )rK   rb  rc  realimagrd   r   r   r   test_array_sort_complexy  s   z%TestNumpySort.test_array_sort_complexc                 C   rk  rl  )r6   r   rZ  rg  ro  r   r   r   test_np_sort_int  rq  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 )NTrm  r8  r:  r   r<  r=  )r6   r   r   r>   r?  rg  )rK   rb  rc  r;  rd   r   r   r   test_np_sort_float  s   z TestNumpySort.test_np_sort_floatc                 C   s   t }tdd|}dD ]B}tjj|dd }tjj|dd }td|tjj|ddk < td|tjj|ddk < tdd	 t||D }| ||| qd S )
NTrm  r8  r:  r   r<  r=  c                 S   rs  r   rt  rv  r   r   r   r    rw  z6TestNumpySort.test_np_sort_complex.<locals>.<listcomp>)r6   r   r   r>   r?  r  rm   rg  )rK   rb  rc  r;  rx  ry  rd   r   r   r   test_np_sort_complex  s   z"TestNumpySort.test_np_sort_complexc                         fdd}|t  |t d S )Nc                    .   t dd| }  D ]	} | || qd S rl  )r   rZ  rj  rb  rc  rd   r   r   r   r        z-TestNumpySort.test_argsort_int.<locals>.checkr$   r9   rK   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 NTrm  r,   )r   rZ  rj  dictrb  r,   rc  rd   r   r   r   r        
z2TestNumpySort.test_argsort_kind_int.<locals>.checkTr  Fr-   r:   r  r   r   r   test_argsort_kind_int  
   z#TestNumpySort.test_argsort_kind_intc                    r~  )Nc                    r  rl  )r   r[  rj  r  r   r   r   r     r  z/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[  rj  r  r  r   r   r   r     r  z<TestNumpySort.test_argsort_float_supplemental.<locals>.checkTr  Fr  r  r   r   r   test_argsort_float_supplemental  r  z-TestNumpySort.test_argsort_float_supplementalc                    r~  )Nc                    s`   t dd| }  D ]"}|d d  }tj| tdd t||D } | || qd S )NTrm  c                 S   rs  r   rt  rv  r   r   r   r    rw  zETestNumpySort.test_argsort_complex.<locals>.check.<locals>.<listcomp>)r   r[  r   r>   rJ   r  rm   rj  )rb  rc  rx  ry  rd   r   r   r   r     s   z1TestNumpySort.test_argsort_complex.<locals>.checkr  r  r   r   r   test_argsort_complex  s   z"TestNumpySort.test_argsort_complexc                    r  )Nc              	      sh   t dd| }  D ]&}|d d  }tj| tdd t||D } | ||t|d qd S )NTrm  c                 S   rs  r   rt  rv  r   r   r   r    rw  zRTestNumpySort.test_argsort_complex_supplemental.<locals>.check.<locals>.<listcomp>r  )	r   r[  r   r>   rJ   r  rm   rj  r  )rb  r,   rc  rx  ry  rd   r   r   r   r     s   
z>TestNumpySort.test_argsort_complex_supplemental.<locals>.checkTr  Fr  r  r   r   r   !test_argsort_complex_supplemental  s
   	z/TestNumpySort.test_argsort_complex_supplementalc                 C   sN   t ddt}d}| tj|}|d  W d    d S 1 s w   Y  d S )NTrm  z#.*Argument "a" must be array-like.*)r   r6   assertRaisesRegexr   TypingError)rK   rc  rh  raisesr   r   r   test_bad_array  s
   
"zTestNumpySort.test_bad_arrayN)r   r   r   rW  rZ  r[  r_  rf  rg  rj  rp  rr  rz  r{  r|  r}  r  r  r  r  r  r  r  r   r   r   r   rT  .  s*    

			rT  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 )NTrm  r|   r9  r   )rE   r   rb   r/   assertNotEqual)rK   rb  rc  r;  rd   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 )NTrm  r  FTr1   )rF   r   rb   r/   r  )rK   rb  rc  r;  r3   rd   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 )NTrm  r  r:  r   )r0   r   r   r>   r/   rK  r  rI   )rK   rb  rc  r;  rd   rd  re  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 )NTrm  r|   r:  r   r  r1   )r4   r   r   r>   r/   rK  r  rI   )rK   rb  rc  r;  rd   r3   rd  re  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   rU  )NiA  rV  r   r   r   r   rW    r  zTestMergeSort.setUpc                 C   s8   t j|||}t j|dd}||}t j|| d S )Nr'   r(   )r   r>   rY  r#   testingassert_equal)rK   sorterlowhighcountdataexpectre  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))rT   r   )r   rH   )r   rH   e   )r   r   i  T)
is_argsortc                    s    | S r   r   )r^  toplevelr   r   ri   ,  rj   z3TestMergeSort.test_argsort_stable.<locals>.<lambda>)r
   run_mergesortr   r  )rK   arglistimpr  r  r   r  r   test_argsort_stable#  s   
z!TestMergeSort.test_argsort_stableN)r   r   r   rW  r  r  r   r   r   r   r    s    r  c                 C   s   | S r   r   rg   r   r   r   ri   1  s    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   v   g d}t dd }t ddd}| ||d d  ||d d   | ||d d  |||d d  | d S )N   rX      rX   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 Nrk   r   r/   rh   rl   new_xr   r   r   foo>     z/TestSortSlashSortedWithKey.test_01.<locals>.foor   r   rK  py_funcrK   ry   r  r  r   r   r   test_016     
&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_keyN  r%   zDTestSortSlashSortedWithKey.test_02.<locals>.foo.<locals>.closure_keyrk   r  )rh   r  r  r   r   r   r  L  s   z/TestSortSlashSortedWithKey.test_02.<locals>.foor  )rK   ry   r  r   r   r   test_02H  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  )rh   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_keyd  r%   zYTestSortSlashSortedWithKey.test_03.<locals>.gen.<locals>.foo.<locals>.closure_escapee_keyr   )rh   r  r  r   r   r  b  s   
z<TestSortSlashSortedWithKey.test_03.<locals>.gen.<locals>.foor   )compilerr  r   r  r   genZ  s
   
z/TestSortSlashSortedWithKey.test_03.<locals>.gen)rK  r   nop_compiler)rK   ry   r  r   r   r   test_03V  s   0z"TestSortSlashSortedWithKey.test_03c                 C   r  )Nry   r3   Br3   CAc                 S   r!   r   upperr  r   r   r   r  p  r  z8TestSortSlashSortedWithKey.test_04.<locals>.external_keyc                 S   r  r  r  r  r   r   r   r  t  r  z/TestSortSlashSortedWithKey.test_04.<locals>.foor   r  r  r   r   r   test_04l  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    r  z8TestSortSlashSortedWithKey.test_05.<locals>.external_keyFc                 S   s4   | d d  }|j ||d t| d d  ||d|fS Nrl   r2   r  rh   rl   r2   r  r   r   r   r    s   z/TestSortSlashSortedWithKey.test_05.<locals>.foo)TFrX   ir   NF)r   r   r   rK  r  )rK   ry   r  r  rl   revr   r   r   test_05~  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_keyrk   r  )rh   	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   r  assertInstr	exception)rK   ry   r  r  TFrh  r   r   r   test_optional_on_key  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  r.   )rh   rl   r2   r   r   r   
foo_sorted     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   r  r  r   r  r   r   r   r    r  zGTestSortSlashSortedWithKey.test_exceptions_sorted.<locals>.external_keyr  illegalrk   z1Key must be None or a Numba JIT compiled functionzgo backwardsr  z$an integer is required for 'reverse'r  r  )rK   r  r  r  ry   implr  r  r   r   r   test_exceptions_sorted  s&   
z1TestSortSlashSortedWithKey.test_exceptions_sortedN)
r   r   r   r  r  r  r  r  r  r  r   r   r   r   r  4  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_kindr*   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  )rK   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)?ra  r   rZ   r>   r   unittestnumpyr   numbar   r   
numba.corer   r   numba.tests.supportr   r   numba.misc.quicksortr   r	   numba.misc.mergesortr
   numba.misc.timsortr   r   r   r   r   r  r  jit_list_timsortr!  rC  rG  r   r$   r-   r0   r4   r6   r9   r:   rE   rF   objectrG   r   r  r  r  r"  r#  rB  rE  rH  rT  r  r  r  r  r  r   mainr   r   r   r   <module>   sn    

	
P  I 		b >- 
