blob: 9412e3502dd950a12919dfedde52d797e824ed0a [file] [log] [blame]
Russ Coxd2cc9882012-02-16 23:50:37 -05001// run
Ken Thompson944ad622008-08-29 13:24:53 -07002
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
Rob Pikeeb37b5b2012-02-24 16:24:24 +11007// Test arrays and slices.
8
Russ Cox00f9f0c2010-03-30 10:34:57 -07009package main
Ken Thompson944ad622008-08-29 13:24:53 -070010
Russ Cox00f9f0c2010-03-30 10:34:57 -070011func setpd(a []int) {
12 // print("setpd a=", a, " len=", len(a), " cap=", cap(a), "\n");
13 for i := 0; i < len(a); i++ {
14 a[i] = i
Ken Thompson944ad622008-08-29 13:24:53 -070015 }
16}
17
Russ Cox00f9f0c2010-03-30 10:34:57 -070018func sumpd(a []int) int {
19 // print("sumpd a=", a, " len=", len(a), " cap=", cap(a), "\n");
20 t := 0
21 for i := 0; i < len(a); i++ {
22 t += a[i]
Ken Thompson944ad622008-08-29 13:24:53 -070023 }
Russ Cox00f9f0c2010-03-30 10:34:57 -070024 // print("sumpd t=", t, "\n");
25 return t
Ken Thompson944ad622008-08-29 13:24:53 -070026}
27
Russ Cox00f9f0c2010-03-30 10:34:57 -070028func setpf(a *[20]int) {
29 // print("setpf a=", a, " len=", len(a), " cap=", cap(a), "\n");
30 for i := 0; i < len(a); i++ {
31 a[i] = i
Ken Thompson944ad622008-08-29 13:24:53 -070032 }
33}
34
Russ Cox00f9f0c2010-03-30 10:34:57 -070035func sumpf(a *[20]int) int {
36 // print("sumpf a=", a, " len=", len(a), " cap=", cap(a), "\n");
37 t := 0
38 for i := 0; i < len(a); i++ {
39 t += a[i]
Ken Thompson944ad622008-08-29 13:24:53 -070040 }
Russ Cox00f9f0c2010-03-30 10:34:57 -070041 // print("sumpf t=", t, "\n");
42 return t
Ken Thompson944ad622008-08-29 13:24:53 -070043}
44
Russ Cox00f9f0c2010-03-30 10:34:57 -070045func res(t int, lb, hb int) {
46 sb := (hb - lb) * (hb + lb - 1) / 2
Ken Thompson944ad622008-08-29 13:24:53 -070047 if t != sb {
Russ Cox00f9f0c2010-03-30 10:34:57 -070048 print("lb=", lb,
Ken Thompson944ad622008-08-29 13:24:53 -070049 "; hb=", hb,
50 "; t=", t,
51 "; sb=", sb,
Russ Cox00f9f0c2010-03-30 10:34:57 -070052 "\n")
Ken Thompson944ad622008-08-29 13:24:53 -070053 panic("res")
54 }
55}
56
57// call ptr dynamic with ptr dynamic
Russ Cox00f9f0c2010-03-30 10:34:57 -070058func testpdpd() {
59 a := make([]int, 10, 100)
Ken Thompson944ad622008-08-29 13:24:53 -070060 if len(a) != 10 && cap(a) != 100 {
Russ Cox00f9f0c2010-03-30 10:34:57 -070061 print("len and cap from new: ", len(a), " ", cap(a), "\n")
62 panic("fail")
Ken Thompson944ad622008-08-29 13:24:53 -070063 }
64
Russ Cox00f9f0c2010-03-30 10:34:57 -070065 a = a[0:100]
66 setpd(a)
Ken Thompson944ad622008-08-29 13:24:53 -070067
Russ Cox00f9f0c2010-03-30 10:34:57 -070068 a = a[0:10]
69 res(sumpd(a), 0, 10)
Ken Thompson944ad622008-08-29 13:24:53 -070070
Russ Cox00f9f0c2010-03-30 10:34:57 -070071 a = a[5:25]
72 res(sumpd(a), 5, 25)
Ian Lance Taylor450c9552012-02-03 06:29:30 -080073
74 a = a[30:95]
75 res(sumpd(a), 35, 100)
Ken Thompson944ad622008-08-29 13:24:53 -070076}
77
78// call ptr fixed with ptr fixed
Russ Cox00f9f0c2010-03-30 10:34:57 -070079func testpfpf() {
80 var a [20]int
Ken Thompson944ad622008-08-29 13:24:53 -070081
Russ Cox00f9f0c2010-03-30 10:34:57 -070082 setpf(&a)
83 res(sumpf(&a), 0, 20)
Ken Thompson944ad622008-08-29 13:24:53 -070084}
85
86// call ptr dynamic with ptr fixed from new
Russ Cox00f9f0c2010-03-30 10:34:57 -070087func testpdpf1() {
88 a := new([40]int)
Ian Lance Taylor5309fae2010-08-31 07:34:01 -070089 setpd(a[0:])
90 res(sumpd(a[0:]), 0, 40)
Ken Thompson944ad622008-08-29 13:24:53 -070091
Russ Cox00f9f0c2010-03-30 10:34:57 -070092 b := (*a)[5:30]
93 res(sumpd(b), 5, 30)
Ken Thompson944ad622008-08-29 13:24:53 -070094}
95
96// call ptr dynamic with ptr fixed from var
Russ Cox00f9f0c2010-03-30 10:34:57 -070097func testpdpf2() {
98 var a [80]int
Ken Thompson944ad622008-08-29 13:24:53 -070099
Ian Lance Taylor5309fae2010-08-31 07:34:01 -0700100 setpd(a[0:])
101 res(sumpd(a[0:]), 0, 80)
Ken Thompson944ad622008-08-29 13:24:53 -0700102}
103
104// generate bounds error with ptr dynamic
Russ Cox00f9f0c2010-03-30 10:34:57 -0700105func testpdfault() {
106 a := make([]int, 100)
Ken Thompson944ad622008-08-29 13:24:53 -0700107
Russ Cox00f9f0c2010-03-30 10:34:57 -0700108 print("good\n")
109 for i := 0; i < 100; i++ {
110 a[i] = 0
Ken Thompson944ad622008-08-29 13:24:53 -0700111 }
Russ Cox00f9f0c2010-03-30 10:34:57 -0700112 print("should fault\n")
113 a[100] = 0
114 print("bad\n")
Ken Thompson944ad622008-08-29 13:24:53 -0700115}
116
117// generate bounds error with ptr fixed
Russ Cox00f9f0c2010-03-30 10:34:57 -0700118func testfdfault() {
119 var a [80]int
Ken Thompson944ad622008-08-29 13:24:53 -0700120
Russ Cox00f9f0c2010-03-30 10:34:57 -0700121 print("good\n")
122 for i := 0; i < 80; i++ {
123 a[i] = 0
Ken Thompson944ad622008-08-29 13:24:53 -0700124 }
Russ Cox00f9f0c2010-03-30 10:34:57 -0700125 print("should fault\n")
126 x := 80
127 a[x] = 0
128 print("bad\n")
Ken Thompson944ad622008-08-29 13:24:53 -0700129}
130
Russ Cox00f9f0c2010-03-30 10:34:57 -0700131func main() {
132 testpdpd()
133 testpfpf()
134 testpdpf1()
135 testpdpf2()
136 // print("testpdfault\n"); testpdfault();
137 // print("testfdfault\n"); testfdfault();
Ken Thompson944ad622008-08-29 13:24:53 -0700138}