U
    ½mœdÞ  ã                   @   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   úS/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/h5py/tests/test_attrs_data.pyÚsetUp   s    zBaseAttrs.setUpc                 C   s   | j r| j  ¡  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                   @   s    e Zd Z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_compoundN)r   r   r   Ú__doc__r#   r,   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_singleA   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     €GÀé
   é#   r   )r   r0   r   r    r!   r(   r1   ÚassertArrayEqualr2   r   r   r   Ú
test_multiI   s    zTestArray.test_multiN)r   r   r   r-   r3   r8   r   r   r   r   r.   ;   s   r.   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 ]L}t j	d|d�}d|d< || j
jd< | j
jd }|  |j|¡ |  ||¡ q(dS )z Storage of integer types r/   r   r4   .r   N)r   r   Zint16Zint32Zint64Zuint8Zuint16Zuint32Zuint64r0   r   r    r(   r   r7   ©r   Zdtypesr*   r+   r"   r   r   r   r#   [   s       ÿzTestTypes.test_intc                 C   s‚   t dd„ dD ƒƒ}|D ]f}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   s   | ]}t  |¡V  qd S r   ©r   r   ©Ú.0r   r   r   r   Ú	<genexpr>i   s     z'TestTypes.test_float.<locals>.<genexpr>)z<f4z>f4z>f8z<f8r/   r   gfffff&E@.r   zdtype: zvalue: N)	Útupler   r0   r   r    Úprintr   r(   r7   r:   r   r   r   Ú
test_floatg   s    zTestTypes.test_floatc                 C   sh   t dd„ dD ƒƒ}|D ]L}tjd|d�}d|d< || jjd< | jjd }|  |j|¡ |  ||¡ qd	S )
z Storage of complex types c                 s   s   | ]}t  |¡V  qd S r   r;   r<   r   r   r   r>   x   s     z)TestTypes.test_complex.<locals>.<genexpr>)z<c8z>c8z<c16z>c16r/   r   y33333óA@ÍÌÌÌÌÌÀ.r   N)r?   r   r0   r   r    r(   r   r7   r:   r   r   r   Útest_complexv   s    zTestTypes.test_complexc                 C   sp   t dd„ dD ƒƒ}|D ]T}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   s   | ]}t  |¡V  qd S r   r;   r<   r   r   r   r>   „   s     z(TestTypes.test_string.<locals>.<genexpr>)z|S1z|S10r/   r   Úh.r   r   N)r?   r   r0   r   r    r(   r   r:   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 ©é   r   )TF.r   r   r   N)r   r0   Zbool_r   r    r(   r   r2   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)ÚencodingrE   r   )ÚHellozHi there!  This is HDF5!.r   r   r   N)Úh5pyZstring_dtyper   r0   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   rJ   ó   xN)r   r    r(   ÚtypeÚstrrK   r   ÚopenÚidÚget_typer   ÚTypeStringIDÚget_csetZ
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) rJ   i@#  z!!r   rM   N)Úchrr   r    r(   rN   rO   rK   r   rP   rQ   rR   r   rS   rT   Z	CSET_UTF8rU   rV   rW   r   r   r   Útest_unicode_scalarµ   s    zTestTypes.test_unicode_scalarN)r   r   r   r-   r#   rA   rB   rD   rG   rL   rZ   r\   r   r   r   r   r9   U   s   r9   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 )Nr5   rM   ZS10)r
   r   r   ÚcreateZNULLr   ZC_S1ÚcopyZset_sizer   r   rQ   rK   ÚEmptyr   r   Ú	empty_obj)r   ZsidrY   rX   r   r   r   r   Æ   s    


zTestEmpty.setUpc                 C   s   |   | j| jjd ¡ d S ©Nr   )r(   ra   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)ra   r   r    rU   r	   r   rP   rQ   r   r   r   r   Ú
test_writeÓ   s    zTestEmpty.test_writec              	   C   s*   |   t¡� | jj dd¡ W 5 Q R X d S )Nr   r   )ÚassertRaisesÚIOErrorr   r    Úmodifyr   r   r   r   Útest_modify×   s    zTestEmpty.test_modifyc                 C   s$   t | jj ¡ ƒ}|  | jg|¡ d S r   ©Úlistr   r    Úvaluesr(   ra   ©r   rm   r   r   r   Útest_valuesÛ   s
     ÿzTestEmpty.test_valuesc                 C   s(   t | jj ¡ ƒ}|  d| jfg|¡ d S rb   ©rl   r   r    Úitemsr(   ra   ©r   rq   r   r   r   Ú
test_itemsâ   s
    
 ÿzTestEmpty.test_itemsc                 C   s$   t | jj ¡ ƒ}|  | jg|¡ d S r   rk   rn   r   r   r   Útest_itervaluesè   s
     ÿzTestEmpty.test_itervaluesc                 C   s(   t | jj ¡ ƒ}|  d| jfg|¡ d S rb   rp   rr   r   r   r   Útest_iteritemsî   s
    
 ÿzTestEmpty.test_iteritemsN)r   r   r   r   rc   rf   rj   ro   rs   rt   ru   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   sP   d}z|| j jd< W n tk
r(   Y nX |  t¡� | j jd  W 5 Q R X dS )z0 ValueError on string write wipes out attribute s   Hello Hellor   N)r   r    Ú
ValueErrorrg   ÚKeyError)r   Úsr   r   r   rf   û   s    zTestWriteException.test_writeN)r   r   r   r-   rf   r   r   r   r   rv   õ   s   rv   )r-   Únumpyr   Úcommonr   r   rK   r   r   r   r   Zh5py._hl.baser	   r
   r   r.   r9   r]   rv   r   r   r   r   Ú<module>
   s   
o1