o
    g%                     @   s   d dl Z d dlZd dlmZmZ d dlmZ ddlm	Z	 d dl
mZ d dlZG dd de	jZG d	d
 d
ZG dd de	jZG dd de	jZG dd dZG dd de	jZG dd dZdS )    N)assert_array_equalassert_equal)markinnerspaces   )util)crackfortranc                   @   s   e Zd ZdZdd ZdS )TestNoSpacea|  
        subroutine subb(k)
          real(8), intent(inout) :: k(:)
          k=k+1
        endsubroutine

        subroutine subc(w,k)
          real(8), intent(in) :: w(:)
          real(8), intent(out) :: k(size(w))
          k=w+1
        endsubroutine

        function t0(value)
          character value
          character t0
          t0 = value
        endfunction
    c                 C   sx   t jg dt jd}t jg dt jd}| j| t||d  | j||g t||d  | jddks:J d S )N)r         )dtyper         2)nparrayfloat64modulesubbr   subct0)selfkw r   `/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/numpy/f2py/tests/test_crackfortran.pytest_module    s   zTestNoSpace.test_moduleN)__name__
__module____qualname__coder   r   r   r   r   r   
   s    r   c                   @   s   e Zd Zdd Zdd ZdS )TestPublicPrivatec                 C   s   |d }| d}|td W d    n1 sw   Y  tt|g}t|dks1J |d }d|d d d	 v sAJ d
|d d d	 vsMJ d|d d d	 v sYJ d
|d d d	 vseJ d|d d d	 vsqJ d
|d d d	 v s}J d S )Nmod.f90r   a>              module foo
              private
              integer :: a
              public :: setA
              integer :: b
            contains
              subroutine setA(v)
                integer, intent(in) :: v
                a = v
              end subroutine setA
            end module foo
            r   r   privatevarsaattrspecpublicbsetaopenwritetextwrapdedentr   strlenr   tmp_pathf_pathffmodr   r   r   test_defaultPrivate,   s   z%TestPublicPrivate.test_defaultPrivatec                 C   s   |d }| d}|td W d    n1 sw   Y  tt|g}t|dks1J |d }d|d d d	 v sAJ d
|d d d	 vsMJ d|d d d	 vsYJ d
|d d d	 v seJ d S )Nr    r   a+              module foo
              public
              integer, private :: a
              public :: setA
            contains
              subroutine setA(v)
                integer, intent(in) :: v
                a = v
              end subroutine setA
            end module foo
            r   r   r!   r"   r#   r$   r%   r'   r(   r/   r   r   r   test_defaultPublicF   s   z$TestPublicPrivate.test_defaultPublicN)r   r   r   r4   r5   r   r   r   r   r   *   s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )TestExternalaY  
        integer(8) function external_as_statement(fcn)
        implicit none
        external fcn
        integer(8) :: fcn
        external_as_statement = fcn(0)
        end

        integer(8) function external_as_attribute(fcn)
        implicit none
        integer(8), external :: fcn
        external_as_attribute = fcn(0)
        end
    c                 C   $   dd }| j |}|dksJ d S )Nc                 S      | d S N{   r   xr   r   r   incrp      z5TestExternal.test_external_as_statement.<locals>.incrr:   )r   external_as_statementr   r=   rr   r   r   test_external_as_statemento      z'TestExternal.test_external_as_statementc                 C   r7   )Nc                 S   r8   r9   r   r;   r   r   r   r=   v   r>   z5TestExternal.test_external_as_attribute.<locals>.incrr:   )r   external_as_attributer@   r   r   r   test_external_as_attributeu   rC   z'TestExternal.test_external_as_attributeN)r   r   r   r   rB   rE   r   r   r   r   r6   ^   s    r6   c                   @   s"   e Zd ZdZedZdd ZdS )TestCrackFortran.f90a  
      subroutine gh2848( &
        ! first 2 parameters
        par1, par2,&
        ! last 2 parameters
        par3, par4)

        integer, intent(in)  :: par1, par2
        integer, intent(out) :: par3, par4

        par3 = par1
        par4 = par2

      end subroutine gh2848
    c                 C   s   | j dd}|dksJ d S )Nr   r	   )r   r	   )r   gh2848)r   rA   r   r   r   test_gh2848   s   zTestCrackFortran.test_gh2848N)r   r   r   suffixr+   r,   r   rI   r   r   r   r   rF   |   s    
rF   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestMarkinnerspacesc                 C   s$   g d}|D ]	}t t|| qd S )N)za z aza b cz'abcdefghij'r   r   )r   	test_listir   r   r   test_do_not_touch_normal_spaces   s   z3TestMarkinnerspaces.test_do_not_touch_normal_spacesc                 C       t tdd t tdd d S )Nza 'b c' \' \'za 'b@_@c' \' \'za "b c" \" \"za "b@_@c" \" \"rL   r   r   r   r   test_one_relevant_space      z+TestMarkinnerspaces.test_one_relevant_spacec                 C   rP   )Nza 'b c" " d' eza 'b@_@c"@_@"@_@d' eza "b c' ' d" eza "b@_@c'@_@'@_@d" erL   rQ   r   r   r   test_ignore_inner_quotes   s   z,TestMarkinnerspaces.test_ignore_inner_quotesc                 C   rP   )Nza 'b c' 'd e'za 'b@_@c' 'd@_@e'za "b c" "d e"za "b@_@c" "d@_@e"rL   rQ   r   r   r   test_multiple_relevant_spaces   rS   z1TestMarkinnerspaces.test_multiple_relevant_spacesN)r   r   r   rO   rR   rT   rU   r   r   r   r   rK      s
    rK   c                   @   s   e Zd ZdZdZedZg dZdgZ	ee	 Z
dZee
D ]\ZZeejeedev r2edd nd	d
7 Zqejde
dd Zejde
dd ZdS )TestDimSpeca  This test suite tests various expressions that are used as dimension
    specifications.

    There exists two usage cases where analyzing dimensions
    specifications are important.

    In the first case, the size of output arrays must be defined based
    on the inputs to a Fortran function. Because Fortran supports
    arbitrary bases for indexing, for instance, `arr(lower:upper)`,
    f2py has to evaluate an expression `upper - lower + 1` where
    `lower` and `upper` are arbitrary expressions of input parameters.
    The evaluation is performed in C, so f2py has to translate Fortran
    expressions to valid C expressions (an alternative approach is
    that a developer specifies the corresponding C expressions in a
    .pyf file).

    In the second case, when user provides an input array with a given
    size but some hidden parameters used in dimensions specifications
    need to be determined based on the input array size. This is a
    harder problem because f2py has to solve the inverse problem: find
    a parameter `p` such that `upper(p) - lower(p) + 1` equals to the
    size of input array. In the case when this equation cannot be
    solved (e.g. because the input array size is wrong), raise an
    error before calling the Fortran function (that otherwise would
    likely crash Python process when the size of input arrays is
    wrong). f2py currently supports this case only when the equation
    is linear with respect to unknown parameter.

    rG   a  
      function get_arr_size_{count}(a, n) result (length)
        integer, intent(in) :: n
        integer, dimension({dimspec}), intent(out) :: a
        integer length
        length = size(a)
      end function

      subroutine get_inv_arr_size_{count}(a, n)
        integer :: n
        ! the value of n is computed in f2py wrapper
        !f2py intent(out) n
        integer, dimension({dimspec}), intent(in) :: a
        if (a({first}).gt.0) then
          print*, "a=", a
        endif
      end subroutine
    )nz2*nz2:nzn/2z5 - n/2z3*n:20zn*(n+1):n*(n+5)z2*n:3*n*n+2*n :r   1)countdimspecfirstr\   c                 C   sH   | j |}t| jd| }dD ]}||\}}t||ks!J qd S )Nget_arr_size_r   r	   r
         )all_dimspecsindexgetattrr   r.   )r   r\   r[   get_arr_sizerW   szr#   r   r   r   test_array_size   s   zTestDimSpec.test_array_sizec                 C   s   | j |}t| jd| }t| jd| }dD ])}||\}}|| jv r-|||}n||}||\}	}
||	ksCJ ||||	fqd S )Nr^   get_inv_arr_size_r_   )rb   rc   rd   r   nonlinear_dimspecs)r   r\   r[   re   get_inv_arr_sizerW   rf   r#   n1sz1_r   r   r   test_inv_array_size   s   
zTestDimSpec.test_inv_array_sizeN)r   r   r   __doc__rJ   r+   r,   code_templatelinear_dimspecsri   rb   r   	enumerater[   r\   formatsplitpytestmarkparametrizerg   rn   r   r   r   r   rV      s"    


	rV   c                   @   s   e Zd Zdd ZdS )TestModuleDeclarationc                 C   s   |d }| d}|td W d    n1 sw   Y  tt|g}t|dks1J |d d d d d	ks?J d S )
Nr    r   z            module foo
              type bar
                character(len = 4) :: text
              end type bar
              type(bar), parameter :: abar = bar('abar')
            end module foo
            r   r   r"   abar=zbar('abar')r(   r/   r   r   r   test_dependencies  s   	 z'TestModuleDeclaration.test_dependenciesN)r   r   r   r{   r   r   r   r   rx     s    rx   )ru   numpyr   numpy.testingr   r   numpy.f2py.crackfortranr   rX   r   
numpy.f2pyr   r+   F2PyTestr   r   r6   rF   rK   rV   rx   r   r   r   r   <module>   s     4a