aboutsummaryrefslogtreecommitdiff
path: root/tests/control_flow.nom
blob: 4fed7b463626b5be8025ab6cad47240715966dfc (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#..
    Tests for the stuff defined in lib/control_flow.nom

use "core"

do nothing

action [test conditionals]
    if: yes
        %loc1 <- (yes)
    if: no
        barf "entered if 'no' conditional"

    unless: yes
        barf "entered unless 'yes' conditional"

    if: yes
        %loc2 <- (yes)
    ..else
        barf "entered if 'yes' else conditional"

    unless: no
        %loc3 <- (yes)
    ..else
        barf "entered unless 'no' else conditional"

assume (all of [%loc1 = (nil), %loc2 = (nil), %loc3 = (nil)]) or barf "conditionals leaking locals"

assume ((5 if (yes) else 1) = 5)
assume ((5 if (no) else 1) = 1)
action [return nil]: return (nil)
assume (((return nil) if (yes) else 99) = (nil))

go to %skip
barf "go-to failed."
--- %skip ---

%tot <- 0
for %x in [1,2,3]
    %tot +<- %x
assume (%tot = 6) or barf "for-loop failed"

%tot <- 0
for all [1,2,3]
    %tot +<- %
assume (%tot = 6) or barf "for-all-loop failed"

%x <- 0
repeat
    %x +<- 1
    if (%x = 3): stop repeating
    if (%x > 3): barf "Failed to stop repeat loop"
assume (%x = 3) or barf "Failed to repeat"

%x <- 0
repeat 5 times
    %x +<- 1
assume (%x = 5) or barf "Failed to repeat 5 times"

<- {%x:0,%y:0}
for all [1,2,3]
    repeat 5 times
        do next repeat
        %x +<- 1
    %y +<- 1
assume ([%x,%y] = [0,3]) or barf "Failed to continue repeat"

<- {%x:0,%y:0}
for all [1,2,3]
    repeat 5 times
        do next %
        %x +<- 1
    %y +<- 1
assume ([%x,%y] = [0,0]) or barf "Failed to continue for"

<- {%x:0,%y:0}
for all [1,2,3]
    repeat 5 times
        stop repeating
        %x +<- 1
    %y +<- 1
assume ([%x,%y] = [0,3]) or barf "Failed to stop repeat"

<- {%x:0,%y:0}
for all [1,2,3]
    repeat 5 times
        stop %
        %x +<- 1
    %y +<- 1
assume ([%x,%y] = [0,0]) or barf "Failed to stop for"

%x <- 0
repeat while: %x < 10
    %x +<- 1
assume (%x = 10) or barf "repeat-while failed"

%x <- 0
repeat until: %x = 10
    %x +<- 1
assume (%x = 10) or barf "repeat-until failed"

%x <- 0
for %i from 1 to 3: %x +<- %i
assume (%x = 6) or barf "Numeric for range failed"

%x <- 0
for %i from 3 to 1 via -1: %x +<- %i
assume (%x = 6) or barf "backwards numeric for range failed"

%x <- 0
for all 1 to 3: %x +<- %
assume (%x = 6) or barf "terse numeric for range failed"

%result <- {}
for %key = %value in {x:1,y:2}
    (%result's ("\%key\%key")) <- (%value * 11)
assume (%result = {xx:11,yy:22}) or barf "key/value iteration failed"

for %key = %value in {x:1,y:2}
    stop %key
    barf "stopping key failed"

for %key = %value in {x:1,y:2}
    stop %value
    barf "stopping value failed"

for %key = %value in {x:1,y:2}
    do next %key
    barf "skipping key failed"

for %key = %value in {x:1,y:2}
    do next %value
    barf "skipping value failed"

action [barfer]: barf "this should never be reached"
when
    * (no): barf "'when' fail"
    * (no)
    * (3 > 4): barf "'when' fail 2"
    * (yes)
    * (barfer): do nothing
    * (99 > 1): barf "Fell through incorrectly"

%else_worked <- (no)
when
    * (no): barf
    * else: %else_worked <- (yes)
assume %else_worked or barf "when..else failed"

action [test when scope]
    when
        * (yes): %leaked <- (yes)
test when scope
assume (not %leaked) or barf "'when' is leaking locals"

%when_worked <- (no)
when 4 = ?
    * 1
    * 2: barf "'when = ?' fail"
    * 3
    * 4
    * (barfer): %when_worked <- (yes)
assume %when_worked

%when_worked <- (no)
when 5 = ?
    * 6: barf
    * else: %when_worked <- (yes)
assume %when_worked

try: barf
..and if it succeeds: barf "try failed."

%worked <- (no)
try: barf
..and if it barfs: %worked <- (yes)
assume %worked or barf "try/catch failed"

%x <- 1
do
    %x <- 2
assume (%x = 2) or barf "'do' is redefining locals"

%x <- 1
try
    %x <- 2
    do
        barf
    ..then always
        %x <- 1
..and if it barfs: do nothing
assume (%x = 1) or barf "do/then always failed"

say "Control flow test passed."

assume
    (..)
        result of
            %n <- 0
            for all [1,2,3]: %n +<- %
            return %n
    ..= 6