summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gfortran.dg/alloc_comp_transformational_1.f90
blob: 13ee8a88bde0b99b9bee3d977caa095ddae2a511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
! { dg-do run }
! Tests the fix for PR41478, in which double frees would occur because
! transformational intrinsics did not copy the allocatable components
! so that they were (sometimes) freed twice on exit.  In addition,
! The original allocatable components of a1 were not freed, so that
! memory leakage occurred.
!
! Contributed by Juergen Reuter <reuter@physik.uni-freiburg.de>
!
  type :: container_t
    integer, dimension(:), allocatable :: entry
    integer index
  end type container_t
  call foo
  call bar
contains
!
! This is the reported problem.
!
  subroutine foo
    type(container_t), dimension(4) :: a1, a2, a3
    integer :: i
    do i = 1, 4
      allocate (a1(i)%entry (2), a2(i)%entry (2), a3(i)%entry (2))
      a1(i)%entry = [1,2]
      a2(i)%entry = [3,4]
      a3(i)%entry = [4,5]
      a1(i)%index = i
      a2(i)%index = i
      a3(i)%index = i
    end do
    a1(1:2) = pack (a2, [.true., .false., .true., .false.])
    do i = 1, 4
      if (.not.allocated (a1(i)%entry)) call abort
      if (i .gt. 2) then
        if (any (a1(i)%entry .ne. [1,2])) call abort
      else
        if (any (a1(i)%entry .ne. [3,4])) call abort
      end if
    end do
!
! Now check unpack
!
    a1 = unpack (a1, [.true., .true., .false., .false.], a3)
    if (any (a1%index .ne. [1,3,3,4])) call abort
    do i = 1, 4
      if (.not.allocated (a1(i)%entry)) call abort
      if (i .gt. 2) then
        if (any (a1(i)%entry .ne. [4,5])) call abort
      else
        if (any (a1(i)%entry .ne. [3,4])) call abort
      end if
    end do
  end subroutine
!
! Other all transformational intrinsics display it. Having done
! PACK and UNPACK, just use TRANSPOSE as a demonstrator.
!
  subroutine bar
    type(container_t), dimension(2,2) :: a1, a2
    integer :: i, j
    do i = 1, 2
      do j = 1, 2
        allocate (a1(i, j)%entry (2), a2(i, j)%entry (2))
        a1(i, j)%entry = [i,j]
        a2(i, j)%entry = [i,j]
        a1(i,j)%index = j + (i - 1)*2
        a2(i,j)%index = j + (i - 1)*2
      end do
    end do
    a1 = transpose (a2)
    do i = 1, 2
      do j = 1, 2
        if (a1(i,j)%index .ne. i + (j - 1)*2) call abort
        if (any (a1(i,j)%entry .ne. [j,i])) call abort
      end do
    end do
  end subroutine
end