1
2
3
4
5 package main
6
7 import (
8 "fmt"
9 "testing"
10 )
11
12 var output string
13
14 func mypanic(t *testing.T, s string) {
15 t.Fatalf(s + "\n" + output)
16
17 }
18
19 func assertEqual(t *testing.T, x, y int) {
20 if x != y {
21 mypanic(t, fmt.Sprintf("assertEqual failed got %d, want %d", x, y))
22 }
23 }
24
25 func TestAddressed(t *testing.T) {
26 x := f1_ssa(2, 3)
27 output += fmt.Sprintln("*x is", *x)
28 output += fmt.Sprintln("Gratuitously use some stack")
29 output += fmt.Sprintln("*x is", *x)
30 assertEqual(t, *x, 9)
31
32 w := f3a_ssa(6)
33 output += fmt.Sprintln("*w is", *w)
34 output += fmt.Sprintln("Gratuitously use some stack")
35 output += fmt.Sprintln("*w is", *w)
36 assertEqual(t, *w, 6)
37
38 y := f3b_ssa(12)
39 output += fmt.Sprintln("*y.(*int) is", *y.(*int))
40 output += fmt.Sprintln("Gratuitously use some stack")
41 output += fmt.Sprintln("*y.(*int) is", *y.(*int))
42 assertEqual(t, *y.(*int), 12)
43
44 z := f3c_ssa(8)
45 output += fmt.Sprintln("*z.(*int) is", *z.(*int))
46 output += fmt.Sprintln("Gratuitously use some stack")
47 output += fmt.Sprintln("*z.(*int) is", *z.(*int))
48 assertEqual(t, *z.(*int), 8)
49
50 args(t)
51 test_autos(t)
52 }
53
54
55 func f1_ssa(x, y int) *int {
56 x = x*y + y
57 return &x
58 }
59
60
61 func f3a_ssa(x int) *int {
62 return &x
63 }
64
65
66 func f3b_ssa(x int) interface{} {
67 return &x
68 }
69
70
71 func f3c_ssa(y int) interface{} {
72 x := y
73 return &x
74 }
75
76 type V struct {
77 p *V
78 w, x int64
79 }
80
81 func args(t *testing.T) {
82 v := V{p: nil, w: 1, x: 1}
83 a := V{p: &v, w: 2, x: 2}
84 b := V{p: &v, w: 0, x: 0}
85 i := v.args_ssa(a, b)
86 output += fmt.Sprintln("i=", i)
87 assertEqual(t, int(i), 2)
88 }
89
90
91 func (v V) args_ssa(a, b V) int64 {
92 if v.w == 0 {
93 return v.x
94 }
95 if v.w == 1 {
96 return a.x
97 }
98 if v.w == 2 {
99 return b.x
100 }
101 b.p.p = &a
102
103 return -1
104 }
105
106 func test_autos(t *testing.T) {
107 test(t, 11)
108 test(t, 12)
109 test(t, 13)
110 test(t, 21)
111 test(t, 22)
112 test(t, 23)
113 test(t, 31)
114 test(t, 32)
115 }
116
117 func test(t *testing.T, which int64) {
118 output += fmt.Sprintln("test", which)
119 v1 := V{w: 30, x: 3, p: nil}
120 v2, v3 := v1.autos_ssa(which, 10, 1, 20, 2)
121 if which != v2.val() {
122 output += fmt.Sprintln("Expected which=", which, "got v2.val()=", v2.val())
123 mypanic(t, "Failure of expected V value")
124 }
125 if v2.p.val() != v3.val() {
126 output += fmt.Sprintln("Expected v2.p.val()=", v2.p.val(), "got v3.val()=", v3.val())
127 mypanic(t, "Failure of expected V.p value")
128 }
129 if which != v3.p.p.p.p.p.p.p.val() {
130 output += fmt.Sprintln("Expected which=", which, "got v3.p.p.p.p.p.p.p.val()=", v3.p.p.p.p.p.p.p.val())
131 mypanic(t, "Failure of expected V.p value")
132 }
133 }
134
135 func (v V) val() int64 {
136 return v.w + v.x
137 }
138
139
140
141
142
143
144
145
146
147
148
149 func (v V) autos_ssa(which, w1, x1, w2, x2 int64) (y, z V) {
150 fill_ssa(v.w, v.x, &v, v.p)
151 var a, b, c, d, e, f, g, h V
152 fill_ssa(w1, x1, &a, &b)
153 fill_ssa(w1, x2, &b, &c)
154 fill_ssa(w1, v.x, &c, &d)
155 fill_ssa(w2, x1, &d, &e)
156 fill_ssa(w2, x2, &e, &f)
157 fill_ssa(w2, v.x, &f, &g)
158 fill_ssa(v.w, x1, &g, &h)
159 fill_ssa(v.w, x2, &h, &a)
160 switch which {
161 case 11:
162 y = a
163 z.getsI(&b)
164 case 12:
165 y.gets(&b)
166 z = c
167 case 13:
168 y.gets(&c)
169 z = d
170 case 21:
171 y.getsI(&d)
172 z.gets(&e)
173 case 22:
174 y = e
175 z = f
176 case 23:
177 y.gets(&f)
178 z.getsI(&g)
179 case 31:
180 y = g
181 z.gets(&h)
182 case 32:
183 y.getsI(&h)
184 z = a
185 default:
186
187 panic("")
188 }
189 return
190 }
191
192
193
194
195 func (to *V) gets(from *V) {
196 *to = *from
197 }
198
199
200
201
202 func (to *V) getsI(from interface{}) {
203 *to = *from.(*V)
204 }
205
206
207
208 func fill_ssa(w, x int64, r, p *V) {
209 *r = V{w: w, x: x, p: p}
210 }
211
View as plain text