github.com/euank/go@v0.0.0-20160829210321-495514729181/src/cmd/compile/internal/gc/testdata/assert_ssa.go (about)

     1  // run
     2  
     3  // Copyright 2015 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  // Tests type assertion expressions and statements
     8  
     9  package main
    10  
    11  import (
    12  	"fmt"
    13  	"runtime"
    14  )
    15  
    16  type (
    17  	S struct{}
    18  	T struct{}
    19  
    20  	I interface {
    21  		F()
    22  	}
    23  )
    24  
    25  var (
    26  	s *S
    27  	t *T
    28  )
    29  
    30  func (s *S) F() {}
    31  func (t *T) F() {}
    32  
    33  func e2t_ssa(e interface{}) *T {
    34  	return e.(*T)
    35  }
    36  
    37  func i2t_ssa(i I) *T {
    38  	return i.(*T)
    39  }
    40  
    41  func testAssertE2TOk() {
    42  	if got := e2t_ssa(t); got != t {
    43  		fmt.Printf("e2t_ssa(t)=%v want %v", got, t)
    44  		failed = true
    45  	}
    46  }
    47  
    48  func testAssertE2TPanic() {
    49  	var got *T
    50  	defer func() {
    51  		if got != nil {
    52  			fmt.Printf("e2t_ssa(s)=%v want nil", got)
    53  			failed = true
    54  		}
    55  		e := recover()
    56  		err, ok := e.(*runtime.TypeAssertionError)
    57  		if !ok {
    58  			fmt.Printf("e2t_ssa(s) panic type %T", e)
    59  			failed = true
    60  		}
    61  		want := "interface conversion: interface {} is *main.S, not *main.T"
    62  		if err.Error() != want {
    63  			fmt.Printf("e2t_ssa(s) wrong error, want '%s', got '%s'\n", want, err.Error())
    64  			failed = true
    65  		}
    66  	}()
    67  	got = e2t_ssa(s)
    68  	fmt.Printf("e2t_ssa(s) should panic")
    69  	failed = true
    70  }
    71  
    72  func testAssertI2TOk() {
    73  	if got := i2t_ssa(t); got != t {
    74  		fmt.Printf("i2t_ssa(t)=%v want %v", got, t)
    75  		failed = true
    76  	}
    77  }
    78  
    79  func testAssertI2TPanic() {
    80  	var got *T
    81  	defer func() {
    82  		if got != nil {
    83  			fmt.Printf("i2t_ssa(s)=%v want nil", got)
    84  			failed = true
    85  		}
    86  		e := recover()
    87  		err, ok := e.(*runtime.TypeAssertionError)
    88  		if !ok {
    89  			fmt.Printf("i2t_ssa(s) panic type %T", e)
    90  			failed = true
    91  		}
    92  		want := "interface conversion: main.I is *main.S, not *main.T"
    93  		if err.Error() != want {
    94  			fmt.Printf("i2t_ssa(s) wrong error, want '%s', got '%s'\n", want, err.Error())
    95  			failed = true
    96  		}
    97  	}()
    98  	got = i2t_ssa(s)
    99  	fmt.Printf("i2t_ssa(s) should panic")
   100  	failed = true
   101  }
   102  
   103  func e2t2_ssa(e interface{}) (*T, bool) {
   104  	t, ok := e.(*T)
   105  	return t, ok
   106  }
   107  
   108  func i2t2_ssa(i I) (*T, bool) {
   109  	t, ok := i.(*T)
   110  	return t, ok
   111  }
   112  
   113  func testAssertE2T2() {
   114  	if got, ok := e2t2_ssa(t); !ok || got != t {
   115  		fmt.Printf("e2t2_ssa(t)=(%v, %v) want (%v, %v)", got, ok, t, true)
   116  		failed = true
   117  	}
   118  	if got, ok := e2t2_ssa(s); ok || got != nil {
   119  		fmt.Printf("e2t2_ssa(s)=(%v, %v) want (%v, %v)", got, ok, nil, false)
   120  		failed = true
   121  	}
   122  }
   123  
   124  func testAssertI2T2() {
   125  	if got, ok := i2t2_ssa(t); !ok || got != t {
   126  		fmt.Printf("i2t2_ssa(t)=(%v, %v) want (%v, %v)", got, ok, t, true)
   127  		failed = true
   128  	}
   129  	if got, ok := i2t2_ssa(s); ok || got != nil {
   130  		fmt.Printf("i2t2_ssa(s)=(%v, %v) want (%v, %v)", got, ok, nil, false)
   131  		failed = true
   132  	}
   133  }
   134  
   135  var failed = false
   136  
   137  func main() {
   138  	testAssertE2TOk()
   139  	testAssertE2TPanic()
   140  	testAssertI2TOk()
   141  	testAssertI2TPanic()
   142  	testAssertE2T2()
   143  	testAssertI2T2()
   144  	if failed {
   145  		panic("failed")
   146  	}
   147  }