Skip to content

Commit bf33954

Browse files
committed
added test cases for padl and padr
1 parent 4cf102e commit bf33954

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
@@ -329,6 +329,81 @@ subroutine test_replace_all
329329

330330
end subroutine test_replace_all
331331

332+
subroutine test_padl
333+
type(string_type) :: test_string
334+
character(len=:), allocatable :: test_char
335+
336+
test_string = "left pad this string"
337+
test_char = " left pad this string "
338+
339+
! output_length > len(string)
340+
call check(padl(test_string, 25, "#") == "#####left pad this string", &
341+
& 'padl: output_length > len(string), test_case 1')
342+
call check(padl(test_string, 22, "$") == "$$left pad this string", &
343+
& 'padl: output_length > len(string), test_case 2')
344+
call check(padl(test_string, 23) == " left pad this string", &
345+
& 'padl: output_length > len(string), test_case 3')
346+
call check(padl(test_char, 26) == " left pad this string ", &
347+
& 'padl: output_length > len(string), test_case 4')
348+
call check(padl(test_char, 26, "&") == "&& left pad this string ", &
349+
& 'padl: output_length > len(string), test_case 5')
350+
call check(padl("", 10, "!") == "!!!!!!!!!!", &
351+
& 'padl: output_length > len(string), test_case 6')
352+
353+
! output_length <= len(string)
354+
call check(padl(test_string, 18, "#") == "left pad this string", &
355+
& 'padl: output_length <= len(string), test_case 1')
356+
call check(padl(test_string, -4, "@") == "left pad this string", &
357+
& 'padl: output_length <= len(string), test_case 2')
358+
call check(padl(test_char, 20, "0") == " left pad this string ", &
359+
& 'padl: output_length <= len(string), test_case 3')
360+
call check(padl(test_char, 17) == " left pad this string ", &
361+
& 'padl: output_length <= len(string), test_case 4')
362+
call check(padl("", 0, "!") == "", &
363+
& 'padl: output_length <= len(string), test_case 5')
364+
call check(padl("", -12, "!") == "", &
365+
& 'padl: output_length <= len(string), test_case 6')
366+
367+
end subroutine test_padl
368+
369+
subroutine test_padr
370+
type(string_type) :: test_string
371+
character(len=:), allocatable :: test_char
372+
373+
test_string = "right pad this string"
374+
test_char = " right pad this string "
375+
376+
! output_length > len(string)
377+
call check(padr(test_string, 25, "#") == "right pad this string####", &
378+
& 'padr: output_length > len(string), test_case 1')
379+
call check(padr(test_string, 22, "$") == "right pad this string$", &
380+
& 'padr: output_length > len(string), test_case 2')
381+
call check(padr(test_string, 24) == "right pad this string ", &
382+
& 'padr: output_length > len(string), test_case 3')
383+
call check(padr(test_char, 27) == " right pad this string ", &
384+
& 'padr: output_length > len(string), test_case 4')
385+
call check(padr(test_char, 27, "&") == " right pad this string &&", &
386+
& 'padr: output_length > len(string), test_case 5')
387+
call check(padr("", 10, "!") == "!!!!!!!!!!", &
388+
& 'padr: output_length > len(string), test_case 6')
389+
390+
! output_length <= len(string)
391+
call check(padr(test_string, 18, "#") == "right pad this string", &
392+
& 'padr: output_length <= len(string), test_case 1')
393+
call check(padr(test_string, -4, "@") == "right pad this string", &
394+
& 'padr: output_length <= len(string), test_case 2')
395+
call check(padr(test_char, 20, "0") == " right pad this string ", &
396+
& 'padr: output_length <= len(string), test_case 3')
397+
call check(padr(test_char, 17) == " right pad this string ", &
398+
& 'padr: output_length <= len(string), test_case 4')
399+
call check(padr("", 0, "!") == "", &
400+
& 'padr: output_length <= len(string), test_case 5')
401+
call check(padr("", -12, "!") == "", &
402+
& 'padr: output_length <= len(string), test_case 6')
403+
404+
end subroutine test_padr
405+
406+
332407
end module test_string_functions
333408

334409

@@ -345,5 +420,7 @@ program tester
345420
call test_slice_gen
346421
call test_find
347422
call test_replace_all
423+
call test_padl
424+
call test_padr
348425

349426
end program tester

0 commit comments

Comments
 (0)