]> Cypherpunks.ru repositories - gostls13.git/blobdiff - test/range.go
cmd/compile/internal/inline: score call sites exposed by inlines
[gostls13.git] / test / range.go
index 8effbe9c53a990decbece1baf23222672203f5c8..3da7d170b58634ae907358f184b1ba15e689325e 100644 (file)
@@ -23,15 +23,68 @@ func seq(lo, hi int) chan int {
        return c
 }
 
+const alphabet = "abcdefghijklmnopqrstuvwxyz"
+
+func testblankvars() {
+       n := 0
+       for range alphabet {
+               n++
+       }
+       if n != 26 {
+               println("for range: wrong count", n, "want 26")
+               panic("fail")
+       }
+       n = 0
+       for _ = range alphabet {
+               n++
+       }
+       if n != 26 {
+               println("for _ = range: wrong count", n, "want 26")
+               panic("fail")
+       }
+       n = 0
+       for _, _ = range alphabet {
+               n++
+       }
+       if n != 26 {
+               println("for _, _ = range: wrong count", n, "want 26")
+               panic("fail")
+       }
+       s := 0
+       for i, _ := range alphabet {
+               s += i
+       }
+       if s != 325 {
+               println("for i, _ := range: wrong sum", s, "want 325")
+               panic("fail")
+       }
+       r := rune(0)
+       for _, v := range alphabet {
+               r += v
+       }
+       if r != 2847 {
+               println("for _, v := range: wrong sum", r, "want 2847")
+               panic("fail")
+       }
+}
+
 func testchan() {
        s := ""
        for i := range seq('a', 'z') {
                s += string(i)
        }
-       if s != "abcdefghijklmnopqrstuvwxyz" {
+       if s != alphabet {
                println("Wanted lowercase alphabet; got", s)
                panic("fail")
        }
+       n := 0
+       for range seq('a', 'z') {
+               n++
+       }
+       if n != 26 {
+               println("testchan wrong count", n, "want 26")
+               panic("fail")
+       }
 }
 
 // test that range over slice only evaluates
@@ -87,6 +140,46 @@ func testslice1() {
        }
 }
 
+func testslice2() {
+       n := 0
+       nmake = 0
+       for range makeslice() {
+               n++
+       }
+       if nmake != 1 {
+               println("range called makeslice", nmake, "times")
+               panic("fail")
+       }
+       if n != 5 {
+               println("wrong count ranging over makeslice", n)
+               panic("fail")
+       }
+}
+
+// test that range over []byte(string) only evaluates
+// the expression after "range" once.
+
+func makenumstring() string {
+       nmake++
+       return "\x01\x02\x03\x04\x05"
+}
+
+func testslice3() {
+       s := byte(0)
+       nmake = 0
+       for _, v := range []byte(makenumstring()) {
+               s += v
+       }
+       if nmake != 1 {
+               println("range called makenumstring", nmake, "times")
+               panic("fail")
+       }
+       if s != 15 {
+               println("wrong sum ranging over []byte(makenumstring)", s)
+               panic("fail")
+       }
+}
+
 // test that range over array only evaluates
 // the expression after "range" once.
 
@@ -127,6 +220,22 @@ func testarray1() {
        }
 }
 
+func testarray2() {
+       n := 0
+       nmake = 0
+       for range makearray() {
+               n++
+       }
+       if nmake != 1 {
+               println("range called makearray", nmake, "times")
+               panic("fail")
+       }
+       if n != 5 {
+               println("wrong count ranging over makearray", n)
+               panic("fail")
+       }
+}
+
 func makearrayptr() *[5]int {
        nmake++
        return &[5]int{1, 2, 3, 4, 5}
@@ -176,6 +285,22 @@ func testarrayptr1() {
        }
 }
 
+func testarrayptr2() {
+       n := 0
+       nmake = 0
+       for range makearrayptr() {
+               n++
+       }
+       if nmake != 1 {
+               println("range called makearrayptr", nmake, "times")
+               panic("fail")
+       }
+       if n != 5 {
+               println("wrong count ranging over makearrayptr", n)
+               panic("fail")
+       }
+}
+
 // test that range over string only evaluates
 // the expression after "range" once.
 
@@ -198,6 +323,26 @@ func teststring() {
                println("wrong sum ranging over makestring", s)
                panic("fail")
        }
+
+       x := []rune{'a', 'b'}
+       i := 1
+       for i, x[i] = range "c" {
+               break
+       }
+       if i != 0 || x[0] != 'a' || x[1] != 'c' {
+               println("wrong parallel assignment", i, x[0], x[1])
+               panic("fail")
+       }
+
+       y := []int{1, 2, 3}
+       r := rune(1)
+       for y[r], r = range "\x02" {
+               break
+       }
+       if r != 2 || y[0] != 1 || y[1] != 0 || y[2] != 3 {
+               println("wrong parallel assignment", r, y[0], y[1], y[2])
+               panic("fail")
+       }
 }
 
 func teststring1() {
@@ -216,6 +361,22 @@ func teststring1() {
        }
 }
 
+func teststring2() {
+       n := 0
+       nmake = 0
+       for range makestring() {
+               n++
+       }
+       if nmake != 1 {
+               println("range called makestring", nmake, "times")
+               panic("fail")
+       }
+       if n != 5 {
+               println("wrong count ranging over makestring", n)
+               panic("fail")
+       }
+}
+
 // test that range over map only evaluates
 // the expression after "range" once.
 
@@ -256,6 +417,22 @@ func testmap1() {
        }
 }
 
+func testmap2() {
+       n := 0
+       nmake = 0
+       for range makemap() {
+               n++
+       }
+       if nmake != 1 {
+               println("range called makemap", nmake, "times")
+               panic("fail")
+       }
+       if n != 5 {
+               println("wrong count ranging over makemap", n)
+               panic("fail")
+       }
+}
+
 // test that range evaluates the index and value expressions
 // exactly once per iteration.
 
@@ -295,16 +472,23 @@ func testcalls() {
 }
 
 func main() {
+       testblankvars()
        testchan()
        testarray()
        testarray1()
+       testarray2()
        testarrayptr()
        testarrayptr1()
+       testarrayptr2()
        testslice()
        testslice1()
+       testslice2()
+       testslice3()
        teststring()
        teststring1()
+       teststring2()
        testmap()
        testmap1()
+       testmap2()
        testcalls()
 }