File: class_32.f90

package info (click to toggle)
lfortran 0.59.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 56,736 kB
  • sloc: cpp: 168,052; f90: 74,272; python: 17,537; ansic: 7,705; yacc: 2,345; sh: 1,334; fortran: 895; makefile: 37; javascript: 15
file content (102 lines) | stat: -rw-r--r-- 2,077 bytes parent folder | download | duplicates (3)
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