Debian Codegraph @

gcc/testsuite/gfortran.dg/transfer_class_4.f90

raw
1 ! { dg-do run }
2 !
3 ! Fix TRANSFER intrinsic for unlimited polymorphic SOURCEs - PR98534
4 ! Note that unlimited polymorphic MOLD is a TODO.
5 !
6 ! Contributed by Paul Thomas <[email protected]>
7 !
8 use, intrinsic :: ISO_FORTRAN_ENV, only: real32
9 implicit none
10 character(*), parameter :: string = "abcdefgh"
11 character(len=:), allocatable :: string_a(:)
12 class(*), allocatable :: star
13 class(*), allocatable :: star_a(:)
14 character(len=:), allocatable :: chr
15 character(len=:), allocatable :: chr_a(:)
16 integer :: sz, sum1, sum2, i
17 real(real32) :: r = 1.0
18
19 ! Part 1: worked correctly
20 star = r
21 sz = storage_size (star)/8
22 allocate (character(len=sz) :: chr)
23 chr = transfer (star, chr)
24 sum1 = sum ([(ichar(chr(i:i)), i = 1, sz)])
25 chr = transfer(1.0, chr)
26 sum2 = sum ([(ichar(chr(i:i)), i = 1, sz)])
27
28 if (sz /= storage_size (r)/8) stop 1
29 if (sum1 /= sum2) stop 2
30
31 deallocate (star) ! The automatic reallocation causes invalid writes
32 ! and memory leaks. Even with this deallocation
33 ! The invalid writes still occur.
34 deallocate (chr)
35
36 ! Part 2: Got everything wrong because '_len' field of unlimited polymorphic
37 ! expressions was not used.
38 star = string
39 sz = storage_size (star)/8
40 if (sz /= len (string)) stop 3 ! storage_size failed
41
42 sz = len (string) ! Ignore previous error in storage_size
43 allocate (character(len=sz) :: chr)
44 chr = transfer (star, chr)
45 sum1 = sum ([(ichar(chr(i:i)), i = 1, sz)])
46 chr = transfer(string, chr)
47 sum2 = sum ([(ichar(chr(i:i)), i = 1, sz)])
48 if (sum1 /= sum2) stop 4 ! transfer failed
49
50 ! Check that arrays are OK for transfer
51 star_a = ['abcde','fghij']
52 allocate (character (len = 5) :: chr_a(2))
53 chr_a = transfer (star_a, chr_a)
54 if (any (chr_a .ne. ['abcde','fghij'])) stop 5
55
56 ! Check that string length and size are correctly handled
57 string_a = ["abcdefgh", "ijklmnop"]
58 star_a = string_a;
59 chr_a = transfer (star_a, chr_a) ! Old string length used for size
60 if (size(chr_a) .ne. 4) stop 6
61 if (len(chr_a) .ne. 5) stop 7
62 if (trim (chr_a(3)) .ne. "klmno") stop 8
63 if (chr_a(4)(1:1) .ne. "p") stop 9
64
65 chr_a = transfer (star_a, string_a) ! Use correct string_length for payload
66 if (size(chr_a) .ne. 2) stop 10
67 if (len(chr_a) .ne. 8) stop 11
68 if (any (chr_a .ne. string_a)) stop 12
69
70 ! Check that an unlimited polymorphic function result is transferred OK
71 deallocate (chr_a)
72 string_a = ['abc', 'def', 'hij']
73 chr_a = transfer (foo (string_a), string_a)
74 if (any (chr_a .ne. string_a)) stop 13
75
76 ! Finally, check that the SIZE gives correct results with unlimited sources.
77 chr_a = transfer (star_a, chr_a, 4)
78 if (chr_a (4) .ne. 'jkl') stop 14
79
80 deallocate (star, chr, star_a, chr_a, string_a)
81 contains
82 function foo (arg) result(res)
83 character(*), intent(in) :: arg(:)
84 class(*), allocatable :: res(:)
85 res = arg
86 end
87 end