a
    aK6                     @   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
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G dd deZG dd deZdS )a  
    Dataset slicing test module.

    Tests all supported slicing operations, including read/write and
    broadcasting operations.  Does not test type conversion except for
    corner cases overlapping with slicing; for example, when selecting
    specific fields of a compound type.
    N   )utTestCase)h5sh5th5d)FileMultiBlockSlicec                   @   s   e Zd Zdd Zdd ZdS )BaseSlicingc                 C   s   t |  d| _d S )Nw)r   mktempfself r   6lib/python3.9/site-packages/h5py/tests/test_slicing.pysetUp   s    zBaseSlicing.setUpc                 C   s   | j r| j   d S Nr   closer   r   r   r   tearDown    s    zBaseSlicing.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 )TestSingleElementzM
        Feature: Retrieving a single element works with NumPy semantics
    c                 C   s,   | j jdddd}|d }| |tj dS )z; Single-element selection with [index] yields array scalar xr   i1dtyper   Nr   create_datasetassertIsInstancenpZint8r   dsetoutr   r   r   test_single_index*   s    z#TestSingleElement.test_single_indexc                 C   s:   | j jdddd}|d }| |tj | |jd dS )z3 Single-element selection with [()] yields ndarray r   r   r   r   r   Nr   r!   r"   r#   ndarrayassertEqualshaper$   r   r   r   test_single_null0   s    z"TestSingleElement.test_single_nullc                 C   s:   | j jdddd}|d }| |tj | |jd dS )z* Slicing with [...] yields scalar ndarray r   r   r   r+   r   .Nr(   r$   r   r   r   test_scalar_index7   s    z#TestSingleElement.test_scalar_indexc                 C   s,   | j jdddd}|d }| |tj dS )z' Slicing with [()] yields array scalar r   r   r   r-   Nr    r$   r   r   r   test_scalar_null>   s    z"TestSingleElement.test_scalar_nullc                 C   sX   t ddg}t jd|d}| jjdd|d}| |d |d  | |d t j dS )	z6 Compound scalar is numpy.void, not tuple (issue 135) )aZi4)bf8)   r   Zfoodatar   N)r#   r   onesr   r!   r*   r"   Zvoid)r   dtvr%   r   r   r   test_compoundD   s
    zTestSingleElement.test_compoundN)	r   r   r   __doc__r'   r,   r.   r/   r9   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 )TestObjectIndexzH
        Feature: numpy.object_ subtypes map to real Python objects
    c                 C   s:   | j jddtjd}| j j|d< | t|d tj dS )z@ Indexing a reference dataset returns a h5py.Reference instance r   r   r   r   Nr   r!   h5py	ref_dtyperefr*   type	Referencer   r%   r   r   r   test_referenceR   s    zTestObjectIndex.test_referencec                 C   sN   | j dd}|jd }| j jddtjd}||d< | t|d tj dS )	zL Indexing a region reference dataset returns a h5py.RegionReference
        r   )
   rD   .yr   r   r   N)r   r!   Z	regionrefr=   Zregionref_dtyper*   r@   ZRegionReference)r   Zdset1ZregrefZdset2r   r   r   test_regrefX   s
    
zTestObjectIndex.test_regrefc                 C   s\   t ddtjfg}| jjdd|d}d| jd jf|d< |d }| t|d	 tj	 d
S )z> Compound types of which a reference is an element work right )r0   ir1   r   r   r   *   /r   r   N)
r#   r   r=   r>   r   r!   r?   r*   r@   rA   r   r7   r%   r&   r   r   r   test_reference_fielda   s
    z$TestObjectIndex.test_reference_fieldc                 C   s:   | j jddtjd}| j j|d< | t|d tj dS )z: Indexing returns a real Python object on scalar datasets r   r   r   Nr<   rB   r   r   r   test_scalark   s    zTestObjectIndex.test_scalarc                 C   s:   | j jddtjddd}d|d< | t|d t dS )	zJ Indexing a byte string dataset returns a real python byte string
        r   r   ascii)encodingr   s   Hello there!r   N)r   r!   r=   Zstring_dtyper*   r@   bytesrB   r   r   r   test_bytestrq   s    zTestObjectIndex.test_bytestrN)	r   r   r   r:   rC   rF   rK   rL   rP   r   r   r   r   r;   L   s   	
r;   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )TestSimpleSlicingzQ
        Feature: Simple NumPy-style slices (start:stop:step) are supported.
    c                 C   s4   t |  d| _td| _| jjd| jd| _d S )Nr   rD   r   r4   )r   r   r   r#   arangearrr!   r%   r   r   r   r   r   ~   s    zTestSimpleSlicing.setUpc                 C   s   | j r| j   d S r   r   r   r   r   r   r      s    zTestSimpleSlicing.tearDownc                 C   s$   |  | jdd | jdd  dS )z0 Negative stop indexes work as they do in NumPy    N)assertArrayEqualr%   rS   r   r   r   r   test_negative_stop   s    z$TestSimpleSlicing.test_negative_stopc                 C   sr   | j dd}td}|dddf |dddf< | t  ||dddf< W d   n1 sd0    Y  dS )z0Assigning to a 1D slice of a 2D dataset
        Zx2)rD   rT   )rD   r   Nr   r   )r   r!   r#   zerosassertRaises	TypeError)r   r%   r   r   r   r   
