o
    ʩZhlU                     @   s   d dl Z d dlZd dlmZmZmZ d dlZd dlm	Z	 G dd de	j
ZG dd de	j
ZG dd	 d	e	j
ZG d
d de	j
ZG dd de	j
ZG dd de	j
Ze jjG dd de	j
Ze jjG dd de	j
ZdS )    N)assert_array_equalassert_equalassert_raises)utilc                   @   s  e Zd ZdZdZg dZdZeD ]ZeZe	dd
eeZeedg de de d	e d
e de de de d	e de de de de de de de de de de de de de de de de de d7 Zqejdedd Zejdedd dd Zejdedd Zejded d! Zejded"d# ZdS )$TestCharacterString.f90Ztest_character_string13star (*))r   z

        subroutine _input_z(c, o, n)
          character*z, intent(in) :: c
          integer n
          !f2py integer, depend(c), intent(hide) :: n = slen(c)
          integer*1, dimension(n) :: o
          !f2py intent(out) o
          o = transfer(c, o)
        end subroutine _output_z, intent(out) :: c
          integer n
          integer*1, dimension(n), intent(in) :: o
          !f2py integer, depend(o), intent(hide) :: n = len(o)
          c = transfer(o, c)
        end subroutine _array_input_z;(c, o, m, n)
          integer m, i, n
          character*aH  , intent(in), dimension(m) :: c
          !f2py integer, depend(c), intent(hide) :: m = len(c)
          !f2py integer, depend(c), intent(hide) :: n = f2py_itemsize(c)
          integer*1, dimension(m, n), intent(out) :: o
          do i=1,m
            o(i, :) = transfer(c(i), o(i, :))
          end do
        end subroutine _array_output_z!(c, o, m, n)
          character*a  , intent(out), dimension(m) :: c
          integer n
          integer*1, dimension(m, n), intent(in) :: o
          !f2py character(f2py_len=n) :: c
          !f2py integer, depend(o), intent(hide) :: m = len(o)
          !f2py integer, depend(o), intent(hide) :: n = shape(o, 1)
          do i=1,m
            c(i) = transfer(o(i, :), c(i))
          end do
        end subroutine _2d_array_input_zH(c, o, m1, m2, n)
          integer m1, m2, i, j, n
          character*a  , intent(in), dimension(m1, m2) :: c
          !f2py integer, depend(c), intent(hide) :: m1 = len(c)
          !f2py integer, depend(c), intent(hide) :: m2 = shape(c, 1)
          !f2py integer, depend(c), intent(hide) :: n = f2py_itemsize(c)
          integer*1, dimension(m1, m2, n), intent(out) :: o
          do i=1,m1
            do j=1,m2
              o(i, j, :) = transfer(c(i, j), o(i, j, :))
            end do
          end do
        end subroutine z	
        lengthc                 C   s\   ddi ||}t| j| jd | }dddd| }t||tjttt	|dd	 d S )
Nr   r   r   aabcabcdeabcdeabcder   u1dtype)
getgetattrmodulefprefixr   nparraylistmapordselfr   fsuffixfr    r'   V/var/www/html/lang_env/lib/python3.10/site-packages/numpy/f2py/tests/test_character.py
test_inputL   s   &zTestCharacterString.test_inputNc                 C   sR   |}t | j| jd | }ddd| }t|tjttt|dd|	  d S )Nr   r   r   )r	   r
   r   r   )
