@@ -4,7 +4,7 @@ module test_string_functions
4
4
use testdrive, only : new_unittest, unittest_type, error_type, check
5
5
use stdlib_string_type, only : string_type, assignment (= ), operator (==), &
6
6
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
8
8
use stdlib_optval, only: optval
9
9
use stdlib_strings, only : to_string
10
10
implicit none
@@ -29,7 +29,8 @@ subroutine collect_string_functions(testsuite)
29
29
new_unittest(" replace_all" , test_replace_all), &
30
30
new_unittest(" padl" , test_padl), &
31
31
new_unittest(" padr" , test_padr), &
32
- new_unittest(" count" , test_count) &
32
+ new_unittest(" count" , test_count), &
33
+ new_unittest(" zfill" , test_zfill) &
33
34
]
34
35
end subroutine collect_string_functions
35
36
@@ -659,6 +660,54 @@ subroutine test_count(error)
659
660
660
661
end subroutine test_count
661
662
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
+
662
711
end module test_string_functions
663
712
664
713
0 commit comments