3
_½µ_Þ  ã               @   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 | jƒ dƒ| _d S )NÚw)r   ÚmktempÚf)Úself© r   ú</tmp/pip-install-xpigntgx/h5py/h5py/tests/test_attrs_data.pyÚsetUp   s    zBaseAttrs.setUpc             C   s   | j r| j jƒ  d S )N)r   Úclose)r   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 }| j|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 jd
dgƒ}t jd|d�}|| jjd< | jjd }| j|t jƒ | j||ƒ | j|d |d ƒ d	S )z) Compound scalars are read as numpy.void ÚaÚiÚbr   r   çÍÌÌÌÌÌ@)r   r   N)r!   r"   )r#   r   )r   r$   )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 }| j|t jƒ | j|jdƒ dS )z/ Single-element arrays are correctly recovered r   r   )r   r   N)r   )r   )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	 }| j|t jƒ | j|jdƒ | j||ƒ dS )z' Rank-1 arrays are correctly recovered é*   r   )r   g      E@Ng     €G@é
   é#   r   )r/   g     €GÀ)r/   )r   r,   r   r   r   r&   r-   ÚassertArrayEqual)r   r(   r   r   r   r   Ú
test_multiI   s    zTestArray.test_multiN)r   r   r   r*   r.   r3   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   s~   t jt jt jt jt jt jt jt jf}xT|D ]L}t j	d|d�}d|d< || j
jd< | j
jd }| j|j|ƒ | j||ƒ q*W dS )z Storage of integer types r   )r   r/   .r   N)r   )r   r   Zint16Zint32Zint64Zuint8Zuint16Zuint32Zuint64r,   r   r   r&   r   r2   )r   Údtypesr'   r(   r   r   r   r   r    [   s    
zTestTypes.test_intc             C   s†   t dd„ dD ƒƒ}xn|D ]f}tjd|d�}d	|d
< || jjd< | jjd }td|j|ƒ td||ƒ | j|j|ƒ | j||ƒ qW dS )z! Storage of floating point types c             s   s   | ]}t j|ƒV  qd S )N)r   r   )Ú.0r   r   r   r   ú	<genexpr>i   s    z'TestTypes.test_float.<locals>.<genexpr>ú<f4ú>f4ú>f8ú<f8r   )r   gfffff&E@.r   zdtype: zvalue: N)r8   r9   r:   r;   )r   )	Útupler   r,   r   r   Úprintr   r&   r2   )r   r5   r'   r(   r   r   r   r   Ú
test_floatg   s    
zTestTypes.test_floatc             C   sl   t dd„ dD ƒƒ}xT|D ]L}tjd|d�}d|d< || jjd< | jjd }| j|j|ƒ | j||ƒ qW dS )z Storage of complex types c             s   s   | ]}t j|ƒV  qd S )N)r   r   )r6   r   r   r   r   r7   x   s    z)TestTypes.test_complex.<locals>.<genexpr>ú<c8ú>c8ú<c16ú>c16r   )r   y        ÍÌÌÌÌÌ@g33333óA@.r   N)r?   r@   rA   rB   )r   y       €ÍÌÌÌÌÌÀy33333óA@ÍÌÌÌÌÌÀ)r<   r   r,   r   r   r&   r   r2   )r   r5   r'   r(   r   r   r   r   Útest_complexv   s    
zTestTypes.test_complexc             C   st   t dd„ dD ƒƒ}x\|D ]T}tjd|d�}d|d< || jjd	< | jjd	 }| j|j|ƒ | j|d
 |d
 ƒ qW dS )z! Storage of fixed-length strings c             s   s   | ]}t j|ƒV  qd S )N)r   r   )r6   r   r   r   r   r7   „   s    z(TestTypes.test_string.<locals>.<genexpr>ú|S1ú|S10r   )r   Úh.r   r   N)rD   rE   )r   )r<   r   r,   r   r   r&   r   )r   r5   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|jƒ | j|d |d ƒ | j|d |d ƒ d	S )z Storage of NumPy booleans é   )r   TF.r   r   r   N)rH   )TF)r   r,   Zbool_r   r   r&   r   )r   r(   r   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|j|ƒ | j|d	 |d	 ƒ | j|d
 |d
 ƒ dS )z# Storage of vlen byte string arraysÚascii)ÚencodingrH   )r   ÚHelloúHi there!  This is HDF5!.r   r   r   N)rH   )rL   rM   )Úh5pyZstring_dtyper   r,   r   r   r&   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 }| j|dƒ | jt|ƒtƒ tjj| j jdƒ}|j	ƒ }| jt|ƒtj
