1! { dg-do run }
2! { dg-options "-std=legacy -ffree-line-length-none -fno-range-check -fwrapv -Wzerotrip" }
3program test
4  integer :: count
5  integer :: i
6  integer(kind=1) :: i1
7  real :: r
8
9#define TEST_LOOP(var,from,to,step,total,test,final) \
10  count = 0 ; do var = from, to, step ; count = count + 1 ; end do ; \
11  if (count /= total) call abort ; \
12  if (test (from, to, step, final) /= total) call abort
13
14  ! Integer loops
15  TEST_LOOP(i, 0, 0, 1, 1, test_i, 1)
16  TEST_LOOP(i, 0, 0, 2, 1, test_i, 2)
17  TEST_LOOP(i, 0, 0, -1, 1, test_i, -1)
18  TEST_LOOP(i, 0, 0, -2, 1, test_i, -2)
19
20  TEST_LOOP(i, 0, 1, 1, 2, test_i, 2)
21  TEST_LOOP(i, 0, 1, 2, 1, test_i, 2)
22  TEST_LOOP(i, 0, 1, 3, 1, test_i, 3)
23  TEST_LOOP(i, 0, 1, huge(0), 1, test_i, huge(0))
24  TEST_LOOP(i, 0, 1, -1, 0, test_i, 0) ! { dg-warning "executed zero times" }
25  TEST_LOOP(i, 0, 1, -2, 0, test_i, 0) ! { dg-warning "executed zero times" }
26  TEST_LOOP(i, 0, 1, -3, 0, test_i, 0) ! { dg-warning "executed zero times" }
27  TEST_LOOP(i, 0, 1, -huge(0), 0, test_i, 0) ! { dg-warning "executed zero times" }
28  TEST_LOOP(i, 0, 1, -huge(0)-1, 0, test_i, 0) ! { dg-warning "executed zero times" }
29
30  TEST_LOOP(i, 1, 0, 1, 0, test_i, 1) ! { dg-warning "executed zero times" }
31  TEST_LOOP(i, 1, 0, 2, 0, test_i, 1) ! { dg-warning "executed zero times" }
32  TEST_LOOP(i, 1, 0, 3, 0, test_i, 1) ! { dg-warning "executed zero times" }
33  TEST_LOOP(i, 1, 0, huge(0), 0, test_i, 1) ! { dg-warning "executed zero times" }
34  TEST_LOOP(i, 1, 0, -1, 2, test_i, -1)
35  TEST_LOOP(i, 1, 0, -2, 1, test_i, -1)
36  TEST_LOOP(i, 1, 0, -3, 1, test_i, -2)
37  TEST_LOOP(i, 1, 0, -huge(0), 1, test_i, 1-huge(0))
38  TEST_LOOP(i, 1, 0, -huge(0)-1, 1, test_i, -huge(0))
39
40  TEST_LOOP(i, 0, 17, 1, 18, test_i, 18)
41  TEST_LOOP(i, 0, 17, 2, 9, test_i, 18)
42  TEST_LOOP(i, 0, 17, 3, 6, test_i, 18)
43  TEST_LOOP(i, 0, 17, 4, 5, test_i, 20)
44  TEST_LOOP(i, 0, 17, 5, 4, test_i, 20)
45  TEST_LOOP(i, 17, 0, -1, 18, test_i, -1)
46  TEST_LOOP(i, 17, 0, -2, 9, test_i, -1)
47  TEST_LOOP(i, 17, 0, -3, 6, test_i, -1)
48  TEST_LOOP(i, 17, 0, -4, 5, test_i, -3)
49  TEST_LOOP(i, 17, 0, -5, 4, test_i, -3)
50
51  TEST_LOOP(i1, -huge(i1)-1_1, huge(i1), 1_1, int(huge(i1))*2+2, test_i1, huge(i1)+1_1)
52  TEST_LOOP(i1, -huge(i1)-1_1, huge(i1), 2_1, int(huge(i1))+1, test_i1, huge(i1)+1_1)
53  TEST_LOOP(i1, -huge(i1)-1_1, huge(i1), huge(i1), 3, test_i1, 2_1*huge(i1)-1_1)
54
55  TEST_LOOP(i1, huge(i1), -huge(i1)-1_1, -1_1, int(huge(i1))*2+2, test_i1, -huge(i1)-2_1)
56  TEST_LOOP(i1, huge(i1), -huge(i1)-1_1, -2_1, int(huge(i1))+1, test_i1, -huge(i1)-2_1)
57  TEST_LOOP(i1, huge(i1), -huge(i1)-1_1, -huge(i1), 3, test_i1, -2_1*huge(i1))
58  TEST_LOOP(i1, huge(i1), -huge(i1)-1_1, -huge(i1)-1_1, 2, test_i1, -huge(i1)-2_1)
59
60  TEST_LOOP(i1, -2_1, 3_1, huge(i1), 1, test_i1, huge(i1)-2_1)
61  TEST_LOOP(i1, -2_1, 3_1, -huge(i1), 0, test_i1, -2_1) ! { dg-warning "executed zero times" }
62  TEST_LOOP(i1, 2_1, -3_1, -huge(i1), 1, test_i1, 2_1-huge(i1))
63  TEST_LOOP(i1, 2_1, -3_1, huge(i1), 0, test_i1, 2_1) ! { dg-warning "executed zero times" }
64
65  ! Real loops
66  TEST_LOOP(r, 0.0, 1.0, 0.11, 1 + int(1.0/0.11), test_r, 0.0)
67  TEST_LOOP(r, 0.0, 1.0, -0.11, 0, test_r, 0.0) ! { dg-warning "executed zero times" }
68  TEST_LOOP(r, 0.0, -1.0, 0.11, 0, test_r, 0.0) ! { dg-warning "executed zero times" }
69  TEST_LOOP(r, 0.0, -1.0, -0.11, 1 + int(1.0/0.11), test_r, 0.0)
70  TEST_LOOP(r, 0.0, 0.0, 0.11, 1, test_r, 0.0)
71  TEST_LOOP(r, 0.0, 0.0, -0.11, 1, test_r, 0.0)
72
73#undef TEST_LOOP
74
75contains
76
77  function test_i1 (from, to, step, final) result(res)
78    integer(kind=1), intent(in) :: from, to, step, final
79    integer(kind=1) :: i
80    integer :: res
81
82    res = 0
83    do i = from, to, step
84      res = res + 1
85    end do
86    if (i /= final) call abort
87  end function test_i1
88
89  function test_i (from, to, step, final) result(res)
90    integer, intent(in) :: from, to, step, final
91    integer :: i
92    integer :: res
93
94    res = 0
95    do i = from, to, step
96      res = res + 1
97    end do
98    if (i /= final) call abort
99  end function test_i
100
101  function test_r (from, to, step, final) result(res)
102    real, intent(in) :: from, to, step, final
103    real :: i
104    integer :: res
105
106    res = 0
107    do i = from, to, step
108      res = res + 1
109    end do
110    ! final is ignored
111  end function test_r
112
113end program test
114