github.com/fjl/memsize@v0.0.2/type_test.go (about)

     1  package memsize
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  var typCacheTests = []struct {
     9  	val  interface{}
    10  	want typInfo
    11  }{
    12  	{
    13  		val:  int(0),
    14  		want: typInfo{isPointer: false, needScan: false},
    15  	},
    16  	{
    17  		val:  make(chan struct{}, 1),
    18  		want: typInfo{isPointer: true, needScan: true},
    19  	},
    20  	{
    21  		val:  struct{ A int }{},
    22  		want: typInfo{isPointer: false, needScan: false},
    23  	},
    24  	{
    25  		val:  struct{ S string }{},
    26  		want: typInfo{isPointer: false, needScan: true},
    27  	},
    28  	{
    29  		val:  structloop{},
    30  		want: typInfo{isPointer: false, needScan: true},
    31  	},
    32  	{
    33  		val:  [3]int{},
    34  		want: typInfo{isPointer: false, needScan: false},
    35  	},
    36  	{
    37  		val:  [3]struct{ A int }{},
    38  		want: typInfo{isPointer: false, needScan: false},
    39  	},
    40  	{
    41  		val:  [3]struct{ S string }{},
    42  		want: typInfo{isPointer: false, needScan: true},
    43  	},
    44  	{
    45  		val:  [3]structloop{},
    46  		want: typInfo{isPointer: false, needScan: true},
    47  	},
    48  	{
    49  		val: struct {
    50  			a [32]uint8
    51  			s [2][]uint8
    52  		}{},
    53  		want: typInfo{isPointer: false, needScan: true},
    54  	},
    55  }
    56  
    57  func TestTypeInfo(t *testing.T) {
    58  	// This cache is shared among all test cases. It is used
    59  	// to verify that putting many different types into the cache
    60  	// doesn't change the resulting info.
    61  	sharedtc := make(typCache)
    62  
    63  	for i := range typCacheTests {
    64  		test := typCacheTests[i]
    65  		typ := reflect.TypeOf(test.val)
    66  		t.Run(typ.String(), func(t *testing.T) {
    67  			tc := make(typCache)
    68  			info := tc.info(typ)
    69  			if !reflect.DeepEqual(info, test.want) {
    70  				t.Fatalf("wrong info from local cache:\ngot %+v, want %+v", info, test.want)
    71  			}
    72  			info = sharedtc.info(typ)
    73  			if !reflect.DeepEqual(info, test.want) {
    74  				t.Fatalf("wrong info from shared cache:\ngot %+v, want %+v", info, test.want)
    75  			}
    76  		})
    77  	}
    78  }