~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to test/ken/embed.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-04-20 17:36:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110420173648-ifergoxyrm832trd
Tags: upstream-2011.03.07.1
Import upstream version 2011.03.07.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
10
type I interface {
 
11
        test1() int
 
12
        test2() int
 
13
        test3() int
 
14
        test4() int
 
15
        test5() int
 
16
        test6() int
 
17
        test7() int
 
18
}
 
19
 
 
20
/******
 
21
 ******
 
22
 ******/
 
23
 
 
24
type SubpSubp struct {
 
25
        a7 int
 
26
        a  int
 
27
}
 
28
 
 
29
func (p *SubpSubp) test7() int {
 
30
        if p.a != p.a7 {
 
31
                println("SubpSubp", p, p.a7)
 
32
                panic("fail")
 
33
        }
 
34
        return p.a
 
35
}
 
36
func (p *SubpSubp) testx() { println("SubpSubp", p, p.a7) }
 
37
 
 
38
/******
 
39
 ******
 
40
 ******/
 
41
 
 
42
type SubpSub struct {
 
43
        a6 int
 
44
        SubpSubp
 
45
        a int
 
46
}
 
47
 
 
48
func (p *SubpSub) test6() int {
 
49
        if p.a != p.a6 {
 
50
                println("SubpSub", p, p.a6)
 
51
                panic("fail")
 
52
        }
 
53
        return p.a
 
54
}
 
55
func (p *SubpSub) testx() { println("SubpSub", p, p.a6) }
 
56
 
 
57
/******
 
58
 ******
 
59
 ******/
 
60
 
 
61
type SubSubp struct {
 
62
        a5 int
 
63
        a  int
 
64
}
 
65
 
 
66
func (p *SubSubp) test5() int {
 
67
        if p.a != p.a5 {
 
68
                println("SubpSub", p, p.a5)
 
69
                panic("fail")
 
70
        }
 
71
        return p.a
 
72
}
 
73
 
 
74
/******
 
75
 ******
 
76
 ******/
 
77
 
 
78
type SubSub struct {
 
79
        a4 int
 
80
        a  int
 
81
}
 
82
 
 
83
func (p *SubSub) test4() int {
 
84
        if p.a != p.a4 {
 
85
                println("SubpSub", p, p.a4)
 
86
                panic("fail")
 
87
        }
 
88
        return p.a
 
89
}
 
90
 
 
91
/******
 
92
 ******
 
93
 ******/
 
94
 
 
95
type Subp struct {
 
96
        a3 int
 
97
        *SubpSubp
 
98
        SubpSub
 
99
        a int
 
100
}
 
101
 
 
102
func (p *Subp) test3() int {
 
103
        if p.a != p.a3 {
 
104
                println("SubpSub", p, p.a3)
 
105
                panic("fail")
 
106
        }
 
107
        return p.a
 
108
}
 
109
 
 
110
/******
 
111
 ******
 
112
 ******/
 
113
 
 
114
type Sub struct {
 
115
        a2 int
 
116
        *SubSubp
 
117
        SubSub
 
118
        a int
 
119
}
 
120
 
 
121
func (p *Sub) test2() int {
 
122
        if p.a != p.a2 {
 
123
                println("SubpSub", p, p.a2)
 
124
                panic("fail")
 
125
        }
 
126
        return p.a
 
127
}
 
128
 
 
129
/******
 
130
 ******
 
131
 ******/
 
132
 
 
133
type S struct {
 
134
        a1 int
 
135
        Sub
 
136
        *Subp
 
137
        a int
 
138
}
 
139
 
 
140
func (p *S) test1() int {
 
141
        if p.a != p.a1 {
 
142
                println("SubpSub", p, p.a1)
 
143
                panic("fail")
 
144
        }
 
145
        return p.a
 
146
}
 
147
 
 
148
/******
 
149
 ******
 
150
 ******/
 
