Source file src/cmd/compile/internal/test/iface_test.go

     1  // Copyright 2016 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  
     5  package test
     6  
     7  import "testing"
     8  
     9  // Test to make sure we make copies of the values we
    10  // put in interfaces.
    11  
    12  var x int
    13  
    14  func TestEfaceConv1(t *testing.T) {
    15  	a := 5
    16  	i := interface{}(a)
    17  	a += 2
    18  	if got := i.(int); got != 5 {
    19  		t.Errorf("wanted 5, got %d\n", got)
    20  	}
    21  }
    22  
    23  func TestEfaceConv2(t *testing.T) {
    24  	a := 5
    25  	sink = &a
    26  	i := interface{}(a)
    27  	a += 2
    28  	if got := i.(int); got != 5 {
    29  		t.Errorf("wanted 5, got %d\n", got)
    30  	}
    31  }
    32  
    33  func TestEfaceConv3(t *testing.T) {
    34  	x = 5
    35  	if got := e2int3(x); got != 5 {
    36  		t.Errorf("wanted 5, got %d\n", got)
    37  	}
    38  }
    39  
    40  //go:noinline
    41  func e2int3(i interface{}) int {
    42  	x = 7
    43  	return i.(int)
    44  }
    45  
    46  func TestEfaceConv4(t *testing.T) {
    47  	a := 5
    48  	if got := e2int4(a, &a); got != 5 {
    49  		t.Errorf("wanted 5, got %d\n", got)
    50  	}
    51  }
    52  
    53  //go:noinline
    54  func e2int4(i interface{}, p *int) int {
    55  	*p = 7
    56  	return i.(int)
    57  }
    58  
    59  type Int int
    60  
    61  var y Int
    62  
    63  type I interface {
    64  	foo()
    65  }
    66  
    67  func (i Int) foo() {
    68  }
    69  
    70  func TestIfaceConv1(t *testing.T) {
    71  	a := Int(5)
    72  	i := interface{}(a)
    73  	a += 2
    74  	if got := i.(Int); got != 5 {
    75  		t.Errorf("wanted 5, got %d\n", int(got))
    76  	}
    77  }
    78  
    79  func TestIfaceConv2(t *testing.T) {
    80  	a := Int(5)
    81  	sink = &a
    82  	i := interface{}(a)
    83  	a += 2
    84  	if got := i.(Int); got != 5 {
    85  		t.Errorf("wanted 5, got %d\n", int(got))
    86  	}
    87  }
    88  
    89  func TestIfaceConv3(t *testing.T) {
    90  	y = 5
    91  	if got := i2Int3(y); got != 5 {
    92  		t.Errorf("wanted 5, got %d\n", int(got))
    93  	}
    94  }
    95  
    96  //go:noinline
    97  func i2Int3(i I) Int {
    98  	y = 7
    99  	return i.(Int)
   100  }
   101  
   102  func TestIfaceConv4(t *testing.T) {
   103  	a := Int(5)
   104  	if got := i2Int4(a, &a); got != 5 {
   105  		t.Errorf("wanted 5, got %d\n", int(got))
   106  	}
   107  }
   108  
   109  //go:noinline
   110  func i2Int4(i I, p *Int) Int {
   111  	*p = 7
   112  	return i.(Int)
   113  }
   114  
   115  func BenchmarkEfaceInteger(b *testing.B) {
   116  	sum := 0
   117  	for i := 0; i < b.N; i++ {
   118  		sum += i2int(i)
   119  	}
   120  	sink = sum
   121  }
   122  
   123  //go:noinline
   124  func i2int(i interface{}) int {
   125  	return i.(int)
   126  }
   127  

View as plain text