Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
90eb9aa
implemented slice function for stdlib_ascii
aman-godara May 23, 2021
e235bc4
added module dependencies of stdlib_math for function slice in Makefi…
aman-godara May 23, 2021
0742ca0
changed names from start to first and end to last
aman-godara May 24, 2021
1a5f78c
forgot to change the dummy argument start to first
aman-godara May 24, 2021
15827d2
shifted slice from stdlib_ascii to stdlib_strings and modified module…
aman-godara May 24, 2021
c7c1e48
removed include_last functionality
aman-godara May 25, 2021
ac607f1
added tests for slice function (with no include_last functionality)
aman-godara May 25, 2021
9d72c69
made complete use of slice interface: added test cases for character …
aman-godara May 25, 2021
a733bc3
documented function slice, corrected documentation of to_title and to…
aman-godara May 26, 2021
fa88905
improved function slice for invalid cases, added new invalid test cases
aman-godara May 27, 2021
42a905d
improved the implementation of last commit fa88905
aman-godara May 28, 2021
ffcb7e4
removed redundant outer loop, improved documentation of slice function
aman-godara May 29, 2021
4598eec
removed dependency of clip function by stdlib_strings.f90
aman-godara May 29, 2021
24d417f
improved documentation and comments for function slice
aman-godara Jun 7, 2021
323bcd9
Add general tester against intrinsic array slice
awvwgk Jun 10, 2021
a895085
Merge pull request #3 from awvwgk/slice
aman-godara Jun 10, 2021
d60dad3
added -inf and +inf concept to make code more intuitive, added descri…
aman-godara Jun 11, 2021
048b638
added the concept of +inf and -inf in documentation
aman-godara Jun 11, 2021
d38e0f4
added fail messages to unit tests
aman-godara Jun 11, 2021
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add general tester against intrinsic array slice
  • Loading branch information
awvwgk committed Jun 10, 2021
commit 323bcd9efb9dba8c21ff959d400a18483618a644
128 changes: 128 additions & 0 deletions src/tests/string/test_string_functions.f90
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
! SPDX-Identifier: MIT
module test_string_functions
use, intrinsic :: iso_fortran_env, only : error_unit
use stdlib_error, only : check
use stdlib_string_type, only : string_type, assignment(=), operator(==), &
to_lower, to_upper, to_title, to_sentence, reverse
use stdlib_strings, only: slice
use stdlib_optval, only: optval
use stdlib_ascii, only : to_string
implicit none

contains
Expand Down Expand Up @@ -105,6 +108,130 @@ subroutine test_slice_string

end subroutine test_slice_string

subroutine test_slice_gen
character(len=*), parameter :: test = &
& "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
integer :: i, j, k
integer, parameter :: offset = 3

do i = 1 - offset, len(test) + offset
call check_slicer(test, first=i)
end do

do i = 1 - offset, len(test) + offset
call check_slicer(test, last=i)
end do

do i = -len(test) - offset, len(test) + offset
call check_slicer(test, stride=i)
end do

do i = 1 - offset, len(test) + offset
do j = 1 - offset, len(test) + offset
call check_slicer(test, first=i, last=j)
end do
end do

do i = 1 - offset, len(test) + offset
do j = -len(test) - offset, len(test) + offset
call check_slicer(test, first=i, stride=j)
end do
end do

do i = 1 - offset, len(test) + offset
do j = -len(test) - offset, len(test) + offset
call check_slicer(test, last=i, stride=j)
end do
end do

do i = 1 - offset, len(test) + offset
do j = 1 - offset, len(test) + offset
do k = -len(test) - offset, len(test) + offset
call check_slicer(test, first=i, last=j, stride=k)
end do
end do
end do
end subroutine test_slice_gen
Comment on lines +165 to +208

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it even harder to see what the expected behavior is supposed to be. The goal of a good test suite is to serve as an example of how to use the code, and a definition of it's expected behavior. You should go back to the specific tests you had and just give them more meaningful descriptions. The message from a failing test should be a hint as to what aspect of the code is not working correctly. Something as vague as "it failed" doesn't accomplish that. Ideally, I should be able to read the test suite alone and understand the expected behavior.


subroutine check_slicer(string, first, last, stride)
character(len=*), intent(in) :: string
integer, intent(in), optional :: first
integer, intent(in), optional :: last
integer, intent(in), optional :: stride

character(len=:), allocatable :: actual, expected, message
logical :: stat

actual = slice(string, first, last, stride)
expected = reference_slice(string, first, last, stride)

stat = actual == expected

if (.not.stat) then
message = "For input '"//string//"'"//new_line('a')

if (present(first)) then
message = message // "first: "//to_string(first)//new_line('a')
end if
if (present(last)) then
message = message // "last: "//to_string(last)//new_line('a')
end if
if (present(stride)) then
message = message // "stride: "//to_string(stride)//new_line('a')
end if
message = message // "Expected: '"//expected//"' but got '"//actual//"'"
end if
call check(stat, message)

end subroutine check_slicer

pure function reference_slice(string, first, last, stride) result(sliced_string)
character(len=*), intent(in) :: string
integer, intent(in), optional :: first
integer, intent(in), optional :: last
integer, intent(in), optional :: stride
character(len=:), allocatable :: sliced_string
character(len=1), allocatable :: carray(:)

integer :: first_, last_, stride_

stride_ = 1
if (present(stride)) then
stride_ = merge(stride_, stride, stride == 0)
else
if (present(first) .and. present(last)) then
if (last < first) stride_ = -1
end if
end if

if (stride_ < 0) then
last_ = min(max(optval(last, 1), 1), len(string)+1)
first_ = min(max(optval(first, len(string)), 0), len(string))
else
first_ = min(max(optval(first, 1), 1), len(string)+1)
last_ = min(max(optval(last, len(string)), 0), len(string))
end if

carray = string_to_carray(string)
carray = carray(first_:last_:stride_)
sliced_string = carray_to_string(carray)

end function reference_slice

pure function string_to_carray(string) result(carray)
character(len=*), intent(in) :: string
character(len=1) :: carray(len(string))

carray = transfer(string, carray)
end function string_to_carray

pure function carray_to_string(carray) result(string)
character(len=1), intent(in) :: carray(:)
character(len=size(carray)) :: string

string = transfer(carray, string)
end function carray_to_string

end module test_string_functions


Expand All @@ -118,5 +245,6 @@ program tester
call test_to_sentence_string
call test_reverse_string
call test_slice_string
call test_slice_gen

end program tester