github.com/ice-blockchain/go/src@v0.0.0-20240403114104-1564d284e521/reflect/type_test.go (about)

     1  // Copyright 2023 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 reflect_test
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  func TestTypeFor(t *testing.T) {
    13  	type (
    14  		mystring string
    15  		myiface  interface{}
    16  	)
    17  
    18  	testcases := []struct {
    19  		wantFrom any
    20  		got      reflect.Type
    21  	}{
    22  		{new(int), reflect.TypeFor[int]()},
    23  		{new(int64), reflect.TypeFor[int64]()},
    24  		{new(string), reflect.TypeFor[string]()},
    25  		{new(mystring), reflect.TypeFor[mystring]()},
    26  		{new(any), reflect.TypeFor[any]()},
    27  		{new(myiface), reflect.TypeFor[myiface]()},
    28  	}
    29  	for _, tc := range testcases {
    30  		want := reflect.ValueOf(tc.wantFrom).Elem().Type()
    31  		if want != tc.got {
    32  			t.Errorf("unexpected reflect.Type: got %v; want %v", tc.got, want)
    33  		}
    34  	}
    35  }
    36  
    37  func TestStructOfEmbeddedIfaceMethodCall(t *testing.T) {
    38  	type Named interface {
    39  		Name() string
    40  	}
    41  
    42  	typ := reflect.StructOf([]reflect.StructField{
    43  		{
    44  			Anonymous: true,
    45  			Name:      "Named",
    46  			Type:      reflect.TypeFor[Named](),
    47  		},
    48  	})
    49  
    50  	v := reflect.New(typ).Elem()
    51  	v.Field(0).Set(
    52  		reflect.ValueOf(reflect.TypeFor[string]()),
    53  	)
    54  
    55  	x := v.Interface().(Named)
    56  	shouldPanic("StructOf does not support methods of embedded interfaces", func() {
    57  		_ = x.Name()
    58  	})
    59  }
    60  
    61  func TestIsRegularMemory(t *testing.T) {
    62  	type args struct {
    63  		t reflect.Type
    64  	}
    65  	type S struct {
    66  		int
    67  	}
    68  	tests := []struct {
    69  		name string
    70  		args args
    71  		want bool
    72  	}{
    73  		{"struct{i int}", args{reflect.TypeOf(struct{ i int }{})}, true},
    74  		{"struct{}", args{reflect.TypeOf(struct{}{})}, true},
    75  		{"struct{i int; s S}", args{reflect.TypeOf(struct {
    76  			i int
    77  			s S
    78  		}{})}, true},
    79  		{"map[int][int]", args{reflect.TypeOf(map[int]int{})}, false},
    80  		{"[4]chan int", args{reflect.TypeOf([4]chan int{})}, true},
    81  		{"[0]struct{_ S}", args{reflect.TypeOf([0]struct {
    82  			_ S
    83  		}{})}, true},
    84  		{"struct{i int; _ S}", args{reflect.TypeOf(struct {
    85  			i int
    86  			_ S
    87  		}{})}, false},
    88  		{"struct{a int16; b int32}", args{reflect.TypeOf(struct {
    89  			a int16
    90  			b int32
    91  		}{})}, false},
    92  		{"struct {x int32; y int16}", args{reflect.TypeOf(struct {
    93  			x int32
    94  			y int16
    95  		}{})}, false},
    96  		{"struct {_ int32 }", args{reflect.TypeOf(struct{ _ int32 }{})}, false},
    97  	}
    98  	for _, tt := range tests {
    99  		t.Run(tt.name, func(t *testing.T) {
   100  			if got := reflect.IsRegularMemory(tt.args.t); got != tt.want {
   101  				t.Errorf("isRegularMemory() = %v, want %v", got, tt.want)
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  var sinkType reflect.Type
   108  
   109  func BenchmarkTypeForString(b *testing.B) {
   110  	for i := 0; i < b.N; i++ {
   111  		sinkType = reflect.TypeFor[string]()
   112  	}
   113  }
   114  
   115  func BenchmarkTypeForError(b *testing.B) {
   116  	for i := 0; i < b.N; i++ {
   117  		sinkType = reflect.TypeFor[error]()
   118  	}
   119  }