-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from sourceryinstitute/shallow-copy-test
Shallow copy test
- Loading branch information
Showing
9 changed files
with
206 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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 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 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,57 @@ | ||
module finalizable_m | ||
!! This module supports the specification_expression_finalization main program | ||
!! (at the bottom of this file), which in turn supports the check_specification_expression | ||
!! unit-test function in ../test/compiler_test.f90. | ||
implicit none | ||
|
||
private | ||
public :: finalizable_t, component | ||
|
||
type finalizable_t | ||
private | ||
integer, pointer :: component_ => null() | ||
contains | ||
final :: finalize | ||
end Type | ||
|
||
interface finalizable_t | ||
module procedure construct | ||
end interface | ||
|
||
contains | ||
|
||
pure function construct(component) result(finalizable) | ||
integer, intent(in) :: component | ||
type(finalizable_t) finalizable | ||
allocate(finalizable%component_, source = component) | ||
end function | ||
|
||
pure function component(self) result(self_component) | ||
type(finalizable_t), intent(in) :: self | ||
integer self_component | ||
if (.not. associated(self%component_)) error stop "component: unassociated component" | ||
self_component = self%component_ | ||
end function | ||
|
||
pure subroutine finalize(self) | ||
type(finalizable_t), intent(inout) :: self | ||
if (associated(self%component_)) deallocate(self%component_) | ||
error stop "finalize: intentional error termination to verify finalization" | ||
end subroutine | ||
|
||
end module | ||
|
||
program specification_expression_finalization | ||
!! Test the finalization of a function result in a specification expression | ||
use finalizable_m, only : finalizable_t, component | ||
implicit none | ||
|
||
call finalize_specification_expression_result | ||
|
||
contains | ||
|
||
subroutine finalize_specification_expression_result | ||
real tmp(component(finalizable_t(component=0))) !! Finalizes the finalizable_t function result | ||
end subroutine | ||
|
||
end program |
This file contains 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 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,41 @@ | ||
Compiler Support Status | ||
======================= | ||
|
||
This directory contains two categories of unit tests separated into two files: | ||
|
||
* `usage_test.f90` exercises the reference-counter library, whereas | ||
* `compiler_test.f90` tests the compiler without using reference-counter, | ||
|
||
`compiler_test.f90` verifies that a compiler calls a type's final subroutine | ||
in each of the scenarios in which the Fortran standard requires finalization | ||
to occur. | ||
|
||
`nagfor` 7.1.0 | ||
-------------- | ||
:trophy: The Numerical Algorithms Group (NAG) Fortran [compiler] passes all | ||
reference-counter tests. | ||
|
||
`gfortran` 12.2.0 | ||
----------------- | ||
Because the first usage test listed below causes a segmentation fault, | ||
obtaining the `gfortran` test results requires skipping that test by, | ||
for example, running running individual tests as follows: | ||
``` | ||
fpm test -- -f "<description>" | ||
``` | ||
Replace <description> above with one of the enumerated test descriptions | ||
below or with a corresponding substring not contained in the first usage | ||
test description. | ||
|
||
### Failing checks in `compiler_test.f90` | ||
1. finalizes a non-allocatable object on the LHS of an intrinsic assignment | ||
2. finalizes an allocated allocatable LHS of an intrinsic assignment | ||
3. finalizes a function reference on the RHS of an intrinsic assignment | ||
4. finalizes a function reference on the RHS of an intrinsic assignment | ||
5. finalizes a specification expression function result | ||
|
||
### Failing checks in `usage_test.f90` | ||
1. copy points to the same resource as the original | ||
2. has zero references after a shallow copy goes out of scope | ||
|
||
[compiler]: https://www.nag.com/content/nag-fortran-compiler |
This file contains 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 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 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,38 @@ | ||
module shallow_m | ||
use reference_counter_m, only: ref_reference_t | ||
|
||
implicit none | ||
private | ||
public :: shallow_t, resource_freed | ||
|
||
type, extends(ref_reference_t) :: shallow_t | ||
integer, pointer :: ref => null() | ||
contains | ||
procedure :: free | ||
end type | ||
|
||
interface shallow_t | ||
module procedure construct | ||
end interface | ||
|
||
integer, allocatable, target, save :: resource | ||
logical, save :: resource_freed = .false. | ||
|
||
contains | ||
function construct() result(shallow) | ||
type(shallow_t) :: shallow | ||
|
||
resource = 42 | ||
shallow%ref => resource | ||
call shallow%start_ref_counter | ||
end function | ||
|
||
subroutine free(self) | ||
class(shallow_t), intent(inout) :: self | ||
|
||
deallocate(resource) | ||
nullify(self%ref) | ||
resource_freed = .true. | ||
end subroutine | ||
|
||
end module |
Oops, something went wrong.