B
    u9ap                 @   s   d dl Z d dlZd dlZdd Zdd Zdd dD Zd	d dD Zd
d dD Zded d dddddded d dddddded d dddddddZdd d!d"gZ	G d#d$ d$ej
Zed%kre  dS )&    Nc                s   d  fdd|D S )N    c             3   s    | ]}|j  tjd dV  qdS )T)signedN)to_bytessys	byteorder).0v)width "/usr/lib/python3.7/test_audioop.py	<genexpr>   s    zpack.<locals>.<genexpr>)join)r	   datar
   )r	   r   pack   s    r   c                s     fddt dt D S )Nc                s*   g | ]"}t j ||  tjd dqS )T)r   )int
from_bytesr   r   )r   i)r   r	   r
   r   
<listcomp>	   s   zunpack.<locals>.<listcomp>r   )rangelen)r	   r   r
   )r   r	   r   unpack   s    r   c             C   s   i | ]}|d dd|qS ))r	   c             W   s
   t | |S )N)r   )r	   r   r
   r
   r   <lambda>   r   z<dictcomp>.<lambda>r
   )r   wr
   r
   r   
<dictcomp>   s    r   )            c             C   s"   i | ]}d d| d  > d  |qS )r      r
   )r   r   r
   r
   r   r      s    c             C   s   i | ]}d d| d > |qS )r   r   r
   )r   r   r
   r
   r   r      s    s    Er   i4  igE  ii  i r   r   iV4 igE iwi i  r   ixV4igEiUvii   )s   abcr   )s   abcr   )s   abr   )s   abcr   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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/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?S )@TestAudioopc          	   C   s   xdD ]}|  td|d |  tt |d |  ttd|d t| }|  t|d|d |  t|ddd|d |  t|t| |t|  |  t|t| |t|   |  tt| |t|   qW d S )N)r   r   r   r   r   r      ir   r   )	assertEqualaudioopmax	bytearray
memoryviewpacks	maxvalues	minvaluesdatas)selfr   pr
   r
   r   test_max"   s    
 "zTestAudioop.test_maxc          	   C   s  xdD ]}|  td|d |  tt |d |  ttd|d t| }|  t|d|d |  t|ddd|d |  t|t| |t| t| f |  t|t| |t| t| f |  tt| |t| t| f qW d S )	N)r   r   r   r   r   )ii   r!   )r!   r!   ir   )ir!   )	r"   r#   minmaxr%   r&   r'   r(   r)   r*   )r+   r   r,   r
   r
   r   test_minmax.   s     
zTestAudioop.test_minmaxc             C   s   xdD ]}|  td|d |  tt |d |  ttd|d |  tt| td |d |  tt| dddddd|d |  tt| |t| t	|   qW d S )	N)r   r   r   r   r   r   d   	   
   r!   r   )
r"   r#   maxppr%   r&   r'   r   r*   r(   r)   )r+   r   r
   r
   r   
test_maxpp@   s    
 &zTestAudioop.test_maxppc          	   C   s0  xdD ]}|  td|d |  tt |d |  ttd|d t| }|  t|d|d |  t|dd|d |  t|dd|d |  t|t| t| |t|  |  t|t| t| |t|  qW |  ttd	 d
