File: nested_18.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 (72 lines) | stat: -rw-r--r-- 2,057 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
! Test for nested function global variables passes 
! with function calls in conditions
! Test for both, scalars and arrays
! Test 1: Do-while loop with function call in condition (3 iterations)
! Test 2: IF block with function call in condition

module nested_18_temp
  contains
    subroutine demo()
      implicit none
      integer :: a
      integer :: arr(5)
      integer :: counter

      ! Test 1: Do-While loop - runs 6 times
      ! Check Upodates from Main to Function
      ! And from function to Main
      a = 0
      arr(1:5) = 0
      counter = 0
      GET_ARGS: do while(increment())
        ! Check updates from function to main
        counter = counter + 1
        if (counter == 1 .and. a /= 1 .and. arr(1) /= 1) error stop
        if (counter == 3 .and. a /= 3 .and. arr(3) /= 3) error stop
        if (counter == 5 .and. a /= 5 .and. arr(5) /= 3) error stop
        print *, "Iteration", counter, ": a =", a, ": arr =", arr
        
        ! Update a inside main to check from main to function
        a = a + 1
        arr = arr + 1
        counter = counter + 1
        print *, "Iteration", counter, ": a =", a, ": arr =", arr
        if (counter == 6) exit GET_ARGS
      end do GET_ARGS
      print *, "a =", a
      print *, "arr =", arr
      if (a /= 6) error stop
      if (arr(1) /= 6) error stop

      ! Test 2: IF block with function call in condition
      a = 10
      if (set_value(25)) then
        print *, "In IF body: a =", a
        if (a /= 25) error stop
        a = a + 5
      end if
      print *, a
      if (a /= 30) error stop

      contains
      logical function increment()
        arr = arr + 1
        a = a + 1
        increment = .true.
      end function increment

      logical function set_value(val)
        integer, intent(in) :: val
        a = val
        set_value = .true.
      end function set_value

    end subroutine demo
end module nested_18_temp

program nested_18
  use nested_18_temp
  implicit none
  call demo()
  print *, "Nested variable test passed"
end program nested_18