r   r   r   r   r   r   r    r!   r"   encoder#   r'   r'   r(   test_outputU   s   zTestCharacterString.test_outputc                 C   sn   |}t | j| jd | }tjdddd| dddd| gd	d
}tjdd |D dd
}t||| d S )Nr   r   r   r   r   AABCABCDEABCDEABCDESr   c                 S      g | ]	}d d |D qS )c                 S      g | ]}|qS r'   r'   .0cr'   r'   r(   
<listcomp>h       zCTestCharacterString.test_array_input.<locals>.<listcomp>.<listcomp>r'   r4   sr'   r'   r(   r6   h       z8TestCharacterString.test_array_input.<locals>.<listcomp>r   r   r   r   r   r   r   r$   r   r%   r&   r   expectedr'   r'   r(   test_array_input_   s   z$TestCharacterString.test_array_inputc                 C   sn   |}t | j| jd | }tjdddd| dddd| gd	d
}tjdd |D dd
}t||| d S )Nr   r   r   r   r   r-   r.   r/   r0   r   c                 S   r1   )c                 S   r2   r'   r'   r3   r'   r'   r(   r6   t   r7   zDTestCharacterString.test_array_output.<locals>.<listcomp>.<listcomp>r'   r8   r'   r'   r(   r6   t   r:   z9TestCharacterString.test_array_output.<locals>.<listcomp>r   r;   )r$   r   r%   r&   r=   r   r'   r'   r(   test_array_outputk   s   z%TestCharacterString.test_array_outputc                 C   s   |}t | j| jd | }tjdddd| dddd| gd	d
dd| dddd| ggdd}tjdd |D ddd}t||| d S )Nr   r   r   r   r   r-   r.   r/   r&   ZfghZfghijfghijfghijFZFGHZFGHIJFGHIJFGHIJr0   r   c                 S   r1   )c                 S   r1   )c                 S   r2   r'   r'   r3   r'   r'   r(   r6      r7   zQTestCharacterString.test_2d_array_input.<locals>.<listcomp>.<listcomp>.<listcomp>r'   )r4   itemr'   r'   r(   r6      r:   zFTestCharacterString.test_2d_array_input.<locals>.<listcomp>.<listcomp>r'   )r4   rowr'   r'   r(   r6      r:   z;TestCharacterString.test_2d_array_input.<locals>.<listcomp>r   r   orderr;   r<   r'   r'   r(   test_2d_array_inputw   s   z'TestCharacterString.test_2d_array_input)__name__
__module____qualname__suffixr   Zlength_listcoder   r%   dictr   Zclengthtextwrapdedentjoinpytestmarkparametrizer)   r,   r>   r?   rE   r'   r'   r'   r(   r      s    		((**,669

	

r   c                   @   s  e Zd ZdZdZedg de de de de de d	e d
e de de de de de de de de de de de de de de de de de dZe	j
dddgd d! Zd"d# Ze	j
dg d$d%d& Zd'd( Ze	j
dg d$d)d* Zd+d, Zd-d. Zd/d0 Ze	j
dddgd1d2 Zd3d4 Ze	j
dddgd5d6 Ze	j
dddgd7d8 Zd9d: Ze	j
d;d<d= Zd>d? Zd@S )ATestCharacterr   Ztest_characterr   
       subroutine z_input(c, o)
          character, intent(in) :: c
          integer*1 o
          !f2py intent(out) o
          o = transfer(c, o)
       end subroutine z_input

       subroutine z_output(c, o)
          character :: c
          integer*1, intent(in) :: o
          !f2py intent(out) c
          c = transfer(o, c)
       end subroutine z_output

       subroutine z_input_output(c, o)
          character, intent(in) :: c
          character o
          !f2py intent(out) o
          o = c
       end subroutine z!_input_output

       subroutine z_inout(c, n)
          character :: c, n
          !f2py intent(in) n
          !f2py intent(inout) c
          c = n
       end subroutine z_inout

       function z_return(o) result (c)
          character :: c
          character, intent(in) :: o
          c = transfer(o, c)
       end function z_return

       subroutine z_array_input(c, o)
          character, intent(in) :: c(3)
          integer*1 o(3)
          !f2py intent(out) o
          integer i
          do i=1,3
            o(i) = transfer(c(i), o(i))
          end do
       end subroutine z _array_input

       subroutine a'  _2d_array_input(c, o)
          character, intent(in) :: c(2, 3)
          integer*1 o(2, 3)
          !f2py intent(out) o
          integer i, j
          do i=1,2
            do j=1,3
              o(i, j) = transfer(c(i, j), o(i, j))
            end do
          end do
       end subroutine z#_2d_array_input

       subroutine z_array_output(c, o)
          character :: c(3)
          integer*1, intent(in) :: o(3)
          !f2py intent(out) c
          do i=1,3
            c(i) = transfer(o(i), c(i))
          end do
       end subroutine z!_array_output

       subroutine z_array_inout(c, n)
          character :: c(3), n(3)
          !f2py intent(in) n(3)
          !f2py intent(inout) c(3)
          do i=1,3
            c(i) = n(i)
          end do
       end subroutine z _array_inout

       subroutine a   _2d_array_inout(c, n)
          character :: c(2, 3), n(2, 3)
          !f2py intent(in) n(2, 3)
          !f2py intent(inout) c(2. 3)
          integer i, j
          do i=1,2
            do j=1,3
              c(i, j) = n(i, j)
            end do
          end do
       end subroutine z!_2d_array_inout

       function z_array_return(o) result (c)
          character, dimension(3) :: c
          character, intent(in) :: o(3)
          do i=1,3
            c(i) = o(i)
          end do
       end function z_array_return

       function z_optional(o) result (c)
          character, intent(in) :: o
          !f2py character o = "a"
          character :: c
          c = o
       end function z_optional
    r   r5   S1c                 C   s   t | j| jd }t|tjd|dtd t|tjd|dtd t|tjdg|dtd t|tjd|dtd t|tjdgg|dtd d S )N_inputr   r      ar   )r   r   r   r   r   r   r"   r$   r   r&   r'   r'   r(   r)      s   $zTestCharacter.test_inputc              
   C   s  t | j| jd }t|dtd t|dtd t|dd t|dd t|dd t|dtd t|d	td t|dgtd t|tdtd t|tdgtd td}t||td tdg}t||td z|g  W n ty } zt|	d
