aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/go.test/test/ken/array.go
blob: 9412e3502dd950a12919dfedde52d797e824ed0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 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 arrays and slices.

package main

func setpd(a []int) {
	//	print("setpd a=", a, " len=", len(a), " cap=", cap(a), "\n");
	for i := 0; i < len(a); i++ {
		a[i] = i
	}
}

func sumpd(a []int) int {
	//	print("sumpd a=", a, " len=", len(a), " cap=", cap(a), "\n");
	t := 0
	for i := 0; i < len(a); i++ {
		t += a[i]
	}
	//	print("sumpd t=", t, "\n");
	return t
}

func setpf(a *[20]int) {
	//	print("setpf a=", a, " len=", len(a), " cap=", cap(a), "\n");
	for i := 0; i < len(a); i++ {
		a[i] = i
	}
}

func sumpf(a *[20]int) int {
	//	print("sumpf a=", a, " len=", len(a), " cap=", cap(a), "\n");
	t := 0
	for i := 0; i < len(a); i++ {
		t += a[i]
	}
	//	print("sumpf t=", t, "\n");
	return t
}

func res(t int, lb, hb int) {
	sb := (hb - lb) * (hb + lb - 1) / 2
	if t != sb {
		print("lb=", lb,
			"; hb=", hb,
			"; t=", t,
			"; sb=", sb,
			"\n")
		panic("res")
	}
}

// call ptr dynamic with ptr dynamic
func testpdpd() {
	a := make([]int, 10, 100)
	if len(a) != 10 && cap(a) != 100 {
		print("len and cap from new: ", len(a), " ", cap(a), "\n")
		panic("fail")
	}

	a = a[0:100]
	setpd(a)

	a = a[0:10]
	res(sumpd(a), 0, 10)

	a = a[5:25]
	res(sumpd(a), 5, 25)

	a = a[30:95]
	res(sumpd(a), 35, 100)
}

// call ptr fixed with ptr fixed
func testpfpf() {
	var a [20]int

	setpf(&a)
	res(sumpf(&a), 0, 20)
}

// call ptr dynamic with ptr fixed from new
func testpdpf1() {
	a := new([40]int)
	setpd(a[0:])
	res(sumpd(a[0:]), 0, 40)

	b := (*a)[5:30]
	res(sumpd(b), 5, 30)
}

// call ptr dynamic with ptr fixed from var
func testpdpf2() {
	var a [80]int

	setpd(a[0:])
	res(sumpd(a[0:]), 0, 80)
}

// generate bounds error with ptr dynamic
func testpdfault() {
	a := make([]int, 100)

	print("good\n")
	for i := 0; i < 100; i++ {
		a[i] = 0
	}
	print("should fault\n")
	a[100] = 0
	print("bad\n")
}

// generate bounds error with ptr fixed
func testfdfault() {
	var a [80]int

	print("good\n")
	for i := 0; i < 80; i++ {
		a[i] = 0
	}
	print("should fault\n")
	x := 80
	a[x] = 0
	print("bad\n")
}

func main() {
	testpdpd()
	testpfpf()
	testpdpf1()
	testpdpf2()
	//	print("testpdfault\n");	testpdfault();
	//	print("testfdfault\n");	testfdfault();
}