-
Notifications
You must be signed in to change notification settings - Fork 224
Add Python-like input() function to stdlib_io #1116
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
base: master
Are you sure you want to change the base?
Changes from all commits
9f3ee69
34df92f
14e46f9
7a6339e
7539182
6b5f2e3
8bc7262
bc73367
dafb662
24423a6
751c69b
6c12c67
c188de7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,11 +13,12 @@ title: io | |
| Experimental | ||
|
|
||
| ### Description | ||
|
|
||
| Loads a rank-2 `array` from a text file. | ||
|
|
||
| ### Syntax | ||
|
|
||
| `call ` [[stdlib_io(module):loadtxt(interface)]] `(filename, array [, skiprows] [, max_rows] [, fmt] [, delimiter])` | ||
| `call` [[stdlib_io(module):loadtxt(interface)]] `(filename, array [, skiprows] [, max_rows] [, fmt] [, delimiter])` | ||
|
|
||
| ### Arguments | ||
|
|
||
|
|
@@ -29,7 +30,7 @@ Loads a rank-2 `array` from a text file. | |
|
|
||
| `max_rows` (optional): Read `max_rows` lines of content after `skiprows` lines. A negative value results in reading all lines. A value of zero results in no lines to be read. The default value is -1. | ||
|
|
||
| `fmt` (optional): Fortran format specifier for the text read. Defaults to the write format for the data type. Setting fmt='*' will specify list directed read. | ||
| `fmt` (optional): Fortran format specifier for the text read. Defaults to the write format for the data type. Setting fmt='*' will specify list directed read. | ||
|
|
||
| `delimiter` (optional): Shall be a character expression of length 1 that contains the delimiter used to separate the columns. The default is `' '`. | ||
|
|
||
|
|
@@ -43,7 +44,6 @@ Returns an allocated rank-2 `array` with the content of `filename`. | |
| {!example/io/example_loadtxt.f90!} | ||
| ``` | ||
|
|
||
|
|
||
| ## `open` - open a file | ||
|
|
||
| ### Status | ||
|
|
@@ -57,15 +57,14 @@ Text files are opened using a sequential access, while binary files are opened u | |
|
|
||
| ### Syntax | ||
|
|
||
| `u = ` [[stdlib_io(module):open(function)]] `(filename [, mode] [, iostat])` | ||
| `u =` [[stdlib_io(module):open(function)]] `(filename [, mode] [, iostat])` | ||
|
|
||
| ### Arguments | ||
|
|
||
| `filename`: Shall be a character expression containing the name of the file to open. | ||
|
|
||
| `mode` (optional): Shall be a character expression containing characters describing the way in which the file will be used. The available modes are: | ||
|
|
||
|
|
||
| | Character | Meaning | | ||
| | --------- | ------- | | ||
| | `'r'` | open for reading (default) | | ||
|
|
@@ -76,14 +75,12 @@ Text files are opened using a sequential access, while binary files are opened u | |
| | `'b'` | binary mode | | ||
| | `'t'` | text mode (default) | | ||
|
|
||
|
|
||
| The default `mode` is `'rt'` (i.e. open for reading a text file). The `mode` may include one of the four different methods for opening a file (i.e., `'r'`, `'w'`, `'x'`, and `'a'`). These four methods can be associated with the character `'+'` to open the file for updating. In addition, it can be specified if the file should be handled as a binary file (`'b'`) or a text file (`'t'`). | ||
|
|
||
| `iostat` (optional): Shall be a scalar of type `integer` that receives the error status of `open`, if provided. If no error exists, `iostat` is zero. | ||
|
|
||
| `u`: Shall be a scalar of type `integer` that specifies the unit number associated with the file `filename`. | ||
|
|
||
|
|
||
| ### Return value | ||
|
|
||
| The result is a scalar of type `integer`. | ||
|
|
@@ -94,19 +91,19 @@ The result is a scalar of type `integer`. | |
| {!example/io/example_open.f90!} | ||
| ``` | ||
|
|
||
|
|
||
| ## `savetxt` - save a 2D array into a text file | ||
|
|
||
| ### Status | ||
|
|
||
| Experimental | ||
|
|
||
| ### Description | ||
|
|
||
| Saves a rank-2 `array` into a text file. | ||
|
|
||
| ### Syntax | ||
|
|
||
| `call ` [[stdlib_io(module):savetxt(interface)]] `(filename, array [, delimiter])` | ||
| `call` [[stdlib_io(module):savetxt(interface)]] `(filename, array [, delimiter])` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this change be reverted (or why is it done)? |
||
|
|
||
| ### Arguments | ||
|
|
||
|
|
@@ -126,7 +123,6 @@ Provides a text file called `filename` that contains the rank-2 `array`. | |
| {!example/io/example_savetxt.f90!} | ||
| ``` | ||
|
|
||
|
|
||
| ## `load_npy` | ||
|
|
||
| ### Status | ||
|
|
@@ -139,7 +135,7 @@ Loads an `array` from a npy formatted binary file. | |
|
|
||
| ### Syntax | ||
|
|
||
| `call ` [[stdlib_io_npy(module):load_npy(interface)]] `(filename, array[, iostat][, iomsg])` | ||
| `call` [[stdlib_io_npy(module):load_npy(interface)]] `(filename, array[, iostat][, iomsg])` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this change be reverted (or why is it done)? |
||
|
|
||
| ### Arguments | ||
|
|
||
|
|
@@ -167,7 +163,6 @@ Returns an allocated `array` with the content of `filename` in case of success. | |
| {!example/io/example_loadnpy.f90!} | ||
| ``` | ||
|
|
||
|
|
||
| ## `save_npy` | ||
|
|
||
| ### Status | ||
|
|
@@ -180,7 +175,7 @@ Saves an `array` into a npy formatted binary file. | |
|
|
||
| ### Syntax | ||
|
|
||
| `call ` [[stdlib_io_npy(module):save_npy(interface)]] `(filename, array[, iostat][, iomsg])` | ||
| `call` [[stdlib_io_npy(module):save_npy(interface)]] `(filename, array[, iostat][, iomsg])` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this change be reverted (or why is it done)? |
||
|
|
||
| ### Arguments | ||
|
|
||
|
|
@@ -220,9 +215,9 @@ Read a whole line from a formatted unit into a string variable | |
|
|
||
| ### Syntax | ||
|
|
||
| `call ` [[stdlib_io(module):get_line(interface)]] ` (unit, line[, iostat][, iomsg])` | ||
| `call` [[stdlib_io(module):get_line(interface)]] `(unit, line[, iostat][, iomsg])` | ||
|
|
||
| `call ` [[stdlib_io(module):get_line(interface)]] ` (line[, iostat][, iomsg])` | ||
| `call` [[stdlib_io(module):get_line(interface)]] `(line[, iostat][, iomsg])` | ||
|
Comment on lines
+218
to
+220
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this change be reverted (or why is it done)? |
||
|
|
||
| ### Arguments | ||
|
|
||
|
|
@@ -272,22 +267,23 @@ Experimental | |
|
|
||
| ### Description | ||
|
|
||
| This subroutine interface reads the entirety of a specified ASCII file and returns its content as a string or an allocatable `character` variable. | ||
| This subroutine interface reads the entirety of a specified ASCII file and returns its content as a string or an allocatable `character` variable. | ||
| The function provides an optional error-handling mechanism via the `state_type` class. If the `err` argument is not provided, exceptions will trigger an `error stop`. The function also supports an optional flag to delete the file after reading. | ||
|
|
||
| ### Syntax | ||
|
|
||
| `call [[stdlib_io(module):get_file(subroutine)]] (filename, file [, err] [, delete=.false.])` | ||
|
|
||
| ### Class | ||
|
|
||
| Function | ||
|
|
||
| ### Arguments | ||
|
|
||
| `filename`: Shall be a character input containing the path to the ASCII file to read. It is an `intent(in)` argument. | ||
|
|
||
| `file`: Shall be a `type(string_type)` or an allocatable `character` variable containing the full content of the specified file. It is an `intent(out)` argument. | ||
|
|
||
| `err` (optional): Shall be a `type(state_type)` variable. It is an `intent(out)` argument used for error handling. | ||
|
|
||
| `delete` (optional): Shall be a `logical` flag. If `.true.`, the file is deleted after reading. Default is `.false.`. It is an `intent(in)` argument. | ||
|
|
@@ -296,7 +292,7 @@ Function | |
|
|
||
| Output variable `file` will contain the full content of the specified file. | ||
|
|
||
| Raises `STDLIB_IO_ERROR` if the file is not found, cannot be opened, read, or deleted. | ||
| Raises `STDLIB_IO_ERROR` if the file is not found, cannot be opened, read, or deleted. | ||
| Exceptions trigger an `error stop` unless the optional `err` argument is provided. | ||
|
|
||
| ### Example | ||
|
|
@@ -305,3 +301,47 @@ Exceptions trigger an `error stop` unless the optional `err` argument is provide | |
| {!example/io/example_get_file.f90!} | ||
| ``` | ||
|
|
||
| ## `input` - Read user input with an optional prompt | ||
|
|
||
| ### Status | ||
|
|
||
| Experimental | ||
|
|
||
| ### Description | ||
|
|
||
| Reads a line of user input from standard input with an optional prompt. | ||
| The prompt, if provided, is displayed on the same line as where the input will be entered. | ||
|
|
||
| ### Syntax | ||
|
|
||
| `str =` [[stdlib_io(module):input(function)]] `([prompt] [, iostat])` | ||
|
|
||
| ### Arguments | ||
|
|
||
| `prompt` (optional): Shall be a `character` expression containing the prompt text to be displayed before reading input. | ||
| The prompt is displayed without a trailing newline, allowing input on the same line. | ||
| This argument is `intent(in)`. | ||
|
|
||
| `iostat` (optional): Shall be a scalar of type `integer` that receives the I/O status. | ||
| Zero indicates success, non-zero indicates an error or end-of-file condition. | ||
| This argument is `intent(out)`. | ||
| If not provided, an error will cause trigger an `error stop`. | ||
|
|
||
|
Comment on lines
+315
to
+329
|
||
| ### Return value | ||
|
|
||
| Returns a deferred-length allocatable `character` string containing the line read from standard input. | ||
| Trailing newline characters are automatically removed. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about trailing blanks (.e.g, if the user enters |
||
|
|
||
| ### Notes | ||
|
|
||
| - If no prompt is provided, the function will wait for user input without displaying any text. | ||
| - Empty input (just pressing Enter) returns an empty string `""` of length equal to 0. | ||
| - The function reads a complete line of input, including any whitespace. | ||
| - For error handling, use the optional `iostat` parameter to detect end-of-file or other I/O exceptions. | ||
| - The function uses `get_line` internally for consistency with other stdlib I/O operations. | ||
|
|
||
| ### Example | ||
|
|
||
| ```fortran | ||
| {!example/io/example_input.f90!} | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| program example_input | ||
| use stdlib_io, only: input | ||
| implicit none | ||
|
|
||
| character(len=:), allocatable :: name | ||
|
|
||
| ! Get user's name with a prompt | ||
| name = input('Enter your name: ') | ||
|
|
||
| ! Display greeting | ||
| print '(a,a)', 'Hello, ', name | ||
|
|
||
| end program example_input |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,7 +6,7 @@ module stdlib_io | |||||||||||||||||||||||||||||||||||||
| !! Provides a support for file handling | ||||||||||||||||||||||||||||||||||||||
| !! ([Specification](../page/specs/stdlib_io.html)) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| use, intrinsic :: iso_fortran_env, only : input_unit | ||||||||||||||||||||||||||||||||||||||
| use, intrinsic :: iso_fortran_env, only : input_unit, output_unit | ||||||||||||||||||||||||||||||||||||||
| use stdlib_kinds, only: sp, dp, xdp, qp, & | ||||||||||||||||||||||||||||||||||||||
| int8, int16, int32, int64 | ||||||||||||||||||||||||||||||||||||||
| use stdlib_io_aux, only: FMT_INT, FMT_REAL_SP, FMT_REAL_DP, FMT_REAL_XDP, FMT_REAL_QP, & | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -18,7 +18,7 @@ module stdlib_io | |||||||||||||||||||||||||||||||||||||
| implicit none | ||||||||||||||||||||||||||||||||||||||
| private | ||||||||||||||||||||||||||||||||||||||
| ! Public API | ||||||||||||||||||||||||||||||||||||||
| public :: loadtxt, savetxt, open, get_line, get_file | ||||||||||||||||||||||||||||||||||||||
| public :: loadtxt, savetxt, open, get_line, get_file, input | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| !! version: experimental | ||||||||||||||||||||||||||||||||||||||
| !! | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -61,6 +61,14 @@ module stdlib_io | |||||||||||||||||||||||||||||||||||||
| module procedure :: get_line_input_string | ||||||||||||||||||||||||||||||||||||||
| end interface get_line | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| !> Version: experimental | ||||||||||||||||||||||||||||||||||||||
| !> | ||||||||||||||||||||||||||||||||||||||
| !> Reads user input with an optional prompt | ||||||||||||||||||||||||||||||||||||||
| !> Returns a deferred-length character string | ||||||||||||||||||||||||||||||||||||||
| interface input | ||||||||||||||||||||||||||||||||||||||
| module procedure :: input_char | ||||||||||||||||||||||||||||||||||||||
| end interface input | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| interface loadtxt | ||||||||||||||||||||||||||||||||||||||
| !! version: experimental | ||||||||||||||||||||||||||||||||||||||
| !! | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -705,4 +713,53 @@ contains | |||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| end subroutine get_file_char | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| !> Version: experimental | ||||||||||||||||||||||||||||||||||||||
| !> | ||||||||||||||||||||||||||||||||||||||
| !> Reads user input with an optional prompt into a deferred-length character variable | ||||||||||||||||||||||||||||||||||||||
| function input_char(prompt, iostat) result(str) | ||||||||||||||||||||||||||||||||||||||
| !> Optional prompt to display before reading input | ||||||||||||||||||||||||||||||||||||||
| character(len=*), intent(in), optional :: prompt | ||||||||||||||||||||||||||||||||||||||
| !> Optional status of operation | ||||||||||||||||||||||||||||||||||||||
| integer, intent(out), optional :: iostat | ||||||||||||||||||||||||||||||||||||||
| !> The input string read from the user | ||||||||||||||||||||||||||||||||||||||
| character(len=:), allocatable :: str | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| integer :: stat | ||||||||||||||||||||||||||||||||||||||
| character(len=:), allocatable :: iomsg | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ! Display prompt if provided (without newline) | ||||||||||||||||||||||||||||||||||||||
| if (present(prompt)) then | ||||||||||||||||||||||||||||||||||||||
| write(output_unit, '(a)', advance='no') prompt | ||||||||||||||||||||||||||||||||||||||
| flush(output_unit) | ||||||||||||||||||||||||||||||||||||||
| end if | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ! Read the input line | ||||||||||||||||||||||||||||||||||||||
| call get_line(input_unit, str, stat, iomsg) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ! Handle iostat | ||||||||||||||||||||||||||||||||||||||
| if (present(iostat)) then | ||||||||||||||||||||||||||||||||||||||
| iostat = stat | ||||||||||||||||||||||||||||||||||||||
| else if (stat /= 0) then | ||||||||||||||||||||||||||||||||||||||
| call error_stop(trim(iomsg)) | ||||||||||||||||||||||||||||||||||||||
| end if | ||||||||||||||||||||||||||||||||||||||
| end function input_char | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| !> Version: experimental | ||||||||||||||||||||||||||||||||||||||
| !> | ||||||||||||||||||||||||||||||||||||||
| !> Reads user input with an optional prompt into a string variable of type `string_type` | ||||||||||||||||||||||||||||||||||||||
| recursive function input_string(prompt, iostat) result(str) | ||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this function is not described in the specs. it is also not publicly available. |
||||||||||||||||||||||||||||||||||||||
| !> Optional prompt to display before reading input | ||||||||||||||||||||||||||||||||||||||
| character(len=*), intent(in), optional :: prompt | ||||||||||||||||||||||||||||||||||||||
| !> Optional status of operation | ||||||||||||||||||||||||||||||||||||||
| integer, intent(out), optional :: iostat | ||||||||||||||||||||||||||||||||||||||
| !> The input string read from the user | ||||||||||||||||||||||||||||||||||||||
| type(string_type) :: str | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| character(len=:), allocatable :: buffer | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ! Use the character version and convert to string_type | ||||||||||||||||||||||||||||||||||||||
| buffer = input_char(prompt, iostat) | ||||||||||||||||||||||||||||||||||||||
| str = string_type(buffer) | ||||||||||||||||||||||||||||||||||||||
| end function input_string | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+747
to
+764
|
||||||||||||||||||||||||||||||||||||||
| !> Version: experimental | |
| !> | |
| !> Read user input with an optional prompt into a string variable | |
| !> This function provides a Python-like input() interface for Fortran | |
| recursive function input_string(prompt, iostat) result(str) | |
| !> Optional prompt to display before reading input | |
| character(len=*), intent(in), optional :: prompt | |
| !> Optional status of operation | |
| integer, intent(out), optional :: iostat | |
| !> The input string read from the user | |
| type(string_type) :: str | |
| character(len=:), allocatable :: buffer | |
| ! Use the character version and convert to string_type | |
| buffer = input_char(prompt, iostat) | |
| str = string_type(buffer) | |
| end function input_string |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,66 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| module test_input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use stdlib_io, only: input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use testdrive, only: new_unittest, unittest_type, error_type, check | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| implicit none | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public :: collect_input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| contains | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Collect all exported unit tests | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| subroutine collect_input(testsuite) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Collection of tests | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type(unittest_type), allocatable, intent(out) :: testsuite(:) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testsuite = [ & | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new_unittest("input_basic", test_input_basic) & | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end subroutine collect_input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| subroutine test_input_basic(error) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Error handling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type(error_type), allocatable, intent(out) :: error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| character(len=:), allocatable :: result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ! Note: This is a basic structure for the test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ! Actual interactive testing would require input redirection or mocking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ! For now, we verify that the function signature is correct | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ! The function should be callable (this verifies compilation) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ! In a real test environment, you would redirect stdin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ! For now, just verify the module compiles and can be used | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| call check(error, .true., "input function is available") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+25
to
+35
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ! Note: This is a basic structure for the test | |
| ! Actual interactive testing would require input redirection or mocking | |
| ! For now, we verify that the function signature is correct | |
| ! The function should be callable (this verifies compilation) | |
| ! In a real test environment, you would redirect stdin | |
| ! For now, just verify the module compiles and can be used | |
| call check(error, .true., "input function is available") | |
| integer :: iu, ios | |
| ! Exercise stdlib_io:input by reading from a scratch unit instead of | |
| ! interactive stdin, so the test is deterministic and automated. | |
| ! | |
| ! 1. Write a line with leading and trailing whitespace to a scratch unit. | |
| ! 2. Rewind the unit and read it using input(unit=..., iostat=...). | |
| ! 3. Check that iostat is zero and that the whitespace is preserved. | |
| open(newunit=iu, status='scratch', action='readwrite') | |
| write(iu, '(a)') ' hello ' | |
| rewind(iu) | |
| result = input("prompt> ", unit=iu, iostat=ios) | |
| call check(error, ios == 0, "input: iostat should be zero on successful read") | |
| call check(error, result == ' hello ', "input: should preserve leading and trailing whitespace") | |
| close(iu) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this change be reverted (or why is it done)?