11program test_strings_format_string
2- use , non_intrinsic :: stdlib_strings, only: format_string
2+ use stdlib_strings, only: format_string, starts_with
3+ use stdlib_error, only: check
4+ use stdlib_optval, only: optval
35 implicit none
46 print * , ' format_string(complex) : '
5- print * , format_string((1 , 1 ))
6- print * , format_string((1 , 1 ), ' (F6.2)' )
7- print * , format_string((1 , 1 ), ' (F6.2)' ), format_string((2 , 2 ), ' (F7.3)' )
7+ call check_formatter(format_string((1 , 1 )), " (1.00000000,1.00000000)" , &
8+ & " Default formatter for complex number" )
9+ call check_formatter(format_string((1 , 1 ), ' (F6.2)' ), " ( 1.00, 1.00)" , &
10+ & " Formatter for complex number" )
11+ call check_formatter(format_string((- 1 , - 1 ), ' (F6.2)' ), " ( -1.00, -1.00)" , &
12+ & " Formatter for negative complex number" )
13+ call check_formatter(format_string((1 , 1 ), ' (SP,F6.2)' ), " ( +1.00, +1.00)" , &
14+ & " Formatter with sign control descriptor for complex number" )
15+ call check_formatter(format_string((1 , 1 ), ' (F6.2)' )// format_string((2 , 2 ), ' (F7.3)' ), &
16+ & " ( 1.00, 1.00)( 2.000, 2.000)" , &
17+ & " Multiple formatters for complex numbers" )
818 print * , ' format_string(integer) : '
9- print * , format_string(100 )
10- print * , format_string(100 , ' (I6)' )
11- print * , format_string(100 , ' (I6)' ), format_string(1000 , ' (I7)' )
19+ call check_formatter(format_string(100 ), " 100" , &
20+ & " Default formatter for integer number" )
21+ call check_formatter(format_string(100 , ' (I6)' ), " 100" , &
22+ & " Formatter for integer number" )
23+ call check_formatter(format_string(100 , ' (I0.6)' ), " 000100" , &
24+ & " Formatter with zero padding for integer number" )
25+ call check_formatter(format_string(100 , ' (I6)' )// format_string(1000 , ' (I7)' ), &
26+ & " 100 1000" , &
27+ & " Multiple formatters for integers" )
28+ call check_formatter(format_string(34 , ' (B8)' ), " 100010" , &
29+ & " Binary formatter for integer number" )
30+ call check_formatter(format_string(34 , ' (O0.3)' ), " 042" , &
31+ & " Octal formatter with zero padding for integer number" )
32+ call check_formatter(format_string(34 , ' (Z3)' ), " 22" , &
33+ & " Hexadecimal formatter for integer number" )
1234 print * , ' format_string(real) : '
13- print * , format_string(100 .)
14- print * , format_string(100 ., ' (F12.2)' )
15- print * , format_string(100 ., ' (F6.2)' ), &
16- format_string(1000 ., ' (F7.3)' ), format_string(1000 , ' (F7.3)' )
17- ! ! Wrong demonstration
35+ call check_formatter(format_string(100 .), " 100.000000" , &
36+ & " Default formatter for real number" )
37+ call check_formatter(format_string(100 ., ' (F6.2)' ), " 100.00" , &
38+ & " Formatter for real number" )
39+ call check_formatter(format_string(289 ., ' (E7.2)' ), " .29E+03" , &
40+ & " Exponential formatter with rounding for real number" )
41+ call check_formatter(format_string(128 ., ' (ES8.2)' ), " 1.28E+02" , &
42+ & " Exponential formatter for real number" )
43+ ! Wrong demonstration
44+ call check_formatter(format_string(- 100 ., ' (F6.2)' ), " *" , &
45+ & " Too narrow formatter for signed real number" , partial= .true. )
46+ call check_formatter(format_string(1000 ., ' (F6.3)' ), " *" , &
47+ & " Too narrow formatter for real number" , partial= .true. )
48+ call check_formatter(format_string(1000 , ' (F7.3)' ), " *" , &
49+ & " Real formatter for integer number" , partial= .true. )
1850 print * , ' format_string(logical) : '
19- print * , format_string(.true. )
20- print * , format_string(.true. , ' (L2)' )
21- print * , format_string(.false. , ' (L2)' ), format_string(.true. , ' (L5)' ), &
22- format_string(.false. , ' (I5)' )
23- ! ! Wrong demonstration
24- end program test_strings_format_string
51+ call check_formatter(format_string(.true. ), " T" , &
52+ & " Default formatter for logcal value" )
53+ call check_formatter(format_string(.true. , ' (L2)' ), " T" , &
54+ & " Formatter for logical value" )
55+ call check_formatter(format_string(.false. , ' (L2)' )// format_string(.true. , ' (L5)' ), &
56+ & " F T" , &
57+ & " Multiple formatters for logical values" )
58+ ! Wrong demonstration
59+ call check_formatter(format_string(.false. , ' (I5)' ), " *" , &
60+ & " Integer formatter for logical value" , partial= .true. )
61+
62+ contains
63+ subroutine check_formatter (actual , expected , description , partial )
64+ character (len=* ), intent (in ) :: actual, expected, description
65+ logical , intent (in ), optional :: partial
66+ logical :: stat
67+ character (len= :), allocatable :: msg
68+
69+ if (optval(partial, .false. )) then
70+ stat = starts_with(actual, expected)
71+ else
72+ stat = actual == expected
73+ end if
74+ if (.not. stat) then
75+ msg = description // new_line(" a" ) // &
76+ & " Expected: '" // expected// " ' but got '" // actual// " '"
77+ else
78+ print ' (" - ", a, /, " Result: '' ", a, "'' ")' , description, actual
79+ end if
80+ call check(stat, msg)
81+ end subroutine check_formatter
82+ end program test_strings_format_string
0 commit comments