3
½ìœ]=  ã               @   sÈ   d Z ddlmZ ddl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.
é    )Úabsolute_importNé   )Ú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-build-pcqn41bp/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_compound4   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_singleE   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   )r0   g     €GÀ)r0   )r   r-   r   r   r   r'   r.   ÚassertArrayEqual)r   r)   r    r   r   r   Ú
test_multiM   s    zTestArray.test_multiN)r   r   r   r+   r/   r4   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   r0   .r   N)r   )r   r   Zint16Zint32Zint64Zuint8Zuint16Zuint32Zuint64r-   r   r   r'   r   r3   )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>m   s    z'TestTypes.test_float.<locals>.<genexpr>ú<f4ú>f4ú>f8ú<f8r   )r   gfffff&E@.r   zdtype: zvalue: N)r9   r:   r;   r<   )r   )	Útupler   r-   r   r   Úprintr   r'   r3   )r   r6   r(   r)   r    r   r   r   Ú
test_floatk   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   )r7   r   r   r   r   r8   |   s    z)TestTypes.test_complex.<locals>.<genexpr>ú<c8ú>c8ú<c16ú>c16r   )r   y        ÍÌÌÌÌÌ@g33333óA@.r   N)r@   rA   rB   rC   )r   y       €ÍÌÌÌÌÌÀy33333óA@ÍÌÌÌÌÌÀ)r=   r   r-   r   r   r'   r   r3   )r   r6   r(   r)   r    r   r   r   Útest_complexz   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   )r7   r   r   r   r   r8   ˆ   s    z(TestTypes.test_string.<locals>.<genexpr>ú|S1ú|S10r   )r   Úh.r   r   N)rE   rF   )r   )r=   r   r-   r   r   r'   r   )r   r6   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)rI   )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)ÚencodingrI   )r   ó   Helloó   Hi there!  This is HDF5!.r   r   r   N)rI   )rM   rN   )Ú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   ó   xN)r   r   r'   ÚtypeÚbytesrO   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 jdƒ d | jjd< | jjd }| j|dt jdƒ d ƒ | jt|ƒt jƒ 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) ZHelloi@#  z!!r   rQ   N)ÚsixÚunichrr   r   r'   rR   Ú	text_typerO   r   rT   rU   rV   r   rW   rX   Z	CSET_UTF8rY   rZ   )r   r    r[   r\   r   r   r   Útest_unicode_scalar¹   s    zTestTypes.test_unicode_scalarN)r   r   r   r+   r!   r?   rD   rH   rJ   rP   r]   ra   r   r   r   r   r5   Y   s   r5   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 )Nr1   rQ   ZS10)r   r   r   ÚcreateZNULLr   ZC_S1ÚcopyZset_sizer   r   rU   rO   ÚEmptyr   r   Ú	empty_obj)r   Zsidr\   r[   r   r   r   r   Ê   s    


zTestEmpty.setUpc             C   s   | j | j| jjd ƒ d S )Nr   )r'   rf   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)rf   r   r   rY   r
   r   rT   rU   )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'   rf   )r   rp   r   r   r   Útest_valuesß   s    zTestEmpty.test_valuesc             C   s(   t | jjjƒ ƒ}| jd| jfg|ƒ d S )Nr   )ro   r   r   Úitemsr'   rf   )r   rr   r   r   r   Ú
test_itemsæ   s    zTestEmpty.test_itemsc             C   s&   t tj| jjƒƒ}| j| jg|ƒ d S )N)ro   r^   Ú
itervaluesr   r   r'   rf   )r   rp   r   r   r   Útest_itervaluesì   s    zTestEmpty.test_itervaluesc             C   s*   t tj| jjƒƒ}| jd| jfg|ƒ d S )Nr   )ro   r^   Ú	iteritemsr   r   r'   rf   )r   rr   r   r   r   Útest_iteritemsò   s    zTestEmpty.test_iteritemsN)r   r   r   r   rg   rj   rn   rq   rs   ru   rw   r   r   r   r   rb   È   s   rb   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   Ú
ValueErrorrk   ÚKeyError)r   Úsr   r   r   rj   ÿ   s    zTestWriteException.test_writeN)r   r   r   r+   rj   r   r   r   r   rx   ù   s   rx   )r+   Ú
__future__r   r^   Znumpyr   Úcommonr   r   rO   r   r   r   r	   Zh5py._hl.baser
   r   r   r,   r5   rb   rx   r   r   r   r   Ú<module>   s   
o1