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