o
    bi(&                     @   s   d Z ddlZddlmZmZ ddlZddlmZmZm	Z	 ddlm
Z
 ddlmZ G dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZdS )z{
    Attribute data transfer testing module

    Covers all data read/write and type-conversion operations for attributes.
    N   )TestCaseut)h5ah5sh5t)File)is_empty_dataspacec                   @   s   e Zd Zdd Zdd ZdS )	BaseAttrsc                 C   s   t |  d| _d S )Nw)r   mktempfself r   N/home/ubuntu/.local/lib/python3.10/site-packages/h5py/tests/test_attrs_data.pysetUp   s   zBaseAttrs.setUpc                 C   s   | j r
| j   d S d S N)r   closer   r   r   r   tearDown   s   zBaseAttrs.tearDownN)__name__
__module____qualname__r   r   r   r   r   r   r
      s    r
   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )
TestScalarzB
        Feature: Scalar types map correctly to array scalars
    c                 C   s6   t jdt jd| jjd< | jjd }| |t j dS )z) Integers are read as correct NumPy type r   dtypexN)nparrayint8r   attrsassertIsInstance)r   outr   r   r   test_int*   s   zTestScalar.test_intc                 C   sf   t ddg}t jd|d}|| jjd< | jjd }| |t j | || | |d |d  dS )z) Compound scalars are read as numpy.void )ai)br   )r   g@r   r   r&   N)r   r   r   r   r    r!   voidassertEqualr   dtdatar"   r   r   r   test_compound0   s   zTestScalar.test_compoundc                 C   s   t dtt jfdtt jfg}t jt jttddt jdt jttddt jdf|dd }|| jj	d	< | jj	d	 }| j
||d
d dS )z; Compound scalars with vlen fields can be written and read r$   r&   r      r      
   r   r   Fcheck_alignmentN)r   r   h5py
vlen_dtypeint32r   listranger   r    assertArrayEqualr)   r   r   r   test_compound_with_vlen_fields:   s   z)TestScalar.test_compound_with_vlen_fieldsc                 C   s   t dtt jfdtt jfg}t dt|fdt jfg}t jtddt jdt jtdd	t jdf}t jtd
dt jdt jtddt jdf}t jt j||g|ddf|dd }|| jj	d< | jj	d }| j
||dd dS )zK Compound scalars with nested compound vlen fields can be written and read r$   r&   f1f2r      r      	   r/               r   r   Fr0   N)r   r   r2   r3   r4   int64r   r6   r   r    r7   )r   dt_innerr*   inner1inner2r+   r"   r   r   r   &test_nesting_compound_with_vlen_fieldsI   s,   z1TestScalar.test_nesting_compound_with_vlen_fieldsc                 C   s   t dt fdt fg}t dt|fg}t jt jddg|df|dd }|| jjd< | jjd }| j||d	d
 dS )zV Compound scalars with vlen compounds containing vlen strings can be written and read r$   r&   r   )s   appless   bananas)s   peachess   orangesr   r   r   Fr0   N)	r   r   r2   string_dtyper3   r   r   r    r7   )r   rC   r*   r+   r"   r   r   r   #test_vlen_compound_with_vlen_string_   s   
"z.TestScalar.test_vlen_compound_with_vlen_stringN)	r   r   r   __doc__r#   r,   r8   rF   rH   r   r   r   r   r   $   s    
r   c                   @   s    e Zd ZdZdd Zdd ZdS )	TestArrayzK
        Feature: Non-scalar types are correctly retrieved as ndarrays
    c                 C   sF   t jddd}|| jjd< | jjd }| |t j | |jd dS )z/ Single-element arrays are correctly recovered r   r   r   r   N)r   ndarrayr   r    r!   r(   shaper   r+   r"   r   r   r   test_singler   s
   zTestArray.test_singlec                 C   sj   t jddd}d|dd< d|dd< || jjd	< | jjd	 }| |t j | |jd | || dS )
z' Rank-1 arrays are correctly recovered )*   r   r   g      E@Ng     Gr/   #   r   )r   rL   r   r    r!   r(   rM   r7   rN   r   r   r   
test_multiz   s   zTestArray.test_multiN)r   r   r   rI   rO   rR   r   r   r   r   rJ   l   s    rJ   c                   @   sP   e Zd 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S )	TestTypeszF
        Feature: All supported types can be stored in attributes
    c                 C   sz   t jt jt jt jt jt jt jt jf}|D ]&}t j	d|d}d|d< || j
