Skip to content

Commit e66abc1

Browse files
committed
added test cases for padl and padr
1 parent b0fbf5d commit e66abc1

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/stdlib_strings.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
!> The specification of this module is available [here](../page/specs/stdlib_strings.html).
66
module stdlib_strings
77
use stdlib_ascii, only: whitespace
8-
use stdlib_string_type, only: string_type, char, verify, repeat
8+
use stdlib_string_type, only: string_type, char, verify, repeat, len
99
use stdlib_optval, only: optval
1010
implicit none
1111
private

src/tests/string/test_string_functions.f90

Lines changed: 78 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, padl, padr
88
use stdlib_optval, only: optval
99
use stdlib_ascii, only : to_string
1010
implicit none
@@ -378,6 +378,81 @@ subroutine test_replace_all
378378

379379
end subroutine test_replace_all
380380

381+
subroutine test_padl
382+
type(string_type) :: test_string
383+
character(len=:), allocatable :: test_char
384+
385+
test_string = "left pad this string"
386+
test_char = " left pad this string "
387+
388+
! output_length > len(string)
389+
call check(padl(test_string, 25, "#") == "#####left pad this string", &
390+
& 'padl: output_length > len(string), test_case 1')
391+
call check(padl(test_string, 22, "$") == "$$left pad this string", &
392+
& 'padl: output_length > len(string), test_case 2')
393+
call check(padl(test_string, 23) == " left pad this string", &
394+
& 'padl: output_length > len(string), test_case 3')
395+
call check(padl(test_char, 26) == " left pad this string ", &
396+
& 'padl: output_length > len(string), test_case 4')
397+
call check(padl(test_char, 26, "&") == "&& left pad this string ", &
398+
& 'padl: output_length > len(string), test_case 5')
399+
call check(padl("", 10, "!") == "!!!!!!!!!!", &
400+
& 'padl: output_length > len(string), test_case 6')
401+
402+
! output_length <= len(string)
403+
call check(padl(test_string, 18, "#") == "left pad this string", &
404+
& 'padl: output_length <= len(string), test_case 1')
405+
call check(padl(test_string, -4, "@") == "left pad this string", &
406+
& 'padl: output_length <= len(string), test_case 2')
407+
call check(padl(test_char, 20, "0") == " left pad this string ", &
408+
& 'padl: output_length <= len(string), test_case 3')
409+
call check(padl(test_char, 17) == " left pad this string ", &
410+
& 'padl: output_length <= len(string), test_case 4')
411+
call check(padl("", 0, "!") == "", &
412+
& 'padl: output_length <= len(string), test_case 5')
413+
call check(padl("", -12, "!") == "", &
414+
& 'padl: output_length <= len(string), test_case 6')
415+
416+
end subroutine test_padl
417+
418+
subroutine test_padr
419+
type(string_type) :: test_string
420+
character(len=:), allocatable :: test_char
421+
422+
test_string = "right pad this string"
423+
test_char = " right pad this string "
424+
425+
! output_length > len(string)
426+
call check(padr(test_string, 25, "#") == "right pad this string####", &
427+
& 'padr: output_length > len(string), test_case 1')
428+
call check(padr(test_string, 22, "$") == "right pad this string$", &
429+
& 'padr: output_length > len(string), test_case 2')
430+
call check(padr(test_string, 24) == "right pad this string ", &
431+
& 'padr: output_length > len(string), test_case 3')
432+
call check(padr(test_char, 27) == " right pad this string ", &
433+
& 'padr: output_length > len(string), test_case 4')
434+
call check(padr(test_char, 27, "&") == " right pad this string &&", &
435+
& 'padr: output_length > len(string), test_case 5')
436+
call check(padr("", 10, "!") == "!!!!!!!!!!", &
437+
& 'padr: output_length > len(string), test_case 6')
438+
439+
! output_length <= len(string)
440+
call check(padr(test_string, 18, "#") == "right pad this string", &
441+
& 'padr: output_length <= len(string), test_case 1')
442+
call check(padr(test_string, -4, "@") == "right pad this string", &
443+
& 'padr: output_length <= len(string), test_case 2')
444+
call check(padr(test_char, 20, "0") == " right pad this string ", &
445+
& 'padr: output_length <= len(string), test_case 3')
446+
call check(padr(test_char, 17) == " right pad this string ", &
447+
& 'padr: output_length <= len(string), test_case 4')
448+
call check(padr("", 0, "!") == "", &
449+
& 'padr: output_length <= len(string), test_case 5')
450+
call check(padr("", -12, "!") == "", &
451+
& 'padr: output_length <= len(string), test_case 6')
452+
453+
end subroutine test_padr
454+
455+
381456
end module test_string_functions
382457

383458

@@ -394,5 +469,7 @@ program tester
394469
call test_slice_gen
395470
call test_find
396471
call test_replace_all
472+
call test_padl
473+
call test_padr
397474

398475
end program tester

0 commit comments

Comments
 (0)