2018-10-09 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / testsuite / gfortran.dg / deferred_character_29.f90
blob2d8a4c2d01808005f1b4ab9fa7e61b0fe25d6c45
1 ! { dg-do compile }
3 ! Test the fix for PR83196 comment #4 (there by mistake)
5 ! Contributed by Arjen Markus <arjen.markus895@gmail.com>
6 !____________________________________________________________
7 ! keyindex.f90 --
8 ! Class implementing a straightforward keyword/index list
9 ! The idea is to have a very simple implementation to
10 ! store keywords (strings) and return the position in the
11 ! list or vice versa.
12 !____________________________________________________________
13 module keyindices
14 implicit none
16 private
18 integer, parameter :: default_keylength = 40
20 type keyindex
21 integer :: keylength
22 integer :: lastindex = 0
23 character(len=:), dimension(:), allocatable :: keyword
24 contains
25 procedure :: init => init_keyindex
26 procedure :: get_index => get_index_from_list
27 procedure :: get_key => get_keyword_from_list
28 procedure :: has_key => has_keyword_in_list
29 end type keyindex
31 public :: keyindex
32 contains
34 ! init_keyindex --
35 ! Initialise the object
37 ! Arguments:
38 ! this Keyindex object
39 ! initial_size Initial size of the list (optimisation)
40 ! keylength Maximum length of a keyword (optional)
42 subroutine init_keyindex( this, initial_size, keylength )
43 class(keyindex), intent(inout) :: this
44 integer, intent(in) :: initial_size
45 integer, intent(in), optional :: keylength
47 integer :: keylength_
49 if ( present(keylength) ) then
50 keylength_ = keylength
51 else
52 keylength_ = default_keylength
53 endif
56 ! Allocate the list of keywords
58 if ( allocated(this%keyword) ) then
59 deallocate( this%keyword )
60 endif
63 allocate( character(len=keylength_):: this%keyword(initial_size) )
65 this%lastindex = 0
66 this%keylength = keylength_
67 end subroutine init_keyindex
69 ! get_index_from_list --
70 ! Look up the keyword in the list and return its index
72 ! Arguments:
73 ! this Keyindex object
74 ! keyword Keyword to be looked up
76 ! Returns:
77 ! Index in the list
79 ! Note:
80 ! If the keyword does not yet exist, add it to the list
82 integer function get_index_from_list( this, keyword )
83 class(keyindex), intent(inout) :: this
84 character(len=*), intent(in) :: keyword
86 integer :: i
87 character(len=this%keylength), dimension(:), allocatable :: newlist
89 if ( .not. allocated(this%keyword) ) then
90 call this%init( 50 )
91 endif
93 get_index_from_list = 0
95 do i = 1,this%lastindex
96 if ( this%keyword(i) == keyword ) then
97 get_index_from_list = i
98 exit
99 endif
100 enddo
103 ! Do we need to add it?
105 if ( get_index_from_list == 0 ) then
106 if ( size(this%keyword) <= this%lastindex ) then
108 ! Allocate a larger list
110 allocate( character(len=this%keylength):: newlist(2*size(this%keyword)) )
112 newlist(1:size(this%keyword)) = this%keyword
113 call move_alloc( newlist, this%keyword )
114 endif
116 get_index_from_list = this%lastindex + 1
117 this%lastindex = get_index_from_list
118 this%keyword(get_index_from_list) = keyword
119 endif
120 end function get_index_from_list
122 ! get_keyword_from_list --
123 ! Look up the keyword in the list by the given index
125 ! Arguments:
126 ! this Keyindex object
127 ! idx Index of the keyword
129 ! Returns:
130 ! Keyword as stored in the list
132 ! Note:
133 ! If the index does not exist, an empty string is returned
135 function get_keyword_from_list( this, idx )
136 class(keyindex), intent(inout) :: this
137 integer, intent(in) :: idx
139 character(len=this%keylength) :: get_keyword_from_list
141 get_keyword_from_list = ' '
143 if ( idx >= 1 .and. idx <= this%lastindex ) then
144 get_keyword_from_list = this%keyword(idx)
145 endif
146 end function get_keyword_from_list
148 ! has_keyword_in_list --
149 ! Look up whether the keyword is stored in the list or not
151 ! Arguments:
152 ! this Keyindex object
153 ! keyword Keyword to be looked up
155 ! Returns:
156 ! True if the keyword is in the list or false if not
158 logical function has_keyword_in_list( this, keyword )
159 class(keyindex), intent(inout) :: this
160 character(len=*), intent(in) :: keyword
162 integer :: i
164 has_keyword_in_list = .false.
166 do i = 1,this%lastindex
167 if ( this%keyword(i) == keyword ) then
168 has_keyword_in_list = .true.
169 exit
170 endif
171 enddo
172 end function has_keyword_in_list
174 end module keyindices
176 use keyindices
177 type(keyindex) :: idx
179 call idx%init (3, 8)
181 if (idx%get_index ("one") .ne. 1) stop 1
182 if (idx%get_index ("two") .ne. 2) stop 2
183 if (idx%get_index ("three") .ne. 3) stop 3
185 ! Check that new span is generated as list is extended.
186 if (idx%get_index ("four") .ne. 4) stop 4
187 if (idx%get_index ("five") .ne. 5) stop 5
188 if (idx%get_index ("six") .ne. 6) stop 6
190 ! Search by keyword
191 if (.not.idx%has_key ("four")) stop 7
192 if (idx%has_key ("seven")) stop 8
194 ! Search by index
195 if (idx%get_key (4) .ne. "four") stop 9
196 if (idx%get_key (10) .ne. "") stop 10