github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/reflect2/test/slice_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  	"github.com/v2pro/plz/reflect2"
     6  	"github.com/v2pro/plz/test"
     7  	"github.com/v2pro/plz/countlog"
     8  	"github.com/v2pro/plz/test/must"
     9  )
    10  
    11  func Test_slice(t *testing.T) {
    12  	var pInt = func(val int) *int {
    13  		return &val
    14  	}
    15  	t.Run("New", testOp(func(api reflect2.API) interface{} {
    16  		valType := api.TypeOf([]int{})
    17  		obj := *valType.New().(*[]int)
    18  		obj = append(obj, 1)
    19  		return obj
    20  	}))
    21  	t.Run("IsNil", testOp(func(api reflect2.API) interface{} {
    22  		valType := api.TypeOf([]int{})
    23  		var nilSlice []int
    24  		s := []int{}
    25  		return []interface{}{
    26  			valType.IsNil(&nilSlice),
    27  			valType.IsNil(&s),
    28  			valType.IsNil(nil),
    29  		}
    30  	}))
    31  	t.Run("SetNil", testOp(func(api reflect2.API) interface{} {
    32  		valType := api.TypeOf([]int{}).(reflect2.SliceType)
    33  		s := []int{1}
    34  		valType.SetNil(&s)
    35  		return s
    36  	}))
    37  	t.Run("Set", testOp(func(api reflect2.API) interface{} {
    38  		valType := api.TypeOf([]int{}).(reflect2.SliceType)
    39  		s1 := []int{1}
    40  		s2 := []int{2}
    41  		valType.Set(&s1, &s2)
    42  		return s1
    43  	}))
    44  	t.Run("MakeSlice", testOp(func(api reflect2.API) interface{} {
    45  		valType := api.TypeOf([]int{}).(reflect2.SliceType)
    46  		obj := *(valType.MakeSlice(5, 10).(*[]int))
    47  		obj[0] = 100
    48  		obj[4] = 20
    49  		return obj
    50  	}))
    51  	t.Run("UnsafeMakeSlice", test.Case(func(ctx *countlog.Context) {
    52  		valType := reflect2.TypeOf([]int{}).(reflect2.SliceType)
    53  		obj := valType.UnsafeMakeSlice(5, 10)
    54  		must.Equal(&[]int{0, 0, 0, 0, 0}, valType.PackEFace(obj))
    55  	}))
    56  	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
    57  		obj := []int{1, 2}
    58  		valType := api.TypeOf(obj).(reflect2.SliceType)
    59  		valType.SetIndex(&obj, 0, pInt(100))
    60  		valType.SetIndex(&obj, 1, pInt(20))
    61  		return obj
    62  	}))
    63  	t.Run("UnsafeSetIndex", test.Case(func(ctx *countlog.Context) {
    64  		obj := []int{1, 2}
    65  		valType := reflect2.TypeOf(obj).(reflect2.SliceType)
    66  		valType.UnsafeSetIndex(reflect2.PtrOf(obj), 0, reflect2.PtrOf(100))
    67  		valType.UnsafeSetIndex(reflect2.PtrOf(obj), 1, reflect2.PtrOf(10))
    68  		must.Equal([]int{100, 10}, obj)
    69  	}))
    70  	t.Run("GetIndex", testOp(func(api reflect2.API) interface{} {
    71  		obj := []int{1, 2}
    72  		valType := api.TypeOf(obj).(reflect2.SliceType)
    73  		return []interface{}{
    74  			valType.GetIndex(&obj, 1).(*int),
    75  		}
    76  	}))
    77  	t.Run("UnsafeGetIndex", test.Case(func(ctx *countlog.Context) {
    78  		obj := []int{1, 2}
    79  		valType := reflect2.TypeOf(obj).(reflect2.SliceType)
    80  		elem0 := valType.UnsafeGetIndex(reflect2.PtrOf(obj), 0)
    81  		must.Equal(1, *(*int)(elem0))
    82  		elem1 := valType.UnsafeGetIndex(reflect2.PtrOf(obj), 1)
    83  		must.Equal(2, *(*int)(elem1))
    84  	}))
    85  	t.Run("Append", testOp(func(api reflect2.API) interface{} {
    86  		obj := make([]int, 2, 3)
    87  		obj[0] = 1
    88  		obj[1] = 2
    89  		valType := api.TypeOf(obj).(reflect2.SliceType)
    90  		ptr := &obj
    91  		valType.Append(ptr, pInt(3))
    92  		// will trigger grow
    93  		valType.Append(ptr, pInt(4))
    94  		return ptr
    95  	}))
    96  	t.Run("UnsafeAppend", test.Case(func(ctx *countlog.Context) {
    97  		obj := make([]int, 2, 3)
    98  		obj[0] = 1
    99  		obj[1] = 2
   100  		valType := reflect2.TypeOf(obj).(reflect2.SliceType)
   101  		ptr := reflect2.PtrOf(obj)
   102  		valType.UnsafeAppend(ptr, reflect2.PtrOf(3))
   103  		valType.UnsafeAppend(ptr, reflect2.PtrOf(4))
   104  		must.Equal(&[]int{1, 2, 3, 4}, valType.PackEFace(ptr))
   105  	}))
   106  	t.Run("Grow", testOp(func(api reflect2.API) interface{} {
   107  		obj := make([]int, 2, 3)
   108  		obj[0] = 1
   109  		obj[1] = 2
   110  		valType := reflect2.TypeOf(obj).(reflect2.SliceType)
   111  		valType.Grow(&obj, 4)
   112  		return obj
   113  	}))
   114  }