Skip to content

Commit 8603221

Browse files
author
Damian Rouson
authored
Merge pull request #11 from sourceryinstitute/add-uddtio-in-assert
feat(assert): add "object" class type guard
2 parents 1e36371 + a0b2f93 commit 8603221

File tree

4 files changed

+114
-20
lines changed

4 files changed

+114
-20
lines changed

src/assertions_implementation.F90

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
module procedure assert
1313
use iso_fortran_env, only : error_unit
1414
use string_functions_interface, only : string
15+
use object_interface, only : object
1516

1617
character(len=:), allocatable :: header, trailer
1718
integer, parameter :: max_this_image_digits=9
@@ -32,21 +33,25 @@
3233
else
3334

3435
block
35-
character(len=*), parameter :: lede = "with diagnostic data"
36+
character(len=*), parameter :: prefix = "with diagnostic data"
37+
integer, parameter :: max_data_length = 1024
3638

3739
select type(diagnostic_data)
3840
type is(character(len=*))
39-
trailer = lede // diagnostic_data
41+
trailer = prefix // diagnostic_data
4042
type is(integer)
41-
trailer = lede // string(diagnostic_data)
43+
trailer = prefix // string(diagnostic_data)
44+
class is(object)
45+
trailer = repeat(" ", ncopies = max_data_length)
46+
write(trailer,*) diagnostic_data
4247
class default
43-
trailer = lede // 'of unsupported type'
48+
trailer = prefix // 'of unsupported type'
4449
end select
4550
end block
4651

4752
end if
4853

49-
error stop header // trailer
54+
error stop header // trim(trailer)
5055

5156
end if
5257

src/object_interface.f90

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
!! author: Damian Rouson, GSE LLC
2-
!! category: Morfeus-FD
3-
!! summary: Abstract base type, `object`
4-
!!
5-
!! ### Copyright notice
6-
!!
7-
!! ```
8-
!! (c) 2019-2020 Guide Star Engineering, LLC
9-
!! This Software was developed for the US Nuclear Regulatory Commission (US NRC) under contract
10-
!! "Multi-Dimensional Physics Implementation into Fuel Analysis under Steady-state and Transients (FAST)",
11-
!! contract # NRC-HQ-60-17-C-0007
12-
!! ```
13-
1+
! ### Copyright notice
2+
!
3+
! ```
4+
! (c) 2019-2020 Guide Star Engineering, LLC
5+
! This Software was developed for the US Nuclear Regulatory Commission (US NRC) under contract
6+
! "Multi-Dimensional Physics Implementation into Fuel Analysis under Steady-state and Transients (FAST)",
7+
! contract # NRC-HQ-60-17-C-0007
8+
! ```
149
module object_interface
1510
implicit none
1611

@@ -31,6 +26,8 @@ module object_interface
3126
contains
3227
procedure :: mark_as_defined
3328
procedure :: user_defined
29+
procedure(write_interface), deferred :: write_formatted
30+
generic :: write(formatted) => write_formatted
3431
end type
3532

3633
interface
@@ -50,4 +47,16 @@ pure module function user_defined(this) result(is_defined)
5047

5148
end interface
5249

50+
abstract interface
51+
subroutine write_interface(self, unit, iotype, v_list, iostat, iomsg)
52+
import object
53+
class(object), intent(in) :: self
54+
integer, intent(in) :: unit
55+
character(*), intent(in) :: iotype
56+
integer, intent(in) :: v_list(:)
57+
integer, intent(out) :: iostat
58+
character(*), intent(inout) :: iomsg
59+
end subroutine
60+
end interface
61+
5362
end module object_interface

tests/main.f90

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ subroutine run()
1010
collective_subroutines_co_sum => test_co_sum
1111
use data_partition_test, only: &
1212
data_partition_data_partition => test_data_partition
13+
use object_interface_test, only: &
14+
object_interface_object => test_object
1315
use single_image_intrinsics_test, only: &
1416
single_image_intrinsics_findloc => test_findloc
1517
use vegetables, only: test_item_t, test_that, run_tests
1618

1719
type(test_item_t) :: tests
18-
type(test_item_t) :: individual_tests(4)
20+
type(test_item_t) :: individual_tests(5)
1921

2022
individual_tests(1) = collective_subroutines_co_all()
2123
individual_tests(2) = collective_subroutines_co_sum()
2224
individual_tests(3) = data_partition_data_partition()
23-
individual_tests(4) = single_image_intrinsics_findloc()
25+
individual_tests(4) = object_interface_object()
26+
individual_tests(5) = single_image_intrinsics_findloc()
2427
tests = test_that(individual_tests)
2528

2629
call run_tests(tests)

tests/object_interface_test.f90

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
module object_interface_test
2+
!! author: Damian Rouson
3+
!!
4+
!! summary: verify object pattern asbtract parent
5+
use vegetables, only: &
6+
result_t, input_t, integer_input_t, test_item_t, & ! types
7+
describe, it, assert_equals, assert_that, assert_not ! functions
8+
use object_interface, only : object
9+
implicit none
10+
11+
private
12+
public :: test_object
13+
14+
type, extends(object) :: subject
15+
contains
16+
procedure write_formatted
17+
end type
18+
19+
contains
20+
21+
function test_object() result(tests)
22+
type(test_item_t) tests
23+
24+
tests = describe( &
25+
"object class", &
26+
[it( &
27+
".not. user_defined() if only default-initialized", &
28+
check_default_initialization), &
29+
it( &
30+
"user_defined() after call mark_as_defined", &
31+
check_mark_as_defined)])
32+
end function
33+
34+
function check_default_initialization() result(result_)
35+
!! Verify that user_defined() is .false. for a default-initialied object
36+
class(object), allocatable :: object_
37+
type(result_t) result_
38+
39+
allocate(subject :: object_)
40+
41+
result_ = assert_not(object_%user_defined())
42+
end function
43+
44+
function check_mark_as_defined() result(result_)
45+
!! Verify that mark_as_defined results in user_defined() being .true.
46+
class(object), allocatable :: object_
47+
type(result_t) result_
48+
49+
allocate(subject :: object_)
50+
51+
call object_%mark_as_defined
52+
result_ = assert_that(object_%user_defined())
53+
end function
54+
55+
subroutine write_formatted(self, unit, iotype, v_list, iostat, iomsg)
56+
class(subject), intent(in) :: self
57+
integer, intent(in) :: unit
58+
character(*), intent(in) :: iotype
59+
integer, intent(in) :: v_list(:)
60+
integer, intent(out) :: iostat
61+
character(*), intent(inout) :: iomsg
62+
63+
select case(iotype)
64+
case('LISTDIRECTED')
65+
write(unit,*) self%user_defined()
66+
iostat = 0
67+
iomsg = ""
68+
case default
69+
iostat = -1
70+
iomsg = "object_interface_test: subject%write_formatted iotype received unsupported iotype " // iotype
71+
end select
72+
73+
associate( unused => v_list)
74+
end associate
75+
end subroutine
76+
77+
end module object_interface_test

0 commit comments

Comments
 (0)