dd	d |  ttd	 ddd	d d S )N)r   r   r   r   r   r   r!   r      ir   i   Pi   pi   `i   i   i   )r"   r#   avgr%   r&   r'   r(   r)   )r+   r   r,   r
   r
   r   test_avgJ   s     

zTestAudioop.test_avgc             C   s   xdD ]}|  td|d |  tt |d |  ttd|d |  tt| td |d |  tt| dddddd|d qW |  ttd dd	 |  ttd
 d
d |  ttd dd |  ttd dd d S )N)r   r   r   r   r   r   r0   r1   r2   r!   r      r   if  r   ig r   l   	
 )r"   r#   avgppr%   r&   r'   r   r*   )r+   r   r
   r
   r   
test_avgpp\   s    
 *zTestAudioop.test_avgppc             C   s*  xdD ]}|  td|d |  tt |d |  ttd|d t| }|  t|td |d | jt|t| d |t| dd | jt|t	| d |t	|  dd qW |  tt
d dd	 |  tt
d
 d
d |  tt
d dd |  tt
d dd d S )N)r   r   r   r   r   r   r0   9   r!   r   )ZdeltaM   r   i!N  r   i"N r   i"N)r"   r#   rmsr%   r&   r'   r   ZassertAlmostEqualr(   r)   r*   )r+   r   r,   r
   r
   r   test_rmsh   s    
zTestAudioop.test_rmsc          
   C   s   xdD ]}|  td|d |  tt |d |  ttd|d t| }|  t|ddd|d |  t|dddd|d |  t|dd	d
d|d |  t|dt| |d |  t|t| t| |d qW d S )N)r   r   r   r   r   r   r   r   r   r6   r   r   )r"   r#   crossr%   r&   r'   r)   r(   )r+   r   r,   r
   r
   r   
test_crossx   s    
zTestAudioop.test_crossc             C   s>  xdD ]|}|  tdd|d |  tt t |d |  ttdtd|d |  tt| dtt|  |t|  qW |  ttd td dd |  ttd td dtd ddd	d
d	d
d |  ttd td dtd ddddddd |  ttd td dtd ddddddd d S )N)r   r   r   r   r       r   s    $r   r   ih$  i  i r6   r   ih$ i i  r   ih$ii   )r"   r#   addr%   r&   r*   r   r'   )r+   r   r
   r
   r   test_add   s     
 zTestAudioop.test_addc             C   s  xbdD ]Z}xTdD ]L}|  td||d |  tt ||d |  ttd||d qW qW |  ttd ddd |  ttd ddd |  ttd ddd |  ttd dd	td  |  ttd
 d
dtd
 ddddddd |  ttd
 d
dtd
 ddddddd |  ttd
 d
dtd
 ddddddd |  ttd
 d
d	td
  |  ttd ddtd ddddddd |  ttd ddtd ddddd d!d |  ttd ddtd ddddd d!d |  ttd dd	td  |  ttd" d"dtd" dd#d$d%d	d&d |  ttd" d"dtd" dd'd(d)d*dd |  ttd" d"dtd" dd+d,d-ddd* |  ttd" d"d	td" d	d.d/d0ddd d S )1N)r   r   r   r   )r   r   r      iii   r   r   s   F r   s   D~ii   r   i5  ihE  ii ir   i3  ifE  ii  i  r6   r   iW4 igE ixi  i iU4 igE ivi i r   iyV4igEiVvi  iwV4igEiTviiwV4igiTv:ixV4igiUv:)r"   r#   biasr%   r&   r*   r'   )r+   r   rH   r
   r
   r   	test_bias   sX    

"


zTestAudioop.test_biasc             C   s`  xndD ]f}|  tt| ||t|  |  ttt| ||t|  |  ttt| ||t|  qW |  ttd ddtd dddddd	d
 |  ttd ddtd ddddddd |  ttd ddtd ddddddd |  ttd ddd |  ttd ddtd ddddddd
 |  ttd ddtd dddd d!dd |  ttd ddd |  ttd ddtd dd"d#d$d%d	d& |  ttd ddtd dd'd(d)d*dd
 |  ttd ddd |  ttd ddtd dd"d#d$d%d	d& |  ttd ddtd dd+d,d-d.dd& d S )/N)r   r   r   r   r   r   r   i   i E  i i   i i r   i   i  E i  i   i  i  r   i   i   Ei   i   i   i   s    Ei 4 i gE i i  i  4i  gEi  i  i4  igE  ii  r   i V4i gEi wi iV4 igE ivi )r"   r#   lin2linr*   r%   r&   r'   )r+   r   r
   r
   r   test_lin2lin   sH    

zTestAudioop.test_lin2linc          
   C   s  |  tddd d |  ttddd d |  ttddd d |  tddd td ddddd	d
df |  tddd td dddddddf |  tddd td dddddddf x.dD ]&}|  td|d d| d df qW d S )Ns   r   )s       )iM(   r   r      )   ir   iM)iMrL   r   i   i )  i i r  i Mr   i   i  ) i  i  r i  M)r   r   r   r   s        rD   r2   )r   r   )r"   r#   	adpcm2linr%   r&   r'   )r+   r   r
   r
   r   test_adpcm2lin   s"    
zTestAudioop.test_adpcm2linc             C   s   |  ttd dd d |  tttd dd d |  tttd dd d x&dD ]}|  tt| |d d q\W x*dD ]"}|  td| d |d d qW d S )	Nr   )s   )i#'   )r   r   r   )s   )   rR   )r   r   r   r   rD   r2   )s        )r   r   )r"   r#   	lin2adpcmr*   r%   r&   )r+   r   r
   r
   r   test_lin2adpcm   s    


zTestAudioop.test_lin2adpcmc             C   s   |  ttjddd |  ttjddd |  ttjddd |  ttjddd |  ttjddd |  ttjddd |  ttjddd |  ttjddd |  ttjddd |  ttjddd d S )NrD   r   i+  )r   r   )r   Y   )ir   )i   r   )assertRaises	TypeErrorr#   rP   rT   
ValueError)r+   r
   r
   r   test_invalid_adpcm_state  s    z$TestAudioop.test_invalid_adpcm_statec             C   sz   |  ttd dd |  tttd dd |  tttd dd x$dD ]}|  tt| |d qVW d S )Nr   s   Շ$*Z)r   r   r   s   Շ$*U)r"   r#   lin2alawr*   r%   r&   )r+   r   r
   r
   r   test_lin2alaw  s    