test_write   s
    
zTestSimpleSlicing.test_writeN)r   r   r   r:   r   r   rW   r[   r   r   r   r   rQ   x   s
   rQ   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 )TestArraySlicingz<
        Feature: Array types are handled appropriately
    c                 C   s   t d}| jjdd|d}| |jd | |j| |d }| |jt d | |jd |d }| |jt d | |jd	 |d
dd
 }| |jt d | |jd dS )z; Read arrays tack array dimensions onto end of shape tuple (3,)f8r   rD   r   .r2   )rD      r   r_   rT      )r_   r_   N)r#   r   r   r!   r*   r+   rJ   r   r   r   	test_read   s    
zTestArraySlicing.test_readc                 C   sR   t d}| jjdd|d}| t d|d< W d   n1 sD0    Y  dS )z@ Array fill from constant is not supported (issue 211).
        (3,)ir   r^   r   rH   .N)r#   r   r   r!   rY   rZ   )r   r7   r%   r   r   r   test_write_broadcast   s    
z%TestArraySlicing.test_write_broadcastc                 C   sR   t d}| jjdd|d}t g d}||d< |d }| t ||k dS )zA Write a single element to the array

        Issue 211.
        r]   r   r^   r   )r   rT   g      @r3   N)r#   r   r   r!   array
assertTrueall)r   r7   r%   r5   r&   r   r   r   test_write_element   s    
z#TestArraySlicing.test_write_elementc                 C   s   t d}t jd|d}t jd|d}| jjdd|d}||dddd	f< | |dddd	f | ||d
ddddf< | |d
ddddf | dS )z Write slices to array type rc   )rT   r   )r3      r   )rD   	      r   rT   r3   r_   r   ri      rk   N)r#   r   r6   r   r!   rV   )r   r7   Zdata1data2r%   r   r   r   test_write_slices   s    
z"TestArraySlicing.test_write_slicesc                 C   sH   t d}| jjdd|d}|d }||d< | t |d |k dS )zO Read the contents of an array and write them back

        Issue 211.
        r]   r   r^   r   .N)r#   r   r   r!   rf   rg   rJ   r   r   r   test_roundtrip   s
    
zTestArraySlicing.test_roundtripN)	r   r   r   r:   rb   rd   rh   rn   ro   r   r   r   r   r\      s   
r\   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	TestZeroLengthSlicingz.
        Slices resulting in empty arrays
    c                 C   s   t g dD ]\}}| jjd| |tdt| d}| |j| |d }| |tj	 | |j| |dd }| |tj	 | |j| t|dkr|ddddf }| |tj	 | |jdd d	 qdS )
