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