Debian Codegraph @

gcc/testsuite/gfortran.dg/ptr_func_assign_6.f08

raw
1 ! { dg-do run }
2 !
3 ! Test the fix for PR105054.
4 !
5 ! Contributed by Arjen Markus <[email protected]>
6 !
7 module string_pointers
8 implicit none
9 character(len=20), dimension(10), target :: array_strings
10 character(len=:), dimension(:), target, allocatable :: array_strings2
11
12 contains
13
14 function pointer_to_string( i , flag)
15 integer, intent(in) :: i, flag
16
17 character(len=:), pointer :: pointer_to_string
18
19 if (flag == 1) then
20 pointer_to_string => array_strings(i)
21 return
22 endif
23
24 if (.not.allocated (array_strings2)) allocate (array_strings2(4), &
25 mold = ' ')
26 pointer_to_string => array_strings2(i)
27 end function pointer_to_string
28
29 function pointer_to_string2( i , flag) result (res)
30 integer, intent(in) :: i, flag
31
32 character(len=:), pointer :: res
33
34 if (flag == 1) then
35 res => array_strings(i)
36 return
37 endif
38
39 if (.not.allocated (array_strings2)) allocate (array_strings2(4), &
40 mold = ' ')
41 res => array_strings2(i)
42 end function pointer_to_string2
43
44 end module string_pointers
45
46 program chk_string_pointer
47 use string_pointers
48 implicit none
49 integer :: i
50 character(*), parameter :: chr(4) = ['1234 ','ABCDefgh ', &
51 '12345678 ',' ']
52
53 pointer_to_string(1, 1) = '1234567890'
54 pointer_to_string(2, 1) = '12345678901234567890'
55
56 if (len(pointer_to_string(3, 1)) /= 20) stop 1
57
58 array_strings(1) = array_strings(1)(1:4) // 'ABC'
59 if (pointer_to_string(1, 1) /= '1234ABC') stop 2
60
61 pointer_to_string(1, 2) = '1234'
62 pointer_to_string(2, 2) = 'ABCDefgh'
63 pointer_to_string(3, 2) = '12345678'
64
65 do i = 1, 3
66 if (trim (array_strings2(i)) /= trim(chr(i))) stop 3
67 enddo
68
69 ! Clear the target arrays
70 array_strings = repeat (' ', 20)
71 deallocate (array_strings2)
72
73 ! Repeat with an explicit result.
74 pointer_to_string2(1, 1) = '1234567890'
75 pointer_to_string2(2, 1) = '12345678901234567890'
76
77 if (len(pointer_to_string(3, 1)) /= 20) stop 4
78
79 array_strings(1) = array_strings(1)(1:4) // 'ABC'
80 if (pointer_to_string(1, 1) /= '1234ABC') stop 5
81
82 pointer_to_string2(1, 2) = '1234'
83 pointer_to_string2(2, 2) = 'ABCDefgh'
84 pointer_to_string2(3, 2) = '12345678'
85
86 do i = 1, 3
87 if (trim (array_strings2(i)) /= trim(chr(i))) stop 6
88 enddo
89 end program chk_string_pointer