o
    tVgC@                     @   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   Q/home/ubuntu/.local/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   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       r8   c                    s    d)NzJust a test.r   r0   )r8   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   r3   r:   r!   r#   cr   )r8   r*   r2   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.   r0   r1   r   r   r3   m   r   z,BufferProxyTest.test_after.<locals>.callbackr'   afterr   r
   )
r(   r   r   r   r   r%   rA   rB   r@   r=   )r   r3   r!   r"   r#   rD   r   r1   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   rC   AttributeErrorgetattrrH   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)   rA   rB   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   r0   rr   r   before_callback   r;   z0BufferProxyTest.test_gc.<locals>.before_callbackc                    rR   )Nr
   r   r0   rS   r   r   after_callback   r;   z/BufferProxyTest.test_gc.<locals>.after_callbackc                   @   r4   )z$BufferProxyTest.test_gc.<locals>.ObjNr5   r   r   r   r   Obj   r9   rW   r   r
   r'   r<   rF   Nc                 S   s   g | ]}t  qS r   )rA   rB   ).0xr   r   r   
<listcomp>   s    z+BufferProxyTest.test_gc.<locals>.<listcomp>r   TF)rM   rN   r(   r   r   some_attributerA   rB   r@   rangeget_objectsr/   r   garbager   )r   rU   rV   rW   r*   aweak_pweak_aweak_r0weak_r1weak_before
weak_afterr!   r"   rP   trackedon1n2r   rS   r   test_gc   sr   










zBufferProxyTest.test_gcc                 C   s$   t jj}tt jj}| || d S r-   )r   r   _PYGAME_C_APItypebaseassertIsInstance)r   api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"   cnameoname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   r|      r   z@BufferProxyTest.test_subclassing.<locals>.MyBufferProxy.__repr__N)r   r6   r7   r|   r   r   r   r   MyBufferProxy   s    r}   r   r'      z*<z>*)r   r(   r   r   r'   rt   )r   r}   r!   r"   rT   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)r   r~   z=ir
   )	   T)r   r   r   r   r   r   r
   )ctypesr   pygame.tests.test_utilsr   ExporterImporterr   r   ru   r   rawbufr%   r   r   r   get_sdl_byteorder
LIL_ENDIANitemsizePyBUF_RECORDSr@   objndimformatr   
suboffsetsPyBUF_SIMPLE)r   r   r   r   r   expbr#   
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)rg   r   r   r   r:   V  r;   z?BufferProxyTest.OLDBUF_test_oldbuf_arg.<locals>.raise_exceptionr   Fr   r   r
   T)r   r   r   r   r<   r   r   |u4rI   r   rI   0   r   )pygame.bufferproxyr   r   r   r   create_string_buffer	addressofr   r   r   rC   r   r\   )r   r   r   r   contentmemorymemaddrr:   bfseglensegaddrsegcountbuflenr   r   r   r   r   O  s   

	z&BufferProxyTest.OLDBUF_test_oldbuf_argN)r   r6   r7   r   r   r   r$   r&   r+   rE   rG   rL   rQ   rj   rq   rz   r   unittestskipIfr   HAVE_NEWBUFr   r   r   rJ   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   ru   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   rI   r   )   r   r   )	r   r   r   r   r   r   rC   r   rK   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   r   memset    r   r
   r   r   F)r   r   r   r~   r   rI   Ts   123)r   r   r   r   r   r   r\   r   r   r   writeru   rC   
IndexError	TypeErrorr   r   rA   rB   )r   r   r   r   r   r   nullbyteBufdata_bufr   r   bpr   r   r   r   
test_write  sJ   
 

z BufferProxyLegacyTest.test_writeN)r   r6   r7   r   r   r   bufferr   r   r   r   r   r   r   r   r   r     s    
r   __main__)rr   rM   rA   r   r   r   r   r   r   	NameErrorTestCaser   r   r   mainr   r   r   r   <module>   s(       X