3 ! Tests functionality of recursive allocatable derived types.
7 type(recurses), allocatable :: left
8 type(recurses), allocatable :: right
9 integer, allocatable :: ia
12 ! Obtain checksum from "keys".
13 recursive function foo (this) result (res)
14 type(recurses) :: this
17 if (allocated (this%left)) res = res + foo (this%left)
18 if (allocated (this%right)) res = res + foo (this%right)
20 ! Return pointer to member of binary tree matching "key", null otherwise.
21 recursive function bar (this, key) result (res)
22 type(recurses), target :: this
23 type(recurses), pointer :: res
25 if (key .eq. this%ia) then
31 if (allocated (this%left)) res => bar (this%left, key)
32 if (associated (res)) return
33 if (allocated (this%right)) res => bar (this%right, key)
38 type(recurses), allocatable, target :: a
39 type(recurses), pointer :: b => NULL ()
41 ! Check chained allocation.
46 allocate (a%left%left)
48 allocate (a%left%right)
54 if (foo(a) .ne. 15) STOP 1
56 ! Return pointer to tree item that is present.
58 if (.not.associated (b) .or. (b%ia .ne. 3)) STOP 2
59 ! Return NULL to tree item that is not present.
61 if (associated (b)) STOP 3
63 ! Deallocate to check that there are no memory leaks.