1
0
mirror of https://github.com/golang/go synced 2024-11-13 17:40:23 -07:00
go/test/ken/range.go

122 lines
1.7 KiB
Go
Raw Normal View History

// run
2008-12-05 19:24:05 -07: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.
// Test 'for range' on arrays, slices, and maps.
2008-12-05 19:24:05 -07:00
package main
const size = 16
2008-12-05 19:24:05 -07:00
var a [size]byte
var p []byte
var m map[int]byte
2008-12-05 19:24:05 -07:00
func f(k int) byte {
return byte(k * 10007 % size)
2008-12-05 19:24:05 -07: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-05 19:24:05 -07:00
}
}
func main() {
var i int
2008-12-05 19:24:05 -07:00
/*
* key only
*/
i = 0
for k := range a {
v := a[k]
2008-12-05 19:24:05 -07:00
if v != f(k) {
println("key array range", k, v, a[k])
panic("fail")
2008-12-05 19:24:05 -07:00
}
i++
2008-12-05 19:24:05 -07:00
}
if i != size {
println("key array size", i)
panic("fail")
2008-12-05 19:24:05 -07:00
}
i = 0
for k := range p {
v := p[k]
2008-12-05 19:24:05 -07:00
if v != f(k) {
println("key pointer range", k, v, p[k])
panic("fail")
2008-12-05 19:24:05 -07:00
}
i++
2008-12-05 19:24:05 -07:00
}
if i != size {
println("key pointer size", i)
panic("fail")
2008-12-05 19:24:05 -07:00
}
i = 0
for k := range m {
v := m[k]
2008-12-05 19:24:05 -07:00
if v != f(k) {
println("key map range", k, v, m[k])
panic("fail")
2008-12-05 19:24:05 -07:00
}
i++
2008-12-05 19:24:05 -07:00
}
if i != size {
println("key map size", i)
panic("fail")
2008-12-05 19:24:05 -07:00
}
/*
* key,value
2008-12-05 19:24:05 -07:00
*/
i = 0
for k, v := range a {
2008-12-05 19:24:05 -07:00
if v != f(k) {
println("key:value array range", k, v, a[k])
panic("fail")
2008-12-05 19:24:05 -07:00
}
i++
2008-12-05 19:24:05 -07:00
}
if i != size {
println("key:value array size", i)
panic("fail")
2008-12-05 19:24:05 -07:00
}
i = 0
for k, v := range p {
2008-12-05 19:24:05 -07:00
if v != f(k) {
println("key:value pointer range", k, v, p[k])
panic("fail")
2008-12-05 19:24:05 -07:00
}
i++
2008-12-05 19:24:05 -07:00
}
if i != size {
println("key:value pointer size", i)
panic("fail")
2008-12-05 19:24:05 -07:00
}
i = 0
for k, v := range m {
2008-12-05 19:24:05 -07:00
if v != f(k) {
println("key:value map range", k, v, m[k])
panic("fail")
2008-12-05 19:24:05 -07:00
}
i++
2008-12-05 19:24:05 -07:00
}
if i != size {
println("key:value map size", i)
panic("fail")
2008-12-05 19:24:05 -07:00
}
}