Skip to content

Commit f598268

Browse files
committed
fixed a bug in lowercase
updated tests so they can be run with fpm test
1 parent 9a75198 commit f598268

File tree

4 files changed

+244
-128
lines changed

4 files changed

+244
-128
lines changed

src/csv_utilities.f90

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -211,48 +211,26 @@ pure elemental subroutine swap(i1,i2)
211211
end subroutine swap
212212
!*******************************************************************************
213213

214-
!*****************************************************************************************
215-
!> author: Jacob Williams
216-
!
217-
! Return the lowercase version of the character.
218-
219-
pure elemental function lowercase_character(c) result(c_lower)
220-
221-
implicit none
222-
223-
character(len=1),intent(in) :: c
224-
character(len=1) :: c_lower
225-
226-
integer :: i !! index in uppercase array
227-
228-
i = index(upper,c)
229-
c_lower = merge(lower(i:i),c,i>0)
230-
231-
end function lowercase_character
232-
!*****************************************************************************************
233-
234214
!*******************************************************************************
235215
!>
236216
! Returns lowercase version of the string.
237217

238-
pure elemental function lowercase_string(str) result(s_lower)
218+
pure function lowercase_string(str) result(s_lower)
239219

240220
implicit none
241221

242222
character(len=*),intent(in) :: str !! input string
243223
character(len=(len(str))) :: s_lower !! lowercase version of the string
244224

245225
integer :: i !! counter
246-
integer :: n !! length of input string
226+
integer :: j !! index of uppercase character
247227

248-
s_lower = ''
249-
n = len_trim(str)
228+
s_lower = str
250229

251-
if (n>0) then
252-
do concurrent (i=1:n)
253-
s_lower(i:i) = lowercase_character(str(i:i))
254-
end do
255-
end if
230+
do i = 1, len_trim(str)
231+
j = index(upper,s_lower(i:i))
232+
if (j>0) s_lower(i:i) = lower(j:j)
233+
end do
256234

257235
end function lowercase_string
258236
!*******************************************************************************

src/tests/csv_read_test.f90

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,28 @@ program csv_read_test
1515
! read the file
1616
call f%read('test.csv',header_row=1,status_ok=status_ok)
1717

18-
! get the header and type info
19-
call f%get_header(header,status_ok)
20-
call f%variable_types(itypes,status_ok)
21-
22-
! get some data
23-
call f%get(1,x,status_ok)
24-
call f%get(2,y,status_ok)
25-
call f%get(3,z,status_ok)
26-
call f%get(4,t,status_ok)
27-
28-
write(*,*) 'x=',x
29-
write(*,*) 'y=',y
30-
write(*,*) 'z=',z
31-
write(*,*) 't=',t
32-
33-
! destroy the file
34-
call f%destroy()
18+
if (status_ok) then
19+
20+
! get the header and type info
21+
call f%get_header(header,status_ok)
22+
call f%variable_types(itypes,status_ok)
23+
24+
! get some data
25+
call f%get(1,x,status_ok)
26+
call f%get(2,y,status_ok)
27+
call f%get(3,z,status_ok)
28+
call f%get(4,t,status_ok)
29+
30+
write(*,*) 'x=',x
31+
write(*,*) 'y=',y
32+
write(*,*) 'z=',z
33+
write(*,*) 't=',t
34+
35+
! destroy the file
36+
call f%destroy()
37+
38+
else
39+
error stop 'could not open file'
40+
end if
3541

3642
end program csv_read_test

src/tests/csv_test.f90

Lines changed: 194 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,85 +11,212 @@ program csv_test
1111

1212
implicit none
1313

