1!
2  type :: t
3    real :: r
4    integer :: i
5    character(3) :: chr
6  end type t
7
8  type :: t2
9    real :: r(2, 2)
10    integer :: i
11    character(3) :: chr
12  end type t2
13
14  type :: s
15    type(t), pointer :: t(:)
16  end type s
17
18  integer, parameter :: sh(2) = (/2,2/)
19  real, parameter :: a1(2,2) = reshape ((/1.0,2.0,3.0,4.0/),sh)
20  real, parameter :: a2(2,2) = reshape ((/5.0,6.0,7.0,8.0/),sh)
21
22  type(t), target :: tar1(2) = (/t(1.0, 2, "abc"), t(3.0, 4, "efg")/)
23  character(4), target :: tar2(2) = (/"abcd","efgh"/)
24  type(s), target :: tar3
25  character(2), target :: tar4(2) = (/"ab","cd"/)
26  type(t2), target :: tar5(2) = (/t2(a1, 2, "abc"), t2(a2, 4, "efg")/)
27
28  integer, pointer :: ptr(:)
29  character(2), pointer :: ptr2(:)
30  real, pointer :: ptr3(:)
31
32!_______________component subreference___________
33  ptr => tar1%i
34  ptr = ptr + 1              ! check the scalarizer is OK
35
36  if (any (ptr .ne. (/3, 5/))) STOP 1
37  if (any ((/ptr(1), ptr(2)/) .ne. (/3, 5/))) STOP 2
38  if (any (tar1%i .ne. (/3, 5/))) STOP 3
39
40! Make sure that the other components are not touched.
41  if (any (tar1%r .ne. (/1.0, 3.0/))) STOP 4
42  if (any (tar1%chr .ne. (/"abc", "efg"/))) STOP 5
43
44! Check that the pointer is passed correctly as an actual argument.
45  call foo (ptr)
46  if (any (tar1%i .ne. (/2, 4/))) STOP 6
47
48! And that dummy pointers are OK too.
49  call bar (ptr)
50  if (any (tar1%i .ne. (/101, 103/))) STOP 7
51
52!_______________substring subreference___________
53  ptr2 => tar2(:)(2:3)
54  ptr2 = ptr2(:)(2:2)//"z"   ! again, check the scalarizer
55
56  if (any (ptr2 .ne. (/"cz", "gz"/))) STOP 8
57  if (any ((/ptr2(1), ptr2(2)/) .ne. (/"cz", "gz"/))) STOP 9
58  if (any (tar2 .ne. (/"aczd", "egzh"/))) STOP 10
59
60!_______________substring component subreference___________
61  ptr2 => tar1(:)%chr(1:2)
62  ptr2 = ptr2(:)(2:2)//"q"   ! yet again, check the scalarizer
63  if (any (ptr2 .ne. (/"bq","fq"/))) STOP 11
64  if (any (tar1%chr .ne. (/"bqc","fqg"/))) STOP 12
65
66!_______________trailing array element subreference___________
67  ptr3 => tar5%r(1,2)
68  ptr3 = (/99.0, 999.0/)
69  if (any (tar5(1)%r .ne. reshape ((/1.0,2.0,99.0,4.0/), sh))) STOP 13
70  if (any (tar5(2)%r .ne. reshape ((/5.0,6.0,999.0,8.0/), sh))) STOP 14
71
72!_______________forall assignment___________
73  ptr2 => tar2(:)(1:2)
74  forall (i = 1:2) ptr2(i)(1:1) = "z"
75  if (any (tar2 .ne. (/"zczd", "zgzh"/))) STOP 15
76
77!_______________something more complicated___________
78  tar3%t => tar1
79  ptr3 => tar3%t%r
80  ptr3 = cos (ptr3)
81  if (any (abs(ptr3 - (/cos(1.0_4), cos(3.0_4)/)) >= epsilon(1.0_4))) STOP 16
82
83  ptr2 => tar3%t(:)%chr(2:3)
84  ptr2 = " x"
85  if (any (tar1%chr .ne. (/"b x", "f x"/))) STOP 17
86
87!_______________check non-subref works still___________
88  ptr2 => tar4
89  if (any (ptr2 .ne. (/"ab","cd"/))) STOP 18
90
91contains
92  subroutine foo (arg)
93    integer :: arg(:)
94    arg = arg - 1
95  end subroutine
96  subroutine bar (arg)
97    integer, pointer :: arg(:)
98    arg = arg + 99
99  end subroutine
100end
101