2012-02-17 08:50:37 +04:00
|
|
|
// run
|
2008-12-06 05:24:05 +03:00
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-24 09:24:24 +04:00
|
|
|
// Test 'for range' on arrays, slices, and maps.
|
|
|
|
|
2008-12-06 05:24:05 +03:00
|
|
|
package main
|
|
|
|
|
2010-03-25 02:46:53 +03:00
|
|
|
const size = 16
|
2008-12-06 05:24:05 +03:00
|
|
|
|
2010-03-25 02:46:53 +03:00
|
|
|
var a [size]byte
|
|
|
|
var p []byte
|
|
|
|
var m map[int]byte
|
2008-12-06 05:24:05 +03:00
|
|
|
|
2010-03-25 02:46:53 +03:00
|
|
|
func f(k int) byte {
|
|
|
|
return byte(k * 10007 % size)
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
|
2010-03-25 02:46:53 +03:00
|
|
|
func init() {
|
|
|
|
p = make([]byte, size)
|
|
|
|
m = make(map[int]byte)
|
|
|
|
for k := 0; k < size; k++ {
|
|
|
|
v := f(k)
|
|
|
|
a[k] = v
|
|
|
|
p[k] = v
|
|
|
|
m[k] = v
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-25 02:46:53 +03:00
|
|
|
func main() {
|
|
|
|
var i int
|
2008-12-06 05:24:05 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* key only
|
|
|
|
*/
|
2010-03-25 02:46:53 +03:00
|
|
|
i = 0
|
2008-12-16 00:44:27 +03:00
|
|
|
for k := range a {
|
2010-03-25 02:46:53 +03:00
|
|
|
v := a[k]
|
2008-12-06 05:24:05 +03:00
|
|
|
if v != f(k) {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("key array range", k, v, a[k])
|
|
|
|
panic("fail")
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
2010-03-25 02:46:53 +03:00
|
|
|
i++
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
if i != size {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("key array size", i)
|
|
|
|
panic("fail")
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
|
2010-03-25 02:46:53 +03:00
|
|
|
i = 0
|
2008-12-16 00:44:27 +03:00
|
|
|
for k := range p {
|
2010-03-25 02:46:53 +03:00
|
|
|
v := p[k]
|
2008-12-06 05:24:05 +03:00
|
|
|
if v != f(k) {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("key pointer range", k, v, p[k])
|
|
|
|
panic("fail")
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
2010-03-25 02:46:53 +03:00
|
|
|
i++
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
if i != size {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("key pointer size", i)
|
|
|
|
panic("fail")
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
|
2010-03-25 02:46:53 +03:00
|
|
|
i = 0
|
2008-12-16 00:44:27 +03:00
|
|
|
for k := range m {
|
2010-03-25 02:46:53 +03:00
|
|
|
v := m[k]
|
2008-12-06 05:24:05 +03:00
|
|
|
if v != f(k) {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("key map range", k, v, m[k])
|
|
|
|
panic("fail")
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
2010-03-25 02:46:53 +03:00
|
|
|
i++
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
if i != size {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("key map size", i)
|
|
|
|
panic("fail")
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-01-26 22:34:38 +03:00
|
|
|
* key,value
|
2008-12-06 05:24:05 +03:00
|
|
|
*/
|
2010-03-25 02:46:53 +03:00
|
|
|
i = 0
|
|
|
|
for k, v := range a {
|
2008-12-06 05:24:05 +03:00
|
|
|
if v != f(k) {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("key:value array range", k, v, a[k])
|
|
|
|
panic("fail")
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
2010-03-25 02:46:53 +03:00
|
|
|
i++
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
if i != size {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("key:value array size", i)
|
|
|
|
panic("fail")
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
|
2010-03-25 02:46:53 +03:00
|
|
|
i = 0
|
|
|
|
for k, v := range p {
|
2008-12-06 05:24:05 +03:00
|
|
|
if v != f(k) {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("key:value pointer range", k, v, p[k])
|
|
|
|
panic("fail")
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
2010-03-25 02:46:53 +03:00
|
|
|
i++
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
if i != size {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("key:value pointer size", i)
|
|
|
|
panic("fail")
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
|
2010-03-25 02:46:53 +03:00
|
|
|
i = 0
|
|
|
|
for k, v := range m {
|
2008-12-06 05:24:05 +03:00
|
|
|
if v != f(k) {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("key:value map range", k, v, m[k])
|
|
|
|
panic("fail")
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
2010-03-25 02:46:53 +03:00
|
|
|
i++
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
if i != size {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("key:value map size", i)
|
|
|
|
panic("fail")
|
2008-12-06 05:24:05 +03:00
|
|
|
}
|
|
|
|
}
|