1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 type MyBuffer bytes
.Buffer
18 func TestImplicitMapConversion(t
*testing
.T
) {
19 // Test implicit conversions in MapIndex and SetMapIndex.
22 m
:= make(map[int]int)
24 mv
.SetMapIndex(ValueOf(1), ValueOf(2))
27 t
.Errorf("#1 after SetMapIndex(1,2): %d, %t (map=%v)", x
, ok
, m
)
29 if n
:= mv
.MapIndex(ValueOf(1)).Interface().(int); n
!= 2 {
30 t
.Errorf("#1 MapIndex(1) = %d", n
)
34 // convert interface key
35 m
:= make(map[interface{}]int)
37 mv
.SetMapIndex(ValueOf(1), ValueOf(2))
40 t
.Errorf("#2 after SetMapIndex(1,2): %d, %t (map=%v)", x
, ok
, m
)
42 if n
:= mv
.MapIndex(ValueOf(1)).Interface().(int); n
!= 2 {
43 t
.Errorf("#2 MapIndex(1) = %d", n
)
47 // convert interface value
48 m
:= make(map[int]interface{})
50 mv
.SetMapIndex(ValueOf(1), ValueOf(2))
53 t
.Errorf("#3 after SetMapIndex(1,2): %d, %t (map=%v)", x
, ok
, m
)
55 if n
:= mv
.MapIndex(ValueOf(1)).Interface().(int); n
!= 2 {
56 t
.Errorf("#3 MapIndex(1) = %d", n
)
60 // convert both interface key and interface value
61 m
:= make(map[interface{}]interface{})
63 mv
.SetMapIndex(ValueOf(1), ValueOf(2))
66 t
.Errorf("#4 after SetMapIndex(1,2): %d, %t (map=%v)", x
, ok
, m
)
68 if n
:= mv
.MapIndex(ValueOf(1)).Interface().(int); n
!= 2 {
69 t
.Errorf("#4 MapIndex(1) = %d", n
)
73 // convert both, with non-empty interfaces
74 m
:= make(map[io
.Reader
]io
.Writer
)
76 b1
:= new(bytes
.Buffer
)
77 b2
:= new(bytes
.Buffer
)
78 mv
.SetMapIndex(ValueOf(b1
), ValueOf(b2
))
81 t
.Errorf("#5 after SetMapIndex(b1, b2): %p (!= %p), %t (map=%v)", x
, b2
, ok
, m
)
83 if p
:= mv
.MapIndex(ValueOf(b1
)).Elem().Pointer(); p
!= uintptr(unsafe
.Pointer(b2
)) {
84 t
.Errorf("#5 MapIndex(b1) = %#x want %p", p
, b2
)
88 // convert channel direction
89 m
:= make(map[<-chan int]chan int)
93 mv
.SetMapIndex(ValueOf(c1
), ValueOf(c2
))
96 t
.Errorf("#6 after SetMapIndex(c1, c2): %p (!= %p), %t (map=%v)", x
, c2
, ok
, m
)
98 if p
:= mv
.MapIndex(ValueOf(c1
)).Pointer(); p
!= ValueOf(c2
).Pointer() {
99 t
.Errorf("#6 MapIndex(c1) = %#x want %p", p
, c2
)
103 // convert identical underlying types
104 // TODO(rsc): Should be able to define MyBuffer here.
105 // 6l prints very strange messages about .this.Bytes etc
106 // when we do that though, so MyBuffer is defined
108 m
:= make(map[*MyBuffer
]*bytes
.Buffer
)
111 b2
:= new(bytes
.Buffer
)
112 mv
.SetMapIndex(ValueOf(b1
), ValueOf(b2
))
115 t
.Errorf("#7 after SetMapIndex(b1, b2): %p (!= %p), %t (map=%v)", x
, b2
, ok
, m
)
117 if p
:= mv
.MapIndex(ValueOf(b1
)).Pointer(); p
!= uintptr(unsafe
.Pointer(b2
)) {
118 t
.Errorf("#7 MapIndex(b1) = %#x want %p", p
, b2
)
124 func TestImplicitSetConversion(t
*testing
.T
) {
125 // Assume TestImplicitMapConversion covered the basics.
126 // Just make sure conversions are being applied at all.
128 b
:= new(bytes
.Buffer
)
129 rv
:= ValueOf(&r
).Elem()
132 t
.Errorf("after Set: r=%T(%v)", r
, r
)
136 func TestImplicitSendConversion(t
*testing
.T
) {
137 c
:= make(chan io
.Reader
, 10)
138 b
:= new(bytes
.Buffer
)
139 ValueOf(c
).Send(ValueOf(b
))
140 if bb
:= <-c
; bb
!= b
{
141 t
.Errorf("Received %p != %p", bb
, b
)
145 func TestImplicitCallConversion(t
*testing
.T
) {
146 // Arguments must be assignable to parameter types.
147 fv
:= ValueOf(io
.WriteString
)
148 b
:= new(bytes
.Buffer
)
149 fv
.Call([]Value
{ValueOf(b
), ValueOf("hello world")})
150 if b
.String() != "hello world" {
151 t
.Errorf("After call: string=%q want %q", b
.String(), "hello world")
155 func TestImplicitAppendConversion(t
*testing
.T
) {
156 // Arguments must be assignable to the slice's element type.
158 sv
:= ValueOf(&s
).Elem()
159 b
:= new(bytes
.Buffer
)
160 sv
.Set(Append(sv
, ValueOf(b
)))
161 if len(s
) != 1 || s
[0] != b
{
162 t
.Errorf("after append: s=%v want [%p]", s
, b
)
166 var implementsTests
= []struct {
171 {new(*bytes
.Buffer
), new(io
.Reader
), true},
172 {new(bytes
.Buffer
), new(io
.Reader
), false},
173 {new(*bytes
.Buffer
), new(io
.ReaderAt
), false},
174 {new(*ast
.Ident
), new(ast
.Expr
), true},
177 func TestImplements(t
*testing
.T
) {
178 for _
, tt
:= range implementsTests
{
179 xv
:= TypeOf(tt
.x
).Elem()
180 xt
:= TypeOf(tt
.t
).Elem()
181 if b
:= xv
.Implements(xt
); b
!= tt
.b
{
182 t
.Errorf("(%s).Implements(%s) = %v, want %v", xv
.String(), xt
.String(), b
, tt
.b
)
187 var assignableTests
= []struct {
192 {new(chan int), new(<-chan int), true},
193 {new(<-chan int), new(chan int), false},
194 {new(*int), new(IntPtr
), true},
195 {new(IntPtr
), new(*int), true},
196 {new(IntPtr
), new(IntPtr1
), false},
197 // test runs implementsTests too
203 func TestAssignableTo(t
*testing
.T
) {
204 for _
, tt
:= range append(assignableTests
, implementsTests
...) {
205 xv
:= TypeOf(tt
.x
).Elem()
206 xt
:= TypeOf(tt
.t
).Elem()
207 if b
:= xv
.AssignableTo(xt
); b
!= tt
.b
{
208 t
.Errorf("(%s).AssignableTo(%s) = %v, want %v", xv
.String(), xt
.String(), b
, tt
.b
)