-
Notifications
You must be signed in to change notification settings - Fork 191
Add getline to read whole line from formatted unit #597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
module test_getline | ||
use stdlib_io, only : getline | ||
use stdlib_string_type, only : string_type, len | ||
use testdrive, only : new_unittest, unittest_type, error_type, check | ||
implicit none | ||
private | ||
|
||
public :: collect_getline | ||
|
||
contains | ||
|
||
!> Collect all exported unit tests | ||
subroutine collect_getline(testsuite) | ||
!> Collection of tests | ||
type(unittest_type), allocatable, intent(out) :: testsuite(:) | ||
|
||
testsuite = [ & | ||
new_unittest("read-char", test_read_char), & | ||
new_unittest("read-string", test_read_string), & | ||
new_unittest("pad-no", test_pad_no), & | ||
new_unittest("iostat-end", test_iostat_end), & | ||
new_unittest("closed-unit", test_closed_unit, should_fail=.true.), & | ||
new_unittest("no-unit", test_no_unit, should_fail=.true.) & | ||
] | ||
end subroutine collect_getline | ||
|
||
subroutine test_read_char(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
|
||
integer :: io, i, stat | ||
character(len=:), allocatable :: line | ||
|
||
open(newunit=io, status="scratch") | ||
write(io, "(a)") repeat("abc", 10), repeat("def", 100), repeat("ghi", 1000) | ||
rewind(io) | ||
|
||
do i = 1, 3 | ||
call getline(io, line, stat) | ||
call check(error, stat) | ||
if (allocated(error)) exit | ||
call check(error, len(line), 3*10**i) | ||
if (allocated(error)) exit | ||
end do | ||
close(io) | ||
end subroutine test_read_char | ||
|
||
subroutine test_read_string(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
|
||
integer :: io, i, stat | ||
type(string_type) :: line | ||
|
||
open(newunit=io, status="scratch") | ||
write(io, "(a)") repeat("abc", 10), repeat("def", 100), repeat("ghi", 1000) | ||
rewind(io) | ||
|
||
do i = 1, 3 | ||
call getline(io, line, stat) | ||
call check(error, stat) | ||
if (allocated(error)) exit | ||
call check(error, len(line), 3*10**i) | ||
if (allocated(error)) exit | ||
end do | ||
close(io) | ||
end subroutine test_read_string | ||
|
||
subroutine test_pad_no(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
|
||
integer :: io, i, stat | ||
character(len=:), allocatable :: line | ||
|
||
open(newunit=io, status="scratch", pad="no") | ||
write(io, "(a)") repeat("abc", 10), repeat("def", 100), repeat("ghi", 1000) | ||
rewind(io) | ||
|
||
do i = 1, 3 | ||
call getline(io, line, stat) | ||
call check(error, stat) | ||
if (allocated(error)) exit | ||
call check(error, len(line), 3*10**i) | ||
if (allocated(error)) exit | ||
end do | ||
close(io) | ||
end subroutine test_pad_no | ||
|
||
subroutine test_iostat_end(error) | ||
use, intrinsic :: iso_fortran_env, only : iostat_end | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
|
||
integer :: io, i, stat | ||
character(len=:), allocatable :: line | ||
|
||
open(newunit=io, status="scratch") | ||
write(io, "(a)") repeat("abc", 10), repeat("def", 100), repeat("ghi", 1000) | ||
rewind(io) | ||
|
||
do i = 1, 3 | ||
call getline(io, line, stat) | ||
call check(error, stat) | ||
if (allocated(error)) exit | ||
call check(error, len(line), 3*10**i) | ||
if (allocated(error)) exit | ||
end do | ||
if (.not.allocated(error)) then | ||
call getline(io, line, stat) | ||
call check(error, stat, iostat_end) | ||
end if | ||
close(io) | ||
end subroutine test_iostat_end | ||
|
||
subroutine test_closed_unit(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
|
||
integer :: io, stat | ||
character(len=:), allocatable :: line, msg | ||
|
||
open(newunit=io, status="scratch") | ||
close(io) | ||
|
||
call getline(io, line, stat, msg) | ||
call check(error, stat, msg) | ||
end subroutine test_closed_unit | ||
|
||
subroutine test_no_unit(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
|
||
integer :: io, stat | ||
character(len=:), allocatable :: line, msg | ||
|
||
io = -1 | ||
call getline(io, line, stat, msg) | ||
call check(error, stat, msg) | ||
end subroutine test_no_unit | ||
|
||
end module test_getline | ||
|
||
|
||
program tester | ||
use, intrinsic :: iso_fortran_env, only : error_unit | ||
use testdrive, only : run_testsuite, new_testsuite, testsuite_type | ||
use test_getline, only : collect_getline | ||
implicit none | ||
integer :: stat, is | ||
type(testsuite_type), allocatable :: testsuites(:) | ||
character(len=*), parameter :: fmt = '("#", *(1x, a))' | ||
|
||
stat = 0 | ||
|
||
testsuites = [ & | ||
new_testsuite("getline", collect_getline) & | ||
] | ||
|
||
do is = 1, size(testsuites) | ||
write(error_unit, fmt) "Testing:", testsuites(is)%name | ||
call run_testsuite(testsuites(is)%collect, error_unit, stat) | ||
end do | ||
|
||
if (stat > 0) then | ||
write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!" | ||
error stop | ||
end if | ||
end program |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.