aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gfortran.dg/alloc_comp_class_5.f03
blob: 913ff0c3d99bdc08e7bddc30c47623f7735927f9 (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
! { dg-do run }
!
! Contributed by Vladimir Fuka
! Check that pr61337 and pr78053, which was caused by this testcase, is fixed.

module array_list

  type container
    class(*), allocatable :: items(:)
  end type

contains

  subroutine add_item(a, e)
    type(container),allocatable,intent(inout) :: a(:)
    class(*),intent(in) :: e(:)
    type(container),allocatable :: tmp(:)

      if (.not.allocated(a)) then
        allocate(a(1))
        allocate(a(1)%items(size(e)), source = e)
      else
        call move_alloc(a,tmp)
        allocate(a(size(tmp)+1))
        a(1:size(tmp)) = tmp
        allocate(a(size(tmp)+1)%items(size(e)), source=e)
      end if
   end subroutine

end module

program test_pr61337

  use array_list

  type(container), allocatable :: a_list(:)
  integer(kind = 8) :: i

  call add_item(a_list, [1, 2])
  call add_item(a_list, [3.0_8, 4.0_8])
  call add_item(a_list, [.true., .false.])
  call add_item(a_list, ["foo", "bar", "baz"])

  if (size(a_list) /= 4) STOP 1
  do i = 1, size(a_list)
          call checkarr(a_list(i))
  end do

  deallocate(a_list)

contains

  subroutine checkarr(c)
    type(container) :: c

    if (allocated(c%items)) then
      select type (x=>c%items)
        type is (integer)
          if (any(x /= [1, 2])) STOP 2
        type is (real(kind=8))
          if (any(x /= [3.0_8, 4.0_8])) STOP 3
        type is (logical)
          if (any(x .neqv. [.true., .false.])) STOP 4
        type is (character(len=*))
          if (len(x) /= 3) STOP 5
          if (any(x /= ["foo", "bar", "baz"])) STOP 6
        class default
          STOP 7
      end select
    else
        STOP 8
    end if
  end subroutine
end