zTestAudioop.test_lin2alawc                s   d}dddddddd	d
dddddddddddddg}xjdD ]b t    fdd|D  }| t| | | tt| | | tt| | q:W ttd}x,dD ]$ t| }| t| | qW d S )Ns    $*QTUXkqiPi0i@i@ir@   r   iiiii  i  i  i  r1   r   r         R   j   )r   r   r   r   c             3   s   | ]}| d  > d? V  qdS )r      Nr
   )r   x)r   r
   r   r   0  s    z,TestAudioop.test_alaw2lin.<locals>.<genexpr>   )r   r   r   )	r'   r"   r#   alaw2linr%   r&   bytesr   r[   )r+   encodedsrcdecodedr
   )r   r   test_alaw2lin*  s    

zTestAudioop.test_alaw2linc             C   sz   |  ttd dd |  tttd dd |  tttd dd x$dD ]}|  tt| |d qVW d S )Nr   s    g)r   r   r   s    ~)r"   r#   lin2ulawr*   r%   r&   )r+   r   r
   r
   r   test_lin2ulaw:  s    
zTestAudioop.test_lin2ulawc                s   d}dddddddd	d
dddddddddddg}xjdD ]b t    fdd|D  }| t| | | tt| | | tt| | q6W ttdttdd }x,dD ]$ t| }| t| | qW d S )Ns    (?Wjv|~iiiAii]iiir6   r   i_  i_  i  i     5      r5   r   )r   r   r   r   c             3   s   | ]}| d  > d? V  qdS )r      Nr
   )r   rb   )r   r
   r   r   K  s    z,TestAudioop.test_ulaw2lin.<locals>.<genexpr>rG      rc   )r   r   r   )	r'   r"   r#   ulaw2linr%   r&   re   r   rj   )r+   rf   rg   rh   r
   )r   r   test_ulaw2linE  s    

zTestAudioop.test_ulaw2linc             C   sB  xdD ]}|  td|dd |  tt |dd |  ttd|dd |  tt| |ddtt|   |  tt| |dt|  qW |  ttd ddd |  ttd ddtd ddd	d
d	d
d |  ttd ddtd ddddddd |  ttd ddtd ddddddd d S )N)r   r   r   r   r   r   r   rD   r   s    $ih$  i  i r6   r   ih$ i i  r   ih$ii   )r"   r#   mulr%   r&   r*   r   r'   )r+   r   r
   r
   r   test_mulV  s$    
zTestAudioop.test_mulc       
      C   s  xdD ]}|  td|dddd d |  tt |dddd d |  ttd|dddd d |  td|dddd d |  td|dddd d	 |  tt| |dddd d
 t|  |  tt| |dddd dd
d
 t|  qW d }tddddd|\}}tddddd|\}}|  || d xdD ]}tt| |dddd \}}d\}}xNtd
tt| |D ]6}tt| |||  |ddd|\}}||7 }q|W |  || |  || q>W td d
ddddddtd d
ddddddtd d
ddddd d!td" d
d#d$d%d&d'd(d}	xfdD ]^}|  tt| |dddd ddd
 |	|  |  tt| |dddd d)d*d
 |	|  q8W | 	t
tjdddddd+ | 	t
tjdddddd, d S )-N)r   r   r   r   r   r   i@  )r   )r   ))r   r   r!   )r   )r   )r   r   ru   ru   ru   ru   i>  )r   )r6   ))r   r   r   s    s       )r   Nra   7   iU   iir   i  iw7  iisV  iifr   i@ ivw7 i6itV ii@gr   i@ivww7i6itViji@g   r2   *   )r   )ry   )r"   r#   ratecvr%   r&   r*   r   r   r'   rW   rX   )
r+   r   stateZd1Zd2Zd0Zstate0dr   Zexpectedr
   r
   r   test_ratecvj  sT    

"

"
"
zTestAudioop.test_ratecvc          	   C   s   xzdD ]r}|  td|d |  tt |d |  ttd|d |  tt| ddd|t| ddd qW d S )N)r   r   r   r   r   r   r   r   )r"   r#   reverser%   r&   r'   )r+   r   r
   r
   r   test_reverse  s    
