set_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.
  4. package reflect_test
  5. import (
  6. "bytes"
  7. "go/ast"
  8. "io"
  9. . "reflect"
  10. "testing"
  11. "unsafe"
  12. )
  13. type MyBuffer bytes.Buffer
  14. func TestImplicitMapConversion(t *testing.T) {
  15. // Test implicit conversions in MapIndex and SetMapIndex.
  16. {
  17. // direct
  18. m := make(map[int]int)
  19. mv := ValueOf(m)
  20. mv.SetMapIndex(ValueOf(1), ValueOf(2))
  21. x, ok := m[1]
  22. if x != 2 {
  23. t.Errorf("#1 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
  24. }
  25. if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
  26. t.Errorf("#1 MapIndex(1) = %d", n)
  27. }
  28. }
  29. {
  30. // convert interface key
  31. m := make(map[interface{}]int)
  32. mv := ValueOf(m)
  33. mv.SetMapIndex(ValueOf(1), ValueOf(2))
  34. x, ok := m[1]
  35. if x != 2 {
  36. t.Errorf("#2 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
  37. }
  38. if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
  39. t.Errorf("#2 MapIndex(1) = %d", n)
  40. }
  41. }
  42. {
  43. // convert interface value
  44. m := make(map[int]interface{})
  45. mv := ValueOf(m)
  46. mv.SetMapIndex(ValueOf(1), ValueOf(2))
  47. x, ok := m[1]
  48. if x != 2 {
  49. t.Errorf("#3 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
  50. }
  51. if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
  52. t.Errorf("#3 MapIndex(1) = %d", n)
  53. }
  54. }
  55. {
  56. // convert both interface key and interface value
  57. m := make(map[interface{}]interface{})
  58. mv := ValueOf(m)
  59. mv.SetMapIndex(ValueOf(1), ValueOf(2))
  60. x, ok := m[1]
  61. if x != 2 {
  62. t.Errorf("#4 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
  63. }
  64. if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
  65. t.Errorf("#4 MapIndex(1) = %d", n)
  66. }
  67. }
  68. {
  69. // convert both, with non-empty interfaces
  70. m := make(map[io.Reader]io.Writer)
  71. mv := ValueOf(m)
  72. b1 := new(bytes.Buffer)
  73. b2 := new(bytes.Buffer)
  74. mv.SetMapIndex(ValueOf(b1), ValueOf(b2))
  75. x, ok := m[b1]
  76. if x != b2 {
  77. t.Errorf("#5 after SetMapIndex(b1, b2): %p (!= %p), %t (map=%v)", x, b2, ok, m)
  78. }
  79. if p := mv.MapIndex(ValueOf(b1)).Elem().Pointer(); p != uintptr(unsafe.Pointer(b2)) {
  80. t.Errorf("#5 MapIndex(b1) = %#x want %p", p, b2)
  81. }
  82. }
  83. {
  84. // convert channel direction
  85. m := make(map[<-chan int]chan int)
  86. mv := ValueOf(m)
  87. c1 := make(chan int)
  88. c2 := make(chan int)
  89. mv.SetMapIndex(ValueOf(c1), ValueOf(c2))
  90. x, ok := m[c1]
  91. if x != c2 {
  92. t.Errorf("#6 after SetMapIndex(c1, c2): %p (!= %p), %t (map=%v)", x, c2, ok, m)
  93. }
  94. if p := mv.MapIndex(ValueOf(c1)).Pointer(); p != ValueOf(c2).Pointer() {
  95. t.Errorf("#6 MapIndex(c1) = %#x want %p", p, c2)
  96. }
  97. }
  98. {
  99. // convert identical underlying types
  100. // TODO(rsc): Should be able to define MyBuffer here.
  101. // 6l prints very strange messages about .this.Bytes etc
  102. // when we do that though, so MyBuffer is defined
  103. // at top level.
  104. m := make(map[*MyBuffer]*bytes.Buffer)
  105. mv := ValueOf(m)
  106. b1 := new(MyBuffer)
  107. b2 := new(bytes.Buffer)
  108. mv.SetMapIndex(ValueOf(b1), ValueOf(b2))
  109. x, ok := m[b1]
  110. if x != b2 {
  111. t.Errorf("#7 after SetMapIndex(b1, b2): %p (!= %p), %t (map=%v)", x, b2, ok, m)
  112. }
  113. if p := mv.MapIndex(ValueOf(b1)).Pointer(); p != uintptr(unsafe.Pointer(b2)) {
  114. t.Errorf("#7 MapIndex(b1) = %#x want %p", p, b2)
  115. }
  116. }
  117. }
  118. func TestImplicitSetConversion(t *testing.T) {
  119. // Assume TestImplicitMapConversion covered the basics.
  120. // Just make sure conversions are being applied at all.
  121. var r io.Reader
  122. b := new(bytes.Buffer)
  123. rv := ValueOf(&r).Elem()
  124. rv.Set(ValueOf(b))
  125. if r != b {
  126. t.Errorf("after Set: r=%T(%v)", r, r)
  127. }
  128. }
  129. func TestImplicitSendConversion(t *testing.T) {
  130. c := make(chan io.Reader, 10)
  131. b := new(bytes.Buffer)
  132. ValueOf(c).Send(ValueOf(b))
  133. if bb := <-c; bb != b {
  134. t.Errorf("Received %p != %p", bb, b)
  135. }
  136. }
  137. func TestImplicitCallConversion(t *testing.T) {
  138. // Arguments must be assignable to parameter types.
  139. fv := ValueOf(io.WriteString)
  140. b := new(bytes.Buffer)
  141. fv.Call([]Value{ValueOf(b), ValueOf("hello world")})
  142. if b.String() != "hello world" {
  143. t.Errorf("After call: string=%q want %q", b.String(), "hello world")
  144. }
  145. }
  146. func TestImplicitAppendConversion(t *testing.T) {
  147. // Arguments must be assignable to the slice's element type.
  148. s := []io.Reader{}
  149. sv := ValueOf(&s).Elem()
  150. b := new(bytes.Buffer)
  151. sv.Set(Append(sv, ValueOf(b)))
  152. if len(s) != 1 || s[0] != b {
  153. t.Errorf("after append: s=%v want [%p]", s, b)
  154. }
  155. }
  156. var implementsTests = []struct {
  157. x interface{}
  158. t interface{}
  159. b bool
  160. }{
  161. {new(*bytes.Buffer), new(io.Reader), true},
  162. {new(bytes.Buffer), new(io.Reader), false},
  163. {new(*bytes.Buffer), new(io.ReaderAt), false},
  164. {new(*ast.Ident), new(ast.Expr), true},
  165. }
  166. func TestImplements(t *testing.T) {
  167. for _, tt := range implementsTests {
  168. xv := TypeOf(tt.x).Elem()
  169. xt := TypeOf(tt.t).Elem()
  170. if b := xv.Implements(xt); b != tt.b {
  171. t.Errorf("(%s).Implements(%s) = %v, want %v", xv.String(), xt.String(), b, tt.b)
  172. }
  173. }
  174. }
  175. var assignableTests = []struct {
  176. x interface{}
  177. t interface{}
  178. b bool
  179. }{
  180. {new(chan int), new(<-chan int), true},
  181. {new(<-chan int), new(chan int), false},
  182. {new(*int), new(IntPtr), true},
  183. {new(IntPtr), new(*int), true},
  184. {new(IntPtr), new(IntPtr1), false},
  185. // test runs implementsTests too
  186. }
  187. type IntPtr *int
  188. type IntPtr1 *int
  189. func TestAssignableTo(t *testing.T) {
  190. for _, tt := range append(assignableTests, implementsTests...) {
  191. xv := TypeOf(tt.x).Elem()
  192. xt := TypeOf(tt.t).Elem()
  193. if b := xv.AssignableTo(xt); b != tt.b {
  194. t.Errorf("(%s).AssignableTo(%s) = %v, want %v", xv.String(), xt.String(), b, tt.b)
  195. }
  196. }
  197. }