Skip to content

Commit 958cb72

Browse files
committed
WIP(test): add shallow-copy usage test
Work in Progress ---------------- This new test exposes what is probably a compiler bug that causes a seg fault in the new test when a variable is passed to the veggies assert_that() procedure.
1 parent c576abe commit 958cb72

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

test/shallow_m.f90

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module shallow_m
2+
use reference_counter_m, only: ref_reference_t
3+
4+
implicit none
5+
private
6+
public :: shallow_t, resource_freed
7+
8+
type, extends(ref_reference_t) :: shallow_t
9+
integer, pointer :: ref => null()
10+
contains
11+
procedure :: shallow_copy
12+
generic :: assignment(=) => shallow_copy
13+
procedure :: free
14+
end type
15+
16+
interface shallow_t
17+
module procedure construct
18+
end interface
19+
20+
integer, allocatable, target, save :: resource
21+
logical, save :: resource_freed = .false.
22+
23+
contains
24+
function construct() result(shallow)
25+
type(shallow_t) :: shallow
26+
27+
resource = 42
28+
shallow%ref => resource
29+
call shallow%start_ref_counter
30+
end function
31+
32+
subroutine free(self)
33+
class(shallow_t), intent(inout) :: self
34+
35+
deallocate(resource)
36+
nullify(self%ref)
37+
resource_freed = .true.
38+
end subroutine
39+
40+
subroutine shallow_copy(lhs, rhs)
41+
class(shallow_t), intent(inout) :: lhs
42+
type(shallow_t), intent(in) :: rhs
43+
lhs%ref => rhs%ref
44+
end subroutine
45+
46+
end module

test/usage_test.f90

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module usage_test
99
describe, &
1010
fail, &
1111
it
12+
use shallow_m, only : shallow_t, resource_freed
1213

1314
implicit none
1415
private
@@ -47,10 +48,11 @@ function test_usage() result(tests)
4748
type(test_item_t) :: tests
4849

4950
tests = describe( &
50-
"Using a reference-counted object", &
51+
"A reference-counted object", &
5152
[ it("creates a resource when constructed", check_creation) &
52-
, it("removes the resource when it goes out of scope", check_deletion) &
53-
, it("a copy points to the same resource", check_copy) &
53+
, it("removes the resource when the object goes out of scope", check_deletion) &
54+
, it("copy points to the same resource as the original", check_copy) &
55+
, it("has zero references afrer a shallow copy goes out of scope", check_shallow_copy) &
5456
])
5557
end function
5658

@@ -87,4 +89,18 @@ function check_copy() result(result_)
8789
object2 = object1
8890
result_ = assert_that(associated(object2%ref, object1%ref))
8991
end function
92+
93+
function check_shallow_copy() result(result_)
94+
type(result_t) :: result_
95+
96+
block
97+
type(shallow_t) shallow_copy
98+
99+
associate(original => shallow_t())
100+
shallow_copy = original
101+
end associate
102+
end block
103+
104+
result_ = assert_that(resource_freed)
105+
end function
90106
end module

0 commit comments

Comments
 (0)