|
| 1 | +module test_getline |
| 2 | + use stdlib_io, only : getline |
| 3 | + use stdlib_string_type, only : string_type, len |
| 4 | + use testdrive, only : new_unittest, unittest_type, error_type, check |
| 5 | + implicit none |
| 6 | + private |
| 7 | + |
| 8 | + public :: collect_getline |
| 9 | + |
| 10 | +contains |
| 11 | + |
| 12 | + !> Collect all exported unit tests |
| 13 | + subroutine collect_getline(testsuite) |
| 14 | + !> Collection of tests |
| 15 | + type(unittest_type), allocatable, intent(out) :: testsuite(:) |
| 16 | + |
| 17 | + testsuite = [ & |
| 18 | + new_unittest("read-char", test_read_char), & |
| 19 | + new_unittest("read-string", test_read_string), & |
| 20 | + new_unittest("pad-no", test_pad_no), & |
| 21 | + new_unittest("iostat-end", test_iostat_end), & |
| 22 | + new_unittest("closed-unit", test_closed_unit, should_fail=.true.), & |
| 23 | + new_unittest("no-unit", test_no_unit, should_fail=.true.) & |
| 24 | + ] |
| 25 | + end subroutine collect_getline |
| 26 | + |
| 27 | + subroutine test_read_char(error) |
| 28 | + !> Error handling |
| 29 | + type(error_type), allocatable, intent(out) :: error |
| 30 | + |
| 31 | + integer :: io, i, stat |
| 32 | + character(len=:), allocatable :: line |
| 33 | + |
| 34 | + open(newunit=io, status="scratch") |
| 35 | + write(io, "(a)") repeat("abc", 10), repeat("def", 100), repeat("ghi", 1000) |
| 36 | + rewind(io) |
| 37 | + |
| 38 | + do i = 1, 3 |
| 39 | + call getline(io, line, stat) |
| 40 | + call check(error, stat) |
| 41 | + if (allocated(error)) exit |
| 42 | + call check(error, len(line), 3*10**i) |
| 43 | + if (allocated(error)) exit |
| 44 | + end do |
| 45 | + close(io) |
| 46 | + end subroutine test_read_char |
| 47 | + |
| 48 | + subroutine test_read_string(error) |
| 49 | + !> Error handling |
| 50 | + type(error_type), allocatable, intent(out) :: error |
| 51 | + |
| 52 | + integer :: io, i, stat |
| 53 | + type(string_type) :: line |
| 54 | + |
| 55 | + open(newunit=io, status="scratch") |
| 56 | + write(io, "(a)") repeat("abc", 10), repeat("def", 100), repeat("ghi", 1000) |
| 57 | + rewind(io) |
| 58 | + |
| 59 | + do i = 1, 3 |
| 60 | + call getline(io, line, stat) |
| 61 | + call check(error, stat) |
| 62 | + if (allocated(error)) exit |
| 63 | + call check(error, len(line), 3*10**i) |
| 64 | + if (allocated(error)) exit |
| 65 | + end do |
| 66 | + close(io) |
| 67 | + end subroutine test_read_string |
| 68 | + |
| 69 | + subroutine test_pad_no(error) |
| 70 | + !> Error handling |
| 71 | + type(error_type), allocatable, intent(out) :: error |
| 72 | + |
| 73 | + integer :: io, i, stat |
| 74 | + character(len=:), allocatable :: line |
| 75 | + |
| 76 | + open(newunit=io, status="scratch", pad="no") |
| 77 | + write(io, "(a)") repeat("abc", 10), repeat("def", 100), repeat("ghi", 1000) |
| 78 | + rewind(io) |
| 79 | + |
| 80 | + do i = 1, 3 |
| 81 | + call getline(io, line, stat) |
| 82 | + call check(error, stat) |
| 83 | + if (allocated(error)) exit |
| 84 | + call check(error, len(line), 3*10**i) |
| 85 | + if (allocated(error)) exit |
| 86 | + end do |
| 87 | + close(io) |
| 88 | + end subroutine test_pad_no |
| 89 | + |
| 90 | + subroutine test_iostat_end(error) |
| 91 | + use, intrinsic :: iso_fortran_env, only : iostat_end |
| 92 | + !> Error handling |
| 93 | + type(error_type), allocatable, intent(out) :: error |
| 94 | + |
| 95 | + integer :: io, i, stat |
| 96 | + character(len=:), allocatable :: line |
| 97 | + |
| 98 | + open(newunit=io, status="scratch") |
| 99 | + write(io, "(a)") repeat("abc", 10), repeat("def", 100), repeat("ghi", 1000) |
| 100 | + rewind(io) |
| 101 | + |
| 102 | + do i = 1, 3 |
| 103 | + call getline(io, line, stat) |
| 104 | + call check(error, stat) |
| 105 | + if (allocated(error)) exit |
| 106 | + call check(error, len(line), 3*10**i) |
| 107 | + if (allocated(error)) exit |
| 108 | + end do |
| 109 | + if (.not.allocated(error)) then |
| 110 | + call getline(io, line, stat) |
| 111 | + call check(error, stat, iostat_end) |
| 112 | + end if |
| 113 | + close(io) |
| 114 | + end subroutine test_iostat_end |
| 115 | + |
| 116 | + subroutine test_closed_unit(error) |
| 117 | + !> Error handling |
| 118 | + type(error_type), allocatable, intent(out) :: error |
| 119 | + |
| 120 | + integer :: io, stat |
| 121 | + character(len=:), allocatable :: line, msg |
| 122 | + |
| 123 | + open(newunit=io, status="scratch") |
| 124 | + close(io) |
| 125 | + |
| 126 | + call getline(io, line, stat, msg) |
| 127 | + call check(error, stat, msg) |
| 128 | + end subroutine test_closed_unit |
| 129 | + |
| 130 | + subroutine test_no_unit(error) |
| 131 | + !> Error handling |
| 132 | + type(error_type), allocatable, intent(out) :: error |
| 133 | + |
| 134 | + integer :: io, stat |
| 135 | + character(len=:), allocatable :: line, msg |
| 136 | + |
| 137 | + io = -1 |
| 138 | + call getline(io, line, stat, msg) |
| 139 | + call check(error, stat, msg) |
| 140 | + end subroutine test_no_unit |
| 141 | + |
| 142 | +end module test_getline |
| 143 | + |
| 144 | + |
| 145 | +program tester |
| 146 | + use, intrinsic :: iso_fortran_env, only : error_unit |
| 147 | + use testdrive, only : run_testsuite, new_testsuite, testsuite_type |
| 148 | + use test_getline, only : collect_getline |
| 149 | + implicit none |
| 150 | + integer :: stat, is |
| 151 | + type(testsuite_type), allocatable :: testsuites(:) |
| 152 | + character(len=*), parameter :: fmt = '("#", *(1x, a))' |
| 153 | + |
| 154 | + stat = 0 |
| 155 | + |
| 156 | + testsuites = [ & |
| 157 | + new_testsuite("getline", collect_getline) & |
| 158 | + ] |
| 159 | + |
| 160 | + do is = 1, size(testsuites) |
| 161 | + write(error_unit, fmt) "Testing:", testsuites(is)%name |
| 162 | + call run_testsuite(testsuites(is)%collect, error_unit, stat) |
| 163 | + end do |
| 164 | + |
| 165 | + if (stat > 0) then |
| 166 | + write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!" |
| 167 | + error stop |
| 168 | + end if |
| 169 | +end program |
0 commit comments