summaryrefslogtreecommitdiffstats
path: root/test/ken/range.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:25:22 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:25:22 +0000
commitf6ad4dcef54c5ce997a4bad5a6d86de229015700 (patch)
tree7cfa4e31ace5c2bd95c72b154d15af494b2bcbef /test/ken/range.go
parentInitial commit. (diff)
downloadgolang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.tar.xz
golang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.zip
Adding upstream version 1.22.1.upstream/1.22.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/ken/range.go')
-rw-r--r--test/ken/range.go121
1 files changed, 121 insertions, 0 deletions
diff --git a/test/ken/range.go b/test/ken/range.go
new file mode 100644
index 0000000..89c14e5
--- /dev/null
+++ b/test/ken/range.go
@@ -0,0 +1,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")
+ }
+}