Wiki

root/cobra/trunk/Tests/100-basics/122-for-numeric.cobra

Revision 2636, 2.6 KB (checked in by Charles.Esterbrook, 7 weeks ago)

JVM back-end progress.
credit:hopscc

  • Property svn:eol-style set to native
Line 
1# numeric for loops follow the half-open interval 0 .. N-1 which lines up with the zero indexing used in .NET for arrays, lists and strings.
2# see also Dijkstra's comments at http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF
3namespace Test
4
5    class Test
6
7        def main
8            is shared
9
10            r as int = 0
11            count as int = 0
12
13            # the usual for numeric:
14            count = 0
15            for x as int in 0 : 100
16                r = x
17                count += 1
18            assert r == 99
19            assert x == 100
20            assert count == 100
21
22            # with a step
23            count = 0
24            for x as int in 0 : 100 : 2
25                r = x
26                count += 1
27            assert r == 98
28            assert x == 100
29            assert count == 50
30
31            count = 0
32            for x as int in 1 : 100 : 2
33                r = x
34                count += 1
35            assert r == 99
36            assert count == 50
37
38            # reverse order
39            count = 0
40            for x in 100 : 0 : -1
41                r = x
42                count += 1
43            assert r == 1
44            assert count == 100
45
46            # by 2
47            count = 0
48            for x as int in 100 : 0 : -2
49                r = x
50                count += 1
51            assert r == 2
52            assert count == 50
53
54            # continue
55            count = 0
56            for x in 1 : 10
57                r = x
58                if x, continue # need conditional, some platforms err on dead code
59                count += 1
60            assert r == 9
61            assert count == 0
62
63            # reverse, but no step specified
64            count = 0
65            r = -1
66            for x in 10 : 2
67                r = x
68                count += 1
69            assert count == 0 and r == -1 and x == 10
70
71            # reverse, but positive step specified
72            count = 0
73            r = -1
74            for x in 10 : 0 : 2
75                r = x
76                count += 1
77            assert count == 0 and r == -1 and x == 10
78
79            # one value implies start of 0
80            count = 0
81            for x in 10
82                r = x
83                count += 1
84            assert count == 10 and r == 9 and x == 10
85
86            # using variables instead of constants
87            i as int = 5
88            j as int = 10
89            count = 0
90            for x in i : j
91                r = x
92                count += 1
93            assert count == 5 and r == 9 and x == 10
94
95            # step it back
96            i = 10
97            j = 5
98            stepp = -2
99            count = 0
100            for x in i : j : stepp
101                r = x
102                count += 1
103            assert count == 3 and r == 6 and x == 4
104
105            # the boundaries get evaluated once
106            i = 5
107            j = 55
108            stepp = 2
109            count = 0
110            for x in i : j : stepp
111                r = x
112                count += 1
113                j = 10
114                stepp = 4
115            assert count == 25 and r == 53 and x == 55
116
117            # special test: node transformation inside a block
118            if true
119                count = 0
120                for x in 10
121                    r = x
122                    count += 1
123                assert count == 10 and r == 9 and x == 10
124
125            Test().more
126
127        def more
128            i = 1
129            # reuse a local
130            for i in 1 : 10
131                assert i
132            assert i == 10
133
134            .foo(5)
135            .bar
136
137        def foo(x as int)
138            assert x == 5
139            # reuse an arg:
140            for x in 1 : 10
141                assert x
142            assert x == 10
143
144        var _b as int
145
146        def bar
147            assert _b == 0
148            # reuse a class var:
149            for _b in 1 : 10
150                assert _b
151            assert _b == 10
Note: See TracBrowser for help on using the browser.