Russ Cox | d2cc988 | 2012-02-16 23:50:37 -0500 | [diff] [blame] | 1 | // run |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 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 | |
Rob Pike | eb37b5b | 2012-02-24 16:24:24 +1100 | [diff] [blame] | 7 | // Test arrays and slices. |
| 8 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 9 | package main |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 10 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 11 | func 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 Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 15 | } |
| 16 | } |
| 17 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 18 | func 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 Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 23 | } |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 24 | // print("sumpd t=", t, "\n"); |
| 25 | return t |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 28 | func 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 Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 35 | func 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 Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 40 | } |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 41 | // print("sumpf t=", t, "\n"); |
| 42 | return t |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 45 | func res(t int, lb, hb int) { |
| 46 | sb := (hb - lb) * (hb + lb - 1) / 2 |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 47 | if t != sb { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 48 | print("lb=", lb, |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 49 | "; hb=", hb, |
| 50 | "; t=", t, |
| 51 | "; sb=", sb, |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 52 | "\n") |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 53 | panic("res") |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // call ptr dynamic with ptr dynamic |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 58 | func testpdpd() { |
| 59 | a := make([]int, 10, 100) |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 60 | if len(a) != 10 && cap(a) != 100 { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 61 | print("len and cap from new: ", len(a), " ", cap(a), "\n") |
| 62 | panic("fail") |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 65 | a = a[0:100] |
| 66 | setpd(a) |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 67 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 68 | a = a[0:10] |
| 69 | res(sumpd(a), 0, 10) |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 70 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 71 | a = a[5:25] |
| 72 | res(sumpd(a), 5, 25) |
Ian Lance Taylor | 450c955 | 2012-02-03 06:29:30 -0800 | [diff] [blame] | 73 | |
| 74 | a = a[30:95] |
| 75 | res(sumpd(a), 35, 100) |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // call ptr fixed with ptr fixed |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 79 | func testpfpf() { |
| 80 | var a [20]int |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 81 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 82 | setpf(&a) |
| 83 | res(sumpf(&a), 0, 20) |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // call ptr dynamic with ptr fixed from new |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 87 | func testpdpf1() { |
| 88 | a := new([40]int) |
Ian Lance Taylor | 5309fae | 2010-08-31 07:34:01 -0700 | [diff] [blame] | 89 | setpd(a[0:]) |
| 90 | res(sumpd(a[0:]), 0, 40) |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 91 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 92 | b := (*a)[5:30] |
| 93 | res(sumpd(b), 5, 30) |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // call ptr dynamic with ptr fixed from var |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 97 | func testpdpf2() { |
| 98 | var a [80]int |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 99 | |
Ian Lance Taylor | 5309fae | 2010-08-31 07:34:01 -0700 | [diff] [blame] | 100 | setpd(a[0:]) |
| 101 | res(sumpd(a[0:]), 0, 80) |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // generate bounds error with ptr dynamic |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 105 | func testpdfault() { |
| 106 | a := make([]int, 100) |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 107 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 108 | print("good\n") |
| 109 | for i := 0; i < 100; i++ { |
| 110 | a[i] = 0 |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 111 | } |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 112 | print("should fault\n") |
| 113 | a[100] = 0 |
| 114 | print("bad\n") |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // generate bounds error with ptr fixed |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 118 | func testfdfault() { |
| 119 | var a [80]int |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 120 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 121 | print("good\n") |
| 122 | for i := 0; i < 80; i++ { |
| 123 | a[i] = 0 |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 124 | } |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 125 | print("should fault\n") |
| 126 | x := 80 |
| 127 | a[x] = 0 |
| 128 | print("bad\n") |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 131 | func main() { |
| 132 | testpdpd() |
| 133 | testpfpf() |
| 134 | testpdpf1() |
| 135 | testpdpf2() |
| 136 | // print("testpdfault\n"); testpdfault(); |
| 137 | // print("testfdfault\n"); testfdfault(); |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 138 | } |