14-
type(csv_file) :: f
15-
type(csv_file) :: f2
16-
integer :: i !! counter
17-
character(len=30),dimension(:),allocatable :: header !! the header
18-
character(len=30),dimension(:,:),allocatable :: csv_data !! the data from the file as strings
19-
real(wp),dimension(:),allocatable :: x !! for getting a real vector from a csv file
20-
logical :: status_ok !! error flag
21-
integer,dimension(:),allocatable :: itypes !! array of variable types in the file
22-
integer :: ifile !! file counter
23-
character(len=30),dimension(:),allocatable :: names
24-
25-
character(len=*),dimension(2),parameter :: files_to_test = ['../files/test.csv ',&
26-
'../files/test_2_columns.csv']
27-
28-
do ifile = 1, size(files_to_test)
29-
30-
! read the file:
31-
if (ifile==1) then
32-
call f%read(trim(files_to_test(ifile)),&
33-
header_row=1,status_ok=status_ok)
34-
else
35-
! also skip a row
36-
call f%read(trim(files_to_test(ifile)),&
37-
header_row=1,skip_rows=[2],status_ok=status_ok)
38-
end if
14+
call csv_test_1()
15+
call csv_write_test()
16+
call csv_read_test()
17+
18+
contains
19+
20+
subroutine csv_test_1()
21+
22+
implicit none
23+
24+
type(csv_file) :: f
25+
type(csv_file) :: f2
26+
integer :: i !! counter
27+
character(len=30),dimension(:),allocatable :: header !! the header
28+
character(len=30),dimension(:,:),allocatable :: csv_data !! the data from the file as strings
29+
real(wp),dimension(:),allocatable :: x !! for getting a real vector from a csv file
30+
logical :: status_ok !! error flag
31+
integer,dimension(:),allocatable :: itypes !! array of variable types in the file
32+
integer :: ifile !! file counter
33+
character(len=30),dimension(:),allocatable :: names
34+
character(len=:),allocatable :: file
35+
36+
character(len=*),dimension(2),parameter :: files_to_test = ['../files/test.csv ',&
37+
'../files/test_2_columns.csv']
3938

4039
write(*,*) ''
41-
write(*,*) 'File: '//trim(files_to_test(ifile))
42-
! print the header and type info:
43-
call f%get_header(header,status_ok)
44-
call f%variable_types(itypes,status_ok)
40+
write(*,*) '============================'
41+
write(*,*) ' csv_test_1 '
42+
write(*,*) '============================'
4543
write(*,*) ''
46-
write(*,'(*(A30,1X,A4))') 'Header', 'Type'
47-
do i=1,size(header)
48-
write(*,'(*(A30,1X,I4))') header(i), itypes(i)
49-
end do
5044

51-
write(*,*) ''
52-
write(*,*) 'print all the rows:'
45+
do ifile = 1, size(files_to_test)
5346

54-
call f%get(csv_data,status_ok)
55-
do i=1,size(csv_data,1)
56-
write(*,'(*(A30,1X))') csv_data(i,:)
57-
end do
47+
file = trim(files_to_test(ifile))
48+
if (.not. file_exists(file)) then
49+
file(1:1) = ' ' ! try from current working directory
50+
file = trim(adjustl(file))
51+
end if
52+
53+
! read the file:
54+
if (ifile==1) then
55+
call f%read(file,header_row=1,status_ok=status_ok)
56+
else
57+
! also skip a row
58+
call f%read(file,header_row=1,skip_rows=[2],status_ok=status_ok)
59+
end if
60+
61+
if (.not. status_ok) then
62+
error stop 'could not open file'
63+
end if
5864

