@@ -4,7 +4,7 @@ module test_string_functions
44 use testdrive, only : new_unittest, unittest_type, error_type, 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, padl, padr, count
7+ use stdlib_strings, only: slice, find, replace_all, padl, padr, count, zfill
88 use stdlib_optval, only: optval
99 use stdlib_strings, only : to_string
1010 implicit none
@@ -29,7 +29,8 @@ subroutine collect_string_functions(testsuite)
2929 new_unittest(" replace_all" , test_replace_all), &
3030 new_unittest(" padl" , test_padl), &
3131 new_unittest(" padr" , test_padr), &
32- new_unittest(" count" , test_count) &
32+ new_unittest(" count" , test_count), &
33+ new_unittest(" zfill" , test_zfill) &
3334 ]
3435 end subroutine collect_string_functions
3536
@@ -659,6 +660,54 @@ subroutine test_count(error)
659660
660661 end subroutine test_count
661662
663+ subroutine test_zfill (error )
664+ ! > Error handling
665+ type (error_type), allocatable , intent (out ) :: error
666+
667+ type (string_type) :: test_string
668+ character (len= :), allocatable :: test_char
669+
670+ test_string = " left pad this string"
671+ test_char = " left pad this string "
672+
673+ ! output_length > len(string)
674+ call check(error, zfill(test_string, 25 ) == " 00000left pad this string" , &
675+ & ' zfill: output_length > len(string), test_case 1' )
676+ if (allocated (error)) return
677+ call check(error, zfill(test_string, 22 ) == " 00left pad this string" , &
678+ & ' zfill: output_length > len(string), test_case 2' )
679+ if (allocated (error)) return
680+ call check(error, zfill(test_string, 23 ) == " 000left pad this string" , &
681+ & ' zfill: output_length > len(string), test_case 3' )
682+ if (allocated (error)) return
683+ call check(error, zfill(test_char, 26 ) == " 00 left pad this string " , &
684+ & ' zfill: output_length > len(string), test_case 4' )
685+ if (allocated (error)) return
686+ call check(error, zfill(" " , 10 ) == " 0000000000" , &
687+ & ' zfill: output_length > len(string), test_case 5' )
688+ if (allocated (error)) return
689+
690+ ! output_length <= len(string)
691+ call check(error, zfill(test_string, 18 ) == " left pad this string" , &
692+ & ' zfill: output_length <= len(string), test_case 1' )
693+ if (allocated (error)) return
694+ call check(error, zfill(test_string, - 4 ) == " left pad this string" , &
695+ & ' zfill: output_length <= len(string), test_case 2' )
696+ if (allocated (error)) return
697+ call check(error, zfill(test_char, 20 ) == " left pad this string " , &
698+ & ' zfill: output_length <= len(string), test_case 3' )
699+ if (allocated (error)) return
700+ call check(error, zfill(test_char, 17 ) == " left pad this string " , &
701+ & ' zfill: output_length <= len(string), test_case 4' )
702+ if (allocated (error)) return
703+ call check(error, zfill(" " , 0 ) == " " , &
704+ & ' zfill: output_length <= len(string), test_case 5' )
705+ if (allocated (error)) return
706+ call check(error, zfill(" " , - 12 ) == " " , &
707+ & ' zfill: output_length <= len(string), test_case 6' )
708+
709+ end subroutine test_zfill
710+
662711end module test_string_functions
663712
664713
0 commit comments