xgo/runtime/vm_for_test.go

239 lines
2.5 KiB
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, `
out = 0
for {
out++
if out == 5 {
break
}
}`, nil, 5)
expect(t, `
out = 0
for {
out++
if out == 5 {
break
}
}`, nil, 5)
expect(t, `
out = 0
a := 0
for {
a++
if a == 3 { continue }
if a == 5 { break }
out += a
}`, nil, 7) // 1 + 2 + 4
expect(t, `
out = 0
a := 0
for {
a++
if a == 3 { continue }
out += a
if a == 5 { break }
}`, nil, 12) // 1 + 2 + 4 + 5
2019-01-09 10:17:42 +03:00
expect(t, `
out = 0
2019-01-09 10:17:42 +03:00
for true {
out++
if out == 5 {
break
}
}`, nil, 5)
2019-01-09 10:17:42 +03:00
expect(t, `
a := 0
for true {
a++
if a == 5 {
break
}
}
out = a`, nil, 5)
expect(t, `
out = 0
a := 0
for true {
a++
if a == 3 { continue }
if a == 5 { break }
out += a
}`, nil, 7) // 1 + 2 + 4
expect(t, `
out = 0
a := 0
for true {
a++
if a == 3 { continue }
out += a
if a == 5 { break }
}`, nil, 12) // 1 + 2 + 4 + 5
2019-01-09 10:17:42 +03:00
expect(t, `
out = 0
2019-01-09 10:17:42 +03:00
func() {
for true {
out++
if out == 5 {
return
}
}
}()`, nil, 5)
2019-01-09 10:17:42 +03:00
expect(t, `
out = 0
for a:=1; a<=10; a++ {
2019-01-09 10:17:42 +03:00
out += a
}`, nil, 55)
2019-01-09 10:17:42 +03:00
expect(t, `
out = 0
for a:=1; a<=3; a++ {
for b:=3; b<=6; b++ {
2019-01-09 10:17:42 +03:00
out += b
}
}`, nil, 54)
2019-01-09 10:17:42 +03:00
expect(t, `
out = 0
func() {
for {
out++
if out == 5 {
break
}
}
}()`, nil, 5)
expect(t, `
out = 0
func() {
for true {
out++
if out == 5 {
break
}
}
}()`, nil, 5)
expect(t, `
out = func() {
a := 0
for {
a++
if a == 5 {
break
}
}
return a
}()`, nil, 5)
expect(t, `
out = func() {
a := 0
for true {
a++
if a== 5 {
break
}
}
return a
}()`, nil, 5)
expect(t, `
out = func() {
a := 0
func() {
for {
a++
if a == 5 {
break
}
}
}()
return a
}()`, nil, 5)
expect(t, `
out = func() {
a := 0
func() {
for true {
a++
if a == 5 {
break
}
}
}()
return a
}()`, nil, 5)
2019-01-09 10:17:42 +03:00
expect(t, `
out = func() {
sum := 0
for a:=1; a<=10; a++ {
2019-01-09 10:17:42 +03:00
sum += a
}
return sum
}()`, nil, 55)
2019-01-09 10:17:42 +03:00
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
}()`, nil, 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`, nil, 5)
2019-01-09 10:17:42 +03:00
expect(t, `
out = 0
for a:=1; a<=10; a++ {
2019-01-09 10:17:42 +03:00
if a == 3 {
continue
}
out += a
if a == 5 {
break
}
}`, nil, 12) // 1 + 2 + 4 + 5
2019-01-09 10:17:42 +03:00
expect(t, `
out = 0
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++
}`, nil, 12) // 1 + 2 + 4 + 5
2019-01-09 10:17:42 +03:00
}