z] Slice a dataset with a zero in its shape vector
            along the zero-length dimension )r   )r   r_   )r   rT   r   x%dr   r   maxshape.Nr   rT   )r   r   
	enumerater   r!   intlenr*   r+   r"   r#   r)   r   rG   r+   r%   r&   r   r   r    test_slice_zero_length_dimension   s     z6TestZeroLengthSlicing.test_slice_zero_length_dimensionc                 C   s|   t g dD ]j\}}| jjd| |tdt| d}| |j| |dd }| |tj	 | |jd|dd   qdS )z_ Slice a dataset with a zero in its shape vector
            along a non-zero-length dimension ))r_   r   )r   rT   r   )rT   r   r   rr   r   rs   Nr   r   ru   ry   r   r   r   test_slice_other_dimension   s     z0TestZeroLengthSlicing.test_slice_other_dimensionc                 C   s   t g dD ]p\}}| jjd| t|tdt| d}| |j| |dd }| 	|tj
 | |jd|dd   qdS )z5 Get a slice of length zero from a non-empty dataset )r`   )rT   rT   )rT   r   ri   rr   r   )r5   rt   r   rq   N)rv   r   r!   r#   rX   rw   rx   r*   r+   r"   r)   ry   r   r   r   test_slice_of_length_zero  s    &z/TestZeroLengthSlicing.test_slice_of_length_zeroN)r   r   r   r:   rz   r{   r|   r   r   r   r   rp      s   
rp   c                   @   sT   e Zd ZdZeg dZejdedZdd Z	dd Z
d	d
 Zdd Zdd ZdS )TestFieldNamesz*
        Field names for read & write
    ))r0   r   )r1   rG   )cZf4d   r   c                 C   s0   t |  | jjdd| jd| _| j| jd< d S )Nr   r   r   .)r
   r   r   r!   r7   r%   r5   r   r   r   r   r     s    
zTestFieldNames.setUpc                 C   s   |  | jd | jd  dS )z! Test read with field selections r0   N)rV   r%   r5   r   r   r   r   rb     s    zTestFieldNames.test_readc                 C   sN   |  | jd | jd  d| jd< | j }d|d< |  | jd |d  dS )z, Unicode field names for for read and write r0   rH   N)rV   r%   r5   copy)r   r5   r   r   r   test_unicode_names#  s
    

z!TestFieldNames.test_unicode_namesc                 C   s   | j  }|d  d9  < || jd< | t| jd |k |d  d9  < || jd< | t| jd |k |d  d9  < |d  d9  < || jd< | t| jd |k d	S )
z" Test write with field selections r0   rT   .r1   r3   r_   r~   )r0   r~   Nr5   r   r%   rf   r#   rg   r   rm   r   r   r   r[   +  s    



zTestFieldNames.test_writec                 C   s:   | j  }d|d< d| jd< | t| jd |k dS )z4 Test write with non-compound source (single-field) g      ?r1   .Nr   r   r   r   r   test_write_noncompound9  s    

z%TestFieldNames.test_write_noncompoundN)r   r   r   r:   r#   r   r7   r6   r5   r   rb   r   r[   r   r   r   r   r   r}     s   r}   c                       s   e Zd Z f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dd Z  ZS )TestMultiBlockSlicec                    s2   t t|   td| _| jjd| jd| _d S )NrD   r   r4   )	superr   r   r#   rR   rS   r   r!   r%   r   	__class__r   r   r   C  s    zTestMultiBlockSlice.setUpc                 C   s2   t  }| |dd tj| j| | j d S )NrD   r   r   rD   r   r	   r*   indicesr#   testingassert_array_equalr%   rS   r   Zmbslicer   r   r   test_defaultH  s    z TestMultiBlockSlice.test_defaultc                 C   s<   t ddddd}| |dd tj| j| | j d S )Nr   rD   r   startcountstrideblockr   r   r   r   r   r   test_default_explicitO  s    z)TestMultiBlockSlice.test_default_explicitc                 C   s>   t dd}| |dd tj| j| tg d d S )Nr3   )r   rD   )r3   r   rl   r   )r3   ri   rl      ra   rj   r	   r*   r   r#   r   r   r%   re   r   r   r   r   
test_startU  s    
zTestMultiBlockSlice.test_startc                 C   s>   t dd}| |dd tj| j| tg d d S )Nr   r   rD   )r   r   r   r   )r   r   rT   r_   r3   ri   rl   r   r   r   r   r   
test_count[  s
    
zTestMultiBlockSlice.test_countc                 C   sB   t dd}| t |d W d    n1 s40    Y  d S )Nrk   r   rD   r	   rY   
ValueErrorr   r   r   r   r   !test_count_more_than_length_errorc  s    
z5TestMultiBlockSlice.test_count_more_than_length_errorc                 C   s>   t dd}| |dd tj| j| tg d d S )NrT   )r   rD   )r   rT   ri   r   )r   rT   r3   rl   ra   r   r   r   r   r   test_strideh  s    
zTestMultiBlockSlice.test_stridec                 C   s@   |  t" tdddd W d    n1 s20    Y  d S )Nr   r   r   rD   )rY   r   r	   r   r   r   r   r   test_stride_zero_errorn  s    z*TestMultiBlockSlice.test_stride_zero_errorc                 C   s8   t ddd}| |dd tj| j| | j d S )NrT   r   rD   )r   rT   ri   rT   r   r   r   r   r   test_stride_block_equals  s    z+TestMultiBlockSlice.test_stride_block_equalc                 C   sn   |  t tdd W d    n1 s*0    Y  |  t tddd W d    n1 s`0    Y  d S )Nr_   )r   rT   r   )rY   r   r	   r   r   r   r   !test_block_more_than_stride_errory  s    (z5TestMultiBlockSlice.test_block_more_than_stride_errorc                 C   s@   t ddd}| |dd tj| j| tg d d S )Nr_   rT   r   rD   )r   r_   r_   rT   )r   r   r_   r3   rl   r   r   r   r   r   r   test_stride_more_than_block  s    z/TestMultiBlockSlice.test_stride_more_than_blockc                 C   sH   t ddddd}| t |d W d    n1 s:0    Y  d S )NrT   ri   r3   r   rD   r   r   r   r   r    test_block_overruns_extent_error  s    z4TestMultiBlockSlice.test_block_overruns_extent_errorc                 C   sD   t ddddd}| |dd tj| j| tg d d S )	Nr   rT   ri   r3   r   rD   )r   ri   rT   r3   )r   rT   r_   r3   rl   r   ra   rj   r   r   r   r   r   test_fully_described  s
    z(TestMultiBlockSlice.test_fully_describedc                 C   sB   t dddd}| |dd tj| j| tg d d S )Nr   r_   rT   r   r   r   rD   )r   r_   r_   rT   )r   rT   r3   ri   r   ra   r   r   r   r   r   test_count_calculated  s    z)TestMultiBlockSlice.test_count_calculatedc                 C   sF   t dddd}| t |d W d    n1 s80    Y  d S )Nra   r3   r_   r   rD   r   r   r   r   r    test_zero_count_calculated_error  s    z4TestMultiBlockSlice.test_zero_count_calculated_error)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r   r   r   A  s   r   )r:   Znumpyr#   commonr   r   r=   r   r   r   r   r	   r
   r   r;   rQ   r\   rp   r}   r   r   r   r   r   <module>
   s   		(,R*0