Skip to content

Commit d60dad3

Browse files
committed
added -inf and +inf concept to make code more intuitive, added descriptive comments to the test cases
1 parent a895085 commit d60dad3

File tree

2 files changed

+61
-48
lines changed

2 files changed

+61
-48
lines changed

src/stdlib_strings.f90

+4-4
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,15 @@ pure function slice_char(string, first, last, stride) result(sliced_string)
321321
character(len=:), allocatable :: sliced_string
322322
length_string = len(string)
323323

324-
first_index = 1
325-
last_index = length_string
324+
first_index = 0 ! first_index = -infinity
325+
last_index = length_string + 1 ! last_index = +infinity
326326
stride_vector = 1
327327

328328
if (present(stride)) then
329329
if (stride /= 0) then
330330
if (stride < 0) then
331-
first_index = length_string
332-
last_index = 1
331+
first_index = length_string + 1 ! first_index = +infinity
332+
last_index = 0 ! last_index = -infinity
333333
end if
334334
stride_vector = stride
335335
end if

src/tests/string/test_string_functions.f90

+57-44
Original file line numberDiff line numberDiff line change
@@ -58,53 +58,66 @@ end subroutine test_reverse_string
5858

5959
subroutine test_slice_string
6060
type(string_type) :: test_string
61-
character(len=:), allocatable :: test_char
6261
test_string = "abcdefghijklmnopqrstuvwxyz"
63-
test_char = "abcdefghijklmnopqrstuvwxyz"
64-
65-
call check(slice(test_string, 2, 16, 3) == "behkn", &
66-
'function slice failed', warn=.false.)
67-
call check(slice(test_char, first=15, stride=-1) == "onmlkjihgfedcba", &
68-
'function slice failed', warn=.false.)
69-
call check(slice(test_string, last=22, stride=-1) == "zyxwv", &
70-
'function slice failed', warn=.false.)
71-
call check(slice(test_char, 7, 2) == "gfedcb", &
72-
'function slice failed', warn=.false.)
73-
call check(slice(test_string, 7, 2, 1) == "", &
74-
'function slice failed', warn=.false.)
75-
call check(slice(test_char, 2, 6, -1) == "", &
76-
'function slice failed', warn=.false.)
77-
call check(slice(test_string, stride=-1) == "zyxwvutsrqponmlkjihgfedcba", &
78-
'function slice failed', warn=.false.)
79-
call check(slice(test_string, 7, 7, -4) == "g", &
80-
'function slice failed', warn=.false.)
81-
call check(slice(test_char, 7, 7, 3) == "g", &
82-
'function slice failed', warn=.false.)
83-
call check(slice(test_string, 7, 7, 3) == "g", &
84-
'function slice failed', warn=.false.)
85-
call check(slice(test_char, 7, -10) == "gfedcba", &
86-
'function slice failed', warn=.false.)
87-
call check(slice(test_string, 500, 22) == "zyxwv", &
88-
'function slice failed', warn=.false.)
89-
call check(slice(test_char, 50, 27) == "", &
90-
'function slice failed', warn=.false.)
91-
call check(slice(test_string, -20, -200) == "", &
92-
'function slice failed', warn=.false.)
93-
call check(slice(test_char, first=0, stride=-1) == "", &
94-
'function slice failed', warn=.false.)
95-
call check(slice(test_string, last=27, stride=-2) == "", &
96-
'function slice failed', warn=.false.)
97-
call check(slice(test_char, first=27, stride=2) == "", &
98-
'function slice failed', warn=.false.)
99-
call check(slice(test_string, -500, 500) == "abcdefghijklmnopqrstuvwxyz", &
100-
'function slice failed', warn=.false.)
10162

