xgo/runtime/vm_for_test.go

91 lines
999 B
Go
Raw Normal View History

2019-01-11 13:27:28 +03:00
package runtime_test
2019-01-09 10:17:42 +03:00
import (
"testing"
)
func TestFor(t *testing.T) {
expect(t, `
for true {
out++
if out == 5 {
break
}
}`, 5)
expect(t, `
func() {
for true {
out++
if out == 5 {
return
}
}
}()`, 5)
expect(t, `
for a:=1; a<=10; a++ {
2019-01-09 10:17:42 +03:00
out += a
}`, 55)
expect(t, `
for a:=1; a<=3; a++ {
for b:=3; b<=6; b++ {
2019-01-09 10:17:42 +03:00
out += b
}
}`, 54)
expect(t, `
out = func() {
sum := 0
for a:=1; a<=10; a++ {
2019-01-09 10:17:42 +03:00
sum += a
}
return sum
}()`, 55)
expect(t, `
out = func() {
sum := 0
for a:=1; a<=4; a++ {
for b:=3; b<=5; b++ {
2019-01-09 10:17:42 +03:00
sum += b
}
}
return sum
}()`, 48) // (3+4+5) * 4
2019-01-09 10:17:42 +03:00
expect(t, `
a := 1
2019-01-09 10:17:42 +03:00
for ; a<=10; a++ {
if a == 5 {
break
}
}
out = a`, 5)
expect(t, `
for a:=1; a<=10; a++ {
2019-01-09 10:17:42 +03:00
if a == 3 {
continue
}
out += a
if a == 5 {
break
}
}`, 12) // 1 + 2 + 4 + 5
expect(t, `
for a:=1; a<=10; {
2019-01-09 10:17:42 +03:00
if a == 3 {
a++
2019-01-09 10:17:42 +03:00
continue
}
out += a
if a == 5 {
break
}
a++
}`, 12) // 1 + 2 + 4 + 5
}