o
    OZhC@                     @   s   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 ze W n e	y1   d dlmZ Y nw G dd dej
ZG dd dej
ZedkrNe  dS dS )	    N)BufferProxy)BufferErrorc                   @   s   e Zd Zddd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eej d d!d" Zzejj W n	 eyZ   Y nw d#d$ Zd%d& Zd'S )(BufferProxyTest)         |u1)r   T)r         shapetypestrdatastridesc                 C   s   |  tjjd d S )Nzpygame.bufferproxy)assertEqualpygamebufferproxy__name__self r   L/var/www/auris/lib/python3.10/site-packages/pygame/tests/bufferproxy_test.pytest_module_name   s   z BufferProxyTest.test_module_namec                 C   s   |  tjd d S )Nr   )r   r   r   r   r   r   r   test_class_name      zBufferProxyTest.test_class_namec                 C   s   | j }t|}t|}| t|d | |d d | |d |d  | |d |d  | |d |d  | |d |d  d S Nr   versionr   r   r   r   r   )view_keywordsr   r   Zget_array_interfacer   lenr   kwdsvdr   r   r   test___array_struct___property   s   
z.BufferProxyTest.test___array_struct___propertyc                 C   s   | j }t|}|j}| t|d | |d d | |d |d  | |d |d  | |d |d  | |d |d  d S r   )r   r   __array_interface__r   r   r   r   r   r   !test___array_interface___property*   s   z1BufferProxyTest.test___array_interface___propertyc                 C   s0   t | j}g }||d< t|}| |j| d S )Nparent)dictr   r   assertIsr&   )r   r    pr!   r   r   r   test_parent_property5   s
   
z$BufferProxyTest.test_parent_propertyc                    sd  fdd}G dd dt   fdd}t| j}g |d< g ||d< t|| td	 j}| td
 | d	  j}| td
 d  }t	  | td
 g ||d< t|| td	 j
}| td
 | d	  j
}| td
 d  }t	  | td
 ||d< t||  fdd d S )Nc                        |  u  d S Nappendr&   r)   successr   r   callback>   r   z-BufferProxyTest.test_before.<locals>.callbackc                   @      e Zd ZdS )z0BufferProxyTest.test_before.<locals>.MyExceptionNr   
__module____qualname__r   r   r   r   MyExceptionA       r7   c                    s    d)NzJust a test.r   r/   )r7   r   r   raise_exceptionD      z4BufferProxyTest.test_before.<locals>.raise_exceptionr&   beforer   r
   c                      s    j S r,   )__array_struct__r   )r!   r   r   <lambda>j   s    z-BufferProxyTest.test_before.<locals>.<lambda>)	Exceptionr'   r   r   r   r   r$   
assertTruegccollectr<   assertRaises)r   r2   r9   r    r"   cr   )r7   r)   r1   r!   r   test_before=   sB   
zBufferProxyTest.test_beforec                    s$   fdd}t | j}g   |d< g ||d< t|}| td |j}| td |j}| td d  }}t  | td | d  g ||d< t|}| td |j	}| td |j	}| td d  }}t  | td | d  d S )Nc                    r+   r,   r-   r/   r0   r   r   r2   m   r   z,BufferProxyTest.test_after.<locals>.callbackr&   afterr   r
   )
