1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
// run
// 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.
package main
const size = 16
var a [size]byte
var p []byte
var m map[int]byte
func f(k int) byte {
return byte(k * 10007 % size)
}
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
}
}
func main() {
var i int
/*
* key only
*/
i = 0
for k := range a {
v := a[k]
if v != f(k) {
println("key array range", k, v, a[k])
panic("fail")
}
i++
}
if i != size {
println("key array size", i)
panic("fail")
}
i = 0
for k := range p {
v := p[k]
if v != f(k) {
println("key pointer range", k, v, p[k])
panic("fail")
}
i++
}
if i != size {
println("key pointer size", i)
panic("fail")
}
i = 0
for k := range m {
v := m[k]
if v != f(k) {
println("key map range", k, v, m[k])
panic("fail")
}
i++
}
if i != size {
println("key map size", i)
panic("fail")
}
/*
* key,value
*/
i = 0
for k, v := range a {
if v != f(k) {
println("key:value array range", k, v, a[k])
panic("fail")
}
i++
}
if i != size {
println("key:value array size", i)
panic("fail")
}
i = 0
for k, v := range p {
if v != f(k) {
println("key:value pointer range", k, v, p[k])
panic("fail")
}
i++
}
if i != size {
println("key:value pointer size", i)
panic("fail")
}
i = 0
for k, v := range m {
if v != f(k) {
println("key:value map range", k, v, m[k])
panic("fail")
}
i++
}
if i != size {
println("key:value map size", i)
panic("fail")
}
}
|