zTestAudioop.test_reversec          	   C   s  xdD ]}t | }tdt| }x,t|D ] }||d | ||d d| < q0W | t||dd| | t||dddt|  x0t|D ]$}||d | ||| d d| < qW | t||dd| | tt||dd| | tt||dd| qW d S )N)r   r   r   r   r   r   r   rD   g      ?)r*   r%   r   r   r"   r#   tomonor&   )r+   r   data1data2kr
   r
   r   test_tomono  s      $zTestAudioop.test_tomonoc          	   C   s  xdD ]}t | }tdt| }x,t|D ] }||d | ||d d| < q0W | t||dd| | t||dddt|  x0t|D ]$}||d | ||| d d| < qW | t||dd| | tt||dd| | tt||dd| qW d S )N)r   r   r   r   r   r   r   rD   )r*   r%   r   r   r"   r#   tostereor&   )r+   r   r   r   r   r
   r
   r   test_tostereo  s      $zTestAudioop.test_tostereoc             C   s   |  ttd td d |  tttd ttd d |  tttd ttd d |  tdttd  td d d S )Nr   g      ?rD   g        )r"   r#   Z
findfactorr*   r%   r&   r   )r+   r
   r
   r   test_findfactor  s    zTestAudioop.test_findfactorc          	   C   s   |  ttd td d |  tttd ttd d |  tttd ttd d |  ttd td dddd |  ttd d d d td  td d d S )	Nr   )r   g      ?r   r   )r   gf@r6   r!   )rx   g      ?)r"   r#   Zfindfitr*   r%   r&   r'   )r+   r
   r
   r   test_findfit  s    *zTestAudioop.test_findfitc             C   sT   |  ttd dd |  tttd dd |  tttd dd d S )Nr   r   r!   )r"   r#   findmaxr*   r%   r&   )r+   r
   r
   r   test_findmax  s    zTestAudioop.test_findmaxc             C   s   xdD ]}t | dddt| t| }| t||dd | tt||dd | tt||dd | t||dd | t||dd | t||dt|  | t||dt|  qW d S )N)r   r   r   r   r   r   r   r   r   r   )r'   r(   r)   r"   r#   	getsampler%   r&   )r+   r   r   r
   r
   r   test_getsample  s    
zTestAudioop.test_getsamplec             C   s   t d td ddddddd	td
 ddddddd	td ddddddd	d}xdD ]}| td|d | tt | |||  | t|| |t |  | ttt | |||  | ttt | |||  qTW d S )Nr   r   r   i4  iEg  iirp   r   r   i4V iEgiw r   i4VxiEgivU)r   r   r   r   r   )r*   r'   r"   r#   Zbyteswapr%   r&   )r+   Zswapped_datasr   r
   r
   r   test_byteswap  s    

zTestAudioop.test_byteswapc             C   s    |  tjtjttdd d S )Nrc   i~)rW   r#   errorr   re   r   )r+   r
   r
   r   test_negativelen  s    zTestAudioop.test_negativelenc             C   s  d }xt D ]\}}|}| tjtj||d | tjtj|| | tjtj|| | tjtj|| | tjtj|| | tjtj	|| | tjtj
|| | tjtj|| | tjtj||d | tjtj||dd | tjtj||dd | tjtj||| | tjtj||d | tjtj|| | tjtj||| | tjtj||ddd| | tjtj|| | tjtj|| | tjtj||| qW d S )Nr   g      ?g      ?r   )INVALID_DATArW   r#   r   r   r$   r.   r7   r>   r:   r3   rB   rs   r   r   rE   rH   r~   rJ   rz   rj   r[   rT   )r+   r{   r   sizeZsize2r
   r
   r   test_issue7673  s,    zTestAudioop.test_issue7673c          
   C   s~  d}d}|  ttj||d |  ttj|| |  ttj|| |  ttj|| |  ttj|| |  ttj|| |  ttj	|| |  ttj
|| |  ttj||d |  ttj||dd |  ttj||dd |  ttj||| |  ttj||d |  ttj|| |  ttj||| |  ttj||dddd  |  ttj|| |  ttj|| |  ttj||d  d S )NZabcdr   r   g      ?g      ?r   )rW   rX   r#   r   r$   r.   r7   r>   r:   r3   rB   rs   r   r   rE   rH   r~   rJ   rz   rj   r[   rT   )r+   r   r   r
   r
   r   test_string  s*    zTestAudioop.test_stringc             C   sX   d}d }xJdD ]B}|  tjtj|| |  tjtj|| |  tjtj||| qW d S )Ns   abcdefgh)r   r   r!   i   )rW   r#   r   rq   rd   rP   )r+   r   r{   r   r
   r
   r   test_wrongsize,  s    
zTestAudioop.test_wrongsizeN)"__name__
__module____qualname__r-   r/   r4   r8   r;   r?   rC   rF   rI   rK   rQ   rU   rZ   r\   ri   rk   rr   rt   r}   r   r   r   r   r   r   r   r   r   r   r   r   r
   r
   r
   r   r        s>   
.'2	r    __main__)r#   r   Zunittestr   r   r'   r(   r)   r*   r   ZTestCaser    r   mainr
   r
   r
   r   <module>   s.       