s W Y d }~nd }~ww t
|j dz|d W n ty } zt|	ds W Y d }~d S d }~ww t
|j d)NrU   r   rV   r   r           abs   abz got 0-listz! should have failed on empty lista   z got int instancez  should have failed on int value)r   r   r   r   r"   r   r   
IndexErrorstrendswithSystemErrorrF   	TypeErrorr$   r&   r   msgr'   r'   r(   test_input_varia   sB   
zTestCharacter.test_input_varia)r5   rT   U1c                 C   sv   t | j| jd }t|tjg d|dtjtttddd t|tjg d|dtjtttddd d S )N_array_inputr   br5   r   r   i1rV      b   c)	r   r   r   r   r   r   r    r!   r"   rW   r'   r'   r(   r>     s   zTestCharacter.test_array_inputc              
   C   s   t | j| jd }t|g dtjtttddd t|g dtjtttddd z|g d W n t	yR } zt
|dsG W Y d }~d S d }~ww t|j d	)
Nre   rf   r   rh   r   ri   r   rg   r5   d)th dimension must be fixed to 3 but got 4" should have failed on wrong input)r   r   r   r   r   r   r    r!   r"   
ValueErrorr]   r^   r_   rF   )r$   r&   rb   r'   r'   r(   test_array_input_varia(  s(   
z$TestCharacter.test_array_input_variac                 C   sZ   t | j| jd }tjg dg dg|dd}||dkr tjntj}t||| d S )NZ_2d_array_inputrf   rm   er&   r@   rC   rd   )	r   r   r   r   r   viewZuint32Zuint8r   )r$   r   r&   r   r=   r'   r'   r(   rE   9  s   
z!TestCharacter.test_2d_array_inputc                 C   s6   t | j| jd }t|tdd t|dd d S )N_outputrV   r   rY   )r   r   r   r   r"   r$   r&   r'   r'   r(   r,   B  s   zTestCharacter.test_outputc                 C   s<   t | j| jd }t|tttdtjtddd d S )NZ_array_outputr   rT   r   )	r   r   r   r   r    r!   r"   r   r   rv   r'   r'   r(   r?   H  s   zTestCharacter.test_array_outputc                 C   s@   t | j| jd }t|dd t|dd t|dd d S )NZ_input_outputrV   r   r   rY   r   r   r   r   rv   r'   r'   r(   test_input_outputN  s   zTestCharacter.test_input_outputc                 C   s   t | j| jd }tjtd|d}||d t|tjtd|jd ||dd  d t|tjtd|jd tjdg|d}||d t|tjdg|jd d S )	N_inoutr   r   r-   Abc   BZABc)r   r   r   r   r   r    r   r   )r$   r   r&   r   r'   r'   r(   
test_inoutU  s   

zTestCharacter.test_inoutc              
   C   s   t | j| jd }tjddd}||d t|tjd|jd tjdgdd}||d t|tjdg|jd z|dd W n ty[ } zt|	dsP W Y d }~d S d }~ww t
|j d)	Nry   r   ZS3r   r-   rz   z
 got 3-strz  should have failed on str value)r   r   r   r   r   r   r   rp   r]   r^   r_   rF   ra   r'   r'   r(   test_inout_variac  s    

zTestCharacter.test_inout_variac              
   C   s.  t | j| jd }tjg d|dd}tjg d|dd}||| t|| tjg d|d}||dd  | t|tjg d	|d tjg dg|dd}||| t|tjg dg|d tjg d|dd}z||| W n ty } zt|d
