|
| 1 | +! SPDX-Identifier: MIT |
| 2 | +module test_string_format_string |
| 3 | + use stdlib_strings, only: format_string, starts_with |
| 4 | + use stdlib_error, only: check |
| 5 | + use stdlib_optval, only: optval |
| 6 | + implicit none |
| 7 | + |
| 8 | +contains |
| 9 | + |
| 10 | + |
| 11 | + subroutine check_formatter(actual, expected, description, partial) |
| 12 | + character(len=*), intent(in) :: actual, expected, description |
| 13 | + logical, intent(in), optional :: partial |
| 14 | + logical :: stat |
| 15 | + character(len=:), allocatable :: msg |
| 16 | + |
| 17 | + if (optval(partial, .false.)) then |
| 18 | + stat = starts_with(actual, expected) |
| 19 | + else |
| 20 | + stat = actual == expected |
| 21 | + end if |
| 22 | + |
| 23 | + if (.not. stat) then |
| 24 | + msg = description // new_line("a") // & |
| 25 | + & "Expected: '" // expected // "' but got '" // actual // "'" |
| 26 | + else |
| 27 | + print '(" - ", a, /, " Result: ''", a, "''")', description, actual |
| 28 | + end if |
| 29 | + |
| 30 | + call check(stat, msg) |
| 31 | + |
| 32 | + end subroutine check_formatter |
| 33 | + |
| 34 | + subroutine test_format_string_complex |
| 35 | + call check_formatter(format_string((1, 1)), "(1.0", & |
| 36 | + & "Default formatter for complex number", partial=.true.) |
| 37 | + call check_formatter(format_string((1, 1), '(F6.2)'), "( 1.00, 1.00)", & |
| 38 | + & "Formatter for complex number") |
| 39 | + call check_formatter(format_string((-1, -1), '(F6.2)'), "( -1.00, -1.00)", & |
| 40 | + & "Formatter for negative complex number") |
| 41 | + call check_formatter(format_string((1, 1), '(SP,F6.2)'), "( +1.00, +1.00)", & |
| 42 | + & "Formatter with sign control descriptor for complex number") |
| 43 | + call check_formatter(format_string((1, 1), '(F6.2)') // format_string((2, 2), '(F7.3)'), & |
| 44 | + & "( 1.00, 1.00)( 2.000, 2.000)", & |
| 45 | + & "Multiple formatters for complex numbers") |
| 46 | + |
| 47 | + end subroutine test_format_string_complex |
| 48 | + |
| 49 | + subroutine test_format_string_integer |
| 50 | + call check_formatter(format_string(100), "100", & |
| 51 | + & "Default formatter for integer number") |
| 52 | + call check_formatter(format_string(100, '(I6)'), " 100", & |
| 53 | + & "Formatter for integer number") |
| 54 | + call check_formatter(format_string(100, '(I0.6)'), "000100", & |
| 55 | + & "Formatter with zero padding for integer number") |
| 56 | + call check_formatter(format_string(100, '(I6)') // format_string(1000, '(I7)'), & |
| 57 | + & " 100 1000", "Multiple formatters for integers") |
| 58 | + call check_formatter(format_string(34, '(B8)'), " 100010", & |
| 59 | + & "Binary formatter for integer number") |
| 60 | + call check_formatter(format_string(34, '(O0.3)'), "042", & |
| 61 | + & "Octal formatter with zero padding for integer number") |
| 62 | + call check_formatter(format_string(34, '(Z3)'), " 22", & |
| 63 | + & "Hexadecimal formatter for integer number") |
| 64 | + |
| 65 | + end subroutine test_format_string_integer |
| 66 | + |
| 67 | + subroutine test_format_string_real |
| 68 | + call check_formatter(format_string(100.), "100.0", & |
| 69 | + & "Default formatter for real number", partial=.true.) |
| 70 | + call check_formatter(format_string(100., '(F6.2)'), "100.00", & |
| 71 | + & "Formatter for real number") |
| 72 | + call check_formatter(format_string(289., '(E7.2)'), ".29E+03", & |
| 73 | + & "Exponential formatter with rounding for real number") |
| 74 | + call check_formatter(format_string(128., '(ES8.2)'), "1.28E+02", & |
| 75 | + & "Exponential formatter for real number") |
| 76 | + |
| 77 | + ! Wrong demonstration |
| 78 | + call check_formatter(format_string(-100., '(F6.2)'), "*", & |
| 79 | + & "Too narrow formatter for signed real number", partial=.true.) |
| 80 | + call check_formatter(format_string(1000., '(F6.3)'), "*", & |
| 81 | + & "Too narrow formatter for real number", partial=.true.) |
| 82 | + call check_formatter(format_string(1000., '(7.3)'), "*", & |
| 83 | + & "Invalid formatter for real number", partial=.true.) |
| 84 | + |
| 85 | + end subroutine test_format_string_real |
| 86 | + |
| 87 | + subroutine test_format_string_logical |
| 88 | + call check_formatter(format_string(.true.), "T", & |
| 89 | + & "Default formatter for logcal value") |
| 90 | + call check_formatter(format_string(.true., '(L2)'), " T", & |
| 91 | + & "Formatter for logical value") |
| 92 | + call check_formatter(format_string(.false., '(L2)') // format_string(.true., '(L5)'), & |
| 93 | + & " F T", "Multiple formatters for logical values") |
| 94 | + |
| 95 | + ! Wrong demonstration |
| 96 | + call check_formatter(format_string(.false., '(1x)'), "*", & |
| 97 | + & "Invalid formatter for logical value", partial=.true.) |
| 98 | + |
| 99 | + end subroutine test_format_string_logical |
| 100 | + |
| 101 | + |
| 102 | +end module test_string_format_string |
| 103 | + |
| 104 | +program tester |
| 105 | + use test_string_format_string |
| 106 | + implicit none |
| 107 | + |
| 108 | + call test_format_string_complex |
| 109 | + call test_format_string_integer |
| 110 | + call test_format_string_logical |
| 111 | + call test_format_string_real |
| 112 | + |
| 113 | +end program tester |
0 commit comments