| 1 |
! PR 101310 |
| 2 |
! { dg-do run } |
| 3 |
! { dg-additional-sources "section-3-c.c dump-descriptors.c" } |
| 4 |
! { dg-additional-options "-g" } |
| 5 |
! |
| 6 |
! This program tests basic use of the CFI_section C library function to |
| 7 |
! take a slice of a 2-dimensional non-pointer array. |
| 8 |
|
| 9 |
module mm |
| 10 |
use ISO_C_BINDING |
| 11 |
type, bind (c) :: m |
| 12 |
integer(C_INT) :: x, y |
| 13 |
end type |
| 14 |
end module |
| 15 |
|
| 16 |
program testit |
| 17 |
use iso_c_binding |
| 18 |
use mm |
| 19 |
implicit none |
| 20 |
|
| 21 |
interface |
| 22 |
subroutine ctest (a, lb0, lb1, ub0, ub1, s0, s1, r) bind (c) |
| 23 |
use iso_c_binding |
| 24 |
use mm |
| 25 |
type(m), target :: a(:,:) |
| 26 |
integer(C_INT), value :: lb0, lb1, ub0, ub1, s0, s1 |
| 27 |
type(m), pointer, intent(out) :: r(:) |
| 28 |
end subroutine |
| 29 |
|
| 30 |
end interface |
| 31 |
|
| 32 |
type(m), target :: aa(10, 20) |
| 33 |
integer :: i0, i1 |
| 34 |
|
| 35 |
! Initialize the test array by numbering its elements. |
| 36 |
do i1 = 1, 20 |
| 37 |
do i0 = 1, 10 |
| 38 |
aa(i0, i1)%x = i0 |
| 39 |
aa(i0, i1)%y = i1 |
| 40 |
end do |
| 41 |
end do |
| 42 |
|
| 43 |
call test (aa, 3, 1, 3, 20, 0, 1) ! full slice 0 |
| 44 |
call test (aa, 1, 8, 10, 8, 1, 0) ! full slice 1 |
| 45 |
call test (aa, 3, 5, 3, 14, 0, 3) ! partial slice 0 |
| 46 |
call test (aa, 2, 8, 10, 8, 2, 0) ! partial slice 1 |
| 47 |
call test (aa, 3, 14, 3, 5, 0, -3) ! backwards slice 0 |
| 48 |
call test (aa, 10, 8, 2, 8, -2, 0) ! backwards slice 1 |
| 49 |
|
| 50 |
contains |
| 51 |
|
| 52 |
! Test function for non-pointer array AA. |
| 53 |
! LB, UB, and S describe the section to take. |
| 54 |
subroutine test (aa, lb0, lb1, ub0, ub1, s0, s1) |
| 55 |
use mm |
| 56 |
type(m) :: aa(10,20) |
| 57 |
integer :: lb0, lb1, ub0, ub1, s0, s1 |
| 58 |
|
| 59 |
type(m), pointer :: rr(:) |
| 60 |
integer :: i0, i1, o0, o1 |
| 61 |
integer, parameter :: hi0 = 10 |
| 62 |
integer, parameter :: hi1 = 20 |
| 63 |
|
| 64 |
! Check the bounds actually specify a "slice" rather than a subarray. |
| 65 |
if (lb0 .ne. ub0 .and. lb1 .ne. ub1) stop 100 |
| 66 |
|
| 67 |
! Call the C function to put a section in rr. |
| 68 |
! The C function expects the section bounds to be 1-based. |
| 69 |
nullify (rr) |
| 70 |
call ctest (aa, lb0, lb1, ub0, ub1, s0, s1, rr) |
| 71 |
|
| 72 |
! Make sure the original array has not been modified. |
| 73 |
do i1 = 1, hi1 |
| 74 |
do i0 = 1, hi0 |
| 75 |
if (aa(i0,i1)%x .ne. i0) stop 103 |
| 76 |
if (aa(i0,i1)%y .ne. i1) stop 103 |
| 77 |
end do |
| 78 |
end do |
| 79 |
|
| 80 |
! Make sure the output array has the expected bounds and elements. |
| 81 |
if (.not. associated (rr)) stop 111 |
| 82 |
if (lbound (rr, 1) .ne. 1) stop 112 |
| 83 |
if (ub0 .eq. lb0) then |
| 84 |
if (ubound (rr, 1) .ne. (ub1 - lb1)/s1 + 1) stop 113 |
| 85 |
o1 = 1 |
| 86 |
do i1 = lb1, ub1, s1 |
| 87 |
if (rr(o1)%x .ne. lb0) stop 114 |
| 88 |
if (rr(o1)%y .ne. i1) stop 114 |
| 89 |
o1 = o1 + 1 |
| 90 |
end do |
| 91 |
else |
| 92 |
if (ubound (rr, 1) .ne. (ub0 - lb0)/s0 + 1) stop 113 |
| 93 |
o0 = 1 |
| 94 |
do i0 = lb0, ub0, s0 |
| 95 |
if (rr(o0)%x .ne. i0) stop 114 |
| 96 |
if (rr(o0)%y .ne. lb1) stop 114 |
| 97 |
o0 = o0 + 1 |
| 98 |
end do |
| 99 |
end if |
| 100 |
end subroutine |
| 101 |
|
| 102 |
end program |
| 103 |
|