s W Y d }~d S d }~ww t	|j
 d)NZ_array_inoutr-   r|   Cr@   rC   rf   rl   r   r{   )r   r-   r|   r   rn   ro   )r   r   r   r   r   r   rp   r]   r^   r_   rF   )r$   r   r&   nr   rb   r'   r'   r(   test_array_inoutu  s2   



zTestCharacter.test_array_inoutc                 C   sb   t | j| jd }tjg dg dg|dd}tjg dg dg|dd}||| t|| d S )NZ_2d_array_inoutr   )DEr@   r@   rC   rf   rr   r;   )r$   r   r&   r   r   r'   r'   r(   test_2d_array_inout  s   


z!TestCharacter.test_2d_array_inoutc                 C   s$   t | j| jd }t|dd d S )NZ_returnr   rV   rw   rv   r'   r'   r(   test_return  s   zTestCharacter.test_returnz*fortran function returning array segfaultsc                 C   s6   t | j| jd }tjtddd}t||| d S )NZ_array_returnr   rT   r   )r   r   r   r   r   r    r   )r$   r&   r   r'   r'   r(   test_array_return  s   zTestCharacter.test_array_returnc                 C   s0   t | j| jd }t| d t|dd d S )NZ	_optionalrV      Brw   rv   r'   r'   r(   test_optional  s   zTestCharacter.test_optionalN)rF   rG   rH   rI   r   rL   rM   rN   rJ   rO   rP   rQ   r)   rc   r>   rq   rE   r,   r?   rx   r}   r~   r   r   r   skipr   r   r'   r'   r'   r(   rR      s    !#+-79@BIKUW]_dg
	#






rR   c                   @   s   e Zd ZdZdZede de de de de de d	e d
e de de de de dZdd Zdd Z	dd Z
dd Zejdddgdd ZdS )TestMiscCharacterr   Ztest_misc_characterrS   a(  _gh18684(x, y, m)
         character(len=5), dimension(m), intent(in) :: x
         character*5, dimension(m), intent(out) :: y
         integer i, m
         !f2py integer, intent(hide), depend(x) :: m = f2py_len(x)
         do i=1,m
           y(i) = x(i)
         end do
       end subroutine z_gh18684

       subroutine z_gh6308(x, i)
         integer i
         !f2py check(i>=0 && i<12) i
         character*5 name, x
         common name(12)
         name(i + 1) = x
       end subroutine z_gh6308

       subroutine z_gh4519(x)
         character(len=*), intent(in) :: x(:)
         !f2py intent(out) x
         integer :: i
         ! Uncomment for debug printing:
         !do i=1, size(x)
         !   print*, "x(",i,")=", x(i)
         !end do
       end subroutine z_gh4519

       pure function aq  _gh3425(x) result (y)
         character(len=*), intent(in) :: x
         character(len=len(x)) :: y
         integer :: i
         do i = 1, len(x)
           j = iachar(x(i:i))
           if (j>=iachar("a") .and. j<=iachar("z") ) then
             y(i:i) = achar(j-32)
           else
             y(i:i) = x(i:i)
           endif
         end do
       end function z_gh3425

       subroutine aO  _character_bc_new(x, y, z)
         character, intent(in) :: x
         character, intent(out) :: y
         !f2py character, depend(x) :: y = x
         !f2py character, dimension((x=='a'?1:2)), depend(x), intent(out) :: z
         character, dimension(*) :: z
         !f2py character, optional, check(x == 'a' || x == 'b') :: x = 'a'
         !f2py callstatement (*f2py_func)(&x, &y, z)
         !f2py callprotoargument character*, character*, character*
         if (y.eq.x) then
           y = x
         else
           y = 'e'
         endif
         z(1) = 'c'
       end subroutine z%_character_bc_new

       subroutine aG  _character_bc_old(x, y, z)
         character, intent(in) :: x
         character, intent(out) :: y
         !f2py character, depend(x) :: y = x[0]
         !f2py character, dimension((*x=='a'?1:2)), depend(x), intent(out) :: z
         character, dimension(*) :: z
         !f2py character, optional, check(*x == 'a' || x[0] == 'b') :: x = 'a'
         !f2py callstatement (*f2py_func)(x, y, z)
         !f2py callprotoargument char*, char*, char*
          if (y.eq.x) then
           y = x
         else
           y = 'e'
         endif
         z(1) = 'c'
       end subroutine z_character_bc_old
    c                 C   s:   t | j| jd }tjddgdd}||}t|| d S )NZ_gh18684abcdeZfghijS5r   r;   )r$   r&   xyr'   r'   r(   test_gh18684  s   zTestMiscCharacter.test_gh18684c                 C   s~   t | j| jd }t| jjjjtd tt| jjjd |dd t| jjjd d |dd t| jjjd d	 d S )
