github.com/hedzr/evendeep@v0.4.8/internal/tool/ref_test.go (about)

     1  package tool_test
     2  
     3  import (
     4  	"github.com/hedzr/evendeep/internal/tool"
     5  	"github.com/hedzr/evendeep/typ"
     6  
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestRdecode(t *testing.T) {
    12  	b := 12
    13  	c := &b
    14  
    15  	vb := reflect.ValueOf(b)
    16  	t.Logf("          vb (%v (%v)) : %v, &b = %v", vb.Kind(), vb.Type(), vb.Interface(), &b)
    17  
    18  	vc := reflect.ValueOf(c)
    19  	t.Logf("         vc (%v (%v)) : %v, &b = %v, c = %v, *c -> %v", vc.Kind(), vc.Type(), vc.Interface(), &b, c, *c)
    20  
    21  	var ii typ.Any // nolint:gosimple
    22  
    23  	ii = c
    24  
    25  	vi := reflect.ValueOf(&ii)
    26  	t.Logf("vi (%v (%v)) : %v, &b = %v", vi.Kind(), vi.Type(), vi.Interface(), &b)
    27  
    28  	vv, prev := tool.Rdecode(vi)
    29  	t.Logf("          vv (%v (%v)) : %v, &b = %v [ rdecode(vi) ]", vc.Kind(), vv.Type(), vv.Interface(), &b)
    30  	value := prev.Interface()
    31  	valptr := value.(*int) //nolint:errcheck //no need
    32  	t.Logf("       prev (%v (%v)) : %v -> %v", prev.Kind(), prev.Type(), value, *valptr)
    33  
    34  	// A result likes:
    35  	//
    36  	//    vb (int (int)) : 12, &b = 0xc00001e350
    37  	//    vc (ptr (*int)) : 0xc00001e350, &b = 0xc00001e350, c = 0xc00001e350, *c -> 12
    38  	//    vi (ptr (*interface {})) : 0xc00006c640, &b = 0xc00001e350
    39  	//    vv (ptr (int)) : 12, &b = 0xc00001e350 [ rdecode(vi) ]
    40  	//    prev (ptr (*int)) : 0xc00001e350 -> 12
    41  }
    42  
    43  func Test1(t *testing.T) {
    44  	b := 12
    45  
    46  	vb := reflect.ValueOf(b)
    47  	t.Logf("vb (%v (%v)) : %v, &b = %v", vb.Kind(), vb.Type(), vb.Interface(), &b)
    48  
    49  	var ii typ.Any // nolint:gosimple
    50  
    51  	ii = b
    52  	vi := reflect.ValueOf(&ii)
    53  	t.Logf("vi (%v (%v)) : %v, &b = %v", vi.Kind(), vi.Type(), vi.Interface(), &b)
    54  	vi = vi.Elem()
    55  	t.Logf("vi (%v (%v)) : %v, &b = %v", vi.Kind(), vi.Type(), vi.Interface(), &b)
    56  	vi = vi.Elem()
    57  	t.Logf("vi (%v (%v)) : %v, &b = %v", vi.Kind(), vi.Type(), vi.Interface(), &b)
    58  
    59  	// nolint:gocritic //no
    60  	// var up = vi.Addr()
    61  	// t.Logf("up = %v", up)
    62  }
    63  
    64  func TestRskipRdecodeAndSoOn(t *testing.T) {
    65  	b := &Employee{Name: "Bob"}
    66  
    67  	vb := reflect.ValueOf(b)
    68  	t.Logf("vb (%v (%v)) : %v, &b = %v", vb.Kind(), vb.Type(), vb.Interface(), &b)
    69  
    70  	var ii typ.Any
    71  
    72  	ii = b
    73  	vi := reflect.ValueOf(&ii)
    74  	kind := vi.Kind()
    75  	t.Logf("vi (%v (%v)) : %v, &b = %v", kind, vi.Type(), vi.Interface(), &b)
    76  	vi = vi.Elem()
    77  	t.Logf("vi (%v (%v)) : %v, &b = %v", vi.Kind(), vi.Type(), vi.Interface(), &b)
    78  	vi = vi.Elem()
    79  	t.Logf("vi (%v (%v)) : %v, &b = %v", vi.Kind(), vi.Type(), vi.Interface(), &b)
    80  	vi = vi.Elem()
    81  	t.Logf("vi (%v (%v)) : %v, &b = %v", vi.Kind(), vi.Type(), vi.Interface(), &b)
    82  
    83  	var prev reflect.Value
    84  	v2 := reflect.ValueOf(&ii)
    85  
    86  	// nolint:gocritic //no
    87  	// c := newCopier()
    88  
    89  	v2, prev = tool.Rskip(v2, reflect.Interface, reflect.Ptr)
    90  	t.Logf("v2 (%v (%v)) : %v, prev (%v %v)", v2.Kind(), v2.Type(), v2.Interface(), prev.Kind(), prev.Type())
    91  
    92  	ii = &b
    93  	v2 = reflect.ValueOf(&ii)
    94  	v2, prev = tool.Rdecode(v2)
    95  	t.Logf("v2 (%v (%v)) : %v, prev (%v %v)", v2.Kind(), v2.Type(), v2.Interface(), prev.Kind(), prev.Type())
    96  
    97  	// nolint:gocritic //no
    98  	// var up = vi.Addr()
    99  	// t.Logf("up = %v", up)
   100  }
   101  
   102  func assertYes(t *testing.T, b bool, expect, got interface{}) {
   103  	if !b {
   104  		t.Fatalf("expecting %v but got %v", expect, got)
   105  	}
   106  }
   107  
   108  func TestIsNumXXX(t *testing.T) {
   109  	var x1 interface{} = 9527 + 0i
   110  	var v1 = reflect.ValueOf(x1)
   111  
   112  	assertYes(t, tool.IsNumericType(v1.Type()) == true, true, false)
   113  	assertYes(t, tool.IsNumComplexKind(v1.Kind()) == true, true, false)
   114  
   115  	x1 = "ok"
   116  	v1 = reflect.ValueOf(x1)
   117  	assertYes(t, tool.IsNumericType(v1.Type()) != true, false, true)
   118  
   119  	//
   120  
   121  	x1 = 13
   122  	v1 = reflect.ValueOf(x1)
   123  	assertYes(t, tool.IsNumIntegerType(v1.Type()) == true, true, false)
   124  
   125  	x1 = uint(13)
   126  	v1 = reflect.ValueOf(x1)
   127  	assertYes(t, tool.IsNumIntegerType(v1.Type()) == true, true, false)
   128  
   129  	x1 = float32(13)
   130  	v1 = reflect.ValueOf(x1)
   131  	assertYes(t, tool.IsNumIntegerType(v1.Type()) != true, false, true)
   132  
   133  	//
   134  
   135  	x1 = 13
   136  	v1 = reflect.ValueOf(x1)
   137  	assertYes(t, tool.IsNumSIntegerKind(v1.Kind()) == true, true, false)
   138  
   139  	x1 = uint(13)
   140  	v1 = reflect.ValueOf(x1)
   141  	assertYes(t, tool.IsNumSIntegerKind(v1.Kind()) != true, false, true)
   142  
   143  	x1 = float32(13)
   144  	v1 = reflect.ValueOf(x1)
   145  	assertYes(t, tool.IsNumSIntegerKind(v1.Kind()) != true, false, true)
   146  
   147  	//
   148  
   149  	x1 = uint(13)
   150  	v1 = reflect.ValueOf(x1)
   151  	assertYes(t, tool.IsNumUIntegerKind(v1.Kind()) == true, true, false)
   152  
   153  	x1 = int(13)
   154  	v1 = reflect.ValueOf(x1)
   155  	assertYes(t, tool.IsNumUIntegerKind(v1.Kind()) != true, false, true)
   156  
   157  	x1 = float32(13)
   158  	v1 = reflect.ValueOf(x1)
   159  	assertYes(t, tool.IsNumUIntegerKind(v1.Kind()) != true, false, true)
   160  
   161  	//
   162  
   163  	x1 = 13
   164  	v1 = reflect.ValueOf(x1)
   165  	assertYes(t, tool.IsNumFloatKind(v1.Kind()) != true, false, true)
   166  
   167  	x1 = float32(13)
   168  	v1 = reflect.ValueOf(x1)
   169  	assertYes(t, tool.IsNumFloatKind(v1.Kind()) == true, true, false)
   170  
   171  	x1 = "ok"
   172  	v1 = reflect.ValueOf(x1)
   173  	assertYes(t, tool.IsNumericType(v1.Type()) != true, false, true)
   174  }