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