NZ_gh6308r      r   r   s   abcdeZ12345   s   12345)	r   r   r   r   Z_BLNK_namer   r   lenrv   r'   r'   r(   test_gh6308  s   

zTestMiscCharacter.test_gh6308c              
   C   s   t | j| jd }dtdtddfdtdtddftjg ddd	td
tddfg dtd
tddfddgtdtddffD ]\}}||}| D ]\}}tt ||| qTqHd S )NZ_gh4519r   r'   rT   )shaper   textZS4)r	   2r
   r   )   )r	   r   Z34ZS2r   )   )	r   r   r   rK   r   r   r   itemsr   )r$   r&   r   r=   rkvr'   r'   r(   test_gh4519  s    zTestMiscCharacter.test_gh4519c                 C   s@   t | j| jd }t|dd t|dd t|dd d S )NZ_gh3425ZabCs   ABCr   rX   ZabC12ds   ABC12Drw   rv   r'   r'   r(   test_gh3425&  s   zTestMiscCharacter.test_gh3425statenewoldc                    sr   t | j| jd |    \}}t|d tt|d  d\}}t|d tt|d tt fdd d S )NZ_character_bc_rV   r{   rj   r   c                      s    dS )Nrk   r'   r'   r&   r'   r(   <lambda>;  s    z5TestMiscCharacter.test_character_bc.<locals>.<lambda>)r   r   r   r   r   r   	Exception)r$   r   r5   r   r'   r   r(   test_character_bc/  s   


z#TestMiscCharacter.test_character_bcN)rF   rG   rH   rI   r   rL   rM   rJ   r   r   r   r   rO   rP   rQ   r   r'   r'   r'   r(   r     sD    	)+:<K
N	r   c                   @   s.   e Zd ZeddddgZdd Zdd Zd	S )
TestStringScalarArrtestssrcstringzscalar_string.f90c                 C   B   | j jj| j jjfD ]}d}|j|ksJ d}|j|ksJ q
d S )Nr'   z|S8)r   string_testr   Zstring77r   r   r$   outr=   r'   r'   r(   	test_charA     zTestStringScalarArr.test_charc                 C   r   )N)r      z|S12)r   r   ZstrarrZstrarr77r   r   r   r'   r'   r(   test_char_arrI  r   z!TestStringScalarArr.test_char_arrN)rF   rG   rH   r   getpathsourcesr   r   r'   r'   r'   r(   r   >  s    r   c                   @   &   e Zd ZeddddgZdd ZdS )TestStringAssumedLengthr   r   r   z	gh24008.fc                 C   s   | j dd d S )NZjoeZbob)r   Zgreet)r$   r'   r'   r(   test_gh24008T  s   z$TestStringAssumedLength.test_gh24008N)rF   rG   rH   r   r   r   r   r'   r'   r'   r(   r   Q      r   c                   @   r   )TestStringOptionalInOutr   r   r   zgh24662.f90c                 C   sx   | j   tjddd}| j | d|  v sJ tt d}| j | W d    d S 1 s5w   Y  d S )NhiZS32r   zoutput stringZHi)	r   Zstring_inout_optionalr   r   tobytesdecoderO   Zraisesr   )r$   r   Zaar'   r'   r(   test_gh24662Z  s   
"z$TestStringOptionalInOut.test_gh24662N)rF   rG   rH   r   r   r   r   r'   r'   r'   r(   r   W  r   r   c                   @   8   e Zd ZeddddeddddgZdZdd Zd	S )
TestNewCharHandlingr   r   r   zgh25286.pyfgh25286.f90_char_handling_testc                 C      | j d}|dksJ d S NTr   r   Zcharintr$   infor'   r'   r(   test_gh25286m     z TestNewCharHandling.test_gh25286NrF   rG   rH   r   r   r   module_namer   r'   r'   r'   r(   r   d      r   c                   @   r   )
TestBCCharHandlingr   r   r   zgh25286_bc.pyfr   r   c                 C   r   r   r   r   r'   r'   r(   r   z  r   zTestBCCharHandling.test_gh25286Nr   r'   r'   r'   r(   r   q  r   r   )rO   rL   Znumpy.testingr   r   r   numpyr   Znumpy.f2py.testsr   ZF2PyTestr   rR   r   r   r   r   rP   Zslowr   r   r'   r'   r'   r(   <module>   s$    ~  , 