github.com/hedzr/evendeep@v0.4.8/ftor_test.go (about)

     1  package evendeep
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/hedzr/evendeep/flags/cms"
     7  	"github.com/hedzr/evendeep/internal/tool"
     8  	"github.com/hedzr/evendeep/typ"
     9  
    10  	"reflect"
    11  	"testing"
    12  )
    13  
    14  func testDeepEqual(printer func(msg string, args ...interface{}), got, expect typ.Any) {
    15  	// a,b:=reflect.ValueOf(got),reflect.ValueOf(expect)
    16  	// switch kind:=a.Kind();kind {
    17  	// case reflect.Map:
    18  	// case reflect.Slice:
    19  	// }
    20  
    21  	if !reflect.DeepEqual(got, expect) {
    22  		printer("expecting %v but got %v", expect, got)
    23  	}
    24  }
    25  
    26  func TestTestDeepEqual(t *testing.T) {
    27  	// defer dbglog.NewCaptureLog(t).Release()
    28  	var mm = []map[string]bool{
    29  		nil, nil,
    30  	}
    31  
    32  	for i := 0; i < 2; i++ {
    33  		mm[i] = make(map[string]bool)
    34  		for s := range map[string]bool{"std": true, "sliceopy": true, "mapcopy": true, "omitempty": true} {
    35  			mm[i][s] = true
    36  		}
    37  	}
    38  
    39  	testDeepEqual(t.Errorf, mm[0], mm[1])
    40  }
    41  
    42  func TestCopyChan(t *testing.T) {
    43  
    44  	c := newCopier()
    45  	// params := newParams(withOwnersSimple(c, nil))
    46  
    47  	var err error
    48  	var so = make(chan struct{})
    49  	var to chan struct{}
    50  
    51  	err = copyChan(c, nil, reflect.ValueOf(so), reflect.ValueOf(&to))
    52  	if err != nil {
    53  		t.Errorf("bad: %v", err)
    54  	} else {
    55  		t.Logf("tgt = %v", to)
    56  		// testDeepEqual(t, to2, [2]int{9, 77})
    57  	}
    58  }
    59  
    60  func TestCopyUnsafePointer(t *testing.T) {
    61  	// defer dbglog.NewCaptureLog(t).Release()
    62  
    63  	// c := newDeepCopier()
    64  	// params := newParams(withOwnersSimple(c, nil))
    65  	//
    66  	// var so = struct{ foo int }{42}
    67  	// var to int
    68  	// reflect.NewAt()
    69  	// copyUnsafePointer(c, from, to)
    70  }
    71  
    72  func TestCopySlice_differModes(t *testing.T) {
    73  	// defer dbglog.NewCaptureLog(t).Release()
    74  
    75  	c := newCloner()
    76  	params := newParams(withOwnersSimple(c, nil))
    77  
    78  	// flags.LazyInitFieldTagsFlags()
    79  
    80  	var so = []int{9, 77}
    81  	var to = []int{}
    82  	var err error
    83  
    84  	var src = reflect.ValueOf(&so)
    85  	var tgt = reflect.ValueOf(&to)
    86  
    87  	err = copySlice(c, params, tool.Rdecodesimple(src), tool.Rdecodesimple(tgt))
    88  	if err != nil {
    89  		t.Errorf("bad: %v", err)
    90  	} else {
    91  		t.Logf("tgt = %v", to)
    92  		testDeepEqual(t.Errorf, to, []int{9, 77})
    93  	}
    94  
    95  	to = []int{1}
    96  	tgt = reflect.ValueOf(&to)
    97  	err = copySlice(c, params, tool.Rdecodesimple(src), tool.Rdecodesimple(tgt))
    98  	if err != nil {
    99  		t.Errorf("bad: %v", err)
   100  	} else {
   101  		t.Logf("tgt = %v", to)
   102  		testDeepEqual(t.Errorf, to, []int{9, 77})
   103  	}
   104  
   105  	to = []int{1}
   106  	tgt = reflect.ValueOf(&to)
   107  	err = copySlice(c, newParams(withFlags(cms.SliceCopyAppend), withOwnersSimple(c, nil)), tool.Rdecodesimple(src), tool.Rdecodesimple(tgt))
   108  	if err != nil {
   109  		t.Errorf("bad: %v", err)
   110  	} else {
   111  		t.Logf("tgt = %v", to)
   112  		testDeepEqual(t.Errorf, to, []int{1, 9, 77})
   113  	}
   114  
   115  	to = []int{}
   116  	tgt = reflect.ValueOf(&to)
   117  	err = copySlice(c, newParams(withFlags(cms.SliceCopyAppend), withOwnersSimple(c, nil)), tool.Rdecodesimple(src), tool.Rdecodesimple(tgt))
   118  	if err != nil {
   119  		t.Errorf("bad: %v", err)
   120  	} else {
   121  		t.Logf("tgt = %v", to)
   122  		testDeepEqual(t.Errorf, to, []int{9, 77})
   123  	}
   124  
   125  	to = []int{2, 9, 1}
   126  	tgt = reflect.ValueOf(&to)
   127  	err = copySlice(c, newParams(withFlags(cms.SliceCopyAppend), withOwnersSimple(c, nil)), tool.Rdecodesimple(src), tool.Rdecodesimple(tgt))
   128  	if err != nil {
   129  		t.Errorf("bad: %v", err)
   130  	} else {
   131  		t.Logf("tgt = %v", to)
   132  		testDeepEqual(t.Errorf, to, []int{2, 9, 1, 9, 77})
   133  	}
   134  
   135  	so = []int{15, 2}
   136  	src = reflect.ValueOf(&so)
   137  	to = []int{2, 9, 1}
   138  	tgt = reflect.ValueOf(&to)
   139  	err = copySlice(c, newParams(withFlags(cms.SliceMerge), withOwnersSimple(c, nil)), tool.Rdecodesimple(src), tool.Rdecodesimple(tgt))
   140  	if err != nil {
   141  		t.Errorf("bad: %v", err)
   142  	} else {
   143  		t.Logf("tgt = %v", to)
   144  		testDeepEqual(t.Errorf, to, []int{2, 9, 1, 15})
   145  	}
   146  
   147  	to = []int{3, 77, 2, 15}
   148  	tgt = reflect.ValueOf(&to)
   149  	err = copySlice(c, newParams(withFlags(cms.SliceMerge), withOwnersSimple(c, nil)), tool.Rdecodesimple(src), tool.Rdecodesimple(tgt))
   150  	if err != nil {
   151  		t.Errorf("bad: %v", err)
   152  	} else {
   153  		t.Logf("tgt = %v", to)
   154  		testDeepEqual(t.Errorf, to, []int{3, 77, 2, 15})
   155  	}
   156  
   157  }
   158  
   159  func TestCopySlice_mergeMode(t *testing.T) {
   160  	// defer dbglog.NewCaptureLog(t).Release()
   161  
   162  	c := newCopier().withFlags(cms.SliceMerge, cms.MapMerge)
   163  	params := newParams(withOwnersSimple(c, nil))
   164  
   165  	var so = []int{9, 77}
   166  	var to = []int{}
   167  	var err error
   168  
   169  	var src = reflect.ValueOf(&so)
   170  	var tgt = reflect.ValueOf(&to)
   171  
   172  	err = copySlice(c, params, tool.Rdecodesimple(src), tool.Rdecodesimple(tgt))
   173  	if err != nil {
   174  		t.Errorf("bad: %v", err)
   175  	} else {
   176  		t.Logf("tgt = %v", to)
   177  		testDeepEqual(t.Errorf, to, []int{9, 77})
   178  	}
   179  
   180  	to = []int{2, 77}
   181  	tgt = reflect.ValueOf(&to)
   182  	err = copySlice(c, params, tool.Rdecodesimple(src), tool.Rdecodesimple(tgt))
   183  	if err != nil {
   184  		t.Errorf("bad: %v", err)
   185  	} else {
   186  		t.Logf("tgt = %v", to)
   187  		testDeepEqual(t.Errorf, to, []int{2, 77, 9})
   188  	}
   189  
   190  }
   191  
   192  func TestCopyArray(t *testing.T) {
   193  	// defer dbglog.NewCaptureLog(t).Release()
   194  
   195  	c := newCopier().withFlags()
   196  	params := newParams(withOwnersSimple(c, nil))
   197  
   198  	var so = [3]int{9, 77, 13}
   199  	var to = [5]int{}
   200  	var err error
   201  
   202  	var src = reflect.ValueOf(&so)
   203  	var tgt = reflect.ValueOf(&to)
   204  
   205  	err = copyArray(c, nil, src, tgt)
   206  	if err != nil {
   207  		t.Errorf("bad: %v", err)
   208  	} else {
   209  		t.Logf("tgt = %v", to)
   210  		testDeepEqual(t.Errorf, to, [5]int{9, 77, 13})
   211  	}
   212  
   213  	to2 := [2]int{77, 2}
   214  	err = copyArray(c, params, src, reflect.ValueOf(&to2))
   215  	if err != nil {
   216  		t.Errorf("bad: %v", err)
   217  	} else {
   218  		t.Logf("tgt = %v", to2)
   219  		testDeepEqual(t.Errorf, to2, [2]int{9, 77})
   220  	}
   221  
   222  	to2 = [2]int{}
   223  	err = copyArray(c, params, src, reflect.ValueOf(&to2))
   224  	if err != nil {
   225  		t.Errorf("bad: %v", err)
   226  	} else {
   227  		t.Logf("tgt = %v", to2)
   228  		testDeepEqual(t.Errorf, to2, [2]int{9, 77})
   229  	}
   230  
   231  }
   232  
   233  func TestCopyStructSlice(t *testing.T) {
   234  
   235  }
   236  
   237  func TestPointerOfPre(t *testing.T) {
   238  	type A struct {
   239  		A int
   240  	}
   241  	var a = &A{9}
   242  	var b = &a
   243  	t.Logf("a = %v, %p", a, a)
   244  	t.Logf("b = %v", b)
   245  	av := reflect.ValueOf(a)
   246  	ptr1 := av.Pointer()
   247  	t.Logf("a.pointer = %v", strconv.FormatUint(uint64(ptr1), 16))
   248  	np := reflect.New(av.Type())
   249  	t.Logf("np = %v, typ = %v", tool.Valfmt(&np), tool.Typfmtv(&np))
   250  
   251  	typ := av.Type() // type of *A
   252  	val := reflect.New(typ)
   253  	valElem := val.Elem()
   254  	ptr, _ := newFromType(typ.Elem())
   255  	valElem.Set(ptr)
   256  	t.Logf("ptr = %+v", *(val.Interface().(**A)))
   257  
   258  	// np.Elem().Set(av.Addr())
   259  	// t.Logf("np = %v, typ = %v", tool.Valfmt(&np), tool.Typfmtv(&np))
   260  	//
   261  	// avp, ok := pointerOf(av)
   262  	// if !ok {
   263  	// 	t.Fail()
   264  	// }
   265  	// t.Logf("avp = %v", tool.Valfmt(&avp))
   266  }