Skip to content

Commit 579a95a

Browse files
committed
added test cases for count function
1 parent 98f5214 commit 579a95a

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/stdlib_strings.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ module stdlib_strings
9595

9696
!> Returns the number of times substring 'pattern' has appeared in the
9797
!> input string 'string'
98-
!> [Specifications](link to the specs - to be completed)
98+
!> [Specifications](../page/specs/stdlib_strings.html#count)
9999
!> Version: experimental
100100
interface count
101101
module procedure :: count_string_string

src/tests/string/test_string_functions.f90

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module test_string_functions
44
use stdlib_error, only : check
55
use stdlib_string_type, only : string_type, assignment(=), operator(==), &
66
to_lower, to_upper, to_title, to_sentence, reverse
7-
use stdlib_strings, only: slice, find, replace_all
7+
use stdlib_strings, only: slice, find, replace_all, count
88
use stdlib_optval, only: optval
99
use stdlib_ascii, only : to_string
1010
implicit none
@@ -378,6 +378,49 @@ subroutine test_replace_all
378378

379379
end subroutine test_replace_all
380380

381+
subroutine test_count
382+
type(string_type) :: test_string_1, test_string_2, test_pattern_1, test_pattern_2
383+
test_string_1 = "DNA sequence: AGAGAGAGTCCTGTCGAGA"
384+
test_string_2 = "DNA sequence: GTCCTGTCCTGTCAGA"
385+
test_pattern_1 = "AGA"
386+
test_pattern_2 = "GTCCTGTC"
387+
388+
! all 2 as string_type
389+
call check(all(count([test_string_1, test_string_2], test_pattern_1) == [4, 1]), &
390+
& 'count: all 2 as string_type, test case 1')
391+
call check(all(count(test_string_1, [test_pattern_1, test_pattern_2], .false.) == [3, 1]), &
392+
& 'count: all 2 as string_type, test case 2')
393+
call check(count(test_string_2, test_pattern_1, .false.) == 1, &
394+
& 'count: all 2 as string_type, test case 3')
395+
call check(all(count([test_string_2, test_string_2, test_string_1], [test_pattern_2, &
396+
& test_pattern_2, test_pattern_1], [.true., .false., .false.]) == [2, 1, 3]), &
397+
& 'count: all 2 as string_type, test case 4')
398+
call check(all(count([[test_string_1, test_string_2], [test_string_1, test_string_2]], [[test_pattern_1, &
399+
& test_pattern_2], [test_pattern_2, test_pattern_1]], .true.) == [[4, 2], [1, 1]]), &
400+
& 'count: all 2 as string_type, test case 5')
401+
402+
! 1 string_type and 1 character scalar
403+
call check(all(count(test_string_1, ["AGA", "GTC"], [.true., .false.]) == [4, 2]), &
404+
& 'count: 1 string_type and 1 character scalar, test case 1')
405+
call check(all(count([test_string_1, test_string_2], ["CTC", "GTC"], [.true., .false.]) &
406+
& == [0, 3]), 'count: 1 string_type and 1 character scalar, test case 2')
407+
call check(all(count(["AGAGAGAGTCCTGTCGAGA", "AGAGAGAGTCCTGTCGAGA"], test_pattern_1, &
408+
& [.false., .true.]) == [3, 4]), &
409+
& 'count: 1 string_type and 1 character scalar, test case 3')
410+
call check(count(test_string_1, "GAG") == 4, &
411+
& 'count: 1 string_type and 1 character scalar, test case 4')
412+
call check(count("DNA sequence: GTCCTGTCCTGTCAGA", test_pattern_2, .false.) == 1, &
413+
& 'count: 1 string_type and 1 character scalar, test case 5')
414+
415+
! all 2 character scalar
416+
call check(all(count("", ["mango", "trees"], .true.) == [0, 0]), &
417+
& 'count: all 2 character scalar, test case 1')
418+
call check(count("", "", .true.) == 0, 'count: all 2 character scalar, test case 2')
419+
call check(all(count(["mango", "trees"], "", .true.) == [0, 0]), &
420+
& 'count: all 2 character scalar, test case 3')
421+
422+
end subroutine test_count
423+
381424
end module test_string_functions
382425

383426

@@ -394,5 +437,6 @@ program tester
394437
call test_slice_gen
395438
call test_find
396439
call test_replace_all
440+
call test_count
397441

398442
end program tester

0 commit comments

Comments
 (0)