59-
write(*,*) ''
60-
write(*,*) 'get some vectors:'
61-
if (ifile==1) then
6265
write(*,*) ''
63-
write(*,*) 'age:'
64-
call f%get(3,x,status_ok)
65-
write(*,'(F6.3,1x)',advance='NO') x
66+
write(*,*) 'File: '//trim(files_to_test(ifile))
67+
! print the header and type info:
68+
call f%get_header(header,status_ok)
69+
call f%variable_types(itypes,status_ok)
6670
write(*,*) ''
67-
else
71+
write(*,'(*(A30,1X,A4))') 'Header', 'Type'
72+
do i=1,size(header)
73+
write(*,'(*(A30,1X,I4))') header(i), itypes(i)
74+
end do
75+
6876
write(*,*) ''
69-
write(*,*) 'name:'
70-
call f%get(2,names,status_ok)
71-
write(*,'(A10,1x)',advance='NO') names
77+
write(*,*) 'print all the rows:'
78+
79+
call f%get(csv_data,status_ok)
80+
do i=1,size(csv_data,1)
81+
write(*,'(*(A30,1X))') csv_data(i,:)
82+
end do
83+
7284
write(*,*) ''
85+
write(*,*) 'get some vectors:'
86+
if (ifile==1) then
87+
write(*,*) ''
88+
write(*,*) 'age:'
89+
call f%get(3,x,status_ok)
90+
write(*,'(F6.3,1x)',advance='NO') x
91+
write(*,*) ''
92+
else
93+
write(*,*) ''
94+
write(*,*) 'name:'
95+
call f%get(2,names,status_ok)
96+
write(*,'(A10,1x)',advance='NO') names
97+
write(*,*) ''
98+
end if
99+
100+
end do
101+
102+
! now test creating a CSV:
103+
call f2%initialize(enclose_strings_in_quotes=.false.,verbose=.true.)
104+
call f2%open('test2.csv',n_cols=4,status_ok=status_ok)
105+
if (status_ok) then
106+
call f2%add(['x','y','z','t']) ! add header as vector
107+
call f2%next_row()
108+
call f2%add(1.0_wp) ! add as scalars
109+
call f2%add(2.0_wp)
110+
call f2%add(3.0_wp)
111+
call f2%add(.true.)
112+
call f2%next_row()
113+
call f2%add([4.0_wp,5.0_wp,6.0_wp],real_fmt='(F5.3)') ! add as vectors
114+
call f2%add(.false.)
115+
call f2%next_row()
73116
end if
117+
call f2%close(status_ok)
118+
119+
end subroutine csv_test_1
120+
121+
subroutine csv_write_test()
122+
123+
implicit none
124+
125+
type(csv_file) :: f
126+
logical :: status_ok
127+
128+
write(*,*) ''
129+
write(*,*) '============================'
130+
write(*,*) ' csv_write_test '
131+
write(*,*) '============================'
132+
write(*,*) ''
133+
134+
! open the file
135+
call f%open('test.csv',n_cols=4,status_ok=status_ok)
136+
if (status_ok) then
137+
138+
! add header
139+
call f%add(['x','y','z','t'])
140+
call f%next_row()
141+
142+
! add some data:
143+
call f%add([1.0_wp,2.0_wp,3.0_wp],real_fmt='(F5.3)')
144+
call f%add(.true.)
145+
call f%next_row()
146+
call f%add([4.0_wp,5.0_wp,6.0_wp],real_fmt='(F5.3)')
147+
call f%add(.false.)
148+
call f%next_row()
149+
150+
! finished
151+
call f%close(status_ok)
152+
153+
else
154+
error stop 'could not open file'
155+
end if
156+
157+
end subroutine csv_write_test
158+
159+
subroutine csv_read_test()
160+
161+
implicit none
162+
163+
type(csv_file) :: f
164+
character(len=30),dimension(:),allocatable :: header
165+
real(wp),dimension(:),allocatable :: x,y,z
166+
logical,dimension(:),allocatable :: t
167+
logical :: status_ok
168+
integer,dimension(:),allocatable :: itypes
169+
170+
write(*,*) ''
171+
write(*,*) '============================'
172+
write(*,*) ' csv_read_test '
173+
write(*,*) '============================'
174+
write(*,*) ''
175+
176+
! read the file
177+
call f%read('test.csv',header_row=1,status_ok=status_ok)
178+
179+
if (status_ok) then
180+
181+
! get the header and type info
182+
call f%get_header(header,status_ok)
183+
call f%variable_types(itypes,status_ok)
184+
185+
! get some data
186+
call f%get(1,x,status_ok)
187+
call f%get(2,y,status_ok)
188+
call f%get(3,z,status_ok)
189+
call f%get(4,t,status_ok)
190+
191+
write(*,*) 'x=',x
192+
write(*,*) 'y=',y
193+
write(*,*) 'z=',z
194+
write(*,*) 't=',t
195+
196+
! destroy the file
197+
call f%destroy()
198+
199+
else
200+
error stop 'could not open file'
201+
end if
202+
203+
end subroutine csv_read_test
204+
205+
function file_exists(file) result(exists)
206+
207+
!! returns True if the file exists
208+
209+
implicit none
210+
character(len=*),intent(in) :: file
211+
logical :: exists
212+
213+
integer :: istat
214+
215+
inquire(file=file,exist=exists,iostat=istat)
216+
217+
exists = exists .and. istat==0 ! just in case?
74218

75-
end do
76-
77-
! now test creating a CSV:
78-
call f2%initialize(enclose_strings_in_quotes=.false.,verbose=.true.)
79-
call f2%open('test2.csv',n_cols=4,status_ok=status_ok)
80-
if (status_ok) then
81-
call f2%add(['x','y','z','t']) ! add header as vector
82-
call f2%next_row()
83-
call f2%add(1.0_wp) ! add as scalars
84-
call f2%add(2.0_wp)
85-
call f2%add(3.0_wp)
86-
call f2%add(.true.)
87-
call f2%next_row()
88-
call f2%add([4.0_wp,5.0_wp,6.0_wp],real_fmt='(F5.3)') ! add as vectors
89-
call f2%add(.false.)
90-
call f2%next_row()
91-
end if
92-
call f2%close(status_ok)
219+
end function file_exists
93220

94221
end program csv_test
95222
!*****************************************************************************************

0 commit comments

Comments
 (0)