summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gfortran.dg/alloc_comp_constructor_1.f90
blob: 969e703094c63681f8f1dca8e0840fff08c4aba3 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
! { dg-do run }
! { dg-options "-fdump-tree-original" }
! Test constructors of derived type with allocatable components (PR 20541).
!
! Contributed by Erik Edelmann  <eedelmann@gcc.gnu.org>
!            and Paul Thomas  <pault@gcc.gnu.org>
!

Program test_constructor

    implicit none

    type :: thytype
        integer(4) :: a(2,2)
    end type thytype

    type :: mytype
        integer(4), allocatable :: a(:, :)
        type(thytype), allocatable :: q(:)
    end type mytype

    type (mytype) :: x
    type (thytype) :: foo = thytype(reshape ([43, 100, 54, 76], [2,2]))
    integer :: y(0:1, -1:0) = reshape ([42, 99, 55, 77], [2,2])
    integer, allocatable :: yy(:,:)
    type (thytype), allocatable :: bar(:)
    integer :: i

    ! Check that null() works
    x = mytype(null(), null())
    if (allocated(x%a) .or. allocated(x%q)) call abort()

    ! Check that unallocated allocatables work
    x = mytype(yy, bar)
    if (allocated(x%a) .or. allocated(x%q)) call abort()

    ! Check that non-allocatables work
    x = mytype(y, [foo, foo])
    if (.not.allocated(x%a) .or. .not.allocated(x%q)) call abort()
    if (any(lbound(x%a) /= lbound(y))) call abort()
    if (any(ubound(x%a) /= ubound(y))) call abort()
    if (any(x%a /= y)) call abort()
    if (size(x%q) /= 2) call abort()
    do i = 1, 2
        if (any(x%q(i)%a /= foo%a)) call abort()
    end do

    ! Check that allocated allocatables work
    allocate(yy(size(y,1), size(y,2)))
    yy = y
    allocate(bar(2))
    bar = [foo, foo]
    x = mytype(yy, bar)
    if (.not.allocated(x%a) .or. .not.allocated(x%q)) call abort()
    if (any(x%a /= y)) call abort()
    if (size(x%q) /= 2) call abort()
    do i = 1, 2
        if (any(x%q(i)%a /= foo%a)) call abort()
    end do

    ! Functions returning arrays
    x = mytype(bluhu(), null())
    if (.not.allocated(x%a) .or. allocated(x%q)) call abort()
    if (any(x%a /= reshape ([41, 98, 54, 76], [2,2]))) call abort()

    ! Functions returning allocatable arrays
    x = mytype(blaha(), null())
    if (.not.allocated(x%a) .or. allocated(x%q)) call abort()
    if (any(x%a /= reshape ([40, 97, 53, 75], [2,2]))) call abort()

    ! Check that passing the constructor to a procedure works
    call check_mytype (mytype(y, [foo, foo]))

contains

    subroutine check_mytype(x)
        type(mytype), intent(in) :: x
        integer :: i

        if (.not.allocated(x%a) .or. .not.allocated(x%q)) call abort()
        if (any(lbound(x%a) /= lbound(y))) call abort()
        if (any(ubound(x%a) /= ubound(y))) call abort()
        if (any(x%a /= y)) call abort()
        if (size(x%q) /= 2) call abort()
        do i = 1, 2
            if (any(x%q(i)%a /= foo%a)) call abort()
        end do

    end subroutine check_mytype


    function bluhu()
        integer :: bluhu(2,2)

        bluhu = reshape ([41, 98, 54, 76], [2,2])
    end function bluhu


    function blaha()
        integer, allocatable :: blaha(:,:)

        allocate(blaha(2,2))
        blaha = reshape ([40, 97, 53, 75], [2,2])
    end function blaha

end program test_constructor
! { dg-final { scan-tree-dump-times "builtin_free" 19 "original" } }
! { dg-final { cleanup-tree-dump "original" } }