github.com/stingnevermore/go@v0.0.0-20180120041312-3810f5bfed72/test/ken/range.go (about) 1 // run 2 3 // Copyright 2009 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // Test 'for range' on arrays, slices, and maps. 8 9 package main 10 11 const size = 16 12 13 var a [size]byte 14 var p []byte 15 var m map[int]byte 16 17 func f(k int) byte { 18 return byte(k * 10007 % size) 19 } 20 21 func init() { 22 p = make([]byte, size) 23 m = make(map[int]byte) 24 for k := 0; k < size; k++ { 25 v := f(k) 26 a[k] = v 27 p[k] = v 28 m[k] = v 29 } 30 } 31 32 func main() { 33 var i int 34 35 /* 36 * key only 37 */ 38 i = 0 39 for k := range a { 40 v := a[k] 41 if v != f(k) { 42 println("key array range", k, v, a[k]) 43 panic("fail") 44 } 45 i++ 46 } 47 if i != size { 48 println("key array size", i) 49 panic("fail") 50 } 51 52 i = 0 53 for k := range p { 54 v := p[k] 55 if v != f(k) { 56 println("key pointer range", k, v, p[k]) 57 panic("fail") 58 } 59 i++ 60 } 61 if i != size { 62 println("key pointer size", i) 63 panic("fail") 64 } 65 66 i = 0 67 for k := range m { 68 v := m[k] 69 if v != f(k) { 70 println("key map range", k, v, m[k]) 71 panic("fail") 72 } 73 i++ 74 } 75 if i != size { 76 println("key map size", i) 77 panic("fail") 78 } 79 80 /* 81 * key,value 82 */ 83 i = 0 84 for k, v := range a { 85 if v != f(k) { 86 println("key:value array range", k, v, a[k]) 87 panic("fail") 88 } 89 i++ 90 } 91 if i != size { 92 println("key:value array size", i) 93 panic("fail") 94 } 95 96 i = 0 97 for k, v := range p { 98 if v != f(k) { 99 println("key:value pointer range", k, v, p[k]) 100 panic("fail") 101 } 102 i++ 103 } 104 if i != size { 105 println("key:value pointer size", i) 106 panic("fail") 107 } 108 109 i = 0 110 for k, v := range m { 111 if v != f(k) { 112 println("key:value map range", k, v, m[k]) 113 panic("fail") 114 } 115 i++ 116 } 117 if i != size { 118 println("key:value map size", i) 119 panic("fail") 120 } 121 }