Skip to content

Commit fb3fad4

Browse files
author
Damian Rouson
committed
feat: add test-oracle & command-line abstractions
1 parent da41774 commit fb3fad4

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
submodule(command_line_interface) command_line_implementation
2+
implicit none
3+
4+
contains
5+
6+
module procedure argument_present
7+
!! list of acceptable arguments
8+
!! sample list: [character(len=len(longest_argument)):: "--benchmark", "-b", "/benchmark", "/b"]
9+
!! where dashes support Linux/macOS and slashes support Windows
10+
integer :: i, argnum, arglen
11+
!! loop counter, argument position, argument length
12+
character(len=32) arg
13+
!! argument position
14+
15+
!! acceptable argument lengths (used to preclude extraneous trailing characters)
16+
17+
associate(acceptable_length => [(len(trim(acceptable_argument(i))), i = 1, size(acceptable_argument))])
18+
19+
found = .false.
20+
21+
do argnum = 1,command_argument_count()
22+
23+
call get_command_argument(argnum, arg, arglen)
24+
25+
if (any( &
26+
[(arg==acceptable_argument(i) .and. arglen==acceptable_length(i), i = 1, size(acceptable_argument))] &
27+
)) then
28+
found = .true.
29+
end if
30+
31+
end do
32+
33+
end associate
34+
35+
end procedure
36+
37+
end submodule

src/command_line_interface.f90

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module command_line_interface
2+
!! return command line argument information
3+
implicit none
4+
5+
private
6+
public :: command_line_t
7+
8+
type command_line_t
9+
contains
10+
procedure, nopass :: argument_present
11+
end type
12+
13+
interface
14+
15+
module function argument_present(acceptable_argument) result(found)
16+
!! result is .true. only if a command-line argument matches an element of this function's argument
17+
character(len=*), intent(in) :: acceptable_argument(:)
18+
!! sample list: [character(len=len(<longest_argument>)):: "--benchmark", "-b", "/benchmark", "/b"]
19+
!! where dashes support Linux/macOS, slashes support Windows, and <longest_argument> must be replaced
20+
!! by the longest list element ("--benchmark" above)
21+
logical found
22+
end function
23+
24+
end interface
25+
26+
end module

src/oracle_implementation.f90

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
submodule(oracle_interface) oracle_implementation
2+
!! define procedures corresponding to the interface bodies in oracle_interface
3+
implicit none
4+
5+
contains
6+
7+
module procedure within_tolerance
8+
class(oracle), allocatable :: error
9+
10+
error = this - reference
11+
in_tolerance = (error%norm() <= tolerance)
12+
13+
end procedure
14+
15+
end submodule oracle_implementation

src/oracle_interface.f90

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module oracle_interface
2+
!! verify actual output against expected
3+
use object_interface, only : object
4+
implicit none
5+
6+
private
7+
public :: oracle
8+
9+
type, abstract, extends(object) :: oracle
10+
!! define procedures for testing output values against expected values
11+
contains
12+
procedure(subtract_interface), deferred :: subtract
13+
procedure(norm_interface), deferred :: norm
14+
generic :: operator(-) => subtract
15+
procedure :: within_tolerance
16+
end type
17+
18+
abstract interface
19+
20+
function subtract_interface(this, rhs) result(difference)
21+
!! result has components corresponding to subtracting rhs's components fron this object's components
22+
import oracle
23+
implicit none
24+
class(oracle), intent(in) :: this, rhs
25+
class(oracle), allocatable :: difference
26+
end function
27+
28+
pure function norm_interface(this) result(norm_of_this)
29+
!! result is a norm of the array formed by concatenating the real components of this object
30+
import oracle
31+
implicit none
32+
class(oracle), intent(in) :: this
33+
real norm_of_this
34+
end function
35+
36+
end interface
37+
38+
interface
39+
40+
module function within_tolerance(this, reference, tolerance) result(in_tolerance)
41+
!! template method with true result iff the difference in state vectors (this - reference) has a norm within tolerance
42+
!! (impure because of internal call to 'subtract' binding)
43+
!! The existence of this procedure eliminates the need to rewrite similar code for every oracle child type.
44+
implicit none
45+
class(oracle), intent(in) :: this, reference
46+
real, intent(in) :: tolerance
47+
logical in_tolerance
48+
end function
49+
50+
end interface
51+
52+
end module

0 commit comments

Comments
 (0)