r'   r   r   r   r   r$   r@   rA   r?   r<   )r   r2   r    r!   r"   rC   r   r0   r   
test_afterl   s8   
zBufferProxyTest.test_afterc                 C   sF   t | j}| tt|d d|_| |jd |`| tt|d d S )N	undefined   )r   r   rB   AttributeErrorgetattrrG   r   )r   r!   r   r   r   test_attribute   s   
zBufferProxyTest.test_attributec                 C   s>   t | j}t|}| | | d }t  | |  d S r,   )r   r   weakrefrefr(   r@   rA   ZassertIsNone)r   r!   weak_vr   r   r   test_weakref   s   

zBufferProxyTest.test_weakrefc                    sb   fdd} fdd}G dd d}| }| }| | g t |}t |}t  d }t  d }	t |}
t |}t| j}||d	< ||d
< ||d< t|}||_t |}d } } } }}t  | | du | | du | |
 du | | du d}dd t	dD  | | du  | | du  | | du  | |
 du  | | du  | | du | |	 du d t  | | du  | |	 du  t| j}g |d	< t|}||_d}t
 D ]	}||u r nqd}| | |d	 | d}t  ttj}d}t  ttj}| || dS )z8refcount agnostic check that contained objects are freedc                        d S )Nr   r   r/   rr   r   before_callback   r:   z0BufferProxyTest.test_gc.<locals>.before_callbackc                    rP   )Nr
   r   r/   rQ   r   r   after_callback   r:   z/BufferProxyTest.test_gc.<locals>.after_callbackc                   @   r3   )z$BufferProxyTest.test_gc.<locals>.ObjNr4   r   r   r   r   Obj   r8   rU   r   r
   r&   r;   rE   Nc                 S   s   g | ]}t  qS r   )r@   rA   ).0xr   r   r   
<listcomp>   s    z+BufferProxyTest.test_gc.<locals>.<listcomp>r   TF)rL   rM   r'   r   r   Zsome_attributer@   rA   r?   rangeZget_objectsr.   r   garbager   )r   rS   rT   rU   r)   aZweak_pZweak_aZweak_r0Zweak_r1Zweak_beforeZ
weak_afterr    r!   rN   ZtrackedoZn1Zn2r   rQ   r   test_gc   sr   










zBufferProxyTest.test_gcc                 C   s$   t jj}tt jj}| || d S r,   )r   r   Z_PYGAME_C_APItypebaseZassertIsInstance)r   apiZapi_typer   r   r   
test_c_api   s   zBufferProxyTest.test_c_apic                 C   sJ   t | j}t j}tdt|d \}}| || | |jt| d S )Nz<([^)]+)\(([^)]+)\)>r   )	r   r   r   refindallreprr   lengthint)r   r!   cnameZonameZovaluer   r   r   	test_repr   s
   
zBufferProxyTest.test_reprc                 C   sl   G dd dt }t| j}d|d< ||}| |jd t|}| |d d d | |dd  d d S )	Nc                   @   s   e Zd Zdd ZdS )z7BufferProxyTest.test_subclassing.<locals>.MyBufferProxyc                 S   s   dt |  dS )N*)r   __repr__r   r   r   r   rj      r   z@BufferProxyTest.test_subclassing.<locals>.MyBufferProxy.__repr__N)r   r5   r6   rj   r   r   r   r   MyBufferProxy   s    rk   r   r&      z*<z>*)r   r'   r   r   r&   rd   )r   rk   r    r!   rR   r   r   r   test_subclassing   s   
z BufferProxyTest.test_subclassingznewbuf not implementedc                 C   s  ddl m} ddlm} |j}|j}|dddd}t|}| |j|j	 | |j
||j|j	 |j}z)| |d d	 | |d
 |j | |d |j | |d |jdf W d }nd }w |dd}t|}| |j|j	 | |j
||j|j	 |j}z<t tjk}|rdnd d|j }	| |d |	 | |d
 |j | |d |j | |d |jdf W d }nd }w |dd}t|}|||j}
| |
j|u  | |
j|j | |
j|j | |
j|j | |
j|j | |
j|j | |
j	|j	 | |
j|j | |
j|j | |
jd u  d	dddd}t|}|||j}
| |
j|u  | |
jd | |
j	d | |
jd  | |
jd | |
jd | |
j | |
jd u  | |
jd u  | |
jd u  d S )Nr   )	string_at)buftools)
   BT)readonlyr   r   r   r   r   )r   z=h<>iF)rq   rl   z=ir
   )	   T)r   r   r   r   rx   rq   r
   )ctypesro   Zpygame.tests.test_utilsrp   ExporterImporterr   r   re   r   rawbufr$   r   r   r   Zget_sdl_byteorderZ
LIL_ENDIANitemsizeZPyBUF_RECORDSr?   objndimformatrs   
suboffsetsZPyBUF_SIMPLE)r   ro   rp   rz   r{   expbr"   Z
lil_endianfimpr   r   r   NEWBUF_test_newbuf  st   

z"BufferProxyTest.NEWBUF_test_newbufc                 C   s   |    d S r,   )OLDBUF_test_oldbuf_argr   r   r   r   test_oldbuf_argL  s   zBufferProxyTest.test_oldbuf_argc                 C   sV  ddl m}m}m} d}t|}t|}dd }tt|fd|dfdd	}||d\}	}
| 	|
d | 	|	d ||d\}	}
| 	|
d | 	|	d ||\}}| 	|d
 | 	|t| ||d\}	}
| 	|
| | 	|	t| ||d\}	}
| 	|
| | 	|	t| tt|fd|dfdd	}||\}}| 	|d
 | 	|t| ||d\}	}
| 	|
| | 	|	t| | 
t||d tt|fd|dfd|d}||\}}| 	|d | 	|d tdd|dfdd	}||\}}| 	|d | 	|d tddD ]}|||\}	}
| 	|
||d   | 	|	d qd S )Nr   )get_segcountget_read_bufferget_write_buffer0                           c                 S   s   t d)NzAn exception)
ValueError)r\   r   r   r   r9   V  r:   z?BufferProxyTest.OLDBUF_test_oldbuf_arg.<locals>.raise_exceptionr   Frw   r   r
   T)r   r   r   r   r;   r   r   |u4rH   r   rH   0   r   )pygame.bufferproxyr   r   r   ry   create_string_buffer	addressofr   r   r   rB   r   rY   )r   r   r   r   contentZmemoryZmemaddrr9   bfZseglenZsegaddrZsegcountbuflenrv   r   r   r   r   O  s   

	z&BufferProxyTest.OLDBUF_test_oldbuf_argN)r   r5   r6   r   r   r   r#   r%   r*   rD   rF   rK   rO   r]   ra   rh   rn   unittestZskipIfr   ZHAVE_NEWBUFr   r   r   rI   r   r   r   r   r   r   r      s8    /$E
Cr   c                   @   s@   e Zd ZdZeeZeedfZdd Z	dd Z
dd Zd	S )
BufferProxyLegacyTestr   Tc                 C   sN   t dd| jdd}| |jt| j t dd| jdd}| |jd d S )Nr   r   r   r   )r   r   $   )r   r   r   re   r   r   r   r   r   r   r   test_length  s   z!BufferProxyLegacyTest.test_lengthc                 C   sv   t t| jfd| jd}| |j| j t dd| jdd}| |j| j t dd| jdd}| tt|d d S )	Nr   )r   r   r   r   r   )r   rH   r   )   r   r|   )	r   r   r   r   r   r|   rB   r   rJ   r   r   r   r   test_raw  s   zBufferProxyLegacyTest.test_rawc                 C   s  ddl m}m}m}m}m} d}|d }|tdd|| d d }||||}	| }
td||
f||
dfd	}z| |j	|||  |
|	 | |j	|	 ||
d||
 |
|	d d d
 |j	}| |d d
 |d
  | |d
d |	d d  | |dd  |||d   |
|	d d |jd  |j	}| |dd  |	d d  | t|j
|	d | t|j
|	d d d | t|j
|	d d |j | t|j
d td||
f||
dfd	}| tj|j
d W d }t  d S d }t  w )Nr   )c_bytesizeofr   ro   memset    rq   r
   r   r   F)r   r   r   rl   r   rH   Ts   123)ry   r   r   r   ro   r   rY   r   r   r|   writere   rB   
IndexError	TypeErrorr   r   r@   rA   )r   r   r   r   ro   r   ZnullbyteZBufZdata_bufr   r}   bpr|   r   r   r   
test_write  sJ   
 

z BufferProxyLegacyTest.test_writeN)r   r5   r6   r   ry   r   bufferr   r   r   r   r   r   r   r   r   r     s    
r   __main__)rb   rL   r@   ry   r   r   r   r   r   	NameErrorZTestCaser   r   r   mainr   r   r   r   <module>   s(       X