1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
module class_32_test_module
implicit none
type :: Composed
integer :: x
end type Composed
type :: Base
class(Composed), allocatable :: obj
end type
type :: Super
integer :: x
end type Super
type, extends(Super) :: Derived
integer :: y
end type
end module class_32_test_module
program class_32
use class_32_test_module
implicit none
class(Composed), allocatable :: c
class(Composed), allocatable :: d
class(Base), allocatable :: c_base
class(Base), allocatable :: d_base
class(Super), allocatable :: c_super
class(Derived), allocatable :: d_derived
! test case 1: assignment of class var to class var
allocate(c)
c%x = 1
print *, "c%x: ", c%x
allocate(d)
d%x = 42
print *, "d%x: ", d%x
c = d
print *, "c%x after assignment: ", c%x
if (c%x /= 42) error stop
d%x = 3
print *, "d%x: ", d%x
! verify deep copy
print *, "c%x: ", c%x
if (c%x == 3) error stop
! test case 2: assignment of class var to struct member class
allocate(c_base)
allocate(d_base)
allocate(c_base%obj) ! needed because lfortran does not automatically allocate this
allocate(d_base%obj) ! needed because lfortran does not automatically allocate this
c_base%obj = c
d_base%obj = d
print *, "c_base%obj%x: ", c_base%obj%x
if (c_base%obj%x /= 42) error stop
print *, "d_base%obj%x: ", d_base%obj%x
if (d_base%obj%x /= 3) error stop
c%x = 20
print *, "c%x: ", c%x
! verify deep copy
print *, "c_base%obj%x: ", c_base%obj%x
if (c_base%obj%x == 20) error stop
! test case 3: assignment of derived class var to base class var
allocate(c_super)
c_super%x = 1
print *, "c_super%x: ", c_super%x
allocate(d_derived)
d_derived%x = 42
print *, "d_derived%x: ", d_derived%x
c_super = d_derived
print *, "c_super%x after assignment: ", c_super%x
d_derived%x = 2
! verify deep copy
print *, "c_super%x after assignment: ", c_super%x
if (c_super%x == 2) error stop
end program class_32
|