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

     1  package tool_test
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  	"unsafe"
     9  
    10  	"github.com/hedzr/log"
    11  
    12  	"github.com/hedzr/evendeep/internal/tool"
    13  	"github.com/hedzr/evendeep/typ"
    14  )
    15  
    16  func TestNestedRecovery(t *testing.T) {
    17  	zero := 0
    18  
    19  	defer func() {
    20  		if e := recover(); e != nil {
    21  			log.Errorf("ERR [TestRec]: %v", e)
    22  		}
    23  	}()
    24  
    25  	func(v int) {
    26  		defer func() {
    27  			if e := recover(); e != nil {
    28  				// log.Errorf("ERR: %v", e)
    29  				log.Panicf("ERR: %v", e)
    30  			}
    31  		}()
    32  
    33  		v /= zero
    34  		t.Log(v)
    35  	}(9)
    36  }
    37  
    38  func TestMinInt(t *testing.T) {
    39  	t.Log(tool.MinInt(1, 9))
    40  	t.Log(tool.MinInt(9, 1))
    41  }
    42  
    43  func TestContainsStringSlice(t *testing.T) {
    44  	t.Log(tool.Contains([]string{"a", "b", "c"}, "c"))
    45  	t.Log(tool.Contains([]string{"a", "b", "c"}, "z"))
    46  
    47  	t.Log(tool.ContainsPartialsOnly([]string{"ac", "could", "ldbe"}, "itcouldbe"))
    48  	t.Log(tool.ContainsPartialsOnly([]string{"a", "b", "c"}, "z"))
    49  
    50  	t.Log(tool.PartialContainsShort([]string{"acoludbe", "bcouldbe", "ccouldbe"}, "could"))
    51  	t.Log(tool.PartialContainsShort([]string{"a", "b", "c"}, "z"))
    52  
    53  	idx, matchedString, containsBool := tool.PartialContains([]string{"acoludbe", "bcouldbe", "ccouldbe"}, "could")
    54  	t.Logf("%v,%v,%v", idx, matchedString, containsBool)
    55  
    56  	idx, matchedString, containsBool = tool.PartialContains([]string{"acoludbe", "bcouldbe", "ccouldbe"}, "byebye")
    57  	t.Logf("%v,%v,%v", idx, matchedString, containsBool)
    58  
    59  }
    60  
    61  func TestReverseSlice(t *testing.T) {
    62  	var ss = []int{8, 9, 7, 9, 3, 5}
    63  	tool.ReverseSlice(ss)
    64  	t.Logf("ss: %v", ss)
    65  
    66  	ss = []int{8, 9, 7, 3, 5}
    67  	tool.ReverseAnySlice(ss)
    68  	t.Logf("ss: %v", ss)
    69  }
    70  
    71  func TestInspectStruct(t *testing.T) {
    72  	a4 := prepareDataA4()
    73  	tool.InspectStruct(reflect.ValueOf(&a4))
    74  }
    75  
    76  func prepareDataA4() *A4 {
    77  
    78  	a4 := &A4{
    79  		A3: &A3{
    80  			A2: &A2{
    81  				Name2: "",
    82  				Int2:  0,
    83  				Bool2: false,
    84  				A1: A1{
    85  					Name1: "",
    86  					Int1:  0,
    87  					Bool1: false,
    88  				},
    89  			},
    90  			Name3: "",
    91  			Int3:  0,
    92  			A1: A1{
    93  				Name1: "",
    94  				Int1:  0,
    95  				Bool1: false,
    96  			},
    97  			Bool3: false,
    98  		},
    99  		Int4: 0,
   100  		A1: &A1{
   101  			Name1: "",
   102  			Int1:  0,
   103  			Bool1: false,
   104  		},
   105  	}
   106  	return a4
   107  }
   108  
   109  type A1 struct {
   110  	Name1 string
   111  	Int1  int
   112  	Bool1 bool
   113  }
   114  type A2 struct {
   115  	Name2 string
   116  	Int2  int
   117  	Bool2 bool
   118  	A1
   119  }
   120  type A3 struct {
   121  	*A2
   122  	Name3 string
   123  	Int3  int
   124  	A1
   125  	Bool3 bool
   126  }
   127  type A4 struct {
   128  	A3   *A3
   129  	Int4 int
   130  	*A1
   131  }
   132  
   133  // Employee type for testing
   134  type Employee struct {
   135  	Name      string
   136  	Birthday  *time.Time
   137  	F11       float32
   138  	F12       float64
   139  	C11       complex64
   140  	C12       complex128
   141  	Feat      []byte
   142  	Sptr      *string
   143  	Nickname  *string
   144  	Age       int64
   145  	FakeAge   int
   146  	EmployeID int64
   147  	DoubleAge int32
   148  	SuperRule string
   149  	Notes     []string
   150  	RetryU    uint8
   151  	TimesU    uint16
   152  	FxReal    uint32
   153  	FxTime    int64
   154  	FxTimeU   uint64
   155  	UxA       uint
   156  	UxB       int
   157  	Retry     int8
   158  	Times     int16
   159  	Born      *int
   160  	BornU     *uint
   161  	// nolint:unused
   162  	flags []byte // nolint:structcheck
   163  	Bool1 bool
   164  	Bool2 bool
   165  	Ro    []int
   166  }
   167  
   168  // X0 type for testing
   169  type X0 struct{}
   170  
   171  // X1 type for testing
   172  type X1 struct {
   173  	A uintptr
   174  	B map[string]typ.Any
   175  	C bytes.Buffer
   176  	D []string
   177  	E []*X0
   178  	F chan struct{}
   179  	G chan bool
   180  	H chan int
   181  	I func()
   182  	J typ.Any
   183  	K *X0
   184  	L unsafe.Pointer
   185  	M unsafe.Pointer
   186  	N []int
   187  	O [2]string
   188  	P [2]string
   189  	Q [2]string
   190  }
   191  
   192  // X2 type for testing
   193  type X2 struct {
   194  	A uintptr
   195  	B map[string]typ.Any
   196  	C bytes.Buffer
   197  	D []string
   198  	E []*X0
   199  	F chan struct{}
   200  	G chan bool
   201  	H chan int
   202  	I func()
   203  	J typ.Any
   204  	K *X0
   205  	L unsafe.Pointer
   206  	M unsafe.Pointer
   207  	N []int `copy:",slicemerge"`
   208  	O [2]string
   209  	P [2]string
   210  	Q [3]string `copy:",slicecopy"`
   211  }
   212  
   213  // Attr _
   214  type Attr struct {
   215  	Attrs []string `copy:",slicemerge"`
   216  }
   217  
   218  // Base _
   219  type Base struct {
   220  	Name      string
   221  	Birthday  *time.Time
   222  	Age       int
   223  	EmployeID int64
   224  }
   225  
   226  // Employee2 _
   227  type Employee2 struct {
   228  	Base
   229  	Avatar  string
   230  	Image   []byte
   231  	Attr    *Attr
   232  	Valid   bool
   233  	Deleted bool
   234  }
   235  
   236  // User _
   237  type User struct {
   238  	Name      string
   239  	Birthday  *time.Time
   240  	Age       int
   241  	EmployeID int64
   242  	Avatar    string
   243  	Image     []byte
   244  	Attr      *Attr
   245  	Valid     bool
   246  	Deleted   bool
   247  }