jƒ | j|jƒ tj
jƒ | j|jƒ ƒ dS )z@ Storage of variable-length byte string scalars (auto-creation) s   Hellor   rL   ó   xN)r   r   r&   ÚtypeÚstrrN   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 }| j|dt dƒ d ƒ | jt|ƒtƒ tjj| jj	dƒ}|j
ƒ }| jt|ƒtjjƒ | j|jƒ tjjƒ | j|jƒ ƒ dS )z< Storage of variable-length unicode strings (auto-creation) rL   i@#  z!!r   rP   N)Úchrr   r   r&   rQ   rR   rN   r   rS   rT   rU   r   rV   rW   Z	CSET_UTF8rX   rY   )r   r   rZ   r[   r   r   r   Útest_unicode_scalarµ   s    zTestTypes.test_unicode_scalarN)r   r   r   r*   r    r>   rC   rG   rI   rO   r\   r^   r   r   r   r   r4   U   s   r4   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 j| ƒ tjtjƒ}tjjƒ }|jdƒ t	j| j
jd||ƒ}tjtjdƒƒ| _d S )Nr0   rP   ZS10)r
   r   r   ÚcreateZNULLr   ZC_S1ÚcopyZset_sizer   r   rT   rN   ÚEmptyr   r   Ú	empty_obj)r   Zsidr[   rZ   r   r   r   r   Æ   s    


zTestEmpty.setUpc             C   s   | j | j| jjd ƒ d S )Nr   )r&   rc   r   r   )r   r   r   r   Ú	test_readÎ   s    zTestEmpty.test_readc             C   s,   | j | jjd< | jttj| jjdƒƒƒ d S )NÚyó   y)rc   r   r   rX   r	   r   rS   rT   )r   r   r   r   Ú
test_writeÓ   s    zTestEmpty.test_writec          
   C   s*   | j tƒ� | jjjddƒ W d Q R X d S )Nr   r   )ÚassertRaisesÚIOErrorr   r   Úmodify)r   r   r   r   Útest_modify×   s    zTestEmpty.test_modifyc             C   s$   t | jjjƒ ƒ}| j| jg|ƒ d S )N)Úlistr   r   Úvaluesr&   rc   )r   rm   r   r   r   Útest_valuesÛ   s    zTestEmpty.test_valuesc             C   s(   t | jjjƒ ƒ}| jd| jfg|ƒ d S )Nr   )rl   r   r   Úitemsr&   rc   )r   ro   r   r   r   Ú
test_itemsâ   s    zTestEmpty.test_itemsc             C   s$   t | jjjƒ ƒ}| j| jg|ƒ d S )N)rl   r   r   rm   r&   rc   )r   rm   r   r   r   Útest_itervaluesè   s    zTestEmpty.test_itervaluesc             C   s(   t | jjjƒ ƒ}| jd| jfg|ƒ d S )Nr   )rl   r   r   ro   r&   rc   )r   ro   r   r   r   Útest_iteritemsî   s    zTestEmpty.test_iteritemsN)r   r   r   r   rd   rg   rk   rn   rp   rq   rr   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}y|| j jd< W n tk
r(   Y nX | jtƒ� | j jd  W dQ R X dS )z0 ValueError on string write wipes out attribute s   Hello Hellor   N)r   r   Ú
ValueErrorrh   ÚKeyError)r   Úsr   r   r   rg   û   s    zTestWriteException.test_writeN)r   r   r   r*   rg   r   r   r   r   rs   õ   s   rs   )r*   Znumpyr   Úcommonr   r   rN   r   r   r   r   Zh5py._hl.baser	   r
   r   r+   r4   r_   rs   r   r   r   r   Ú<module>   s   
o1