Imported Upstream version 4.7.2
[platform/upstream/gcc48.git] / gcc / testsuite / go.test / test / ken / array.go
1 // $G $D/$F.go && $L $F.$A && ./$A.out
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 package main
8
9 func setpd(a []int) {
10         //      print("setpd a=", a, " len=", len(a), " cap=", cap(a), "\n");
11         for i := 0; i < len(a); i++ {
12                 a[i] = i
13         }
14 }
15
16 func sumpd(a []int) int {
17         //      print("sumpd a=", a, " len=", len(a), " cap=", cap(a), "\n");
18         t := 0
19         for i := 0; i < len(a); i++ {
20                 t += a[i]
21         }
22         //      print("sumpd t=", t, "\n");
23         return t
24 }
25
26 func setpf(a *[20]int) {
27         //      print("setpf a=", a, " len=", len(a), " cap=", cap(a), "\n");
28         for i := 0; i < len(a); i++ {
29                 a[i] = i
30         }
31 }
32
33 func sumpf(a *[20]int) int {
34         //      print("sumpf a=", a, " len=", len(a), " cap=", cap(a), "\n");
35         t := 0
36         for i := 0; i < len(a); i++ {
37                 t += a[i]
38         }
39         //      print("sumpf t=", t, "\n");
40         return t
41 }
42
43 func res(t int, lb, hb int) {
44         sb := (hb - lb) * (hb + lb - 1) / 2
45         if t != sb {
46                 print("lb=", lb,
47                         "; hb=", hb,
48                         "; t=", t,
49                         "; sb=", sb,
50                         "\n")
51                 panic("res")
52         }
53 }
54
55 // call ptr dynamic with ptr dynamic
56 func testpdpd() {
57         a := make([]int, 10, 100)
58         if len(a) != 10 && cap(a) != 100 {
59                 print("len and cap from new: ", len(a), " ", cap(a), "\n")
60                 panic("fail")
61         }
62
63         a = a[0:100]
64         setpd(a)
65
66         a = a[0:10]
67         res(sumpd(a), 0, 10)
68
69         a = a[5:25]
70         res(sumpd(a), 5, 25)
71 }
72
73 // call ptr fixed with ptr fixed
74 func testpfpf() {
75         var a [20]int
76
77         setpf(&a)
78         res(sumpf(&a), 0, 20)
79 }
80
81 // call ptr dynamic with ptr fixed from new
82 func testpdpf1() {
83         a := new([40]int)
84         setpd(a[0:])
85         res(sumpd(a[0:]), 0, 40)
86
87         b := (*a)[5:30]
88         res(sumpd(b), 5, 30)
89 }
90
91 // call ptr dynamic with ptr fixed from var
92 func testpdpf2() {
93         var a [80]int
94
95         setpd(a[0:])
96         res(sumpd(a[0:]), 0, 80)
97 }
98
99 // generate bounds error with ptr dynamic
100 func testpdfault() {
101         a := make([]int, 100)
102
103         print("good\n")
104         for i := 0; i < 100; i++ {
105                 a[i] = 0
106         }
107         print("should fault\n")
108         a[100] = 0
109         print("bad\n")
110 }
111
112 // generate bounds error with ptr fixed
113 func testfdfault() {
114         var a [80]int
115
116         print("good\n")
117         for i := 0; i < 80; i++ {
118                 a[i] = 0
119         }
120         print("should fault\n")
121         x := 80
122         a[x] = 0
123         print("bad\n")
124 }
125
126 func main() {
127         testpdpd()
128         testpfpf()
129         testpdpf1()
130         testpdpf2()
131         //      print("testpdfault\n"); testpdfault();
132         //      print("testfdfault\n"); testfdfault();
133 }