2 ! { dg-options "-fdump-tree-original" }
3 ! Test constructors of derived type with allocatable components (PR 20541).
5 ! Contributed by Erik Edelmann <eedelmann@gcc.gnu.org>
6 ! and Paul Thomas <pault@gcc.gnu.org>
9 Program test_constructor
18 integer(4), allocatable
:: a(:, :)
19 type(thytype
), allocatable
:: q(:)
23 type (thytype
) :: foo
= thytype(reshape ([43, 100, 54, 76], [2,2]))
24 integer :: y(0:1, -1:0) = reshape ([42, 99, 55, 77], [2,2])
25 integer, allocatable
:: yy(:,:)
26 type (thytype
), allocatable
:: bar(:)
29 ! Check that null() works
30 x
= mytype(null(), null())
31 if (allocated(x
%a
) .or
. allocated(x
%q
)) call abort()
33 ! Check that unallocated allocatables work
35 if (allocated(x
%a
) .or
. allocated(x
%q
)) call abort()
37 ! Check that non-allocatables work
38 x
= mytype(y
, [foo
, foo
])
39 if (.not
.allocated(x
%a
) .or
. .not
.allocated(x
%q
)) call abort()
40 if (any(lbound(x
%a
) /= lbound(y
))) call abort()
41 if (any(ubound(x
%a
) /= ubound(y
))) call abort()
42 if (any(x
%a
/= y
)) call abort()
43 if (size(x
%q
) /= 2) call abort()
45 if (any(x
%q(i
)%a
/= foo
%a
)) call abort()
48 ! Check that allocated allocatables work
49 allocate(yy(size(y
,1), size(y
,2)))
54 if (.not
.allocated(x
%a
) .or
. .not
.allocated(x
%q
)) call abort()
55 if (any(x
%a
/= y
)) call abort()
56 if (size(x
%q
) /= 2) call abort()
58 if (any(x
%q(i
)%a
/= foo
%a
)) call abort()
61 ! Functions returning arrays
62 x
= mytype(bluhu(), null())
63 if (.not
.allocated(x
%a
) .or
. allocated(x
%q
)) call abort()
64 if (any(x
%a
/= reshape ([41, 98, 54, 76], [2,2]))) call abort()
66 ! Functions returning allocatable arrays
67 x
= mytype(blaha(), null())
68 if (.not
.allocated(x
%a
) .or
. allocated(x
%q
)) call abort()
69 if (any(x
%a
/= reshape ([40, 97, 53, 75], [2,2]))) call abort()
71 ! Check that passing the constructor to a procedure works
72 call check_mytype (mytype(y
, [foo
, foo
]))
76 subroutine check_mytype(x
)
77 type(mytype
), intent(in
) :: x
80 if (.not
.allocated(x
%a
) .or
. .not
.allocated(x
%q
)) call abort()
81 if (any(lbound(x
%a
) /= lbound(y
))) call abort()
82 if (any(ubound(x
%a
) /= ubound(y
))) call abort()
83 if (any(x
%a
/= y
)) call abort()
84 if (size(x
%q
) /= 2) call abort()
86 if (any(x
%q(i
)%a
/= foo
%a
)) call abort()
89 end subroutine check_mytype
95 bluhu
= reshape ([41, 98, 54, 76], [2,2])
100 integer, allocatable
:: blaha(:,:)
103 blaha
= reshape ([40, 97, 53, 75], [2,2])
106 end program test_constructor
107 ! { dg-final { scan-tree-dump-times "builtin_free" 21 "original" } }
108 ! { dg-final { cleanup-tree-dump "original" } }