jd< | j
jd }| |j| | || qdS )z Storage of integer types rK   r   rP   .r   N)r   r   int16r4   rB   uint8uint16uint32uint64rL   r   r    r(   r   r7   r   dtypesr*   r+   r"   r   r   r   r#      s   zTestTypes.test_intc                 C   s   t dd dD }|D ]3}tjd|d}d|d< || jjd< | jjd }td	|j| td
|| | |j| | || qdS )z! Storage of floating point types c                 s       | ]}t |V  qd S r   r   r   .0r   r   r   r   	<genexpr>       z'TestTypes.test_float.<locals>.<genexpr>)z<f4z>f4z>f8z<f8rK   r   gfffff&E@.r   zdtype: zvalue: N)	tupler   rL   r   r    printr   r(   r7   rY   r   r   r   
test_float   s   zTestTypes.test_floatc                 C   sh   t dd dD }|D ]&}tjd|d}d|d< || jjd< | jjd }| |j| | || qd	S )
z Storage of complex types c                 s   r[   r   r\   r]   r   r   r   r_      r`   z)TestTypes.test_complex.<locals>.<genexpr>)z<c8z>c8z<c16z>c16rK   r   y33333A@.r   N)ra   r   rL   r   r    r(   r   r7   rY   r   r   r   test_complex   s   zTestTypes.test_complexc                 C   sp   t dd dD }|D ]*}tjd|d}d|d< || jjd< | jjd }| |j| | |d	 |d	  qd
S )z! Storage of fixed-length strings c                 s   r[   r   r\   r]   r   r   r   r_      r`   z(TestTypes.test_string.<locals>.<genexpr>)z|S1z|S10rK   r   h.r   r   N)ra   r   rL   r   r    r(   r   rY   r   r   r   test_string   s   zTestTypes.test_stringc                 C   sl   t jdt jd}d|d< || jjd< | jjd }| |j|j | |d |d  | |d |d  dS )	z Storage of NumPy booleans rA   r   )TF.r   r   r   N)r   rL   bool_r   r    r(   r   rN   r   r   r   	test_bool   s   zTestTypes.test_boolc                 C   st   t jdd}tjd|d}d|d< || jjd< | jjd }| |j| | |d |d  | |d	 |d	  d
S )z# Storage of vlen byte string arraysascii)encodingrg   r   )HellozHi there!  This is HDF5!.r   r   r   N)r2   rG   r   rL   r   r    r(   r   r)   r   r   r   test_vlen_string_array   s   z TestTypes.test_vlen_string_arrayc                 C   s   d| j jd< | j jd }| |d | t|t tj| j jd}|	 }| t|tj
j | | tj
j | |  dS )z@ Storage of variable-length byte string scalars (auto-creation) s   Hellor   rl      xN)r   r    r(   typestrr2   r   openidget_typer   TypeStringIDget_cset
CSET_ASCII
assertTrueis_variable_strr   r"   aidtidr   r   r   test_string_scalar   s   zTestTypes.test_string_scalarc                 C   s   dt d d | jjd< | jjd }| |dt d d  | t|t tj| jj	d}|
 }| t|tjj | | tjj | |  dS )z< Storage of variable-length unicode strings (auto-creation) rl   i@#  z!!r   rn   N)chrr   r    r(   ro   rp   r2   r   rq   rr   rs   r   rt   ru   	CSET_UTF8rw   rx   ry   r   r   r   test_unicode_scalar   s   zTestTypes.test_unicode_scalarN)r   r   r   rI   r#   rc   rd   rf   ri   rm   r|   r   r   r   r   r   rS      s    rS   c                   @   sL   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S )	TestEmptyc                 C   sT   t |  ttj}tj }|d t	| j
jd||}ttd| _d S )Nr/   rn   S10)r
   r   r   createNULLr   C_S1copyset_sizer   r   rr   r2   Emptyr   r   	empty_obj)r   sidr{   rz   r   r   r   r      s   


zTestEmpty.setUpc                 C   s   |  | j| jjd  d S Nr   )r(   r   r   r    r   r   r   r   	test_read   s   zTestEmpty.test_readc                 C   s,   | j | jjd< | tt| jjd d S )Ny   y)r   r   r    rw   r	   r   rq   rr   r   r   r   r   
test_write  s   zTestEmpty.test_writec                 C   s@   |  t | jjdd W d    d S 1 sw   Y  d S )Nr   r   )assertRaisesOSErrorr   r    modifyr   r   r   r   test_modify  s   "zTestEmpty.test_modifyc                 C   $   t | jj }| | jg| d S r   r5   r   r    valuesr(   r   r   r   r   r   r   test_values  s   zTestEmpty.test_valuesc                 C   (   t | jj }| d| jfg| d S r   r5   r   r    itemsr(   r   r   r   r   r   r   
test_items     zTestEmpty.test_itemsc                 C   r   r   r   r   r   r   r   test_itervalues  s   zTestEmpty.test_itervaluesc                 C   r   r   r   r   r   r   r   test_iteritems  r   zTestEmpty.test_iteritemsN)r   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 )TestWriteExceptionzH
        Ensure failed attribute writes don't leave garbage behind.
    c                 C   sd   d}z|| j jd< W n	 ty   Y nw | t | j jd  W d   dS 1 s+w   Y  dS )z0 ValueError on string write wipes out attribute s   Hello Hellor   N)r   r    
ValueErrorr   KeyError)r   sr   r   r   r   ,  s   "zTestWriteException.test_writeN)r   r   r   rI   r   r   r   r   r   r   &  s    r   )rI   numpyr   commonr   r   r2   r   r   r   r   h5py._hl.baser	   r
   r   rJ   rS   r   r   r   r   r   r   <module>   s   	
Ho1