...
Run Format

Source file test/convT2X.go

Documentation: test

     1  // run
     2  
     3  // Copyright 2012 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  // Test conversion from non-interface types to the empty interface.
     8  
     9  package main
    10  
    11  type J interface {
    12  	Method()
    13  }
    14  
    15  type (
    16  	U16  uint16
    17  	U32  uint32
    18  	U64  uint64
    19  	U128 [2]uint64
    20  	F32  float32
    21  	F64  float64
    22  	C128 complex128
    23  	S    string
    24  	B    []byte
    25  	M    map[int]int
    26  	C    chan int
    27  	Z    struct{}
    28  )
    29  
    30  func (U16) Method()  {}
    31  func (U32) Method()  {}
    32  func (U64) Method()  {}
    33  func (U128) Method() {}
    34  func (F32) Method()  {}
    35  func (F64) Method()  {}
    36  func (C128) Method() {}
    37  func (S) Method()    {}
    38  func (B) Method()    {}
    39  func (M) Method()    {}
    40  func (C) Method()    {}
    41  func (Z) Method()    {}
    42  
    43  var (
    44  	u16  = U16(1)
    45  	u32  = U32(2)
    46  	u64  = U64(3)
    47  	u128 = U128{4, 5}
    48  	f32  = F32(6)
    49  	f64  = F64(7)
    50  	c128 = C128(8 + 9i)
    51  	s    = S("10")
    52  	b    = B("11")
    53  	m    = M{12: 13}
    54  	c    = make(C, 14)
    55  	z    = Z{}
    56  	p    = &z
    57  	pp   = &p
    58  )
    59  
    60  var (
    61  	iu16  interface{} = u16
    62  	iu32  interface{} = u32
    63  	iu64  interface{} = u64
    64  	iu128 interface{} = u128
    65  	if32  interface{} = f32
    66  	if64  interface{} = f64
    67  	ic128 interface{} = c128
    68  	is    interface{} = s
    69  	ib    interface{} = b
    70  	im    interface{} = m
    71  	ic    interface{} = c
    72  	iz    interface{} = z
    73  	ip    interface{} = p
    74  	ipp   interface{} = pp
    75  
    76  	ju16  J = u16
    77  	ju32  J = u32
    78  	ju64  J = u64
    79  	ju128 J = u128
    80  	jf32  J = f32
    81  	jf64  J = f64
    82  	jc128 J = c128
    83  	js    J = s
    84  	jb    J = b
    85  	jm    J = m
    86  	jc    J = c
    87  	jz J = z
    88  	jp J = p // The method set for *T contains the methods for T.
    89  	// pp does not implement error.
    90  )
    91  
    92  func second(a ...interface{}) interface{} {
    93  	return a[1]
    94  }
    95  
    96  func main() {
    97  	// Test equality.
    98  	if u16 != iu16 {
    99  		panic("u16 != iu16")
   100  	}
   101  	if u16 != ju16 {
   102  		panic("u16 != ju16")
   103  	}
   104  	if u32 != iu32 {
   105  		panic("u32 != iu32")
   106  	}
   107  	if u32 != ju32 {
   108  		panic("u32 != ju32")
   109  	}
   110  	if u64 != iu64 {
   111  		panic("u64 != iu64")
   112  	}
   113  	if u64 != ju64 {
   114  		panic("u64 != ju64")
   115  	}
   116  	if u128 != iu128 {
   117  		panic("u128 != iu128")
   118  	}
   119  	if u128 != ju128 {
   120  		panic("u128 != ju128")
   121  	}
   122  	if f32 != if32 {
   123  		panic("f32 != if32")
   124  	}
   125  	if f32 != jf32 {
   126  		panic("f32 != jf32")
   127  	}
   128  	if f64 != if64 {
   129  		panic("f64 != if64")
   130  	}
   131  	if f64 != jf64 {
   132  		panic("f64 != jf64")
   133  	}
   134  	if c128 != ic128 {
   135  		panic("c128 != ic128")
   136  	}
   137  	if c128 != jc128 {
   138  		panic("c128 != jc128")
   139  	}
   140  	if s != is {
   141  		panic("s != is")
   142  	}
   143  	if s != js {
   144  		panic("s != js")
   145  	}
   146  	if c != ic {
   147  		panic("c != ic")
   148  	}
   149  	if c != jc {
   150  		panic("c != jc")
   151  	}
   152  	// There are no tests for b and m, as slices and maps are not comparable by ==.
   153  	if z != iz {
   154  		panic("z != iz")
   155  	}
   156  	if z != jz {
   157  		panic("z != jz")
   158  	}
   159  	if p != ip {
   160  		panic("p != ip")
   161  	}
   162  	if p != jp {
   163  		panic("p != jp")
   164  	}
   165  	if pp != ipp {
   166  		panic("pp != ipp")
   167  	}
   168  	// pp does not implement J.
   169  
   170  	// Test that non-interface types can be used as ...interface{} arguments.
   171  	if got := second(z, p, pp, u16, u32, u64, u128, f32, f64, c128, s, b, m, c); got != ip {
   172  		println("second: got", got, "want", ip)
   173  		panic("fail")
   174  	}
   175  
   176  	// Test that non-interface types can be sent on a chan interface{}.
   177  	const n = 100
   178  	uc := make(chan interface{})
   179  	go func() {
   180  		for i := 0; i < n; i++ {
   181  			select {
   182  			case uc <- nil:
   183  			case uc <- u32:
   184  			case uc <- u64:
   185  			case uc <- u128:
   186  			}
   187  		}
   188  	}()
   189  	for i := 0; i < n; i++ {
   190  		if got := <-uc; got != nil && got != u32 && got != u64 && got != u128 {
   191  			println("recv: i", i, "got", got)
   192  			panic("fail")
   193  		}
   194  	}
   195  }
   196  

View as plain text