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

     1  package test
     2  
     3  import (
     4  	"testing"
     5  	"github.com/v2pro/plz/reflect2"
     6  )
     7  
     8  func Test_slice_map(t *testing.T) {
     9  	t.Run("MakeSlice", testOp(func(api reflect2.API) interface{} {
    10  		valType := api.TypeOf([]map[int]int{}).(reflect2.SliceType)
    11  		obj := valType.MakeSlice(5, 10)
    12  		obj.([]map[int]int)[0] = map[int]int{1:1}
    13  		obj.([]map[int]int)[4] = map[int]int{2:2}
    14  		return obj
    15  	}))
    16  	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
    17  		obj := []map[int]int{{1: 1}, nil}
    18  		valType := api.TypeOf(obj).(reflect2.SliceType)
    19  		valType.SetIndex(obj, 0, &map[int]int{10:10})
    20  		valType.SetIndex(obj, 1, &map[int]int{2:2})
    21  		return obj
    22  	}))
    23  	t.Run("GetIndex", testOp(func(api reflect2.API) interface{} {
    24  		obj := []map[int]int{{1:1}, nil}
    25  		valType := api.TypeOf(obj).(reflect2.SliceType)
    26  		return []interface{}{
    27  			valType.GetIndex(&obj, 0),
    28  			valType.GetIndex(&obj, 1),
    29  			valType.GetIndex(obj, 0),
    30  			valType.GetIndex(obj, 1),
    31  		}
    32  	}))
    33  	t.Run("Append", testOp(func(api reflect2.API) interface{} {
    34  		obj := make([]map[int]int, 2, 3)
    35  		obj[0] = map[int]int{1:1}
    36  		obj[1] = map[int]int{2:2}
    37  		valType := api.TypeOf(obj).(reflect2.SliceType)
    38  		valType.Append(obj, map[int]int{3:3})
    39  		// will trigger grow
    40  		valType.Append(obj, map[int]int{4:4})
    41  		return obj
    42  	}))
    43  }