OSDN Git Service

Add Go frontend, libgo library, and Go testsuite.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / go.test / test / interface / convert.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 // Check uses of all the different interface
8 // conversion runtime functions.
9
10 package main
11
12 type Stringer interface {
13         String() string
14 }
15 type StringLengther interface {
16         String() string
17         Length() int
18 }
19 type Empty interface{}
20
21 type T string
22
23 func (t T) String() string {
24         return string(t)
25 }
26 func (t T) Length() int {
27         return len(t)
28 }
29
30 type U string
31
32 func (u U) String() string {
33         return string(u)
34 }
35
36 var t = T("hello")
37 var u = U("goodbye")
38 var e Empty
39 var s Stringer = t
40 var sl StringLengther = t
41 var i int
42 var ok bool
43
44 func hello(s string) {
45         if s != "hello" {
46                 println("not hello: ", s)
47                 panic("fail")
48         }
49 }
50
51 func five(i int) {
52         if i != 5 {
53                 println("not 5: ", i)
54                 panic("fail")
55         }
56 }
57
58 func true(ok bool) {
59         if !ok {
60                 panic("not true")
61         }
62 }
63
64 func false(ok bool) {
65         if ok {
66                 panic("not false")
67         }
68 }
69
70 func main() {
71         // T2I
72         s = t
73         hello(s.String())
74
75         // I2T
76         t = s.(T)
77         hello(t.String())
78
79         // T2E
80         e = t
81
82         // E2T
83         t = e.(T)
84         hello(t.String())
85
86         // T2I again
87         sl = t
88         hello(sl.String())
89         five(sl.Length())
90
91         // I2I static
92         s = sl
93         hello(s.String())
94
95         // I2I dynamic
96         sl = s.(StringLengther)
97         hello(sl.String())
98         five(sl.Length())
99
100         // I2E (and E2T)
101         e = s
102         hello(e.(T).String())
103
104         // E2I
105         s = e.(Stringer)
106         hello(s.String())
107
108         // I2T2 true
109         t, ok = s.(T)
110         true(ok)
111         hello(t.String())
112
113         // I2T2 false
114         _, ok = s.(U)
115         false(ok)
116
117         // I2I2 true
118         sl, ok = s.(StringLengther)
119         true(ok)
120         hello(sl.String())
121         five(sl.Length())
122
123         // I2I2 false (and T2I)
124         s = u
125         sl, ok = s.(StringLengther)
126         false(ok)
127
128         // E2T2 true
129         t, ok = e.(T)
130         true(ok)
131         hello(t.String())
132
133         // E2T2 false
134         i, ok = e.(int)
135         false(ok)
136
137         // E2I2 true
138         sl, ok = e.(StringLengther)
139         true(ok)
140         hello(sl.String())
141         five(sl.Length())
142
143         // E2I2 false (and T2E)
144         e = u
145         sl, ok = e.(StringLengther)
146         false(ok)
147 }