63+
! Only one argument is given
64+
! Valid
65+
call check(slice(test_string, first=10) == "jklmnopqrstuvwxyz") ! last=+inf
66+
call check(slice(test_string, last=10) == "abcdefghij") ! first=-inf
67+
call check(slice(test_string, stride=3) == "adgjmpsvy") ! first=-inf, last=+inf
68+
call check(slice(test_string, stride=-3) == "zwtqnkheb") ! first=+inf, last=-inf
69+
70+
! Invalid
71+
call check(slice(test_string, first=27) == "") ! last=+inf
72+
call check(slice(test_string, first=-10) == "abcdefghijklmnopqrstuvwxyz") ! last=+inf
73+
call check(slice(test_string, last=-2) == "") ! first=-inf
74+
call check(slice(test_string, last=30) == "abcdefghijklmnopqrstuvwxyz") ! first=-inf
75+
call check(slice(test_string, stride=0) == "abcdefghijklmnopqrstuvwxyz") ! stride=1
76+
77+
! Only two arguments are given
78+
! Valid
79+
call check(slice(test_string, first=10, last=20) == "jklmnopqrst")
80+
call check(slice(test_string, first=7, last=2) == "gfedcb") ! stride=-1
81+
call check(slice(test_string, first=10, stride=-2) == "jhfdb") ! last=-inf
82+
call check(slice(test_string, last=21, stride=-2) == "zxv") ! first=+inf
83+
84+
! Atleast one argument is invalid
85+
call check(slice(test_string, first=30, last=-3) == "zyxwvutsrqponmlkjihgfedcba")
86+
call check(slice(test_string, first=1, last=-20) == "a")
87+
call check(slice(test_string, first=7, last=-10) == "gfedcba")
88+
call check(slice(test_string, first=500, last=22) == "zyxwv")
89+
call check(slice(test_string, first=50, last=27) == "")
90+
call check(slice(test_string, first=-20, last=0) == "")
91+
call check(slice(test_string, last=-3, stride=-2) == "zxvtrpnljhfdb") ! first=+inf
92+
call check(slice(test_string, last=10, stride=0) == "abcdefghij") ! stride=1
93+
call check(slice(test_string, first=-2, stride=-2) == "") ! last=-inf
94+
call check(slice(test_string, first=27, stride=2) == "") ! last=+inf
95+
call check(slice(test_string, last=27, stride=-1) == "") ! first=+inf
96+
97+
! All three arguments are given
98+
! Valid
99+
call check(slice(test_string, first=2, last=16, stride=3) == "behkn")
100+
call check(slice(test_string, first=16, last=2, stride=-3) == "pmjgd")
101+
call check(slice(test_string, first=7, last=7, stride=-4) == "g")
102+
call check(slice(test_string, first=7, last=7, stride=3) == "g")
103+
call check(slice(test_string, first=2, last=6, stride=-1) == "")
104+
call check(slice(test_string, first=20, last=10, stride=2) == "")
105+
106+
! Invalid
107+
call check(slice(test_string, first=20, last=30, stride=2) == "tvxz")
108+
call check(slice(test_string, first=-20, last=30, stride=2) == "acegikmoqsuwy")
109+
call check(slice(test_string, first=26, last=30, stride=1) == "z")
110+
call check(slice(test_string, first=1, last=-20, stride=-1) == "a")
111+
call check(slice(test_string, first=26, last=20, stride=1) == "")
112+
call check(slice(test_string, first=1, last=20, stride=-1) == "")
113+
102114
test_string = ""
103-
test_char = ""
104-
call check(slice(test_string, 2, 16, 3) == "", &
105-
'function slice failed', warn=.false.)
106-
call check(slice(test_char, 2, 16, 3) == "", &
107-
'function slice failed', warn=.false.)
115+
! Empty string input
116+
call check(slice(test_string, first=-2, last=6) == "")
117+
call check(slice(test_string, first=6, last=-2) == "")
118+
call check(slice(test_string, first=-10) == "") ! last=+inf
119+
call check(slice(test_string, last=10) == "") ! first=-inf
120+
call check(slice(test_string) == "")
108121

109122
end subroutine test_slice_string
110123

0 commit comments

Comments
 (0)