151
 
 
152
func main() {
 
153
        var i I
 
154
        var s *S
 
155
 
 
156
        // allocate
 
157
        s = new(S)
 
158
        s.Subp = new(Subp)
 
159
        s.Sub.SubSubp = new(SubSubp)
 
160
        s.Subp.SubpSubp = new(SubpSubp)
 
161
 
 
162
        // explicit assignment
 
163
        s.a = 1
 
164
        s.Sub.a = 2
 
165
        s.Subp.a = 3
 
166
        s.Sub.SubSub.a = 4
 
167
        s.Sub.SubSubp.a = 5
 
168
        s.Subp.SubpSub.a = 6
 
169
        s.Subp.SubpSubp.a = 7
 
170
 
 
171
        // embedded (unique) assignment
 
172
        s.a1 = 1
 
173
        s.a2 = 2
 
174
        s.a3 = 3
 
175
        s.a4 = 4
 
176
        s.a5 = 5
 
177
        s.a6 = 6
 
178
        s.a7 = 7
 
179
 
 
180
        // unique calls with explicit &
 
181
        if s.test1() != 1 {
 
182
                println("t1", 1)
 
183
                panic("fail")
 
184
        }
 
185
        if (&s.Sub).test2() != 2 {
 
186
                println("t1", 2)
 
187
                panic("fail")
 
188
        }
 
189
        if s.Subp.test3() != 3 {
 
190
                println("t1", 3)
 
191
                panic("fail")
 
192
        }
 
193
        if (&s.Sub.SubSub).test4() != 4 {
 
194
                println("t1", 4)
 
195
                panic("fail")
 
196
        }
 
197
        if s.Sub.SubSubp.test5() != 5 {
 
198
                println("t1", 5)
 
199
                panic("fail")
 
200
        }
 
201
        if (&s.Subp.SubpSub).test6() != 6 {
 
202
                println("t1", 6)
 
203
                panic("fail")
 
204
        }
 
205
        if s.Subp.SubpSubp.test7() != 7 {
 
206
                println("t1", 7)
 
207
                panic("fail")
 
208
        }
 
209
 
 
210
        // automatic &
 
211
        if s.Sub.test2() != 2 {
 
212
                println("t2", 2)
 
213
                panic("fail")
 
214
        }
 
215
        if s.Sub.SubSub.test4() != 4 {
 
216
                println("t2", 4)
 
217
                panic("fail")
 
218
        }
 
219
        if s.Subp.SubpSub.test6() != 6 {
 
220
                println("t2", 6)
 
221
                panic("fail")
 
222
        }
 
223
 
 
224
        // embedded calls
 
225
        if s.test1() != s.a1 {
 
226
                println("t3", 1)
 
227
                panic("fail")
 
228
        }
 
229
        if s.test2() != s.a2 {
 
230
                println("t3", 2)
 
231
                panic("fail")
 
232
        }
 
233
        if s.test3() != s.a3 {
 
234
                println("t3", 3)
 
235
                panic("fail")
 
236
        }
 
237
        if s.test4() != s.a4 {
 
238
                println("t3", 4)
 
239
                panic("fail")
 
240
        }
 
241
        if s.test5() != s.a5 {
 
242
                println("t3", 5)
 
243
                panic("fail")
 
244
        }
 
245
        if s.test6() != s.a6 {
 
246
                println("t3", 6)
 
247
                panic("fail")
 
248
        }
 
249
        if s.test7() != s.a7 {
 
250
                println("t3", 7)
 
251
                panic("fail")
 
252
        }
 
253
 
 
254
        // run it thru an interface
 
255
        i = s
 
256
        s = i.(*S)
 
257
 
 
258
        // same as t3
 
259
        if s.test1() != s.a1 {
 
260
                println("t4", 1)
 
261
                panic("fail")
 
262
        }
 
263
        if s.test2() != s.a2 {
 
264
                println("t4", 2)
 
265
                panic("fail")
 
266
        }
 
267
        if s.test3() != s.a3 {
 
268
                println("t4", 3)
 
269
                panic("fail")
 
270
        }
 
271
        if s.test4() != s.a4 {
 
272
                println("t4", 4)
 
273
                panic("fail")
 
274
        }
 
275
        if s.test5() != s.a5 {
 
276
                println("t4", 5)
 
277
                panic("fail")
 
278
        }
 
279
        if s.test6() != s.a6 {
 
280
                println("t4", 6)
 
281
                panic("fail")
 
282
        }
 
283
        if s.test7() != s.a7 {
 
284
                println("t4", 7)
 
285
                panic("fail")
 
286
        }
 
287
 
 
288
        // call interface
 
289
        if i.test1() != s.test1() {
 
290
                println("t5", 1)
 
291
                panic("fail")
 
292
        }
 
293
        if i.test2() != s.test2() {
 
294
                println("t5", 2)
 
295
                panic("fail")
 
296
        }
 
297
        if i.test3() != s.test3() {
 
298
                println("t5", 3)
 
299
                panic("fail")
 
300
        }
 
301
        if i.test4() != s.test4() {
 
302
                println("t5", 4)
 
303
                panic("fail")
 
304
        }
 
305
        if i.test5() != s.test5() {
 
306
                println("t5", 5)
 
307
                panic("fail")
 
308
        }
 
309
        if i.test6() != s.test6() {
 
310
                println("t5", 6)
 
311
                panic("fail")
 
312
        }
 
313
        if i.test7() != s.test7() {
 
314
                println("t5", 7)
 
315
                panic("fail")
 
316
        }
 
317
}